Skip to main content

Lesson 3 of 5 18 min read

CSS selectors, layouts and responsive design

Style pages with CSS selectors and the box model, lay out content with Flexbox, and adapt to screen sizes with media queries.

In this lesson you will

  • Link a stylesheet and write rules with element, class and id selectors
  • Explain the box model and use margin, padding and border
  • Build flexible layouts with Flexbox and make them responsive with media queries

Adding CSS to a page

CSS (Cascading Style Sheets) is usually written in a separate file and linked from the <head>:

HTML
<link rel="stylesheet" href="styles.css">

A CSS rule has a selector and a block of declarations:

CSS
h1 {
  color: #0B1220;
  font-size: 2rem;
}

h1 is the selector, color is a property and #0B1220 is its value.

Selectors

CSS
p { line-height: 1.6; }               /* every <p> element */
.card { border-radius: 12px; }        /* every element with class="card" */
#signup { background: #2563EB; }      /* the one element with id="signup" */
nav a { text-decoration: none; }      /* <a> elements inside <nav> */
a:hover { color: #22D3EE; }           /* links while the pointer is over them */

Classes are the workhorse of CSS: you can reuse a class on many elements, and an element can have several classes (class="card featured"). IDs must be unique, so reserve them for one-off elements.

When two rules target the same element, the more specific selector wins (an id beats a class, which beats an element). If specificity is equal, the rule that comes later in the file wins. This is the cascade.

The box model

Every element is a rectangular box made of four layers:

The box model, from the outside in

Each inside the previous: Margin contains Border contains Padding contains Content

CSS
.card {
  padding: 16px;              /* space inside the border */
  border: 1px solid #CBD5E1;
  margin: 24px 0;             /* space outside the border */
}

* {
  box-sizing: border-box;     /* width includes padding and border */
}

With box-sizing: border-box, a box declared width: 300px stays 300px wide even after you add padding, which makes layouts far easier to reason about.

Layout with Flexbox

Flexbox arranges children in a row or column and distributes space between them:

CSS
.nav {
  display: flex;
  justify-content: space-between; /* spread items along the main axis */
  align-items: center;            /* centre items on the cross axis */
  gap: 16px;                      /* space between items */
}

.cards {
  display: flex;
  flex-wrap: wrap;                /* wrap onto new lines when space runs out */
  gap: 24px;
}

.cards .card {
  flex: 1 1 250px;                /* grow, shrink, start at 250px */
}

With flex-wrap: wrap and flex: 1 1 250px, the cards sit side by side on wide screens and stack automatically on narrow ones. CSS Grid is another powerful layout system for two-dimensional layouts; Flexbox is the best place to start.

Responsive design with media queries

More than half of web traffic comes from phones, so pages must adapt to the screen width. Start with styles for small screens (mobile-first), then add media queries for larger ones:

CSS
.hero h1 {
  font-size: 1.75rem;
}

@media (min-width: 768px) {
  .hero h1 {
    font-size: 3rem;
  }
}

The rule inside @media (min-width: 768px) applies only when the viewport is at least 768px wide. Remember the viewport <meta> tag from the previous lesson; without it, phones pretend to be a wide desktop screen and media queries don't behave as expected.

Hands-on exercise

Create styles.css for the page you built in the previous lesson. Give the body a readable font and line-height, style the nav links with Flexbox so they sit in a row with a gap, and turn each project into a card with padding, a border and rounded corners. Add a media query so the cards show side by side only when the screen is at least 768px wide. Test by resizing the browser window or using the device toolbar in developer tools.

Knowledge check

3 questions • 70% to pass • Practice only, no certificate

  1. 1.Which property adds space inside an element's border?
  2. 2.Which declaration places a container's children in a row?
  3. 3.An element has class="btn" and id="buy". Both .btn and #buy set a colour. Which colour applies?
0 of 3 answered

Key concepts

  • A CSS rule is a selector plus declarations of properties and values.
  • Classes are reusable; ids are unique; more specific selectors win in the cascade.
  • Every element is a box of content, padding, border and margin; box-sizing border-box simplifies sizing.
  • Flexbox lays out items in a row or column, and mobile-first media queries adapt layouts to larger screens.