BRAINBIT INFOTECH · RANCHI
COMPLETE HTML + CSS COURSE · PART 3 OF 4

Layout & Responsive Design

Arrange pages like a pro with Flexbox and Grid, make them work on every phone, and bring them to life with animation.

5 modules~17.5 hours25 practice examplesNeeds Parts 1-2

By the end of this part you can build

  • Navbars, card rows and footers with Flexbox
  • Galleries and dashboards with Grid
  • Mobile-first pages that work on any screen
  • Dark mode, flip cards, loaders and progress bars

Every example opens in a live browser preview. Click ✎ Edit on any preview, or press L, to open the Code Lab.

ROADMAP FOR THIS PART

Module 13Flexbox4 h
Module 14CSS Grid4 h
Module 15Responsive Design & Media Queries3.5 h
Module 16CSS Variables, Dark Mode & Functions2.5 h
Module 17Transitions, Transforms & Animations3.5 h
HTML + CSSBefore we begin

How to Use This Part

Suggested teaching hours per module (Part 3)012354 hM134 hM143.5 hM152.5 hM163.5 hM17

Every module has

  1. 1
    A "Where is it used?" diagram of a real web page
  2. 2
    Concept slides with code + live browser preview
  3. 3
    Animations and playgrounds: click, drag and watch
  4. 4
    A Live Lab: edit code, see the result instantly
  5. 5
    5 practice examples with solutions
  6. 6
    An animated recap + quiz
Controls→ next, ← back, M menu, L Code Lab, F fullscreen. Click ✎ Edit on any preview to change its code.
MODULE 13

Flexbox

⏱ 4 hours5 practice examplesLive code lab

You will learn

  • Flex container and flex items
  • Main axis and cross axis
  • justify-content, align-items, flex-direction, flex-wrap, gap
  • flex-grow, flex-shrink, flex-basis, order, align-self
  • Perfect centring and common layout patterns

You will build

  • A navbar with logo left, links right
  • A wrapping row of product cards
  • A 3-column footer and a comment layout
main axis
Module 13Real-world use

Where Is This Used on a Web Page?

LOGOHeaderNavigation menuCards / boxesFooter

You will find it here

  • Navigation bars: logo on the left, links on the right
  • Card rows that wrap to the next line on small screens
  • Footers with 3-4 columns
  • Buttons with icons, centred content, form rows
  • Flexbox is used in almost every modern website
Module 13Why Flexbox?

One Line of CSS That Changed Web Layout

file:///D:/my-website/navbar.html
navbar.cssCSS
✘

Before Flexbox (old days)

Floats, tables and clever hacks. Centring something vertically was famously difficult.
✔

With Flexbox

display: flex on the parent, then simple words: space-between, center, wrap.
One dimensionFlexbox arranges items in one direction at a time – a row OR a column. For rows AND columns together, use Grid (Module 14).
Module 13Core idea

Container, Items and the Two Axes

.container { display: flex } item item item MAIN AXIS (justify-content) CROSSAXIS(align-items)
  1. 1
    Put display: flex on the parent (the container)
  2. 2
    Its direct children become flex items and line up in a row
  3. 3
    justify-content moves items along the main axis
  4. 4
    align-items moves them along the cross axis
  5. 5
    flex-direction: column swaps the axes
Common mistakeWriting display: flex on the items instead of the parent. Flex goes on the box that contains the things you want to arrange.
Module 13Playground

Flexbox Playground: Click and Watch

flex-direction
justify-content
align-items
flex-wrap
gap: 10px
container width: 600px
file:///D:/my-website/playground.html
.row { flex-direction: row; }
.row { justify-content: flex-start; }
.row { align-items: stretch; }
.row { flex-wrap: nowrap; }
.row { gap: 10px; }
.row { width: 600px; }
Module 13Cheat sheet

justify-content at a Glance (Main Axis)

flex-start
center
flex-end
space-between
space-around
space-evenly
Rememberspace-between: first and last stick to the edges (navbars). space-evenly: all gaps equal. center: group in the middle.
Module 13Cheat sheet

align-items at a Glance (Cross Axis)

stretch
Aa
Aa
Aa
flex-start
Aa
Aa
Aa
center
Aa
Aa
Aa
flex-end
Aa
Aa
Aa
baseline
Aa
Aa
Aa

Perfect centring

center.cssCSS
Module 13Wrapping

flex-wrap + gap: Cards That Fit Any Screen

Drag the slider: 4 cards in a row on desktop, 2 on tablet, 1 on mobile – with no media query!

products.cssCSS
  • flex-wrap: wrap lets items move to the next line
  • gap adds space between items (no margin tricks)
  • flex: 1 1 160px = grow, shrink, start at 160px wide
Module 13Item properties

flex-grow, flex-shrink, flex-basis (Item B)

flex-grow (B): 0
flex-basis (B): 80px
flex-grow (all)
order (B)
align-self (B)
PropertyMeaning
flex-growShare of the EXTRA space (0 = do not grow)
flex-shrinkCan it shrink when space is short? (1 = yes)
flex-basisStarting size before growing/shrinking
flex: 1Shorthand: grow 1, shrink 1, basis 0 – equal columns
orderChange visual order without changing HTML
align-selfOne item overrides align-items
file:///D:/my-website/playground.html
.b { flex-grow: 0; }
.b { flex-basis: 80px; }
.row div { flex-grow: 0; }
.b { order: 0; }
.b { align-self: auto; }
Module 13Patterns

