Chapter 3: Cascading Style Sheets (CSS)
62 slides · use ←/→ or Space to navigate, F for fullscreen.
Jump to a lesson deck: Introduction to CSS & Its Evolution CSS Syntax, Selectors & Specificity Applying Styles: Tag, Class & ID Levels of Style Sheets & Cascade Priority Units, Fonts, Text & Colour Custom Properties, Lists & Spacing The Box Model, Flexbox & Grid Links, Pseudo-classes & Responsive Design CSS Animations & Sprite Sheets Modern CSS 2025 & Summary
Chapter 3: Cascading Style Sheets (CSS)
CSS describes the presentation of HTML — layout, colours, fonts, spacing, layout systems and animation.
Introduction to CSS & Its Evolution
What CSS is, what it does, and how it evolved from 1996 to 2025.
What is CSS?
CSS (Cascading Style Sheets) describes the presentation of HTML — layout, colours, fonts and spacing.
- Separates style from content — HTML is structure & content, CSS is visual presentation. This keeps code clean and maintainable.
- Applies to many documents — one stylesheet can style thousands of pages: edit once, update everywhere.
- W3C standard — maintained by the World Wide Web Consortium; validate your CSS at
jigsaw.w3.org/css-validatorfor cross-browser consistency.
CSS has been the industry standard since CSS1 in 1996.
CSS Evolution (1996 → 2025)
| Year | Milestone |
|---|---|
| 1996 | CSS1 — basic selectors, fonts, colours, margins |
| 1998 | CSS2 — positioning, z-index, media types |
| 2011 | CSS3 modules — animations, gradients, flexbox |
| 2017 | CSS Grid Layout for page layouts |
| 2022 | @layer cascade layers, container queries |
| 2025 | Nesting, @scope, scroll-driven animations, Houdini |
What Does CSS Do?
| Area | Capabilities |
|---|---|
| Text & fonts | Font family, size, weight; line height, letter spacing; text decoration & transform |
| Layout & sizing | Width, height, min/max; Flexbox & Grid (2025 standard); positioning (static/relative/fixed) |
| Visual effects | Shadows, gradients, opacity; filters & backdrop-blur; clip-path & masks |
| Animation & motion | Keyframe animations; transitions between states; scroll-driven animations (2025) |
Activity — complete the code
Complete the CSS rule so it targets every h1 and sets a 2rem font size.
/* A CSS rule = selector + declarations */{color: blue;font-size: ;}
CSS Syntax, Selectors & Specificity
The anatomy of a rule, the selector types, and how the cascade decides which style wins.
Anatomy of a CSS Rule
h1 {
color: blue;
font-size: 2rem;
}
| Part | Meaning |
|---|---|
| Selector | Targets the HTML element(s) to style |
| Property | The style attribute to change (e.g. color) |
| Value | The specific style to apply (e.g. blue) |
| Declaration | A property: value pair, separated by semicolons |
CSS Selectors
| Type | Example | Targets |
|---|---|---|
| Element | h1 { } | All <h1> tags |
| Class | .highlight { } | Elements with class="highlight" |
| ID | #header { } | The unique id="header" element |
| Attribute | input[type='email'] { } | Matches by attribute value (CSS3+) |
| Pseudo-class | a:hover { } | Styles based on element state |
| Parent (2025) | div:has(img) { } | The long-awaited parent selector — now baseline in all browsers |
CSS Specificity & the Cascade
When rules conflict, the more specific rule wins. From highest to lowest priority:
!important— overrides everything- Inline style
- ID selector (
#id) - Class / pseudo-class
- Element selector
2025 tip: use @layer instead of !important to manage specificity without conflicts.
Activity — complete the code
Match each selector type to its example.
{ color: blue; } /* element */{ background: yellow; } /* class */{ font-size: 2rem; } /* id */a: { color: red; } /* state */
Applying Styles: Tag, Class & ID
The three core ways to target elements, and when to use each.
Ways to Apply CSS Rules
| Method | Syntax | Notes |
|---|---|---|
| Tag selector | h1 { color: blue; } | Affects every <h1> on the page. Good for global defaults. |
| Class attribute | .highlight { color: red; } | Reusable across many elements. Best for repeated styles. |
| ID attribute | #hero { font-size: 3rem; } | Unique — one element per page. Highest CSS specificity. |
The Class Attribute
Classes let different occurrences of the same tag use different styles.
/* Attached class */
p.narrow { width: 380px; }
p.wide { width: 680px; }
/* Generic class — applies to any tag */
.highlight { background-color: yellow; font-weight: bold; }
/* Multiple classes on one element */
.card { border-radius: 8px; }
.card.featured { border: 2px solid gold; }
Applied in HTML with the class attribute:
<p class="narrow">...</p>
<div class="card featured large">...</div>
2025 best practice: use BEM naming — .card, .card__title, .card--featured.
The ID Attribute
An ID is unique — each value can be used only once per HTML document.
<p id="header">...</p>
#header { font-size: 2rem; }
- Higher specificity than class selectors.
- Use IDs for landmark regions:
#main-nav,#hero,#footer. - Avoid using IDs just for styling — prefer classes for reusability.
- IDs are also used as anchor links:
<a href="#section2">.
Activity — complete the code
Complete the tag, class and ID selectors.
/* Tag selector */p { color: black; }/* Class selector */{ color: red; }/* ID selector */{ font-size: 3rem; }
Levels of Style Sheets & Cascade Priority
Inline, embedded and external CSS — plus @import vs @layer.
CSS Cascade Priority
From highest to lowest priority:
- Inline styles —
<h1 style="color:red">. Applied directly on the element; highest priority. - Embedded / document —
<style> h1 { color: red; } </style>in the <head>. Applies to the whole document. - External stylesheet — a separate
.cssfile that can be shared across many pages. - Browser default — the user-agent stylesheet. Lowest priority.
Inline Style Sheets
Placed directly inside the HTML tag using the style attribute. Applies only to that single element — the most specific.
<h1 style="color: #008408; font-size: 2rem;">Styled Heading</h1>
- Useful for quick one-off overrides or dynamic styling via JavaScript.
- 2025 warning: avoid heavy use — hard to maintain and can't be cached.
- Better alternative: CSS custom properties + utility classes.
Embedded (Document-Level) Style Sheets
Placed inside a <style> tag in the <head>. Applies to the whole document.
<head>
<style>
/* Document-level styles apply to this page only */
body {
background-color: #001B2A;
color: #CAD7EE;
font-family: 'Segoe UI', sans-serif;
}
h1 { color: #00B4D8; }
h1, h3, p { margin-bottom: 1rem; }
p.tip {
background: #142236;
padding: 0.75rem;
border-left: 4px solid #FFB703;
}
</style>
</head>
- The
type="text/css"attribute is optional in HTML5. - Use
/* */for CSS comments. - Good for single-page apps, page-specific overrides and prototyping.
External Style Sheets
Stored in a separate .css file, linked to one or more HTML pages.
<!-- HTML file: index.html -->
<head>
<link rel="stylesheet" href="style.css">
</head>
/* style.css */
body {
background: #001B2A;
color: #CAD7EE;
font-family: 'Segoe UI', sans-serif;
font-size: 1rem;
}
h1 { color: #00B4D8; font-size: 2rem; }
h2 { color: #90E0EF; }
Why external CSS is preferred
- Single file controls all pages — update once, site-wide change.
- The browser caches the
.cssfile — faster repeat visits. - Cleaner HTML — no style clutter mixed with content.
You can also import: @import url('style.css') inside another CSS file.
@import vs @layer — 2025 Best Practice
@importloads another CSS file from within a CSS file. Avoid chaining multiple imports — it blocks parallel loading.- The modern approach is CSS Cascade Layers with
@layer, which controls specificity without using!important. Layers declared first have lower priority than later layers.
/* Traditional @import (still valid) */
@import url('reset.css');
@import url('theme.css');
/* 2025: CSS Cascade Layers */
@layer reset, base, components, utilities;
@layer reset {
* { margin: 0; padding: 0; box-sizing: border-box; }
}
@layer base {
body { font-family: system-ui; }
}
@layer components {
.card { border-radius: 0.5rem; }
}
@layer utilities {
.text-center { text-align: center; }
}
/* Later layers win — no !important needed */ Activity — complete the code
Complete the three levels of style sheets.
<!-- 1. Inline --><h1 ="color: red;">Inline</h1><!-- 2. Embedded --><head><>h1 { color: red; }</style></head><!-- 3. External --><link rel="" href="style.css">
Units, Fonts, Text & Colour
Value types, absolute vs relative units, typography properties and modern colour.
CSS Units & Property Value Forms
| Absolute units | Relative units (2025 preferred) |
|---|---|
px pixelsin inchescm centimetresmm millimetrespt pointspc picas (12 points)
|
em — relative to parent fontrem — relative to root font% — percentage of parentvw / vh — viewport width/heightsvh — small viewport heightcqi — container query inline size
|
Other value types
- Keywords:
left,center,bold - Percentages:
50%,100% - URL:
url('image.png') - Colour:
#RGB,rgb(),hsl(),oklch() - Variables:
var(--custom)
Font Properties
font-family— list of fonts; the browser uses the first available, e.g.'Segoe UI', Arial, sans-seriffont-size— length value or keyword (1remrecommended)font-style—normal | italic | obliquefont-weight—normal | bold | 100–900font— shorthand: style weight size family, e.g.font: italic bold 1rem 'Times New Roman'
p.big {
font-size: 1.2rem;
font-style: italic;
font-family: 'Times New Roman', serif;
}
/* 2025: web font with display swap */
@font-face {
font-family: 'My Font';
src: url('font.woff2') format('woff2');
font-display: swap; /* prevents invisible text flash */
}
/* 2025: variable font — one file, full weight range */
body { font-family: 'Inter Variable', sans-serif; font-weight: 100 900; }
Text Properties
| Property | Values |
|---|---|
text-align | left | center | right | justify |
text-indent | Indents the first line, e.g. 2rem |
text-decoration | underline | overline | line-through | none |
text-transform | uppercase | lowercase | capitalize |
line-height | Spacing between lines, e.g. 1.6 recommended |
letter-spacing / word-spacing | Space between characters / words |
h2 { text-transform: uppercase; letter-spacing: 0.08em; }
a:hover { text-decoration: underline; }
/* 2025: smart line wrapping */
h1 { text-wrap: balance; }
/* 2025: truncate overflow with an ellipsis */
.card-title {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Colours in CSS — 2025 Complete Guide
| Form | Example | Notes |
|---|---|---|
| Named | color: tomato; | 147 named colours in CSS |
Hex #RGB | color: #00B4D8; | Most common — 16.7M colours |
rgb() | color: rgb(0 180 216); | CSS4: no commas needed |
hsl() | color: hsl(194 100% 42%); | Hue, Saturation, Lightness |
oklch() | color: oklch(0.55 0.15 220); | Perceptual, wide-gamut P3 (2025) |
color-mix() | color: color-mix(in oklch, blue, red); | Native colour blending (2025) |
Activity — complete the code
Complete the typography rule using a relative unit and a comfortable line height.
body {font-family: 'Segoe UI', , sans-serif;font-size: ;line-height: ;}
Custom Properties, Lists & Spacing
CSS variables (design tokens), list styling and the classic spacing properties.
CSS Custom Properties (Variables)
- Defined with a
--prefix in:root { }for global scope. - Used with
var(--name, fallback)throughout the stylesheet. - Cascade and inherit like regular CSS properties, and can be changed per-component or per-theme.
- Enable design tokens — a single source of truth for spacing and colour.
- Fully dynamic — can be updated with JavaScript at runtime.
/* Design tokens in :root */
:root {
--color-primary: #00B4D8;
--color-bg: #001B2A;
--color-text: #CAD7EE;
--space-sm: 0.5rem;
--space-md: 1rem;
--radius: 0.5rem;
}
/* Use variables everywhere */
.button {
background: var(--color-primary);
padding: var(--space-sm) var(--space-md);
border-radius: var(--radius);
}
/* Dark mode override — change one place */
@media (prefers-color-scheme: dark) {
:root {
--color-bg: #060D14;
--color-text: #E8F4FD;
}
}
List Properties
list-style-type— bullet or numbering style. Unordered:disc(default),circle,square,none. Ordered:decimal,upper-roman,lower-alpha,upper-alpha.list-style-image— use a custom image as the bullet.list-style— shorthand combining type, position and image.- 2025: the
::markerpseudo-element styles bullets directly.
/* Unordered list bullets */
ul { list-style-type: square; }
/* Custom image bullet */
ul.special { list-style-image: url('dot.svg'); }
/* Ordered list types */
ol { list-style-type: upper-roman; }
/* Per-item override */
li.special { list-style-type: disc; }
/* 2025: style the bullet itself */
li::marker {
color: #FFB703;
font-size: 1.2em;
font-weight: bold;
content: "> ";
}
/* List shorthand */
ul { list-style: square inside url('img.png'); }
Text Alignment, Float & Margins
| Property | Purpose |
|---|---|
text-align | left | center | right | justify |
text-indent | Indents the first line of text |
float | Wraps text around an element (left | right | none) |
margin | Space outside the element border |
padding | Space between the border and the content |
/* Text alignment */
p { text-align: justify; }
h1 { text-align: center; }
.note { text-indent: 2rem; }
/* Float (legacy — for text wrapping around images) */
img { float: right; margin-left: 1rem; margin-bottom: 1rem; }
/* Clear floats */
.clearfix::after { content: ""; display: block; clear: both; }
/* Margin shorthand: top right bottom left */
.box { margin: 1rem 2rem; } /* top/bottom 1rem, left/right 2rem */
.box { margin-inline: auto; } /* centre block horizontally (2025) */
.box { padding-block: 1.5rem; } /* top + bottom padding */
2025: modern layout uses Flexbox and Grid — float is rarely needed now.
Activity — complete the code
Complete the custom property usage and list styling.
:root {--primary: #2563eb;}.button {background: (--primary);}ul {list-style-type: ;}
The Box Model, Flexbox & Grid
Every element is a box — master the model, then lay out with Flexbox (1D) and Grid (2D).
The CSS Box Model
Every element is a box made of four layers, from outside in:
- Margin — space outside the border; pushes other elements away.
- Border — the visible frame around the element.
- Padding — space between the content and the border.
- Content — the actual text, image or other content.
Box Model in CSS — box-sizing Best Practice
- The default
box-sizingiscontent-box— width counts only the content. - Setting
box-sizing: border-boxincludes padding + border in the width. - 2025 best practice: apply
border-boxglobally in every project.
/* 2025 global reset — every project */
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.box {
width: 300px;
padding: 20px; /* inner space */
border: 2px solid #00B4D8;
margin: 1rem auto; /* centered */
}
/* With border-box: total width stays 300px
Without: 300px + 40px + 4px = 344px */
/* 2025: logical properties */
.card {
padding-block: 1rem; /* top + bottom */
padding-inline: 1.5rem; /* left + right */
margin-inline: auto; /* center */
}
CSS Flexbox — Modern 1D Layout
| Property | Values / Purpose |
|---|---|
display: flex | Turns a container into a flex container |
flex-direction | row (default) | column | row-reverse | column-reverse |
justify-content | Aligns items along the main axis: flex-start | center | flex-end | space-between | space-evenly |
align-items | Aligns items along the cross axis: stretch | center | flex-start | flex-end | baseline |
gap | Space between flex items (no margins needed!) |
flex: 1 | Item grows to fill available space |
flex-wrap: wrap | Items flow to the next row when needed |
/* Flex container */
.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
/* Flex item sizing */
.sidebar { flex: 0 0 260px; } /* fixed */
.main { flex: 1; } /* fills the rest */
/* Centering — the easiest way in 2025 */
.hero {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Wrapping card grid */
.card-row { display: flex; flex-wrap: wrap; gap: 1.5rem; }
.card { flex: 1 1 250px; } /* grow, shrink, min 250px */
CSS Grid — Modern 2D Layout
| Feature | Purpose |
|---|---|
display: grid | Creates a 2D grid container |
grid-template-columns / -rows | Define tracks |
fr unit | Fractional share of available space — 1fr 2fr is 33% / 67% |
gap | Row and column gaps |
grid-area | Name areas for semantic placement |
repeat(auto-fill, minmax(250px, 1fr)) | Responsive grid with NO media queries |
subgrid | 2025: nested items align to parent grid tracks |
/* Basic grid */
.layout {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-rows: auto 1fr auto;
gap: 1rem;
}
/* Named areas — like a blueprint */
.page {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
/* Responsive grid — NO media queries! */
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 1.5rem;
} Activity — complete the code
Complete the global reset and the flex container.
*, *::before, *::after {box-sizing: ;}.nav {display: ;justify-content: ;gap: 1rem;}
Links, Pseudo-classes & Responsive Design
Interactive states and media/container queries for responsive layouts.
Link & Interaction Pseudo-class Selectors
| Pseudo-class | Matches |
|---|---|
a:link | Unvisited link — default state |
a:visited | Link the user has already clicked |
a:hover | Mouse is over the link |
a:active | Link is being clicked right now |
button:focus-visible | 2025: keyboard focus only (not mouse click) |
input:invalid | Form field fails validation |
:user-invalid | 2025: shows only after user interaction |
a { font-weight: bold; }
a:link { color: #0084D8; }
a:visited { color: #90E0EF; }
a:hover { color: #FF8703; text-decoration: none; }
a:active { color: #FB8500; }
button:focus-visible { outline: 3px solid #0084D8; }
Responsive Design & Media Queries (2025)
Media queries apply styles only under specific conditions.
@media (min-width: 768px)— mobile-first approach.- Common breakpoints: 640px, 768px, 1024px, 1280px.
- Also query
prefers-color-schemeandprefers-reduced-motion.
/* Mobile-first default */
.grid { display: block; }
/* Tablet+ */
@media (min-width: 768px) {
.grid { display: grid; grid-template-columns: 1fr 1fr; }
}
/* Desktop */
@media (min-width: 1024px) {
.grid { grid-template-columns: repeat(3, 1fr); }
}
/* Accessibility — reduce motion */
@media (prefers-reduced-motion: reduce) {
* { animation: none !important; }
}
Container Queries (2025)
Container queries respond to the parent container's width rather than the viewport — components become truly reusable.
.card-wrapper { container-type: inline-size; }
@container (min-width: 400px) {
.card { display: flex; flex-direction: row; }
} Activity — complete the code
Complete the pseudo-class and the media queries.
a: { color: red; }@media (min-width: ) {.grid { display: grid; }}@media (prefers-reduced-motion: ) {* { animation: none; }}
CSS Animations & Sprite Sheets
@keyframes, the animation properties, and sprite-sheet animation with steps().
What is a CSS Animation?
- Keyframe-based —
@keyframesdefines the animation. Specify styles at 0%, 50%, 100%, or use thefrom/toshorthand. - Bound to elements —
animation-namelinks to a@keyframesrule; runs when the element is visible and can be triggered by JavaScript. - Fully controllable — duration, delay, iteration, direction, timing, fill mode; pause, play and stop.
CSS animations change any CSS property over time — no JavaScript needed for visual effects.
CSS Animation Properties
| Property | Purpose |
|---|---|
@keyframes | Defines the animation sequence with style steps |
animation-name | Links the element to the @keyframes rule name |
animation-duration | How long one cycle takes (e.g. 1s, 300ms) |
animation-delay | Wait before starting (negative = skip ahead) |
animation-iteration-count | Number of cycles: 1, 3, infinite |
animation-direction | normal | reverse | alternate | alternate-reverse |
animation-timing-function | Speed curve: ease | linear | steps(n) | cubic-bezier() |
animation-fill-mode | Style before/after: none | forwards | backwards | both |
@keyframes — Defining the Animation
- Use
from/tofor simple two-state animations. - Use percentages (0%, 50%, 100%) for multi-step sequences.
- The rule name is referenced by
animation-name; multiple@keyframescan exist in one stylesheet.
/* Simple two-state */
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
/* Multi-step */
@keyframes slideAndFade {
0% { transform: translateY(-20px); opacity: 0; }
50% { opacity: 1; }
100% { transform: translateY(0); opacity: 1; }
}
animation-name & animation-duration
.element {
animation-name: enemy1;
animation-duration: 0.8s; /* REQUIRED — default is 0s (no animation) */
}
/* Multiple animations: comma-separated names and durations */
.hero-card {
animation-name: fadeIn, float;
animation-duration: 0.5s, 3s;
}
animation-delay
- Positive value: the animation waits N seconds before starting.
- Negative value: starts as if it has already been running —
-2sstarts 2 seconds in. - Useful for staggered effects on list items.
.element { animation-name: sprite-anim; animation-duration: 4s; }
.element-2 { animation-name: sprite-anim; animation-duration: 4s; animation-delay: -2s; }
/* Staggered list animation */
.item:nth-child(1) { animation-delay: 0.1s; }
.item:nth-child(2) { animation-delay: 0.2s; }
.item:nth-child(3) { animation-delay: 0.3s; }
animation-iteration-count
- Default:
1— plays once then stops. infinite— loops forever (great for sprite animations).- A number plays exactly that many times; decimals like
1.5are allowed.
.sprite { animation-name: sprite-loop; animation-duration: 4s; animation-iteration-count: infinite; }
.bounce { animation-name: bounceIn; animation-duration: 0.6s; animation-iteration-count: 3; }
animation-direction
| Value | Behaviour |
|---|---|
normal | Plays forwards (0% → 100%). Default. |
reverse | Plays backwards (100% → 0%). |
alternate | Forwards, then backwards on the next cycle. |
alternate-reverse | Backwards first, then forwards on the next cycle. |
.pendulum {
animation-name: swing;
animation-duration: 1.5s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-timing-function: ease-in-out;
}
animation-timing-function
ease— slow start, fast middle, slow end (default)linear— constant speed throughoutease-in/ease-out/ease-in-outcubic-bezier(x1,y1,x2,y2)— custom curvesteps(n)— discrete jumps — essential for sprite sheets (e.g.steps(6)= 6 frames per cycle)
#sprite-image {
animation-name: enemy1;
animation-duration: 0.8s;
animation-timing-function: steps(6); /* 6 frames in the sprite strip */
}
animation-fill-mode
| Value | Behaviour |
|---|---|
none | Default — reverts to original styles before and after. |
forwards | Keeps the final keyframe styles after the animation ends. |
backwards | Applies first keyframe styles during the animation-delay wait. |
both | Applies backwards before and forwards after — most flexible. |
.fly-in {
animation-name: slideIn;
animation-duration: 0.5s;
animation-delay: 0.3s;
animation-fill-mode: forwards; /* without it, the element snaps back! */
}
Animation Shorthand
/* Individual properties... */
.sprite {
animation-name: enemy1;
animation-duration: 0.8s;
animation-timing-function: steps(6);
animation-delay: 0;
animation-iteration-count: infinite;
animation-direction: normal;
}
/* ...same thing as one shorthand */
.sprite { animation: enemy1 0.8s steps(6) 0 infinite normal; }
/* Multiple animations — comma separated */
.hero { animation: fadeIn 0.4s ease forwards, float 3s ease-in-out 0.4s infinite alternate; }
Complete Sprite Animation Example
enemy1.png is a sprite strip containing 6 animation frames. steps(6) jumps between them while background-position slides from 0px to -1754px — producing a walking character loop.
/* sprite3.css */
#sprite-image {
height: 155px;
width: 295px;
background: url("enemy1.png") 0px 0px;
animation: enemy1 0.8s steps(6) 0 infinite normal;
}
@keyframes enemy1 {
100% { background-position: -1754px; }
}
<!doctype html>
<html>
<head>
<title>CSS Sprite Animation</title>
<link rel="stylesheet" href="sprite3.css">
</head>
<body>
<h1>CSS Animation</h1>
<div id="sprite-container">
<div id="sprite-image"></div>
</div>
</body>
</html> Activity — complete the code
Complete the keyframes and animation properties.
@ walk {to { background-position: -300px 0; }}.strip {animation-name: ;animation-duration: 0.9s;animation-timing-function: (3);animation-iteration-count: ;}
Modern CSS 2025 & Summary
The newest CSS features and the key takeaways for writing modern, maintainable stylesheets.
Modern CSS 2025
| Feature | What it does |
|---|---|
| CSS Nesting | Native nesting — no Sass needed |
| Container Queries | Respond to the parent container's width |
:has() | The long-awaited parent selector |
| CSS Cascade Layers | @layer for managing specificity |
| Scroll-driven animations | Zero JavaScript |
| CSS Custom Properties | Design tokens |
oklch() | Wide-gamut perceptual colour |
text-wrap: balance | Smart headings |
Key Takeaways
- Separate style from content — use external CSS.
box-sizing: border-box— always reset it globally.- Use
rem/emoverpxfor scalable layouts. - Flexbox for 1D layouts, Grid for 2D layouts.
animation-fill-mode: bothfor smooth entry/exit.steps()for sprite-sheet frame control.- Mobile-first with
min-widthmedia queries. - Validate at
jigsaw.w3.org/css-validator.
Activity — complete the code
Complete the three modern CSS 2025 features.
/* Parent selector */div:(img) { border: 1px solid; }/* Cascade layers */@ reset, base, components;/* Smart headings */h1 { text-wrap: ; }