Chapter 1 deck Chapter 2 deck Chapter 3 deck Chapter 4 deck Chapter 5 deck Chapter 6 deck Chapter 7 deck Chapter 8 deck Chapter 9 deck Chapter 10 deck Chapter 11 deck Chapter 12 deck Chapter 13 deck Chapter 14 deck

Chapter 3: Cascading Style Sheets (CSS)

62 slides · use ←/→ or Space to navigate, F for fullscreen.

Reading view

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)
Fullscreen for presentation · Print / PDF for a handout
1 / 62
Chapter 3

Chapter 3: Cascading Style Sheets (CSS)

CSS describes the presentation of HTML — layout, colours, fonts, spacing, layout systems and animation.

Lesson

Introduction to CSS & Its Evolution

What CSS is, what it does, and how it evolved from 1996 to 2025.

Introduction to CSS & Its Evolution

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-validator for cross-browser consistency.

CSS has been the industry standard since CSS1 in 1996.

Introduction to CSS & Its Evolution

CSS Evolution (1996 → 2025)

YearMilestone
1996CSS1 — basic selectors, fonts, colours, margins
1998CSS2 — positioning, z-index, media types
2011CSS3 modules — animations, gradients, flexbox
2017CSS Grid Layout for page layouts
2022@layer cascade layers, container queries
2025Nesting, @scope, scroll-driven animations, Houdini
Introduction to CSS & Its Evolution

What Does CSS Do?

AreaCapabilities
Text & fontsFont family, size, weight; line height, letter spacing; text decoration & transform
Layout & sizingWidth, height, min/max; Flexbox & Grid (2025 standard); positioning (static/relative/fixed)
Visual effectsShadows, gradients, opacity; filters & backdrop-blur; clip-path & masks
Animation & motionKeyframe animations; transitions between states; scroll-driven animations (2025)
Introduction to CSS & Its Evolution

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: ;
}

Lesson

CSS Syntax, Selectors & Specificity

The anatomy of a rule, the selector types, and how the cascade decides which style wins.

CSS Syntax, Selectors & Specificity

Anatomy of a CSS Rule

h1 {
  color: blue;
  font-size: 2rem;
}
PartMeaning
SelectorTargets the HTML element(s) to style
PropertyThe style attribute to change (e.g. color)
ValueThe specific style to apply (e.g. blue)
DeclarationA property: value pair, separated by semicolons
CSS Syntax, Selectors & Specificity

CSS Selectors

TypeExampleTargets
Elementh1 { }All <h1> tags
Class.highlight { }Elements with class="highlight"
ID#header { }The unique id="header" element
Attributeinput[type='email'] { }Matches by attribute value (CSS3+)
Pseudo-classa:hover { }Styles based on element state
Parent (2025)div:has(img) { }The long-awaited parent selector — now baseline in all browsers
CSS Syntax, Selectors & Specificity

CSS Specificity & the Cascade

When rules conflict, the more specific rule wins. From highest to lowest priority:

  1. !important — overrides everything
  2. Inline style
  3. ID selector (#id)
  4. Class / pseudo-class
  5. Element selector

2025 tip: use @layer instead of !important to manage specificity without conflicts.

CSS Syntax, Selectors & Specificity

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 */

Lesson

Applying Styles: Tag, Class & ID

The three core ways to target elements, and when to use each.

Applying Styles: Tag, Class & ID

Ways to Apply CSS Rules

MethodSyntaxNotes
Tag selectorh1 { 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.
Applying Styles: Tag, Class & ID

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.

Applying Styles: Tag, Class & ID

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">.
Applying Styles: Tag, Class & ID

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; }

Lesson

Levels of Style Sheets & Cascade Priority

Inline, embedded and external CSS — plus @import vs @layer.

Levels of Style Sheets & Cascade Priority

CSS Cascade Priority

From highest to lowest priority:

  1. Inline styles — <h1 style="color:red">. Applied directly on the element; highest priority.
  2. Embedded / document — <style> h1 { color: red; } </style> in the <head>. Applies to the whole document.
  3. External stylesheet — a separate .css file that can be shared across many pages.
  4. Browser default — the user-agent stylesheet. Lowest priority.
Levels of Style Sheets & Cascade 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.
Levels of Style Sheets & Cascade Priority

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.
Levels of Style Sheets & Cascade Priority

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 .css file — faster repeat visits.
  • Cleaner HTML — no style clutter mixed with content.

You can also import: @import url('style.css') inside another CSS file.

Levels of Style Sheets & Cascade Priority

@import vs @layer — 2025 Best Practice

  • @import loads 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 */
Levels of Style Sheets & Cascade Priority

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">

Lesson

Units, Fonts, Text & Colour

Value types, absolute vs relative units, typography properties and modern colour.

Units, Fonts, Text & Colour

CSS Units & Property Value Forms

Absolute unitsRelative units (2025 preferred)
px pixels
in inches
cm centimetres
mm millimetres
pt points
pc picas (12 points)
em — relative to parent font
rem — relative to root font
% — percentage of parent
vw / vh — viewport width/height
svh — small viewport height
cqi — 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)
Units, Fonts, Text & Colour