Three Flexbox Patterns You Will Use Daily

file:///D:/my-website/patterns.html
PatternKey CSS
Navbarjustify-content: space-between; align-items: center
Media object (avatar + text)display: flex; gap; align-items: flex-start
Footer stays at bottompage: flex-direction: column; min-height, main: flex: 1
Evenly spread linksjustify-content: space-around
NestingA flex item can itself be a flex container (nav > .links). Real layouts are flex boxes inside flex boxes.
Module 13Live Lab

Live Lab: Header + Centred Hero

flex-lab.html – edit me!
Live preview
TryChange space-between to space-around. Add flex-direction: column to the header. Put 3 boxes in the hero and give the middle one flex: 2.
Module 13Practice Example 1 of 5

Navbar: Logo Left, Links Right

BeginnerRanchi Mart, an online grocery shop, needs a top navigation bar.

Your tasks

  1. header with display flex, space-between, align-items center
  2. Logo image + shop name grouped on the left (flex + gap)
  3. Links in a ul turned into a flex row (no bullets)
  4. A cart button at the end with a different colour
  5. Padding and background colour
Where this is used: The header of almost every website.
navbar.htmlHTML
Module 13Practice Example 2 of 5

Perfectly Centred Hero

BeginnerA coming-soon page for a new Jamshedpur gym must have its message exactly in the centre of the screen.

Your tasks

  1. Section with height 100vh (here 300px), background image gym.jpg
  2. display flex, justify-content and align-items center
  3. flex-direction column so heading and button stack
  4. gap between them
  5. Text white with a dark overlay
Where this is used: Landing pages, login screens, empty states, popups.
coming-soon.htmlHTML
Module 13Practice Example 3 of 5

Product Cards Row with Wrap

IntermediateJharkhand Handicrafts shows 4 products. On a phone they must wrap into fewer columns.

Your tasks

  1. Container: display flex, flex-wrap wrap, gap 14px
  2. Card: flex: 1 1 180px, white, rounded, shadow
  3. Inside each card use flex column so the button sits at the bottom (margin-top: auto)
  4. Image height 110px with object-fit contain
  5. Price in bold green
Hintmargin-top: auto on the button eats all free space above it, pushing it to the bottom. All buttons line up even when titles have different lengths.
Where this is used: E-commerce listings, course catalogues, blog post lists.
products.htmlHTML
Module 13Practice Example 4 of 5

Three-Column Footer

IntermediateBuild the footer for BrainBit Infotech: About, Quick Links and Contact columns, plus a bottom copyright bar.

Your tasks

  1. Footer columns in a flex row with gap and wrap
  2. Each column flex: 1 1 160px
  3. Column headings in gold, links in light grey
  4. Bottom bar centred with flex
  5. Dark background
Where this is used: Every website footer.
footer.htmlHTML
Module 13Practice Example 5 of 5

Comment / Chat Layout (Media Object)

AdvancedA school discussion forum shows comments with a photo on the left and text on the right; replies are indented; your own messages appear on the right.

Your tasks

  1. .msg: display flex, gap, align-items flex-start
  2. Avatar fixed size (flex: 0 0 44px)
  3. Text bubble grows (flex: 1)
  4. .reply indented with margin-left
  5. .me uses flex-direction: row-reverse and a green bubble
Where this is used: WhatsApp-style chats, comment sections, notifications, reviews.
chat.htmlHTML
Module 13Module complete

Recap: What You Completed in Module 13

  • ✓display: flex on the parent; children become items
  • ✓Main axis (justify-content) and cross axis (align-items)
  • ✓flex-direction row / column swaps axes
  • ✓flex-wrap + gap for responsive card rows
  • ✓flex: grow shrink basis; flex: 1 for equal columns
  • ✓order and align-self for single items
  • ✓Patterns: navbar, centring, footer, media object
100%Module 13 done

Coming next

Module 14: CSS Grid – rows AND columns together for galleries and dashboards.

Quick quiz – click an answer

Q1. Logo left, links right. Which value?

space-between pushes first and last items to the edges.

Q2. Centre vertically in a flex row:

In a row, vertical = cross axis = align-items.

Q3. Where do you write display: flex?

The container controls the layout of its children.
MODULE 14

CSS Grid

⏱ 4 hours5 practice examplesLive code lab

You will learn

  • Grid container, rows, columns and gap
  • The fr unit, repeat() and minmax()
  • Placing items with lines and span
  • Named areas with grid-template-areas
  • auto-fit for responsive grids; Flexbox vs Grid

You will build

  • A photo gallery
  • A school dashboard layout
  • A calendar month view
Module 14Real-world use

Where Is This Used on a Web Page?

LOGOWhole pageCards / boxes

