Every topic: simple explanation first, then examples.
HTML builds the structure of a page — headings, paragraphs, images. CSS is what makes it look good. CSS controls color, font, size, spacing, layout — everything visual. Without CSS, every page looks like a plain text document.
<style> tag in the <head> of your HTML file. Good for small single-page experiments..css file and linked to HTML. This is the correct, professional way. Use this always.<h1 style="color: red; font-size: 32px;">Hello</h1>
<style> h1 { color: red; } </style>
<!-- In your HTML head --> <link rel="stylesheet" href="style.css"> /* In style.css */ h1 { color: red; }
h1, .card){ } curly braces that contain one or more rules; at the end of each rule is required.
selector { property: value; /* one rule */ property: value; /* another rule */ } /* Real example */ p { color: #333333; font-size: 16px; }
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css"> </head> <body> <h1>Hello, CSS!</h1> <p>This paragraph is styled with CSS.</p> </body> </html>
body { background-color: #f0f4f8; font-family: Arial, sans-serif; padding: 20px; } h1 { color: #c84b31; font-size: 36px; } p { color: #333333; font-size: 16px; }
This paragraph is styled with CSS.
A selector tells CSS which element to style. The better you understand selectors, the more precisely you can control your styles. This module covers every selector type you need to know.
p tags)/* Universal — selects everything */ * { margin: 0; padding: 0; } /* Element — all p tags */ p { color: #333; } /* Class — any element with class="btn" */ .btn { background: blue; color: white; } /* ID — the one element with id="logo" */ #logo { font-size: 24px; }
| Selector | Targets | Example |
|---|---|---|
| * | All elements | * { margin: 0; } |
| p | All <p> tags | p { color: gray; } |
| .card | Elements with class="card" | .card { padding: 20px; } |
| #logo | Element with id="logo" | #logo { color: red; } |
/* Grouping — same style for h1, h2, h3 */ h1, h2, h3 { font-family: Georgia, serif; } /* Descendant — only p tags that are inside .card */ .card p { color: gray; } /* Direct child — only li that are direct children of .nav */ .nav > li { list-style: none; } /* Adjacent sibling — h2 that comes directly after h1 */ h1 + h2 { margin-top: 0; }
element:state.
Common pseudo-classes: :hover (mouse over), :first-child, :last-child, :nth-child(), :visited, :focus.
/* When mouse hovers over a button */ button:hover { background: darkblue; } /* The first item in any list */ li:first-child { font-weight: bold; } /* Every even-numbered table row */ tr:nth-child(even) { background: #f5f5f5; } /* A link that the user has already visited */ a:visited { color: purple; }
element::part.::before (adds content before), ::after (adds content after), ::first-letter, ::selection (text the user highlights).
/* Make the first letter of a paragraph large */ p::first-letter { font-size: 48px; color: red; float: left; } /* Add a checkmark before each list item */ li::before { content: "✓ "; color: green; } /* Change the highlight color when user selects text */ ::selection { background: yellow; color: black; }
p { color: gray; } /* 1 point — loses */ .intro { color: blue; } /* 10 points */ #special { color: red; } /* 100 points — wins! */
color: red !important; but avoid using it. It overrides everything and makes your CSS very hard to debug later.Color brings a webpage to life. CSS gives you multiple ways to define and apply colors — to text, backgrounds, and borders.
red, blue, tomato. Limited options.#. Most commonly used by designers. Example: #ff0000rgb(255, 0, 0)p { color: red; } /* Named */ p { color: #ff0000; } /* HEX — most common */ p { color: rgb(255, 0, 0); } /* RGB */ p { color: rgba(255, 0, 0, 0.5); } /* RGBA — 50% transparent */
rgba() when you need transparency. You can pick HEX codes from any color picker tool.background-color — sets a solid colorbackground-image — sets an image or gradientbackground-size: cover — makes the image fill the element completelybackground-position: center — centers the imagebackground-repeat: no-repeat — prevents the image from tiling/* Solid background color */ .hero { background-color: #1a1a2e; } /* Background image that fills the element */ .banner { background-image: url('photo.jpg'); background-size: cover; background-position: center; background-repeat: no-repeat; height: 400px; } /* Gradient background — no image file needed */ .gradient-box { background: linear-gradient(135deg, #667eea, #764ba2); }
2px)solid, dashed, dotted, double)border-radius. A value of 50% on a square element creates a perfect circle.
/* Shorthand: width style color */ .box { border: 2px solid #333; } .dashed { border: 2px dashed red; } .dotted { border: 2px dotted blue; } /* Border on one side only */ .underline { border-bottom: 3px solid #c84b31; } /* Rounded corners */ .card { border-radius: 12px; } .pill { border-radius: 50px; } /* pill shape */ .circle { border-radius: 50%; } /* perfect circle */
Typography is about how text looks — which font, what size, how much spacing between lines and letters. Good typography makes content easy to read and professional looking.
font-family sets which font to use. You can use:
Arial, Georgia, monospace. Available on every device without importing.<head>, then use it in CSS.body { font-family: Arial, sans-serif; } h1 { font-family: Georgia, serif; }
<link href="https://fonts.googleapis.com/css2?family=Inter&display=swap" rel="stylesheet">body { font-family: 'Inter', sans-serif; } h1 { font-family: 'Playfair Display', serif; }
font-size — how big the text is. Use rem units (explained below) for best practice.font-weight — how bold the text is. Values go from 100 (thin) to 900 (heavy). 400 is normal, 700 is bold.font-style — can be normal or italic.h1 { font-size: 2.5rem; } /* large heading */ p { font-size: 1rem; } /* normal body text */ .small { font-size: 0.8rem; } /* smaller text */ .bold { font-weight: 700; } .thin { font-weight: 300; } .italic { font-style: italic; }
2rem — Heading
1.25rem — Subheading
1rem — Body text
0.8rem — Small / caption
| Unit | What it means | Best used for |
|---|---|---|
| px | Fixed pixels — always same size | Borders, shadows |
| rem | Relative to root font-size (16px default) | Font sizes ✅ |
| em | Relative to parent's font-size | Padding inside components |
| % | Percentage of parent element | Widths, layout |
p { text-align: center; /* left | right | center | justify */ line-height: 1.8; /* space between lines — 1.6 to 1.8 is readable */ letter-spacing: 0.05em; /* space between characters */ text-transform: uppercase; /* uppercase | lowercase | capitalize */ text-decoration: none; /* underline | line-through | none */ } /* Cut off long text with "..." */ .truncate { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
The Box Model is the most fundamental concept in CSS. Every single element on a webpage — a heading, a button, an image — is treated as a rectangular box. Understanding this box is the key to understanding all spacing and layout in CSS.
margin and padding use the same shorthand syntax. When you give one value, it applies to all 4 sides. When you give two values, the first is top/bottom, the second is left/right.
/* 1 value — all 4 sides */ .box { margin: 20px; } /* 2 values — top/bottom | left/right */ .box { margin: 20px 40px; } /* 4 values — top | right | bottom | left (clockwise) */ .box { margin: 10px 20px 30px 40px; } /* Individual sides */ .box { margin-top: 10px; } /* Center an element horizontally (must have a width) */ .center { width: 600px; margin: 0 auto; /* auto left and right = centered */ }
width: 300px on an element and then add padding: 20px, the element becomes 340px wide (300 + 20 + 20). This breaks layouts.box-sizing: border-box — this makes padding and border count inside the width, so the element stays exactly 300px.
Always add this to every project at the top of your CSS file.
* { box-sizing: border-box; }
.box { width: 400px; /* fixed width */ height: 200px; /* fixed height */ max-width: 100%; /* never wider than its parent */ min-height: 50px; /* never shorter than this */ } .full-width { width: 100%; } /* fills its parent completely */ .hero { height: 100vh; } /* full viewport height */
Flexbox is a layout system that makes it easy to arrange elements in a row or column, control the space between them, and align them. Before Flexbox, creating layouts in CSS was complicated. Now it's the standard way.
display: flex to a container element. This makes the container a flex container, and all its direct children automatically become flex items that follow flexbox rules..container { display: flex; /* turns flexbox on */ }
flex-direction — row (left to right) or column (top to bottom)justify-content — alignment along the main axis (horizontal in row, vertical in column)align-items — alignment along the cross axis (vertical in row, horizontal in column)gap — space between itemsflex-wrap: wrap — allows items to wrap to the next line if they don't fit.container { display: flex; flex-direction: row; /* row | column | row-reverse */ justify-content: space-between; /* flex-start | flex-end | center | space-between | space-around */ align-items: center; /* flex-start | flex-end | center | stretch */ gap: 16px; flex-wrap: wrap; }
flex: 1 — tells the item to grow and fill the available space equally with other flex itemsflex: 2 — takes up twice as much space as an item with flex: 1align-self — overrides align-items for just this one itemorder — changes the visual order of items without changing the HTML.item { flex: 1; } /* grow equally */ .item { flex: 2; } /* grow double compared to flex:1 items */ .item { flex: 0 0 200px; } /* fixed 200px, don't grow */ .special { align-self: flex-end; } .last { order: 99; }
CSS Grid is a two-dimensional layout system — it controls both rows and columns at the same time. Use it for full page layouts, dashboards, and image galleries.
display: grid to a container, then define your columns using grid-template-columns. The fr unit means "fraction of available space" — it's like flex: 1 but for grid.
.grid { display: grid; /* 3 equal columns */ grid-template-columns: 1fr 1fr 1fr; grid-template-columns: repeat(3, 1fr); /* same thing, shorter */ /* Sidebar (fixed) + main content (fills rest) */ grid-template-columns: 250px 1fr; gap: 20px; }
/* Span 2 columns wide */ .wide { grid-column: span 2; } /* Span 2 rows tall */ .tall { grid-row: span 2; } /* Place at exact grid position */ .header { grid-column: 1 / 3; /* from column line 1 to line 3 */ }
auto-fill — fit as many columns as possibleminmax(250px, 1fr) — each column is at least 250px wide, but can grow to fill space equally.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 20px; }
.page { display: grid; grid-template-areas: "header header" "sidebar main" "footer footer"; grid-template-columns: 240px 1fr; min-height: 100vh; } .header { grid-area: header; } .sidebar { grid-area: sidebar; } .main { grid-area: main; } .footer { grid-area: footer; }
Responsive design means your website looks good on every screen size — mobile, tablet, and desktop. The main tool for this is the media query, which lets you apply different styles based on screen width.
@media followed by a condition in parentheses. Everything inside the curly braces only applies when that condition is true.
/* Base styles — apply to all screens */ .container { width: 100%; padding: 16px; } /* Only applies when screen is 768px or wider */ @media (min-width: 768px) { .container { max-width: 720px; margin: 0 auto; } } /* Only applies when screen is 1024px or wider */ @media (min-width: 1024px) { .container { max-width: 1100px; } }
| Name | Device | min-width |
|---|---|---|
| Default | Small phones | No query (your base styles) |
| sm | Phones | 576px |
| md | Tablets | 768px |
| lg | Laptops | 1024px |
| xl | Desktops | 1280px |
min-width media queries to progressively add more complex styles for larger screens./* MOBILE — base styles, single column */ .cards { display: flex; flex-direction: column; /* stacks vertically */ gap: 16px; } .nav-links { display: none; } /* hide navigation links */ .hamburger { display: block; } /* show hamburger icon */ /* TABLET — 768px and above */ @media (min-width: 768px) { .cards { flex-direction: row; flex-wrap: wrap; } } /* DESKTOP — 1024px and above */ @media (min-width: 1024px) { .nav-links { display: flex; } /* show full nav */ .hamburger { display: none; } /* hide hamburger */ }
/* clamp() — font that scales with the screen */ h1 { /* minimum 1.8rem, grows with screen, maximum 3.5rem */ font-size: clamp(1.8rem, 5vw, 3.5rem); } /* Images — never overflow their container */ img { max-width: 100%; /* never wider than parent */ height: auto; /* keeps the original ratio */ display: block; }
img { max-width: 100%; height: auto; } — without this, images will overflow on mobile screens.CSS can animate elements without any JavaScript. There are two tools: Transitions for simple state changes (like hover), and Animations for complex multi-step motion you define with keyframes.
all for everything0.3s)ease, linear, ease-in-out).btn { background: #c84b31; padding: 12px 28px; border-radius: 8px; border: none; cursor: pointer; transition: all 0.3s ease; /* put this on the base state */ } .btn:hover { background: #a83a24; transform: translateY(-3px); /* move up 3px */ box-shadow: 0 8px 24px rgba(0,0,0,0.2); }
transform changes the position, size, or rotation of an element without affecting the layout around it. It is commonly used with transitions to create smooth hover effects. You can combine multiple transforms in one line.
.box:hover { transform: translateX(20px); /* move right */ transform: translateY(-10px); /* move up */ transform: scale(1.1); /* 10% bigger */ transform: rotate(45deg); /* rotate 45 degrees */ /* Combine multiple transforms */ transform: translateY(-5px) scale(1.05); }
0% to 100%, then apply the animation to an element.animation-name — the name you gave to the @keyframesanimation-duration — how long one cycle takesanimation-timing-function — speed curveanimation-iteration-count — how many times to repeat (infinite = loop forever)animation-delay — wait before startinganimation: name duration timing count
/* Step 1: Define the animation steps */ @keyframes bounce { 0% { transform: translateY(0); } 50% { transform: translateY(-30px); } 100% { transform: translateY(0); } } /* Step 2: Apply it to an element */ .ball { animation: bounce 1s ease-in-out infinite; } /* Fade + slide in (common for page load) */ @keyframes fadeUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } /* Stagger elements in one by one with delay */ .hero h1 { animation: fadeUp 0.8s ease both; } .hero p { animation: fadeUp 0.8s ease 0.2s both; } /* 0.2s later */ .hero .btn{ animation: fadeUp 0.8s ease 0.4s both; } /* 0.4s later */
These are the tools that take your CSS from functional to professional-looking — CSS Variables, Box Shadows, Gradients, and Text Shadows. Real websites use all of these.
:root (which means the whole page can access them) with two dashes before the name: --variable-name. You use them with var().
/* Define variables on :root — accessible everywhere */ :root { --primary: #c84b31; --secondary: #1d4ed8; --text: #1a1a1a; --bg: #f8f9fa; --radius: 12px; --shadow: 0 4px 20px rgba(0,0,0,0.1); } /* Use them with var() */ body { background: var(--bg); color: var(--text); } .btn { background: var(--primary); border-radius: var(--radius); box-shadow: var(--shadow); } /* Dark mode — just override the variables */ @media (prefers-color-scheme: dark) { :root { --bg: #0d0d0d; --text: #f0f0f0; } }
inset at the start makes the shadow appear inside the element instead of outside.
/* Basic soft shadow */ .card { box-shadow: 0 4px 20px rgba(0,0,0,0.1); } /* Layered shadow — looks more realistic */ .card { box-shadow: 0 2px 4px rgba(0,0,0,0.08), 0 20px 40px rgba(0,0,0,0.12); } /* Colored glow effect */ .btn-red { box-shadow: 0 8px 30px rgba(200,75,49,0.4); } /* Shadow on the inside */ .input { box-shadow: inset 0 2px 4px rgba(0,0,0,0.1); } /* Shadow grows on hover */ .card { box-shadow: none; transition: box-shadow 0.3s ease, transform 0.3s ease; } .card:hover { box-shadow: 0 20px 60px rgba(0,0,0,0.15); transform: translateY(-6px); }
background property — no image file needed.linear-gradient() — colors flow in a straight line (top, bottom, left, right, or a diagonal angle)radial-gradient() — colors radiate outward from a center point in a circle/* Linear gradient — left to right */ .box { background: linear-gradient(to right, #c84b31, #f59e0b); } /* Linear gradient — at an angle */ .box { background: linear-gradient(135deg, #667eea, #764ba2); } /* Radial gradient — radiates from center */ .box { background: radial-gradient(circle at center, #1d4ed8, #0d0d0d); } /* Gradient text — a popular design technique */ .gradient-text { background: linear-gradient(135deg, #c84b31, #f59e0b); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; }
text-shadow works just like box-shadow but applies to text. The syntax is: x-offset y-offset blur-radius color. You can add multiple shadows separated by commas.
/* Basic text shadow */ h1 { text-shadow: 2px 2px 4px rgba(0,0,0,0.3); } /* Glow effect */ .glow { text-shadow: 0 0 20px rgba(29,78,216,0.8); } /* Multiple shadows — retro/stacked look */ .retro { text-shadow: 3px 3px 0 #c84b31, 6px 6px 0 #f59e0b; }