The Ultimate CSS Guide: From Basics to Advanced Techniques
Part 1: CSS Fundamentals
1. What is CSS?
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML documents. It controls layout, colors, fonts, and responsive designs.2. Adding CSS to HTML
There are three ways to apply CSS:Inline CSS (directly in HTML elements):
HTML:
<p style="color: blue;">This is blue text</p>
Internal CSS (in the <head> section):
HTML:
<head>
<style>
p {
color: blue;
}
</style>
</head>
External CSS (separate .css file):
HTML:
<head>
<link rel="stylesheet" href="styles.css">
</head>
styles.css:
CSS:
p {
color: blue;
}
3. Basic CSS Syntax
CSS:
selector {
property: value;
}
Example:
CSS:
h1 {
color: #ff0000;
font-size: 24px;
text-align: center;
}
4. Common CSS Properties
CSS:
/* Text styling */
p {
font-family: Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
text-decoration: underline;
}
/* Colors */
body {
color: #333; /* Text color */
background-color: #f5f5f5; /* Background color */
}
/* Box model */
div {
width: 300px;
height: 200px;
padding: 20px;
margin: 10px;
border: 1px solid #ccc;
}
Part 2: Intermediate CSS
5. CSS Selectors
Basic Selectors:
CSS:
/* Element selector */
p { color: blue; }
/* Class selector */
.highlight { background: yellow; }
/* ID selector */
#header { height: 100px; }
Advanced Selectors:
CSS:
/* Descendant selector */
article p { font-style: italic; }
/* Child selector */
ul > li { list-style: none; }
/* Attribute selector */
a[target="_blank"] { color: red; }
/* Pseudo-classes */
a:hover { text-decoration: none; }
li:nth-child(odd) { background: #eee; }
6. The Box Model
Every element is a box with:- Content
- Padding (inner space)
- Border
- Margin (outer space)
CSS:
.box {
box-sizing: border-box; /* Includes padding/border in width */
width: 300px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
7. Positioning Elements
CSS:
/* Static (default) */
.static { position: static; }
/* Relative to normal position */
.relative { position: relative; top: 20px; }
/* Absolute (relative to nearest positioned ancestor) */
.absolute { position: absolute; right: 0; }
/* Fixed (relative to viewport) */
.fixed { position: fixed; bottom: 0; }
/* Sticky */
.sticky { position: sticky; top: 0; }
Part 3: Advanced CSS
8. Flexbox Layout
CSS:
.container {
display: flex;
justify-content: center; /* Main axis alignment */
align-items: center; /* Cross axis alignment */
flex-wrap: wrap; /* Allow wrapping */
}
.item {
flex: 1; /* Grow/shrink equally */
min-width: 200px;
}
9. CSS Grid Layout
CSS:
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
}
.grid-item {
grid-column: span 2; /* Span 2 columns */
}
10. Animations & Transitions
Transitions:
CSS:
.button {
transition: all 0.3s ease;
}
.button:hover {
transform: scale(1.1);
background: #4CAF50;
}
Animations:
CSS:
@keyframes slidein {
from { transform: translateX(-100%); }
to { transform: translateX(0); }
}
.slide {
animation: slidein 1s forwards;
}
Part 4: Modern CSS Techniques
11. Responsive Design
CSS:
/* Media queries */
@media (max-width: 768px) {
.menu { display: none; }
.mobile-menu { display: block; }
}
/* Responsive units */
.container {
width: 90%;
max-width: 1200px;
margin: 0 auto;
}
img {
max-width: 100%;
height: auto;
}
12. CSS Variables
CSS:
:root {
--primary-color: #3498db;
--spacing: 20px;
}
.box {
color: var(--primary-color);
padding: var(--spacing);
}
13. Modern CSS Features
CSS:
/* Custom properties */
:root {
--shadow: 0 4px 6px rgba(0,0,0,0.1);
}
/* CSS Filters */
.image {
filter: blur(2px) brightness(120%);
}
/* Blend modes */
.overlay {
background-blend-mode: multiply;
}
CSS Best Practices
- Use external stylesheets for better maintainability
- Organize your CSS with comments and sections
- Follow naming conventions (BEM, OOCSS)
- Minify CSS for production
- Use developer tools for debugging
Next Steps
- Learn CSS preprocessors (Sass, Less)
- Explore CSS frameworks (Bootstrap, Tailwind)
- Master CSS architecture patterns
The Complete Web Development Masterclass: HTML, CSS & Beyond
Part 1: HTML - The Structure of the Web
1.1 HTML Fundamentals
HTML:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
</ul>
</nav>
</header>
<main>
<article>
<h2>Article Title</h2>
<p>This is paragraph text.</p>
</article>
</main>
<footer>
<p>© 2023 My Site</p>
</footer>
</body>
</html>
1.2 Semantic HTML5 Elements
- <header>/<footer> - Document header/footer
- <nav> - Navigation links
- <main> - Primary content
- <article> - Independent content
- <section> - Thematic grouping
- <aside> - Sidebar content
- <figure>/<figcaption> - Media with caption
1.3 HTML Forms
HTML:
<form action="/submit" method="post">
<fieldset>
<legend>User Information</legend>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" minlength="8" required>
<button type="submit">Register</button>
</fieldset>
</form>
Part 2: CSS - Styling the Web
2.1 CSS Fundamentals
CSS:
/* Box Model */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Segoe UI', system-ui, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f9f9f9;
}
.container {
width: min(90%, 1200px);
margin: 0 auto;
padding: 2rem;
}
2.2 Layout Techniques
Flexbox:
CSS:
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
gap: 2rem;
}
.card-container {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
}
.card {
flex: 1 1 300px;
}
CSS Grid:
CSS:
.grid-layout {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
}
.header {
grid-column: 1 / -1;
}
2.3 Responsive Design
CSS:
/* Mobile First Approach */
.hero {
padding: 2rem 1rem;
text-align: center;
}
/* Tablet */
@media (min-width: 768px) {
.hero {
padding: 4rem 2rem;
text-align: left;
}
}
/* Desktop */
@media (min-width: 1024px) {
.hero {
display: flex;
align-items: center;
gap: 3rem;
}
}
Part 3: Modern Web Techniques
3.1 CSS Variables
CSS:
:root {
--primary-color: #4361ee;
--secondary-color: #3f37c9;
--text-color: #2b2d42;
--light-gray: #f8f9fa;
--spacing: 1rem;
--border-radius: 8px;
}
.button {
background-color: var(--primary-color);
color: white;
padding: var(--spacing);
border-radius: var(--border-radius);
transition: all 0.3s ease;
}
.button:hover {
background-color: var(--secondary-color);
}
3.2 CSS Animations
CSS:
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.animated-card {
animation: fadeIn 0.6s ease-out forwards;
animation-delay: calc(var(--order) * 0.1s);
}
3.3 Advanced Selectors
CSS:
/* Attribute selectors */
input[type="text"] {
border: 1px solid #ddd;
}
/* Pseudo-elements */
blockquote::before {
content: open-quote;
font-size: 2em;
}
/* :where() for lower specificity */
:where(article, section) h2 {
margin-bottom: 1em;
}
Part 4: The Future of CSS
4.1 Container Queries
CSS:
.component {
container-type: inline-size;
}
@container (min-width: 400px) {
.component {
display: flex;
gap: 1rem;
}
}
4.2 CSS Nesting
CSS:
.card {
padding: 1rem;
& .title {
font-size: 1.2rem;
}
&:hover {
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
}
4.3 Viewport Units
CSS:
.hero {
min-height: 100dvh;
padding: 10dvh 5dvw;
}
Complete CSS Language Reference with Meanings and Usage (English)
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML or XML documents. Below is a comprehensive list of CSS properties, their meanings, and usage.1. CSS Selectors
Selectors target HTML elements to apply styles.| Selector | Meaning | Example Usage |
|---|---|---|
| element | Selects HTML elements by tag name. | p { color: red; } |
| .class | Selects elements with a specific class. | .header { font-size: 20px; } |
| #id | Selects an element with a specific ID. | #main { width: 100%; } |
| * | Universal selector (selects all elements). | * { margin: 0; } |
| selector1, selector2 | Groups multiple selectors. | h1, h2 { color: blue; } |
| parent > child | Direct child selector. | div > p { font-weight: bold; } |
| ancestor descendant | Descendant selector. | div p { color: green; } |
| selector1 + selector2 | Adjacent sibling selector. | h1 + p { margin-top: 0; } |
| selector1 ~ selector2 | General sibling selector. | h1 ~ p { color: gray; } |
| [attribute] | Selects elements with an attribute. | [target] { border: 1px solid; } |
| :hover | Applies style on mouse hover. | a:hover { color: orange; } |
| :active | Applies style when element is active (clicked). | button:active { background: black; } |
| :focus | Applies style when element is focused. | input:focus { border-color: blue; } |
2. CSS Properties (Commonly Used)
Text & Font Properties
| Property | Meaning | Example |
|---|---|---|
| color | Sets text color. | color: #FF0000; |
| font-family | Defines font type. | font-family: Arial, sans-serif; |
| font-size | Sets text size. | font-size: 16px; |
| font-weight | Sets boldness. | font-weight: bold; |
| text-align | Aligns text (left, right, center). | text-align: center; |
| text-decoration | Adds underline/overline. | text-decoration: underline; |
| line-height | Sets space between lines. | line-height: 1.5; |
Box Model Properties
| Property | Meaning | Example |
|---|---|---|
| width / height | Sets element dimensions. | width: 300px; |
| margin | Sets outer spacing. | margin: 10px; |
| padding | Sets inner spacing. | padding: 15px; |
| border | Sets border style. | border: 1px solid black; |
| box-sizing | Defines sizing behavior. | box-sizing: border-box; |
Layout & Positioning
| Property | Meaning | Example |
|---|---|---|
| display | Sets display behavior. | display: flex; |
| position | Defines positioning (static, relative, absolute, fixed). | position: relative; |
| top, right, bottom, left | Offsets positioned elements. | top: 20px; |
| float | Aligns elements left/right. | float: left; |
| clear | Prevents floating overlap. | clear: both; |
Flexbox & Grid
| Property | Meaning | Example |
|---|---|---|
| flex-direction | Sets flex container direction. | flex-direction: row; |
| justify-content | Aligns flex items horizontally. | justify-content: center; |
| align-items | Aligns flex items vertically. | align-items: center; |
| grid-template-columns | Defines grid columns. | grid-template-columns: 1fr 1fr; |
| gap | Sets spacing between grid/flex items. | gap: 10px; |
Background & Colors
| Property | Meaning | Example |
|---|---|---|
| background-color | Sets background color. | background-color: #f0f0f0; |
| background-image | Adds a background image. | background-image: url('bg.jpg'); |
| background-size | Controls image size. | background-size: cover; |
| background-repeat | Prevents image repetition. | background-repeat: no-repeat; |
Transitions & Animations
| Property | Meaning | Example |
|---|---|---|
| transition | Adds smooth property changes. | transition: all 0.3s ease; |
| animation | Applies keyframe animations. | animation: fade 2s infinite; |
| @keyframes | Defines animation steps. | @keyframes fade { from { opacity: 0; } } |
Responsive Design (Media Queries)
CSS:
@media (max-width: 768px) {
body { font-size: 14px; }
}
3. CSS Units
| Unit | Meaning | Example |
|---|---|---|
| px | Pixels (fixed size). | font-size: 16px; |
| % | Percentage (relative to parent). | width: 50%; |
| em | Relative to font size. | padding: 2em; |
| rem | Relative to root font size. | margin: 1rem; |
| vw / vh | Viewport width/height. | width: 100vw; |
4. CSS Example (Complete Usage)
CSS:
/* Selectors & Basic Styling */
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
/* Class & ID Styling */
.header {
background: #333;
color: white;
padding: 1rem;
text-align: center;
}
#main-content {
width: 80%;
margin: 2rem auto;
}
/* Flexbox Layout */
.container {
display: flex;
justify-content: space-between;
gap: 20px;
}
/* Hover & Transition Effects */
.button {
background: blue;
color: white;
padding: 10px 20px;
transition: background 0.3s;
}
.button:hover {
background: darkblue;
}
/* Media Query for Responsive Design */
@media (max-width: 600px) {
.container {
flex-direction: column;
}
}
CSS controls the visual presentation of web pages. Key concepts include:- Selectors (target elements)
- Properties (define styles)
- Units (measure sizes)
- Layout Models (Flexbox, Grid)
- Responsive Design (Media Queries)
Next Steps in Your Web Dev Journey
- JavaScript Fundamentals
- DOM Manipulation
- Event Handling
- Async Programming
- Frontend Frameworks
- React/Vue/Angular
- State Management
- Component Architecture
- Backend Development
- Node.js/Express
- Databases
- API Design
- Performance Optimization
- Lazy Loading
- Code Splitting
- Caching Strategies
- Web Accessibility
- ARIA Attributes
- Keyboard Navigation
- Color Contrast