Software Development free courses

Master the TrickFoundationFree

Responsive CSS Layouts

Build a flexible layout with box-model reasoning, Grid or Flexbox, and narrow-screen accessibility checks.

3 reading lessons · about 30 min ·written by MTT

The idea

An element occupies space through its content, padding, border and margins. With content-box sizing, padding and borders add to the declared width; with border-box, they fit inside it. Fixed widths can overflow small viewports. Prefer flexible sizing with sensible limits, and make images fit their containers while preserving their aspect ratio.

Worked example

A 300-pixel content-box element with 16-pixel padding on both sides and 1-pixel borders occupies 334 pixels before margins. In a 320-pixel viewport it overflows. Border-box includes that padding and border within the 300 pixels, but a flexible width is still needed when the available parent space is smaller.

Try it

Calculate the outer width of a 240-pixel content-box element with 12-pixel padding and 2-pixel borders on both sides. Recalculate using border-box. Sketch a responsive image container and explain why setting an image's width without preserving its ratio can distort it.

Lesson 1 of 3 · About 10 min

Account for the box model

Check your understanding

Course quiz

Finish the course to unlock the quiz

Complete all 3 lessons and 5 questions open up here. You have 3 to go.

What you will learn

  • Explain content, padding and border sizing
  • Choose Grid or Flexbox for a layout
  • Test wrapping, focus and horizontal overflow

Before you start

Prerequisites
Basic HTML familiarity. A text editor and browser are useful for the optional practice.
Cost
Free introductory reading lessons, exercises and quiz. Optional third-party tools, hosting or AI subscriptions may cost money.

Original introductory lessons and assessment by Master the Trick. Estimated times include the suggested exercises.