You will find it here

  • Whole page layouts: header, sidebar, content, footer
  • Photo galleries and product grids
  • Dashboards with tiles of different sizes
  • Calendars, timetables, pricing tables
  • Magazine and news homepages
Module 14Grid vs Flex

Flexbox vs Grid: Which One?

FLEXBOX = 1 directionGRID = 2 directions
FlexboxGrid
DirectionRow OR columnRows AND columns
Thinks fromThe content outwardsThe layout inwards
Best forNavbars, button rows, small partsPage layouts, galleries, dashboards
Overlapping itemsHardEasy
Use both!Grid for the overall page, Flexbox inside each part. They work perfectly together.
Module 14Playground

Grid Playground: Columns and Gaps

grid-template-columns
gap: 10px
grid-auto-rows
justify-items
The fr unitfr = fraction of free space. 1fr 2fr 1fr splits into 4 parts: 1/4, 2/4, 1/4. Like dividing a cake.
file:///D:/my-website/playground.html
.grid { grid-template-columns: 1fr 1fr 1fr; }
.grid { gap: 10px; }
.grid { grid-auto-rows: auto; }
.grid { justify-items: stretch; }
Module 14Grid functions

repeat(), minmax() and the Magic auto-fit Line

You writeMeans
repeat(3, 1fr)Same as 1fr 1fr 1fr
repeat(2, 100px 1fr)100px 1fr 100px 1fr
minmax(200px, 1fr)At least 200px, at most 1 share
repeat(auto-fit, minmax(200px, 1fr))As many 200px+ columns as fit; stretch them to fill
gallery.cssCSS
Most copied line in CSSThis single line makes a fully responsive card grid with no media queries.

Drag the slider and watch the columns change from 6 to 1.

Module 14Placing items

Placing Items: Lines and span

grid-column (.big)
grid-row (.big)
123451 / 3

Grid lines are numbered from 1. 1 / -1 = from the first to the last line (full width).

file:///D:/my-website/playground.html
.big { grid-column: 1 / 3; }
.big { grid-row: 1 / 2; }
Module 14Named areas

grid-template-areas: Draw Your Layout with Words

layout.cssCSS
file:///D:/my-website/portal.html
Like drawing a mapEach string is one row; each word is one cell. Repeat a name to make an area span cells. A dot . means an empty cell.
Module 14Grid patterns

Changing the Map for Mobile (Preview of Module 15)

layout.cssCSS
DesktopheadsidemainfootMobileheadmainsidefoot
Same HTMLOnly the CSS map changes. This is why Grid areas are perfect for responsive layouts.
Module 14Live Lab

Live Lab: Dashboard Tiles

grid-lab.html – edit me!
Live preview
TryMake t1 span 3 columns. Change the columns to repeat(auto-fit, minmax(140px, 1fr)). Add a 5th tile. Try grid-auto-flow: dense.
Module 14Practice Example 1 of 5

Photo Gallery Grid

BeginnerTurn the Jharkhand tourism gallery from Module 3 into a neat grid.

Your tasks

  1. display: grid with 3 equal columns and gap 12px
  2. Images fill their cell: width 100%, height 140px, object-fit cover
  3. Rounded corners
  4. Caption under each image
  5. Change to repeat(auto-fit, minmax(160px, 1fr)) for responsiveness
Where this is used: Instagram-style galleries, portfolios, real-estate listings.
gallery.htmlHTML
Module 14Practice Example 2 of 5

School Dashboard with Named Areas

IntermediateBuild the layout of the RPS student portal: header, sidebar menu, main area with stats, and footer.

Your tasks

  1. grid-template-areas with head / side main / foot
  2. Sidebar 200px, main 1fr
  3. Main contains 3 stat cards in their own grid
  4. Each area gets a background and rounded corners
  5. min-height so the footer is at the bottom
Where this is used: Admin panels, school ERPs, banking dashboards (like BrainBit Infotech).
dashboard.htmlHTML
Module 14Practice Example 3 of 5

Responsive Course Cards with auto-fit

IntermediateThe BrainBit Infotech courses page should show as many course cards per row as fit the screen.

Your tasks

  1. repeat(auto-fit, minmax(200px, 1fr))
  2. Card with coloured top bar, title, duration and fee
  3. Gap 16px
  4. Card content uses flex column, button at the bottom
  5. Test by resizing the browser
Hintvar(--col) is a CSS variable. You will learn them properly in Module 16.
Where this is used: Course catalogues, pricing plans, blog listings.
courses.htmlHTML
Module 14Practice Example 4 of 5

Magazine Layout with span

AdvancedThe school magazine homepage has one big story, two medium ones and several small ones.

Your tasks

  1. 4-column grid, rows 110px (grid-auto-rows)
  2. Main story spans 2 columns and 2 rows
  3. Second story spans 2 columns
  4. Others take 1 cell each
  5. Each story: background image + dark gradient + white title at the bottom
Where this is used: News homepages, Pinterest-style boards, portfolio showcases.
magazine.htmlHTML
Module 14Practice Example 5 of 5

Calendar Month View

AdvancedBuild a November calendar for the school events board.

