Web Developer Path ยท lesson 13 of 18
Responsive design and the 12-column grid
Build a page that rearranges itself for phones, tablets and desktops, using a container, a row and twelve columns.
What you will learn
- why pages must be responsive, and the viewport meta tag
- what a CSS framework gives you, and how to add one
- the grid:
.container,.rowand.col-*classes that add up to 12 - breakpoints:
col-sm,col-md,col-lg,col-xl
Understand the idea
People open websites on phones, tablets, laptops and big monitors. A responsive page adapts to the width of the screen instead of forcing everyone to zoom and scroll sideways. It starts with one line in the head: <meta name="viewport" content="width=device-width, initial-scale=1">, which tells phones to use their real width. Designers work mobile first: make the small-screen layout simple and single-column, then add rules for wider screens.
A CSS framework is a ready-made stylesheet of classes for common jobs, so you do not write the same CSS for every project. Bootstrap 4, used in this module, is the best known. In a real page you add it with one <link> to its stylesheet (see the example). Here a small built-in version with the same class names runs in the preview, so you can practise without installing anything.
The layout system is a grid of twelve columns. Put a .container around the page content (it centres it and sets side padding), put a .row inside it, and put columns inside the row. A column class says how many of the 12 columns it should take: .col-6 takes half the row, .col-4 takes a third, and the columns in one row should add up to 12 (more than 12 wraps onto a new line).
The size name in the class says from which screen width the columns sit side by side: col-sm-* from 576px, col-md-* from 768px, col-lg-* from 992px and col-xl-* from 1200px in the real framework. Below that width the columns stack, one per line, so .col-md-4 means "a third wide on tablets and up, full width on phones". The preview is narrow, so its steps are shrunk to fit.
The preview has a small built-in version of the framework with the same class names, and its screen-width steps are shrunk to fit the narrow preview (the real ones are 576, 768, 992 and 1200 pixels).
Example
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4"><h3>One</h3><p>First column.</p></div>
<div class="col-md-4"><h3>Two</h3><p>Second column.</p></div>
<div class="col-md-4"><h3>Three</h3><p>Third column.</p></div>
</div>
</div>
</body>
</html>The <link> line loads the real framework on a live site. In the preview it is skipped and the built-in version is used instead.
Your turn: code to type and practise
Inside a div with class container, build a div with class row that holds three div columns with class col-md-4, each containing an h3 heading and a paragraph.
Stretch challenge
Change the columns to col-md-6 to make two halves, then try an 8 and 4 split with col-md-8 and col-md-4.
More in this module
Keep going in Web Developer Path
Typography classes
Write an `h1` with the class `display-4`, a paragraph with the class `lead`, and a paragraph that uses `text-center`, `text-muted` and `text-uppercase` (the same element can carry several classes).
Start โ15Containers and spacing utilities
Make a `container-fluid` with the classes `bg-light` and `p-4`, and inside it a heading with `mb-3` and a paragraph with `mb-0`. Then add a second `div` with the classes `container` and `mt-3` that holds a box with the class `mx-auto`.
Start โ16Colours, badges, alerts, buttons and borders
Build a `div` with the classes `alert` and `alert-success`, two `span` badges with the class `badge`, a `button` with `btn` and `btn-primary`, a `btn-group` holding three `btn` buttons, and a `div` that has both `border` and `rounded`.
Start โ17Navbars and cards
Build a `nav` with the classes `navbar` and `bg-dark`, a `navbar-brand` link and a `navbar-nav` list with at least two `nav-link` links. Then build a `card` containing a `card-body` with a `card-title`, a `card-text` and a link styled as a `btn`.
Start โ18Carousels and scrollspy
Build a carousel with the id `slides`: an indicators list with three `li` items, a `carousel-inner` with three `carousel-item` slides (only the first is `active`), and a `carousel-caption` in the first slide. Then write `<body data-spy="scroll" data-target="#mynav">` as the start of a page with a `nav` whose id is `mynav`.
Start โPractise the idea
๐ Play Fraction Pizza
Build, compare and add fractions by cutting up a pizza.
FAQ
Responsive design and the 12-column grid: common questions
What is responsive web design?
Designing pages so they adapt to different screen sizes, using flexible layouts and CSS rules that change with the screen width.
Why does the grid use 12 columns?
Twelve divides evenly into halves, thirds, quarters and sixths, so many layouts are easy to make.
What does col-md-4 mean?
A column that is four of twelve columns wide (a third) on medium screens and wider, and full width below that.
I am stuck. What can I do?
Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.
Does my work get saved?
Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.
Start a conversation
Enjoying coding? Meet a teacher.
Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay