Skip to content
nuggetof joy

Post · October 20, 2026

Box-sizing and @ Media Screen

AMDE 155 · Introduction to Web Design

Box-Sizing +
Responsive CSS

Learn why boxes sometimes become wider than expected, how box-sizing: border-box makes dimensions easier to control, and how @media screen rules help a page adapt to different screen sizes.

HTML + CSSBox modelMedia queriesCodePen lab

Start here / Your roadmap

1

Understand the box

Learn how content, padding, borders, and margins affect an element.

2

Fix the sizing

Use box-sizing: border-box to make widths more predictable.

3

Adapt to screens

Use @media screen to change styles at a chosen viewport width.

4

Build and test

Use one HTML panel in CodePen, then resize the preview to test your work.

Today’s goal: Create a simple card layout that fits neatly on a large screen and remains readable on a phone.

01 / What is the CSS box model?

Browsers treat each HTML element as a box. That box is made up of content, padding, border, and margin.

Content

The text, image, or other material inside an element. By default, width and height refer to this area.

Padding

The space between the content and its border. Padding is inside the border.

Border

The visible edge around the content and padding.

Margin

The space outside the border that separates this element from others.

Remember: With the default content-box model, padding and borders are added to the declared width. This can make an element wider than you planned.

02 / Why box-sizing matters

Imagine you set an element’s width to 300 pixels, add 20 pixels of padding on each side, and add a 5-pixel border on each side.

Default: content-box

Declared width: 300px

300px content
+ padding + borders

Total outside width: 350px

300 + 40 padding + 10 border = 350px.

With border-box

Declared width: 300px

300px total width
includes padding + borders

Total outside width: 300px

The content area shrinks to make room inside the 300px total.

These are illustrations of the two sizing models. The important difference is what the declared width includes.

03 / The one-line fix

A common best practice is to apply box-sizing: border-box to every element, including pseudo-elements.

*,
*::before,
*::after {
  box-sizing: border-box;
}

The * selector means “all elements.” The pseudo-element selectors help ensure generated content follows the same sizing rule.

Why use it? If you set an element to 300px wide, its padding and border fit within those 300 pixels. That makes cards and columns easier to size.

04 / What is @media screen?

A media query lets you apply CSS only when a condition matches. The screen media type targets screen-based devices and displays. A max-width condition can apply different styles when the viewport becomes narrow.

/* Default: three columns on a wider screen */
.card-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 20px;
}

/* At 600px wide or narrower, use one column */
@media screen and (max-width: 600px) {
  .card-grid {
    grid-template-columns: 1fr;
  }
}

Default rules

The styles outside the media query apply unless a matching rule overrides them.

max-width: 600px

The styles inside apply when the viewport is 600 pixels wide or narrower.

1fr

The cards become one column, giving each card the available width.

Key idea: Responsive design is not just shrinking everything. Decide what should rearrange, resize, or stack on a smaller screen.

05 / See a responsive layout

This sample layout starts with three cards. At tablet size it changes to two columns, and on a phone it becomes one column.

Card 01
Featured project
Card 02
Project notes
Card 03
More details

Resize your browser window to see this lesson page’s cards reflow. The example above is a visual reference for the assignment you will build in CodePen.

06 / Your CodePen activity

Build a responsive three-card page

Work entirely in CodePen. Use the HTML editor only for this exercise: put your page structure inside the HTML panel and place CSS inside a <style> element in the same panel.

  1. Open CodePen and create a new Pen.
  2. In the HTML panel, paste the starter code below.
  3. Change the page title, heading, card titles, and descriptions to make the page your own.
  4. Keep the universal box-sizing rule and the two media queries.
  5. Resize the preview and check the layout at wide, medium, and narrow widths.
  6. Fix any overflow, cramped text, or uneven spacing before submitting.

Starter code / paste everything into CodePen’s HTML panel

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Responsive Cards</title>
  <style>
    *,
    *::before,
    *::after {
      box-sizing: border-box;
    }

    body {
      margin: 0;
      padding: 24px;
      font-family: Arial, sans-serif;
      background: #f4f1eb;
      color: #182033;
    }

    .page {
      max-width: 1000px;
      margin: 0 auto;
    }

    h1 {
      font-size: clamp(2rem, 5vw, 3.5rem);
      line-height: 1.05;
    }

    .card-grid {
      display: grid;
      grid-template-columns: repeat(3, minmax(0, 1fr));
      gap: 18px;
    }

    .card {
      padding: 22px;
      border: 2px solid #182033;
      background: white;
      min-width: 0;
    }

    .card h2 {
      margin-top: 0;
    }

    /* Medium screens: two columns */
    @media screen and (max-width: 800px) {
      .card-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
      }
    }

    /* Small screens: one column */
    @media screen and (max-width: 600px) {
      body {
        padding: 14px;
      }

      .card-grid {
        grid-template-columns: 1fr;
      }

      .card {
        padding: 18px;
      }
    }
  </style>
</head>
<body>
  <main class="page">
    <p>AMDE 155 / Responsive layout practice</p>
    <h1>A Collection of Ideas</h1>
    <p>A short introduction to your collection. Replace this text with your own topic.</p>

    <section class="card-grid">
      <article class="card">
        <h2>Idea One</h2>
        <p>Describe the first item in your collection.</p>
      </article>
      <article class="card">
        <h2>Idea Two</h2>
        <p>Describe the second item in your collection.</p>
      </article>
      <article class="card">
        <h2>Idea Three</h2>
        <p>Describe the third item in your collection.</p>
      </article>
    </section>
  </main>
</body>
</html>
CodePen tip: This is a complete HTML document for the HTML panel. Because the CSS is inside the <style> element, you do not need to use CodePen’s separate CSS panel for this activity.

07 / Your checklist

  • The entire activity is completed in one CodePen HTML panel.
  • The page includes a heading, introduction, and at least three cards.
  • The universal box-sizing: border-box rule is included.
  • The cards use CSS Grid on wider screens.
  • At least two @media screen rules change the layout at different widths.
  • The page changes to two columns at medium width and one column on a small screen.
  • Text remains readable and elements do not overflow the viewport.
  • You customize the content and at least some visual styling.
  • You save your Pen and submit its share URL as instructed in class.
Quick reflection: What changed when you added box-sizing: border-box? What does each media query change? At what width did your layout need the biggest adjustment?

Quick reference

GoalCSSWhat it does
Use predictable sizingbox-sizing: border-box;Includes padding and border within declared width and height.
Make a griddisplay: grid;Turns the element into a grid container.
Three equal columnsrepeat(3, minmax(0, 1fr))Creates three flexible columns.
One columngrid-template-columns: 1fr;Stacks items in one column.
Apply styles by screen width@media screen and (max-width: 600px)Applies enclosed rules at 600px wide or narrower.