Your tasks

  1. 7-column grid for Mon to Sun
  2. Weekday headings in a dark row
  3. November 2026 starts on a Sunday: first date uses grid-column-start: 7
  4. Weekends in light red; event days highlighted with a label
  5. Today (15th) circled
HintBecause the first date starts in column 7, the nth-child(7n) trick for weekends no longer lines up. Check it, and try a class like .sun instead!
Where this is used: Booking calendars, event pages, attendance sheets.
calendar.htmlHTML
Module 14Module complete

Recap: What You Completed in Module 14

  • ✓display: grid with grid-template-columns / rows
  • ✓fr unit, repeat(), minmax()
  • ✓repeat(auto-fit, minmax(200px, 1fr)) for responsive grids
  • ✓Placing with lines (1 / 3, 1 / -1) and span
  • ✓grid-template-areas to draw layouts
  • ✓Changing the area map for mobile
  • ✓Grid for pages, Flexbox for parts
100%Module 14 done

Coming next

Module 15: Responsive Design – one website that works on phones, tablets and laptops.

Quick quiz – click an answer

Q1. 1fr 2fr splits the width:

3 parts: 1 part and 2 parts.

Q2. Make an item span the full width of the grid:

From the first line to the last line.

Q3. Best tool for a whole page layout with sidebar?

Grid handles rows and columns together.
MODULE 15

Responsive Design & Media Queries

⏱ 3.5 hours5 practice examplesLive code lab

You will learn

  • Why responsive: most users browse on phones
  • The viewport meta tag
  • Fluid layouts: %, max-width, flexible images
  • Media queries and breakpoints
  • Mobile-first CSS, responsive images, testing in DevTools

You will build

  • A 3-column to 1-column layout
  • A mobile menu that toggles (no JavaScript)
  • A mobile-first gym landing page
@media (max-width: 600px)
Module 15Real-world use

Where Is This Used on a Web Page?

LOGOWhole pageNavigation menuCards / boxes

You will find it here

  • Every modern site must work on a 360px phone and a 1920px monitor
  • Menus collapse into a hamburger icon on phones
  • Card rows go from 4 columns to 1 column
  • Images shrink to fit, text sizes adjust
  • Google ranks mobile-friendly pages higher
Module 15Why responsive?

Your Visitors Use Many Screen Sizes

Common screen widths (CSS pixels)Phone 360-430px360pxTablet 768-1024px768pxLaptop 1366px1366pxDesktop 1920px1920px

In India, most people visit websites on a mobile phone.

R

Responsive design

ONE website, ONE set of HTML, that adapts its layout to the screen size using CSS.

Three ingredients

  1. 1
    The viewport meta tag
  2. 2
    Fluid sizes (%, fr, max-width, rem) instead of fixed px widths
  3. 3
    Media queries to change the layout at certain widths
Module 15Step 1

The Viewport Meta Tag (Never Forget It!)

in the headHTML
PartMeaning
width=device-widthPage width = phone screen width
initial-scale=1.0No zoom when the page opens
Without itPhones pretend to be 980px wide and shrink the whole page. Text becomes tiny and users must pinch-zoom. Your media queries will not work either.
tiny text!WITHOUTWITH viewport
Module 15Step 2

Fluid Layouts and Flexible Images

Drag to 400px: the red box breaks out, the green box adapts.

fluid.cssCSS
  • Use max-width instead of a fixed width
  • img { max-width: 100%; height: auto; } – images never overflow
  • Use %, fr, rem and flex/grid instead of big px values
  • Avoid fixed heights on text boxes
Rule of thumbIf you scroll sideways on a phone, something has a fixed width that is too wide. Find it in DevTools.
Module 15Step 3

Media Queries: CSS That Depends on Screen Width

@mediaat-rule
(min-width: 600px)condition
{
...rules... apply only when true
}
media.cssCSS

Drag the slider across 600px and 992px.

QueryTrue when
(min-width: 600px)Screen is 600px or wider
(max-width: 599px)Screen is 599px or narrower
(orientation: landscape)Wider than tall
printWhen printing the page
Module 15Breakpoints

Breakpoints and Mobile-First Thinking

MobileTabletLaptop / Desktop0600px992px1400px+Mobile-first: write mobile CSS first,then ADD changes with min-width queries.
mobile-first.cssCSS
Why mobile first?Simple layout first, extra features for bigger screens. Phones load less CSS work, and you design for most of your users first.
Module 15Responsive menu

A Mobile Menu Without JavaScript (Checkbox Trick)

At 420px: tap ☰ in the preview. Then drag above 700px.

  1. 1
    A hidden checkbox remembers open/closed
  2. 2
    The ☰ label toggles the checkbox when tapped
  3. 3
    #menu-toggle:checked ~ nav shows the menu
  4. 4
    A media query shows the normal menu on wide screens and hides ☰
menu.cssCSS
Real sitesMost real sites use a few lines of JavaScript for this. The CSS-only trick is great for learning selectors.
Module 15Images

Responsive Images: srcset and picture

images.htmlHTML
ToolUse when
max-width: 100%Always (basic rule)
srcset + sizesSame picture, different file sizes → phones download smaller files
<picture>Different picture or crop for mobile (art direction)
loading="lazy"Load images only when scrolled near
Example file sizes of one photo (KB)013827641455245400w140800w4801600w