Font Properties

  • font-family — list of fonts; the browser uses the first available, e.g. 'Segoe UI', Arial, sans-serif
  • font-size — length value or keyword (1rem recommended)
  • font-style — normal | italic | oblique
  • font-weight — normal | bold | 100–900
  • font — 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; }
Units, Fonts, Text & Colour

Text Properties

PropertyValues
text-alignleft | center | right | justify
text-indentIndents the first line, e.g. 2rem
text-decorationunderline | overline | line-through | none
text-transformuppercase | lowercase | capitalize
line-heightSpacing between lines, e.g. 1.6 recommended
letter-spacing / word-spacingSpace 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;
}
Units, Fonts, Text & Colour

Colours in CSS — 2025 Complete Guide

FormExampleNotes
Namedcolor: tomato;147 named colours in CSS
Hex #RGBcolor: #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)
Units, Fonts, Text & Colour

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: ;
}

Lesson

Custom Properties, Lists & Spacing

CSS variables (design tokens), list styling and the classic spacing properties.

Custom Properties, Lists & Spacing

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;
  }
}
Custom Properties, Lists & Spacing

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 ::marker pseudo-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'); }
Custom Properties, Lists & Spacing

Text Alignment, Float & Margins

PropertyPurpose
text-alignleft | center | right | justify
text-indentIndents the first line of text
floatWraps text around an element (left | right | none)
marginSpace outside the element border
paddingSpace 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.

Custom Properties, Lists & Spacing

Activity — complete the code

Complete the custom property usage and list styling.

:root {
--primary: #2563eb;
}
 
.button {
background: (--primary);
}
 
ul {
list-style-type: ;
}

Lesson

The Box Model, Flexbox & Grid

Every element is a box — master the model, then lay out with Flexbox (1D) and Grid (2D).

The Box Model, Flexbox & Grid

The CSS Box Model

Every element is a box made of four layers, from outside in:

  1. Margin — space outside the border; pushes other elements away.
  2. Border — the visible frame around the element.
  3. Padding — space between the content and the border.
  4. Content — the actual text, image or other content.
The Box Model, Flexbox & Grid

Box Model in CSS — box-sizing Best Practice

  • The default box-sizing is content-box — width counts only the content.
  • Setting box-sizing: border-box includes padding + border in the width.
  • 2025 best practice: apply border-box globally 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 */
}
The Box Model, Flexbox & Grid

CSS Flexbox — Modern 1D Layout

PropertyValues / Purpose
display: flexTurns a container into a flex container
flex-directionrow (default) | column | row-reverse | column-reverse
justify-contentAligns items along the main axis: flex-start | center | flex-end | space-between | space-evenly
align-itemsAligns items along the cross axis: stretch | center | flex-start | flex-end | baseline
gapSpace between flex items (no margins needed!)
flex: 1Item grows to fill available space
flex-wrap: wrapItems 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 */
The Box Model, Flexbox & Grid

CSS Grid — Modern 2D Layout

FeaturePurpose
display: gridCreates a 2D grid container
grid-template-columns / -rowsDefine tracks
fr unitFractional share of available space — 1fr 2fr is 33% / 67%
gapRow and column gaps
grid-areaName areas for semantic placement
repeat(auto-fill, minmax(250px, 1fr))Responsive grid with NO media queries
subgrid2025: 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;
}
The Box Model, Flexbox & Grid

Activity — complete the code

Complete the global reset and the flex container.

*, *::before, *::after {
box-sizing: ;
}
 
.nav {
display: ;
justify-content: ;
gap: 1rem;
}

