⚡0 XP🔥0

Box Model, Flexbox, Grid and Responsive Design

Lesson 5 of 6 ⏱ 5 min read ❓ 5-question quiz ▶ Runs HTML

Margin, border and padding, box-sizing, display and position, Flexbox for rows and columns, CSS Grid for page layouts, and media queries for phones.

The box model#

Every element is a rectangular box made of four layers:

+--------------- margin ---------------+
|  +------------ border -------------+  |
|  |  +--------- padding ---------+  |  |
|  |  |          content          |  |  |
|  |  +---------------------------+  |  |
|  +---------------------------------+  |
+---------------------------------------+
▶ Try it YourselfHTML
Ctrl + Enter to run
Tip

Almost every modern site starts its CSS with this line, so width means the full visible width:

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

Shorthands go clockwise from the top: margin: 10px 20px 30px 40px; = top, right, bottom, left. margin: 10px 20px; = top/bottom 10, left/right 20. margin: 0 auto; centres a block that has a width.

हिंदी में समझें

हर element एक डिब्बे (box) की तरह है। अंदर content है, उसके चारों ओर padding (अंदर की गद्दी), फिर border (डिब्बे की दीवार), और सबसे बाहर margin (दूसरे डिब्बों से दूरी)। Padding अंदर की जगह है, margin बाहर की।

display: block, inline and inline-block#

Value Behaviour Examples
block starts on a new line, takes the full width div, p, h1, section
inline flows inside text; width/height ignored span, a, strong
inline-block flows inline but accepts width, height, padding buttons, badges
none hidden completely menus that open on click
flex / grid turns the element into a layout container see below

position#

Value Meaning
static normal flow (default)
relative normal flow, but can be nudged with top/left; becomes the anchor for absolute children
absolute taken out of the flow, placed relative to the nearest positioned ancestor
fixed stays at the same place on screen while scrolling (chat buttons, cookie bars)
sticky scrolls normally, then sticks at a top value (sticky headers)

Flexbox - layouts in one direction#

Make the parent display: flex and its children line up in a row (or a column).

▶ Try it YourselfHTML
Ctrl + Enter to run
On the container Does
flex-direction: row / column the main axis
justify-content spacing on the main axis: flex-start, center, space-between, space-around
align-items alignment on the cross axis: stretch, center, flex-start
flex-wrap: wrap let items move to a new line
gap: 12px space between items

On a child, flex: 1 1 150px means grow: 1, shrink: 1, start at 150px. Change the preview width and watch the cards wrap.

CSS Grid - layouts in two directions#

Grid is perfect for whole page layouts and card galleries.

▶ Try it YourselfHTML
Ctrl + Enter to run

repeat(auto-fill, minmax(100px, 1fr)) makes as many columns as fit, each at least 100px - a responsive gallery with no media queries.

Did you know?

Flexbox or Grid? Use Flexbox for a single row or column of items (navbars, buttons, a row of cards). Use Grid when you need rows and columns to line up (page layouts, galleries, dashboards). Real sites use both together.

Responsive design with media queries#

Most Indian users open websites on phones, so design mobile-first: write the phone layout first, then add rules for bigger screens.

▶ Try it YourselfHTML
Ctrl + Enter to run

Responsive checklist:

  1. <meta name="viewport" content="width=device-width, initial-scale=1"> in the head.
  2. img { max-width: 100%; height: auto; } so images never overflow.
  3. Flexible widths (%, fr, minmax) instead of fixed pixel widths.
  4. Media queries for the few places the layout must change.
  5. Test in the browser's device mode (F12 → phone icon).

Summary#

  • Box = content + padding + border + margin; use box-sizing: border-box.
  • display decides how a box flows; position takes it out of the flow when needed.
  • Flexbox: one-direction layouts with justify-content, align-items, gap.
  • Grid: two-direction layouts with grid-template-columns/rows/areas.
  • Mobile-first + media queries = responsive design.

Quick Quiz

Q1.

An element has width: 200px; padding: 20px; border: 5px solid; and the default box-sizing. How wide is it on screen?

Q2.

Which property is the space outside an element's border?

Q3.

In a flex container with flex-direction: row, which property centres items horizontally?

Q4.

What does grid-template-columns: repeat(3, 1fr) create?

Q5.

What does @media (max-width: 600px) { ... } do?