A phone on 4G should not download the 480 KB desktop version.

Module 15Testing

Test Like a Pro: DevTools Device Mode

  1. 1
    Open DevTools (F12)
  2. 2
    Click the phone/tablet icon or press Ctrl+Shift+M
  3. 3
    Choose a device (e.g. a common Android phone) or "Responsive" and drag the width
  4. 4
    Rotate to landscape, try slow 4G in the Network tab
  5. 5
    Finally, test on a real phone: same Wi-Fi + Live Server IP address
Checklist
No sideways scrolling at 320px☐
Text at least 16px, readable without zoom☐
Buttons/links at least 44px tall to tap☐
Menu usable on mobile☐
Images not stretched or cut badly☐
Forms use the right keyboards (tel, email)☐
Fluid typefont-size: clamp(1.5rem, 4vw, 3rem); grows with the screen but stays between a minimum and maximum (Module 16).
Module 15Live Lab

Live Lab: Mobile-First Columns

responsive-lab.html – edit me!
Live preview
TryClick Full screen and resize the browser window. Add a 4th subject. Change the 900px breakpoint to 4 columns. Make the background change colour on mobile only.
Module 15Practice Example 1 of 5

Three Columns to One Column

BeginnerThe "Why choose us" section of a coaching site shows 3 features side by side on laptops, but must stack on phones.

Your tasks

  1. Mobile first: features in one column (grid, gap)
  2. At 768px and up: 3 columns
  3. Each feature has an icon, heading and text
  4. Section padding smaller on mobile, bigger on desktop
  5. Test with the resizer / DevTools
Where this is used: Feature sections, services lists, team grids.
why-us.htmlHTML
Module 15Practice Example 2 of 5

Collapsible Mobile Menu

IntermediateMake the Ranchi Mart menu collapse into a ☰ button below 700px (checkbox trick).

Your tasks

  1. Hidden checkbox + label with ☰
  2. Menu hidden by default on mobile
  3. :checked ~ nav shows it as a vertical list
  4. Media query (min-width: 700px): hide ☰, show links in a row
  5. Header uses flex + wrap
Where this is used: Every mobile website header.
mobile-menu.htmlHTML
Module 15Practice Example 3 of 5

Responsive Hero Text

IntermediateThe Netarhat tour hero has a huge heading that breaks badly on phones.

Your tasks

  1. Heading uses font-size: clamp(1.6rem, 6vw, 3.2rem)
  2. Hero padding in rem and %
  3. Buttons stack on mobile (flex-direction column), row on 600px+
  4. Background image with overlay, cover
  5. Text centred on mobile, left-aligned on desktop
Where this is used: Landing page heroes, promo banners.
hero.htmlHTML
Module 15Practice Example 4 of 5

Responsive Table (Scroll on Mobile)

IntermediateA wide fee table breaks the page on phones. Make it scroll sideways inside its own box only.

Your tasks

  1. Wrap the table in .table-wrap with overflow-x: auto
  2. Table min-width: 600px
  3. Sticky first column with position: sticky; left: 0
  4. Zebra stripes and padding
  5. Small hint text "Swipe to see more" visible only on mobile
Where this is used: Pricing comparisons, timetables, results on mobile.
fees-table.htmlHTML
Module 15Practice Example 5 of 5

Mobile-First Gym Landing Page

AdvancedBuild the full landing page for Iron Fit Gym, Jamshedpur. Start with mobile CSS, then enhance for tablet and desktop.

Your tasks

  1. Viewport meta + box-sizing
  2. Mobile: header, hero, 1-column plans, footer
  3. 600px+: plans in 2 columns; 992px+: 3 columns and bigger hero
  4. Images max-width 100%, buttons at least 44px tall
  5. Fluid heading with clamp()
Where this is used: Every landing page you will ever build.
gym.htmlHTML
Module 15Module complete

Recap: What You Completed in Module 15

  • ✓Why responsive design matters
  • ✓Viewport meta tag in every page
  • ✓Fluid widths, max-width and max-width: 100% images
  • ✓Media queries: min-width / max-width / orientation / print
  • ✓Breakpoints (600, 992) and mobile-first CSS
  • ✓Checkbox-trick mobile menu
  • ✓srcset, picture, lazy loading; testing in DevTools
100%Module 15 done

Coming next

Module 16: CSS Variables, Dark Mode & Functions – themes and smart sizing.

Quick quiz – click an answer

Q1. Which tag is required for media queries to work properly on phones?

Without viewport, phones pretend to be ~980px wide.

Q2. Mobile-first CSS uses mostly:

Base styles for mobile, then add for bigger screens.

Q3. Stop images from overflowing:

They shrink to fit their container.
MODULE 16

CSS Variables, Dark Mode & Functions

⏱ 2.5 hours5 practice examplesLive code lab

You will learn

  • Custom properties (CSS variables) and :root
  • Theming: change one value, update the whole site
  • Dark mode with prefers-color-scheme and a toggle
  • calc(), min(), max() and clamp()
  • aspect-ratio and modern selectors :is() and :where()