Lesson

Links, Pseudo-classes & Responsive Design

Interactive states and media/container queries for responsive layouts.

Links, Pseudo-classes & Responsive Design

Link & Interaction Pseudo-class Selectors

Pseudo-classMatches
a:linkUnvisited link — default state
a:visitedLink the user has already clicked
a:hoverMouse is over the link
a:activeLink is being clicked right now
button:focus-visible2025: keyboard focus only (not mouse click)
input:invalidForm field fails validation
:user-invalid2025: 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; }
Links, Pseudo-classes & Responsive Design

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-scheme and prefers-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; }
}
Links, Pseudo-classes & Responsive Design

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; }
}
Links, Pseudo-classes & Responsive Design

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; }
}

Lesson

CSS Animations & Sprite Sheets

@keyframes, the animation properties, and sprite-sheet animation with steps().

CSS Animations & Sprite Sheets

What is a CSS Animation?

  • Keyframe-based — @keyframes defines the animation. Specify styles at 0%, 50%, 100%, or use the from / to shorthand.
  • Bound to elements — animation-name links to a @keyframes rule; 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 Animations & Sprite Sheets

CSS Animation Properties

PropertyPurpose
@keyframesDefines the animation sequence with style steps
animation-nameLinks the element to the @keyframes rule name
animation-durationHow long one cycle takes (e.g. 1s, 300ms)
animation-delayWait before starting (negative = skip ahead)
animation-iteration-countNumber of cycles: 1, 3, infinite
animation-directionnormal | reverse | alternate | alternate-reverse
animation-timing-functionSpeed curve: ease | linear | steps(n) | cubic-bezier()
animation-fill-modeStyle before/after: none | forwards | backwards | both
CSS Animations & Sprite Sheets

@keyframes — Defining the Animation

  • Use from / to for simple two-state animations.
  • Use percentages (0%, 50%, 100%) for multi-step sequences.
  • The rule name is referenced by animation-name; multiple @keyframes can 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; }
}
CSS Animations & Sprite Sheets

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;
}
CSS Animations & Sprite Sheets

animation-delay

  • Positive value: the animation waits N seconds before starting.
  • Negative value: starts as if it has already been running — -2s starts 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; }
CSS Animations & Sprite Sheets

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.5 are 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; }
CSS Animations & Sprite Sheets

animation-direction

ValueBehaviour
normalPlays forwards (0% → 100%). Default.
reversePlays backwards (100% → 0%).
alternateForwards, then backwards on the next cycle.
alternate-reverseBackwards 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;
}
CSS Animations & Sprite Sheets

animation-timing-function

  • ease — slow start, fast middle, slow end (default)
  • linear — constant speed throughout
  • ease-in / ease-out / ease-in-out
  • cubic-bezier(x1,y1,x2,y2) — custom curve
  • steps(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 */
}
CSS Animations & Sprite Sheets

animation-fill-mode

ValueBehaviour
noneDefault — reverts to original styles before and after.
forwardsKeeps the final keyframe styles after the animation ends.
backwardsApplies first keyframe styles during the animation-delay wait.
bothApplies 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! */
}
CSS Animations & Sprite Sheets

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; }
CSS Animations & Sprite Sheets

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>
CSS Animations & Sprite Sheets

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: ;
}

Lesson

Modern CSS 2025 & Summary

The newest CSS features and the key takeaways for writing modern, maintainable stylesheets.

Modern CSS 2025 & Summary

Modern CSS 2025

FeatureWhat it does
CSS NestingNative nesting — no Sass needed
Container QueriesRespond to the parent container's width
:has()The long-awaited parent selector
CSS Cascade Layers@layer for managing specificity
Scroll-driven animationsZero JavaScript
CSS Custom PropertiesDesign tokens
oklch()Wide-gamut perceptual colour
text-wrap: balanceSmart headings
Modern CSS 2025 & Summary

Key Takeaways

  • Separate style from content — use external CSS.
  • box-sizing: border-box — always reset it globally.
  • Use rem / em over px for scalable layouts.
  • Flexbox for 1D layouts, Grid for 2D layouts.
  • animation-fill-mode: both for smooth entry/exit.
  • steps() for sprite-sheet frame control.
  • Mobile-first with min-width media queries.
  • Validate at jigsaw.w3.org/css-validator.
Modern CSS 2025 & Summary

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: ; }