You will build

  • A brand theme with variables
  • A dark-mode toggle (no JavaScript)
  • Fluid typography and video thumbnails
--brand: #e34c26;
Module 16Real-world use

Where Is This Used on a Web Page?

LOGOWhole pageButtonHeader

You will find it here

  • Brand themes: one colour variable used in buttons, links, headers
  • Dark mode on apps like YouTube and WhatsApp Web
  • Fluid sizes: headings and spacing that grow smoothly with the screen
  • Design systems: Bootstrap and Tailwind are built on variables
Module 16Variables

CSS Variables: Define Once, Use Everywhere

theme.cssCSS
--brand on :root
--radius on :root: 12px
file:///D:/my-website/playground.html
:root { --brand: #e34c26; }
:root { --radius: 12px; }
Module 16Variables

Variable Rules: :root, Fallbacks, Local Overrides

RuleExample
Name starts with ----brand: #e34c26;
Use with var()color: var(--brand);
Global in :root:root = the html element
Fallback valuevar(--accent, orange)
Local override.dark-card { --brand: gold; }
Case sensitive--Brand ≠ --brand
Why not just find-and-replace?Variables can change while the page runs: in a media query, inside one section, or for dark mode. Find-and-replace cannot do that.
--brandbuttonslinksbordersChange --brand once and all three update.
Module 16Dark mode

Dark Mode with Variables (Toggle in the Preview!)

file:///D:/my-website/dark-mode.html
Tick "Dark mode"Only the 4 variable values change. Every element using them updates with a smooth transition.
dark.cssCSS
auto-dark.cssCSS
:has()body:has(#dark:checked) means "the body that contains a ticked #dark checkbox". It works in all modern browsers.
Module 16Functions

Maths in CSS: calc(), min(), max(), clamp()

FunctionExampleResult
calc()calc(100% - 150px)Mix units with + - * /
min()min(90%, 400px)Smaller of the two
max()max(60px, 10vh)Bigger of the two
clamp()clamp(1.4rem, 5vw, 3rem)min, preferred, max
clamp(
1.4remnever smaller
, 5vwideal: grows with screen
, 3remnever bigger
)
Fluid typographyOne clamp() line replaces several media queries for font sizes.
Module 16Modern CSS

aspect-ratio and Modern Selectors :is() / :where()

file:///D:/my-website/modern.html
modern.cssCSS
  • aspect-ratio: 16 / 9 keeps video thumbnails the right shape at any width
  • aspect-ratio: 1 makes perfect squares (and circles with radius 50%)
  • :is(h3, h4):hover is short for h3:hover, h4:hover
  • :where() is the same but has zero specificity (easy to override)
Native CSS nestingModern browsers also allow writing .card { &:hover { ... } } – nesting like Sass, without any tool.
Module 16Live Lab

Live Lab: Theme + Dark Mode + clamp()

theme-lab.html – edit me!
Live preview
TryChange --brand to orange. In dark mode, also change --brand to a lighter green. Add --radius and use it on the button.
Module 16Practice Example 1 of 5

Brand Theme with Variables

BeginnerKanke Dhaba wants to try 3 colour themes before choosing. Build the page so that changing ONE line switches the theme.

Your tasks

  1. :root with --primary, --secondary, --text, --radius
  2. Header, buttons and price tags use the variables
  3. Add classes .theme-green and .theme-blue on body that override the variables
  4. Apply one theme class to test
  5. No colour value written twice
Where this is used: White-label products, theme settings in apps, WordPress themes.
themes.htmlHTML
Module 16Practice Example 2 of 5

Dark Mode Toggle (No JavaScript)

IntermediateAdd a dark mode switch to the school notice page using a checkbox and :has().

Your tasks

  1. Variables --bg, --text, --card, --accent in :root
  2. Dark values when body:has(#mode:checked)
  3. Smooth transition on background and colour
  4. Toggle styled as a pill switch using the label
  5. Also support prefers-color-scheme: dark
Where this is used: Apps and websites with light/dark themes.
dark-notice.htmlHTML
Module 16Practice Example 3 of 5

Fluid Typography with clamp()

IntermediateA news site wants headings that look right on any screen without many media queries.

Your tasks

  1. h1: clamp(1.75rem, 5vw, 3.5rem)
  2. h2: clamp(1.25rem, 3vw, 2rem)
  3. Body text: clamp(1rem, 1.2vw, 1.15rem)
  4. Section padding: clamp(12px, 4vw, 48px)
  5. Test with the resizer or DevTools
Where this is used: News sites, blogs, marketing pages.
fluid-type.htmlHTML
Module 16Practice Example 4 of 5

Sidebar Layout with calc()

IntermediateA documentation page has a fixed 220px sidebar and the content must take exactly the rest of the width.

Your tasks

  1. Sidebar width 220px, float or inline-block
  2. Content width calc(100% - 240px)
  3. Gap of 20px between them
  4. Content min-height calc(100vh - 80px) (here use 300px)
  5. A variable --side for the sidebar width used in both places
HintToday Grid (grid-template-columns: 220px 1fr) does this more easily, but calc() is still used everywhere for sizes.
Where this is used: Docs sites, admin panels, chat apps.
calc-layout.htmlHTML
Module 16Practice Example 5 of 5

Video Thumbnails with aspect-ratio

AdvancedThe BrainBit Infotech YouTube lessons page shows video thumbnails that must stay 16:9 at every screen size.

Your tasks

  1. Grid with repeat(auto-fit, minmax(180px, 1fr))
  2. Thumb box with aspect-ratio: 16 / 9 and object-fit cover
  3. Duration label absolute in the bottom-right corner
  4. Play icon centred with flex on hover (opacity transition)
  5. Title clamped to 2 lines with -webkit-line-clamp
Where this is used: YouTube-style video lists, course platforms, news video sections.
videos.htmlHTML
Module 16Module complete

Recap: What You Completed in Module 16

  • ✓Custom properties: --name and var(--name, fallback)
  • ✓Global :root and local overrides
  • ✓Themes by overriding variables on a class
  • ✓Dark mode: prefers-color-scheme and :has() toggle
  • ✓calc(), min(), max(), clamp()
  • ✓Fluid typography with clamp()
  • ✓aspect-ratio, :is(), :where() and nesting
100%Module 16 done

Coming next

Module 17: Transitions, Transforms & Animations – bring your pages to life.

Quick quiz – click an answer

Q1. How do you use a variable called --brand?

var() reads a custom property.

Q2. clamp(1rem, 4vw, 2rem) on a very small phone gives:

It never goes below the minimum.

Q3. Where are global variables usually defined?

:root is the top of the page, so every element can read them.
MODULE 17

Transitions, Transforms & Animations

⏱ 3.5 hours5 practice examplesLive code lab

You will learn

  • transition: smooth changes on hover and focus
  • Timing functions: ease, linear, ease-in-out, cubic-bezier
  • transform: translate, rotate, scale, skew; transform-origin
  • 3D flip cards with perspective
  • @keyframes animations, loaders, and accessible motion

You will build

  • Hover button effects
  • Flip flash-cards for revision
  • A loader, progress bars and a news ticker
@keyframes
Module 17Real-world use

Where Is This Used on a Web Page?

LOGOButtonCards / boxesImageHero banner

You will find it here

  • Buttons that lift or glow on hover
  • Cards that grow or flip to show more details
  • Images that zoom inside their frame
  • Loaders while data loads, progress bars, scrolling news tickers
  • Small, quick animations make a site feel professional
Module 17Transitions

transition: From Jump to Smooth

file:///D:/my-website/transition.html
Hover both buttonsSame hover styles. The first one animates over 0.3 seconds; the second jumps instantly.
transition:
background-colorproperty
0.3sduration
easetiming
0sdelay
button.cssCSS
  • Put transition on the normal state, not only on :hover
  • Works with numbers and colours: colour, size, opacity, transform, shadow
  • transition: all 0.3s is quick to write but can be slow
  • Good durations: 0.15s - 0.4s for UI
Cannot animatedisplay: none to block does not transition. Use opacity + visibility instead.
Module 17Timing

Timing Functions Race: Who Accelerates How?

file:///D:/my-website/race.html
FunctionFeels likeUse for
linearRobot, constant speedSpinners, tickers
ease (default)Natural start and stopMost UI
ease-inSlow start, fast endThings leaving the screen
ease-outFast start, gentle stopThings entering the screen
ease-in-outSlow-fast-slowMoving across the screen
cubic-bezier()Your own curve (even bounce)Playful effects
Design your curveChrome DevTools shows a curve editor when you click the small icon next to any timing function.
Module 17Transforms

transform Playground: Move, Turn, Grow, Tilt

transform
transform-origin
rotate (deg): 0deg
Why transform?transform does not push other elements around and is very fast (GPU). Prefer it over changing top/left or width for animations.
file:///D:/my-website/playground.html
.box { transform: none; }
.box { transform-origin: center; }
.box { rotate: 0deg; }
Module 173D

3D Flip Cards (Hover the Cards)

file:///D:/my-website/flip.html
  1. 1
    perspective on the outer box gives 3D depth
  2. 2
    transform-style: preserve-3d on the inner box
  3. 3
    Both faces position: absolute and backface-visibility: hidden
  4. 4
    Back face starts at rotateY(180deg)
  5. 5
    On hover rotate the inner box 180deg
flip.cssCSS
Module 17Keyframes

@keyframes: Animations That Run by Themselves

file:///D:/my-website/keyframes.html
bounce.cssCSS
PropertyExampleMeaning
animation-namebounceWhich @keyframes
animation-duration1sOne cycle
animation-timing-functionease-in-outSpeed curve
animation-delay0.5sWait before start
animation-iteration-count3 / infiniteHow many times
animation-directionalternateForwards then backwards
animation-fill-modeforwardsKeep the last frame
animation-play-statepausedPause (e.g. on hover)
Transition vs animationTransition: A to B when something changes (hover). Animation: many steps, can run by itself and loop.
Module 17Keyframes

Animation Playground: Speed, Direction, Repeat

animation-duration
animation-timing-function
animation-direction
animation-iteration-count
animation-play-state
RestartAfter changing iteration-count to 1 or 3, click ↻ in the preview bar to restart.
file:///D:/my-website/playground.html
.car { animation-duration: 2s; }
.car { animation-timing-function: ease-in-out; }
.car { animation-direction: alternate; }
.car { animation-iteration-count: infinite; }
.car { animation-play-state: running; }
Module 17Best practice

Smooth, Fast and Kind Animations

How cheap is it to animate? (5 = smooth)transform5/5opacity5/5colour / shadow3/5width / height2/5top / left / margin2/5

Changing size or position forces the browser to recalculate the whole layout on every frame.

accessibility.cssCSS
  • Keep UI animations short: 150-400 ms
  • Animate transform and opacity where possible
  • Do not animate everything: one or two effects per section
  • Never flash things more than 3 times per second
  • Honour prefers-reduced-motion
Module 17Live Lab

Live Lab: An Animated Tour Card

animate-lab.html – edit me!
Live preview
TryHover the card. Change scale(1.1) to scale(1.3) rotate(3deg). Make the tag pulse forever with a new @keyframes. Change the timing to linear.
Module 17Practice Example 1 of 5

Hover Button Effects

BeginnerCreate 4 different hover styles for the "Apply Now" button of a coaching site.

Your tasks

  1. All buttons: transition 0.3s
  2. .lift: move up 4px + bigger shadow
  3. .grow: scale(1.08)
  4. .fill: background slides in using a gradient and background-position
  5. .glow: coloured box-shadow ring on hover and focus
Where this is used: Call-to-action buttons on every landing page.
hover-buttons.htmlHTML
Module 17Practice Example 2 of 5

Flip Flash-Cards for Revision

IntermediateClass 10 students want flash cards: question on the front, answer on the back. Flip on hover (and on keyboard focus).

Your tasks

  1. 3 cards in a flex row with wrap
  2. Outer box with perspective 900px
  3. Inner box rotates 180deg on :hover and :focus-within
  4. Front: question, back: answer in a different colour
  5. Add tabindex="0" so keyboard users can focus a card
Where this is used: Quiz apps, learning sites, product cards that show details on hover.
flashcards.htmlHTML
Module 17Practice Example 3 of 5

Loading Spinner

BeginnerWhile results load on the JAC portal, show a spinner and a pulsing "Loading..." text.

Your tasks

  1. A 48px circle with a light border and one coloured side (border-top-color)
  2. @keyframes spin to 360deg, linear, infinite
  3. Text with a pulse animation on opacity
  4. Centre everything with flex
  5. A second "dots" loader with 3 dots and staggered animation-delay
Where this is used: Every app: page loading, form submitting, payments processing.
loader.htmlHTML
Module 17Practice Example 4 of 5

Animated Skill / Progress Bars

IntermediateA student portfolio page shows skill levels with bars that fill up when the page opens.

Your tasks

  1. Each bar: grey track, coloured fill
  2. Fill width from a variable: style="--w: 85%"
  3. @keyframes fill from width 0 to var(--w)
  4. Stagger each bar with animation-delay
  5. Show the percentage at the end of the bar
HintClick ↻ on the preview to replay the animation.
Where this is used: Portfolios, course progress, file uploads, survey results.
skills.htmlHTML
Module 17Practice Example 5 of 5

News Ticker for School Notices

AdvancedShow the latest notices scrolling across the top of the school website (like TV news).

Your tasks

  1. Ticker bar with a fixed "LATEST" label on the left (flex)
  2. Moving track with all notices in one line (white-space: nowrap)
  3. @keyframes scroll from translateX(100%) to translateX(-100%)
  4. Pause on hover with animation-play-state: paused
  5. Turn off for prefers-reduced-motion
Where this is used: News sites, school portals, stock prices, offer strips on shopping sites.
ticker.htmlHTML
Module 17Module complete

Recap: What You Completed in Module 17

  • ✓transition: property duration timing delay
  • ✓Timing functions: linear, ease, ease-in/out, cubic-bezier
  • ✓transform: translate, rotate, scale, skew + transform-origin
  • ✓3D flip with perspective and backface-visibility
  • ✓@keyframes + 8 animation properties
  • ✓Animate transform and opacity for smoothness
  • ✓prefers-reduced-motion for accessibility
100%Module 17 done

Coming next

Part 4: Pure-CSS components, professional workflow and 5 capstone websites.

Quick quiz – click an answer

Q1. Where should you write the transition property?

Then it animates both ways: in and out.

Q2. Which is smoothest to animate?

transform and opacity are handled by the GPU.

Q3. Loop an animation forever:

infinite repeats with no end.
PART 3 COMPLETE

Well done!

You have finished Part 3 of the Complete HTML + CSS Course.

Modules completed

  • Module 13: Flexbox
  • Module 14: CSS Grid
  • Module 15: Responsive Design & Media Queries
  • Module 16: CSS Variables, Dark Mode & Functions
  • Module 17: Transitions, Transforms & Animations

Keep all your practice files in D:\my-website. You will reuse them in the capstone projects.

</> Code Lab
Edit the code – the preview updates as you type
Live preview