Loading

T2 CSS · Lesson 3

The box model

The four layers of every box: content, padding, border and margin; a box's real width and box-sizing; margins that collapse and centre; block, inline and inline-block, and ways to hide; and overflow.

  • name the four layers of every box, and set each side of padding, border and margin
  • work out a box's real width, and make width mean the whole box with box-sizing
  • space boxes with margins, centre a box, and predict when margins collapse
  • choose between block, inline and inline-block, and hide an element with or without its space
  • decide what happens to content too big for its box, with overflow
About 60 minutesChecked against Node.js on the laptop (the kit runs every code block with it) 22.23.1 (2026-11-06)Play this lesson's games

The back cover

Every element on a page is drawn as a box, even a single word. Look closely and each box has four layers, like a framed photo on a wall. There is the picture, the card mount around it, the frame, and the bare wall that keeps it apart from its neighbours.

These pages name the four layers and the short forms that set them. They show why a box can come out wider than you asked, and the one line that fixes it. They explain margins that collapse and margins that centre. They cover the three kinds of display, the two ways to hide a box, and content too big for its box.

Before you start

The story: A notice that will not fit

The harbour wall has a row of notice frames, each exactly 320 wide. Meher gives you the slot for the Fair. Corin has already tried his own notice in the next slot. He made it 320 wide, then added a soft lining all round and a stitched edge, and now it sticks out over his neighbour's frame.

Worse, his list of opening hours runs out of the bottom of the notice and over the one below it. And his two notices touch, with no gap at all.

Meher hands you her measuring tape. "Every notice is a box inside a box inside a box," she says. "Measure each layer, and it will fit."

Lessons 1 and 2 coloured and sized text. This lesson is about the boxes the text sits in. Every element on a page, every heading, paragraph, button and picture, is drawn as a rectangle, and every rectangle has the same four layers. Once you can see the layers, most "why does it not fit?" questions answer themselves. It builds on these ideas; each has a tiny example.

What you already knowA tiny exampleWhere it comes back
Block and inline elements.A div starts a new line; a span sits inside one.Each kind treats width, height and spacing differently.
background-color fills a box.A maroon board behind white letters.It shows exactly how far the box reaches.
Units: px, em, rem, %.padding: 1em; grows with the text.Every layer of the box is measured in them.
% is a share of the parent.width: 50% of a 400px box is 200px.Even padding in % is a share of the parent's width.
The browser's defaults.A p has a 16px margin; the body has 8px.Many "mystery gaps" are these defaults.
The doctype and quirks mode.Without the doctype, old rules apply.The box model is measured differently in quirks mode, so every page here starts with the doctype.
Checks measure what was drawn.getBoundingClientRect() in the HTML lessons.Every block here measures its boxes in pixels.
A tailor's layers.Cloth, lining, stitched edge, and the space around the garment.The four layers of every box, from the inside out.
DevTools' Computed tab.It shows the final size of every property.Its box diagram shows all four layers at once.

By the end of this lesson you will be able to:

  • name the four layers of every box, and set each side of padding, border and margin;
  • work out a box's real width, and make width mean the whole box with box-sizing;
  • space boxes with margins, centre a box, and predict when margins collapse;
  • choose between block, inline and inline-block, and hide an element with or without its space;
  • decide what happens to content too big for its box, with overflow.

In one line

Every element is a box of four layers, content, padding, border and margin; you need lesson 2's units and the HTML step's block and inline elements.

The four layers of every box

At a glance

Every box has four layers, from the inside out. The content is the text or picture itself. Padding is space inside the box, around the content, and it shows the box's background colour. The border is a line around the padding, with a width, a style and a colour. A border with no style is not drawn at all.

Margin is see-through space outside the border, keeping other boxes away. Each layer can be set side by side, and the short forms take one to four values, going round clockwise from the top.

Take a guess: If a box has 10px of padding and a 2px border, how far is its content from the border's outer edge?

Picture a framed photo on a wall. In the middle is the photo itself. Around it, a white card mount gives it room to breathe. Around the mount, the wooden frame. And around the frame, the bare wall that keeps it apart from the next photo. CSS draws every element the same way, as four layers, from the inside out.

LayerLikeWhat it isIts background
contentthe photothe text or picture itselfthe element's background-color
paddingthe card mountspace inside the border, around the contentthe same background-color
borderthe framea line around the paddingits own colour
marginthe wall aroundspace outside the border, keeping other boxes awayalways see-through
<!DOCTYPE html>
<style>
  .notice {
    padding: 20px;
    border: 5px solid maroon;
    margin: 30px;
    background-color: lightyellow;
  }
</style>
<p class="notice">Lamplighters' Fair, Saturday</p>

Checked in Chrome

  • 20px of padding inside the border
  • a 5px solid border
  • 30px of margin outside it

Look at the page: the yellow reaches out to the border, because padding shares the content's background. Outside the border, the margin is see-through, so the page shows through it. That is the one way to tell padding from margin by eye: padding is inside the colour, margin is outside it.

A border needs a style

The border property takes three things: a width, a style and a colour, as in 5px solid maroon. The style is the kind of line: solid, dashed, dotted or double. It is the one part you cannot leave out: with no style, there is no border at all, whatever the width.

<!DOCTYPE html>
<style>
  .a { border: 3px dashed teal; }
  .b { border: 3px teal; }
</style>
<p class="a">A dashed border</p>
<p class="b">No style, so no border</p>

Checked in Chrome

  • a dashed line
  • (the slip: with no style, the border's width is 0)

Each side on its own

Padding, border and margin each have four sides, and each side can be set alone: padding-top, margin-left, border-bottom, and so on. The short forms set several sides at once, and the number of values decides which.

You writeMeansRead it as
padding: 10pxall four sides 10pxone value: everywhere
padding: 10px 20pxtop and bottom 10px; left and right 20pxtwo values: up-down, then sideways
padding: 10px 20px 30pxtop 10px; left and right 20px; bottom 30pxthree values: top, sideways, bottom
padding: 10px 20px 30px 40pxtop 10px, right 20px, bottom 30px, left 40pxfour values: clockwise from the top

A memory aid

Four values go clockwise from twelve o'clock: top, right, bottom, left. Some remember it as TRouBLe: T, R, B, L. Margin and border-width use the same order.

<!DOCTYPE html>
<style>
  .two { padding: 10px 20px; }
  .four { margin: 1px 2px 3px 4px; }
</style>
<div class="two">Two values</div>
<div class="four">Four values</div>

Checked in Chrome

  • two values: top and bottom 10px, sides 20px
  • four values: top 1, right 2, bottom 3, left 4

Padding in %: a surprise

Padding and margin in % are a share of the parent's width, on every side, even the top and bottom. So padding: 10% inside a 400px parent is 40px at the top, not a share of any height.

<!DOCTYPE html>
<style>
  .shelf { width: 400px; }
  .card { padding: 10%; background-color: lightyellow; }
</style>
<div class="shelf"><div class="card">Toor dal</div></div>

Checked in Chrome

  • 10% of the shelf's 400px width, even at the top: 40px

Worked example 1

A price tag should have 8px of space above and below its text and 16px at the sides, a 2px solid maroon edge, and 12px of space between it and anything around it. Write the rule.

What we have: spacing inside, an edge, and spacing outside

What we need: padding, border and margin, with the right number of values

The plan: inside the edge is padding (two values: up-down, sideways); the edge is border; outside is margin (one value)

<!DOCTYPE html>
<style>
  .tag {
    padding: 8px 16px;
    border: 2px solid maroon;
    margin: 12px;
  }
</style>
<p class="tag">₹180</p>

Checked in Chrome

  • 8px above and below, 16px at the sides
  • a 2px maroon edge
  • 12px all round, outside
  • Space inside the edge, around the text: padding. Space outside the edge: margin.
  • padding: 8px 16px; border: 2px solid maroon; margin: 12px;

    Check: the checks read every side of each layer, and each holds the value asked for.

    When you are stuck: the four layers

    Inside the colour, around the content: padding. The line: border. Outside, keeping others away: margin.

    A border that will not show: give it a style, such as solid.

    Short forms: one value everywhere, two for up-down then sideways, four clockwise from the top.

    Your turn

    Which layer shares the content's background colour?Tap to reveal
    1. The card mount, not the wall.

    The padding.

    What does margin: 0 20px set?Tap to reveal
    1. Two values.

    Top and bottom 0; left and right 20px.

    Why does border: 3px teal draw nothing?Tap to reveal
    1. One part of a border cannot be left out.

    It has no style, such as solid, so there is no border.

    Did you know?

    As old as CSS itself

    The four layers are as old as CSS. The first CSS standard, a W3C Recommendation of 17 December 1996, says that each element results in one or more rectangular boxes. It goes on: all boxes have a core content area, with optional padding, border and margin areas around it. The CSS 2.1 standard describes every box with the same four areas.

    Why it fits here: This topic's four layers, content, padding, border and margin, are the very areas that first standard named.

    Sources: W3C: CSS level 1 (1996); W3C: CSS 2.1, Box model

    Half of the width, not the height

    A padding-top of 50% is half the parent's width, not half its height. The CSS 2.1 standard says padding percentages come from the width of the containing block, even for padding-top and padding-bottom. Developers once used this on purpose. A box with no height and a padding-top of 56.25% keeps the 16:9 shape of a video at any width, because 9 ÷ 16 = 0.5625.

    Why it fits here: The lesson's part on padding in percent shows this rule; the old trick shows how it was put to use.

    Sources: W3C: CSS 2.1, Box model; web.dev: The aspect-ratio property

    In one line

    Every box is content, then padding (inside the colour), then a border (width, style and colour), then margin (outside, see-through); short forms take one, two, three or four values, clockwise from the top.

    How wide is a box, really?

    At a glance

    You set width: 300px on a box, add some padding and a border, and the box comes out wider than 300 pixels. That is because, by default, width and height measure only the content. The padding and the border are added outside it, on both sides.

    One line changes the rule. With box-sizing: border-box, the width is the whole box, border included, and the content shrinks to fit. Limits such as min-width and max-width let a box grow and shrink between two sizes instead of keeping one fixed size.

    Take a guess: If a box has width: 200px, padding: 20px and a 5px border, how wide is it on the screen?

    You order a photo frame for a 30 cm wall space, and you tell the framer "30 cm". The framer makes the photo 30 cm, then adds a 2 cm mount and a 1 cm frame on each side. The finished frame is 36 cm, and it does not fit the wall. CSS, by its old default, measures the same way.

    width means the content, by default

    The width and height properties set the size of the content layer only. Padding and border are added outside it. Margin is outside that again, as space, but it is not part of the box you see.

    <!DOCTYPE html>
    <style>
      .notice {
        width: 300px;
        padding: 20px;
        border: 5px solid maroon;
      }
    </style>
    <div class="notice">Lamplighters' Fair</div>

    Checked in Chrome

    • drawn 350px wide: 300 + 20 + 20 + 5 + 5
    • though its width says 300px
    LayerLeftContentRightRunning total
    content300300
    padding2020340
    border55350

    So the real width is width + padding on both sides + border on both sides. This surprises everyone once, as it did Corin in the story.

    box-sizing: border-box

    The property box-sizing changes what width means. With box-sizing: border-box, width is the size of the whole box, border included, and the content shrinks to fit inside. Most developers prefer this: 300 means 300.

    <!DOCTYPE html>
    <style>
      .notice {
        box-sizing: border-box;
        width: 300px;
        padding: 20px;
        border: 5px solid maroon;
      }
    </style>
    <div class="notice">Lamplighters' Fair</div>

    Checked in Chrome

    • drawn exactly 300px wide
    • the content shrinks to 250px: 300 minus the padding and the border
    box-sizingwidth isReal width of width 300, padding 20, border 5
    content-box (the default)the content only350
    border-boxthe whole box, border included300

    One rule for the whole site

    Many sites start their stylesheet with *, *::before, *::after { box-sizing: border-box; }: every element, and the small extra boxes CSS can draw before and after it, measured the border-box way. From then on, every width means the whole box.

    min and max sizes

    A fixed width is often wrong: too wide for a phone, too narrow for a big screen. max-width sets an upper limit and lets the box shrink below it; min-width, min-height and max-height work the same way.

    <!DOCTYPE html>
    <style>
      .article { max-width: 300px; background-color: lightyellow; }
      .banner { min-height: 100px; background-color: gold; }
    </style>
    <div class="article">A column of text that is never wider than 300px.</div>
    <div class="banner">At least 100px tall</div>

    Checked in Chrome

    • the article stops at 300px, though the window is wider
    • the banner is at least 100px tall, though its text needs less

    A column of reading text with max-width is easy on the eye on a wide screen, and still shrinks to fit a phone. The responsive design lesson builds on exactly this.

    Worked example 2

    A notice must fit a 320px frame exactly, with 16px of padding and a 4px border. Write it twice: the default way, and with border-box. What width do you write each time?

    What we have: a 320px slot, 16px of padding, a 4px border

    What we need: the width to write so the whole box is 320px, in each model

    The plan: with content-box, subtract the padding and border from 320; with border-box, write 320

    1. Content-box: 320 − 16 − 16 − 4 − 4 = 280, so width: 280px.
    2. Border-box: width: 320px, and the browser works out the content.
    <!DOCTYPE html>
    <style>
      .old { width: 280px; padding: 16px; border: 4px solid teal; }
      .new { box-sizing: border-box; width: 320px; padding: 16px; border: 4px solid teal; }
    </style>
    <div class="old">The default way</div>
    <div class="new">The border-box way</div>

    Checked in Chrome

    • content-box with width 280px: 320px in all
    • border-box with width 320px: 320px in all

    width: 280px the default way; width: 320px with border-box. Both draw 320px.

    Check: the browser drew both boxes exactly 320px wide.

    A path that fails: Writing width: 320px with the default content-box. The box comes out 360px and juts into the next frame, exactly Corin's slip.

    Worked example 3

    Two cards should sit side by side, each exactly half of a 600px shelf. Each has width: 50% and padding: 20px. How wide is each card drawn, and why do they not fit side by side?

    What we have: a 600px shelf, cards of 50% with 20px of padding

    What we need: each card's real width, and a fix

    The plan: work out 50% of the shelf, add the padding, then compare two cards with the shelf

    1. 50% of 600px is 300px: that is the content.
    2. Add 20px of padding on each side: 300 + 20 + 20 = 340px.
    3. Two cards need 680px, more than the shelf's 600px, so the second one cannot fit beside the first.
    <!DOCTYPE html>
    <style>
      .shelf { width: 600px; }
      .card { width: 50%; padding: 20px; }
      .fixed { box-sizing: border-box; }
    </style>
    <div class="shelf"><div class="card">Default</div><div class="card fixed">Border-box</div></div>

    Checked in Chrome

    • the default card is 340px, too wide for half
    • with border-box, the card is exactly 300px, half the shelf

    Each card is drawn 340px, so two need 680px; with box-sizing: border-box each is exactly 300px, and two fit.

    Check: the browser drew the default card 340px and the border-box card 300px.

    When you are stuck: box sizes

    Add it up: width, plus padding on both sides, plus border on both sides. That is the real width, unless box-sizing is border-box.

    Tired of adding up? Put box-sizing: border-box on everything.

    A box too wide for a phone: max-width instead of width.

    Your turn

    A box has width 200px, padding 10px and a 2px border, by default. How wide is it drawn?Tap to reveal
    1. 200 + 10 + 10 + 2 + 2.

    224px.

    With box-sizing: border-box, what does width measure?Tap to reveal
    1. Content, or the whole box?

    The whole box, border included.

    Which property lets a box shrink on a phone but never grow beyond 600px?Tap to reveal
    1. An upper limit.

    max-width: 600px.

    Did you know?

    The Internet Explorer box model

    When the first CSS standards came out, the two major browsers, Netscape 4.0 and Internet Explorer 4.0, both measured width from border to border. The W3C rule measured only the content, so the same box came out narrower in one than in the other. The browsers' way became known as the Internet Explorer box model, often called a bug. Today's standard offers both ways, and border-box puts the padding and border inside the width.

    Why it fits here: This topic's box-sizing: border-box is that old browser rule, made standard and chosen on purpose.

    Sources: Wikipedia: CSS box model; W3C: CSS Box Sizing Level 3

    One famous line for every box

    On 1 February 2012, the developer Paul Irish wrote a short post titled "* { box-sizing: border-box } FTW". He called the relationship of width and padding one of his least favourite parts of CSS layout, and he wrote that applying border-box to all elements is safe and wise. In August 2014 he updated the code: border-box on the html element, inherited by every other element.

    Why it fits here: It is this topic's box-sizing: border-box, set once for a whole page instead of box by box.

    Source: Paul Irish: * { box-sizing: border-box } FTW

    In one line

    By default width and height size only the content, so the real width adds the padding and border on both sides. With box-sizing: border-box, width is the whole box, and min- and max- sizes set limits instead of fixed sizes.

    Margins: the space between boxes

    At a glance

    A margin is the see-through space outside a box's border. It pushes other boxes away, and the browser gives some elements a margin of their own, such as paragraphs and headings. Side by side, two margins add up.

    Stacked one above the other, they do something surprising: they collapse, and only the larger one is kept. A child's top margin can even escape through its parent. A margin can also be auto, which shares out the free space and centres a box that has a width.

    Take a guess: If two stacked paragraphs each have a 20px margin all round, how big is the gap between them?

    Two people in a queue each want an arm's length of space around them. They do not stand two arm's lengths apart: one arm's length between them satisfies both. Vertical margins in CSS behave the same way, and it is the most surprising rule of the whole box model.

    Margins push boxes apart

    A margin is see-through space outside the border. It pushes other boxes away. Most elements have none, but the browser's own stylesheet gives some a margin: the body 8px all round, paragraphs 16px above and below, headings more.

    Vertical margins collapse

    When one box's bottom margin meets the next box's top margin, the two do not add up. The gap is only the larger of the two. This is called margin collapsing.

    <!DOCTYPE html>
    <style>
      p { margin: 0; }
      .first { margin-bottom: 20px; }
      .second { margin-top: 30px; }
    </style>
    <p class="first">Rice, 5 kg</p>
    <p class="second">Dal, 1 kg</p>

    Checked in Chrome

    • the gap is 30px, the larger margin, not 20 + 30 = 50

    Only vertical margins collapse, top and bottom. Left and right margins always add up. And padding and borders never collapse.

    A memory aid

    Vertical margins share; horizontal margins add. Like the queue: one arm's length between two people, whichever arm is longer.

    A child's margin can escape its parent

    A stranger case: when a box has no border and no padding at its top, its first child's top margin passes straight through it. The parent itself moves down, as if the margin were its own.

    <!DOCTYPE html>
    <style>
      body { margin: 0; }
      .card { background-color: gold; }
      .card p { margin-top: 40px; }
    </style>
    <div class="card"><p>Diwali offers</p></div>

    Checked in Chrome

    • (the surprise: the gold card itself starts 40px down; the child's margin escaped)
    • and the paragraph sits at the very top of the card

    You expected gold space above the text, inside the card. Instead the card moved down. The fix is to give the parent something at its top, so the margin cannot pass through: a little padding, a border, or display: flow-root.

    <!DOCTYPE html>
    <style>
      body { margin: 0; }
      .card { background-color: gold; padding-top: 1px; }
      .card p { margin-top: 40px; }
    </style>
    <div class="card"><p>Diwali offers</p></div>

    Checked in Chrome

    • with 1px of padding, the card stays at the top
    • and the margin now makes gold space inside it: 1px of padding + 40px

    Centring a box: margin auto

    A block with a width narrower than its parent can be centred with margin: 0 auto. The auto values share the leftover space equally between the left and right margins.

    <!DOCTYPE html>
    <style>
      body { margin: 0; }
      .shelf { width: 400px; }
      .card { width: 200px; margin: 0 auto; background-color: lightyellow; }
    </style>
    <div class="shelf"><div class="card">Centred</div></div>

    Checked in Chrome

    • the card starts 100px in: (400 − 200) ÷ 2 on each side

    It works only on a block with a width (or a max-width) smaller than its parent. Text inside a box is centred differently, with text-align: center, which the typography lesson covers.

    Negative margins

    A margin can be negative: margin-top: -10px pulls a box 10px upward, over whatever is above it. It has real uses, such as letting a picture overlap a banner, but it is easy to overdo; reach for it last.

    Worked example 4

    Two notices are stacked. The first has margin-bottom 24px; the second has margin-top 16px. A developer expects a 40px gap. What is the gap, and how can it be made 40px?

    What we have: two vertical margins that meet

    What we need: the real gap, and a way to reach 40px

    The plan: vertical margins collapse to the larger one; to get 40, make the larger one 40

    1. The margins collapse: the gap is the larger, 24px, not 24 + 16.
    2. To get 40px, set one of them to 40px, say margin-bottom: 40px on the first.
    <!DOCTYPE html>
    <style>
      p { margin: 0; }
      .first { margin-bottom: 40px; }
      .second { margin-top: 16px; }
    </style>
    <p class="first">Rice, 5 kg</p>
    <p class="second">Dal, 1 kg</p>

    Checked in Chrome

    • the gap is 40px, the larger margin

    The gap is 24px; make it 40px by setting the larger margin to 40px.

    Check: the browser measured a 40px gap, the larger of 40 and 16.

    When you are stuck: margins

    A gap smaller than expected between two stacked boxes: their margins collapsed to the larger one.

    A parent that moved down instead of showing space inside: its child's margin escaped; give the parent a little padding or display: flow-root.

    A gap you never set: the browser's default margins on body, p and headings.

    Your turn

    Two paragraphs have margins of 10px and 25px where they meet. How big is the gap?Tap to reveal
    1. Vertical margins share.

    25px, the larger.

    Do left and right margins collapse?Tap to reveal
    1. Only one direction collapses.

    No: horizontal margins always add.

    What centres a 300px box inside a wider parent?Tap to reveal
    1. Two values: none above and below, auto at the sides.

    margin: 0 auto.

    Did you know?

    Collapsing, by design, since 1996

    Margin collapsing is no accident of browsers. The first CSS standard, of 1996, says that two or more adjoining vertical margins, with no border, padding or content between them, collapse to the largest of them. It even gives the reason: in most cases the result is visually more pleasing and closer to what the designer expects. The same standard says that horizontal margins are not collapsed, and CSS 2.1 repeats it.

    Why it fits here: This is the rule this topic calls the most surprising one in the box model, with the standard's own reason for it.

    Sources: W3C: CSS level 1 (1996); W3C: CSS 2.1, Box model

    No collapsing in flex and grid

    Margin collapsing happens between ordinary blocks, but not everywhere. The Flexbox standard says it plainly: the margins of adjacent flex items do not collapse. The Grid standard says the same of grid items. So two cards with 16px margins, stacked in a flex column, sit 32px apart, while the same two cards stacked as ordinary blocks sit only 16px apart.

    Why it fits here: It explains why a gap can suddenly double when ordinary blocks are put into a flex or grid layout.

    Sources: W3C: CSS Flexible Box Layout; W3C: CSS Grid Layout

    In one line

    Margins are see-through space that pushes boxes apart. Vertical margins collapse to the larger one and a child's top margin can escape its parent; horizontal margins add, and margin: 0 auto centres a box with a width.

    Block, inline and inline-block

    At a glance

    Some elements take a whole line to themselves, like a headline, and some sit inside a sentence, like a bold word. The display property decides which. A block box starts a new line and obeys width and height. An inline box sits in the text and ignores them.

    In between is inline-block: it sits in the line like a word, but it obeys width, height and padding like a block. Display can also hide a box: none removes it and its space, while visibility: hidden keeps the empty gap.

    Take a guess: If you give a span a width of 100px, does the span get any wider?

    In a newspaper, a headline takes a whole line to itself, while a word in bold sits inside a sentence. HTML elements behave the same two ways, as the HTML step showed: block elements such as div and p, and inline elements such as span and a. The display property is what decides it, and you can change it.

    displayStarts a new line?width and heightVertical padding and marginExamples by default
    blockyes; takes the full widthobeyedpush other boxes awaydiv, p, h1, ul, section
    inlineno; sits in the line of textignoreddrawn, but do not push the lines apartspan, a, strong, em
    inline-blockno; sits in the lineobeyedpush the line apart, like a blocka button is close to this

    Inline boxes ignore width and height

    <!DOCTYPE html>
    <style>
      .tag {
        width: 200px;
        height: 100px;
        padding: 10px;
        background-color: gold;
      }
      .badge { display: inline-block; }
    </style>
    <p>Rice <span class="tag">sale</span> today</p>
    <p>Rice <span class="tag badge">sale</span> today</p>

    Checked in Chrome

    • the plain span ignores width 200px: it is only as wide as its word and padding
    • as inline-block, it is 200 + 10 + 10 wide and 100 + 10 + 10 tall, and still sits in the line

    A span is inline, so its width and height are ignored: an inline box is exactly as big as its text, plus padding. Change it to inline-block, and it sits in the line like a word, but takes its width and height like a block. Tags, badges and buttons in a line of text are usually inline-block.

    Hiding: display none, or visibility hidden

    Two ways to hide an element, with one big difference.

    <!DOCTYPE html>
    <style>
      .gone { display: none; }
      .invisible { visibility: hidden; }
    </style>
    <p>Before</p>
    <p class="gone">Removed from the page</p>
    <p class="invisible">Invisible, but still taking space</p>
    <p>After</p>

    Checked in Chrome

    • display: none: no box at all; the page closes up
    • visibility: hidden: nothing shows, but its space stays, leaving a gap
    display: nonevisibility: hidden
    shows on the pagenono
    takes up spaceno: the page closes upyes: an empty gap
    read by screen readersnono
    use it forremoving something, such as a closed menukeeping a layout steady while something is hidden

    Hiding things you want read

    Both ways also hide the element from screen readers. To hide something only from the eye, such as a skip link until it is reached with Tab, sites use a special "visually hidden" set of rules instead. The skip link of the accessibility lesson is where it is used.

    Worked example 5

    A shop's product name and its price, a span, sit on one line. The price should be a gold badge 80px wide with 6px of padding, still on the same line. Why does width: 80px do nothing on the span, and what is the fix?

    What we have: a span with a width that has no effect

    What we need: the reason, and a badge 80px wide in the line

    The plan: a span is inline, and inline boxes ignore width; inline-block keeps it in the line and obeys width

    <!DOCTYPE html>
    <style>
      .price {
        display: inline-block;
        width: 80px;
        padding: 6px;
        background-color: gold;
      }
    </style>
    <p>Toor dal <span class="price">₹180</span></p>

    Checked in Chrome

    • 80 + 6 + 6 = 92px wide
    • and it sits inside the paragraph's line, after the name

    display: inline-block on the price: inline boxes ignore width, inline-block obeys it and stays in the line.

    Check: the badge is drawn 92px wide, its width plus its padding, beside the name.

    When you are stuck: display

    A width or height that does nothing: the element is probably inline. Make it inline-block or block.

    Something hidden but leaving a gap: that is visibility: hidden; use display: none to close the gap.

    Not sure which an element is? DevTools' Computed tab shows its display.

    Your turn

    Which display value ignores width and height?Tap to reveal
    1. The one for words inside a sentence.

    inline.

    What is the difference between display: none and visibility: hidden?Tap to reveal
    1. Does the space stay?

    display: none removes the space; visibility: hidden keeps an empty gap.

    Which display value sits in a line of text but obeys width and height?Tap to reveal
    1. Half of one, half of the other.

    inline-block.

    Did you know?

    inline-block came later

    The first CSS standard, of 1996, knew only four kinds of display: block, inline, list-item and none. The value inline-block, which this topic uses for a box that sits in a line but obeys width and padding, came later. The list of changes in CSS 2.1 records it as a new display value.

    Why it fits here: Block and inline, two of this topic's three kinds, date from 1996; the third, inline-block, arrived with CSS 2.1.

    Sources: W3C: CSS level 1 (1996); W3C: CSS 2.1, Changes

    Two words inside every display

    The newest CSS standard on display splits each display type into two qualities. The outer display type says how a box takes part in the flow among its neighbours. The inner display type says how it lays out its own children. Seen that way, inline-block is short for inline flow-root: inline on the outside, an ordinary block on the inside.

    Why it fits here: It names both halves of this topic's inline-block: in the line like a word, sized like a block.

    Source: W3C: CSS Display Level 3

    In one line

    The display property decides the kind of box: block takes a full line and obeys sizes, inline sits in the text and ignores width and height, and inline-block sits in the line and obeys them. Hiding with display: none removes a box; visibility: hidden keeps its gap.

    When the content is too big for its box

    At a glance

    Give a box a fixed height and more text than fits, and the extra spills out, over whatever comes next. The overflow property decides what happens to it. The default, visible, lets it spill. hidden cuts it off, and auto adds a scrollbar only when it is needed.

    Often the best fix is no fixed height at all, so that the box grows with its content. One very long word, such as a web address, can also burst out of a narrow box, and overflow-wrap lets it break.

    Take a guess: If a box 100px tall holds text that needs 300px, with no overflow rule, where does the extra text go?

    Pour two litres of milk into a one-litre jug, and it spills over the counter. Give a box a fixed height and too much text, and the same happens on a page. The overflow property decides what happens to what does not fit.

    overflowWhat happens to the extraUse it for
    visible (the default)drawn outside the box, over whatever comes nextboxes whose height grows with their content anyway
    hiddencut off and not shownpictures cropped to a frame; nothing that must be read
    autoa scrollbar appears, only when neededa box of fixed height with a long list inside
    scrolla scrollbar always, even when not neededrarely: when the box must look the same full or empty
    <!DOCTYPE html>
    <style>
      .hours {
        height: 60px;
        width: 200px;
        border: 1px solid gray;
      }
    </style>
    <div class="hours">Monday: 9 to 9<br>Tuesday: 9 to 9<br>Wednesday: 9 to 9<br>Thursday: 9 to 9<br>Friday: 9 to 9<br>Saturday: 9 to 6</div>
    <p>Delivery is free above ₹499.</p>

    Checked in Chrome

    • the text is taller than the 60px box
    • but the box stays 60px tall (62 with its border), so the extra spills out
    • and the next paragraph is not pushed down: the spilled lines are drawn over it

    This is the default, visible. The box does not grow, because its height was fixed. The extra lines are drawn below it anyway, and the next paragraph, which only knows about the box's own height, sits underneath them. The two overlap and both become hard to read.

    Giving the box its own scrollbar

    <!DOCTYPE html>
    <style>
      .hours {
        height: 60px;
        width: 200px;
        border: 1px solid gray;
        overflow: auto;
      }
    </style>
    <div class="hours">Monday: 9 to 9<br>Tuesday: 9 to 9<br>Wednesday: 9 to 9<br>Thursday: 9 to 9<br>Friday: 9 to 9<br>Saturday: 9 to 6</div>
    <p>Delivery is free above ₹499.</p>

    Checked in Chrome

    • the box scrolls when its content is too tall
    • the hours are still all there, reached by scrolling inside the box

    Now the extra stays inside, and the visitor scrolls the box to read it. Nothing is drawn over the paragraph. Use hidden only for what does not need reading, such as the edge of a photo: hidden text is lost to the visitor.

    Often the real fix is no fixed height

    Most overflow comes from a fixed height that should not be there. Remove the height, or use min-height, and the box grows to fit its content, which is what a box does by default. Reach for overflow when the height really must stay fixed.

    One very long word

    A long web address or a word with no spaces cannot be broken onto two lines, so it can stick out of a narrow box sideways. overflow-wrap: anywhere lets the browser break it wherever needed.

    <!DOCTYPE html>
    <style>
      .note { width: 120px; border: 1px solid gray; overflow-wrap: anywhere; }
    </style>
    <p class="note">Visit https://ashastores.example/offers/diwali-2026 today</p>

    Checked in Chrome

    • the long address breaks inside the box instead of sticking out

    Worked example 6

    A shop's sidebar is 200px tall and holds a list of 15 categories, which needs about 400px. The list spills over the footer. Give the list its own scrolling, and say what else could solve it.

    What we have: a fixed 200px box with 400px of content

    What we need: content kept inside the box, and another way

    The plan: overflow: auto gives the box a scrollbar when needed; or drop the fixed height

    <!DOCTYPE html>
    <style>
      .sidebar { height: 200px; overflow: auto; border: 1px solid gray; }
    </style>
    <ul class="sidebar"><li>Category 1</li><li>Category 2</li><li>Category 3</li><li>Category 4</li><li>Category 5</li><li>Category 6</li><li>Category 7</li><li>Category 8</li><li>Category 9</li><li>Category 10</li><li>Category 11</li><li>Category 12</li><li>Category 13</li><li>Category 14</li><li>Category 15</li></ul>

    Checked in Chrome

    • the list is taller than the box
    • and the box scrolls instead of spilling
  • With overflow: auto, the list stays inside its 200px, with a scrollbar.
  • overflow: auto on the sidebar; or remove its fixed height so it grows to fit.

    Check: the check finds the content taller than the box, and the box set to scroll.

    Another way: If the sidebar need not stay 200px tall, delete the height: the list then takes its full 400px and nothing overflows at all.

    When you are stuck: overflow

    Text spilling over the next element: the box has a fixed height. Remove it, use min-height, or add overflow: auto.

    Never hide text that must be read with overflow: hidden.

    A long word or address sticking out sideways: overflow-wrap: anywhere.

    Your turn

    What is the default value of overflow, and what does it do?Tap to reveal
    1. The milk spills over.

    visible: the extra is drawn outside the box, over what follows.

    Which value shows a scrollbar only when it is needed?Tap to reveal
    1. Not scroll.

    auto.

    What is often a better fix than overflow for spilling text?Tap to reveal
    1. Why is the box too small?

    Remove the fixed height (or use min-height), so the box grows.

    Did you know?

    A property with an old name

    The property that lets one very long word break began as a Microsoft extension called word-wrap, and most browsers copied it under the same name. It has since been renamed overflow-wrap. The CSS Text standard tells browsers to keep treating word-wrap as a legacy name for it, so old style sheets still work.

    Why it fits here: This topic's part on one very long word uses overflow-wrap, and many older pages still use its first name.

    Sources: MDN: overflow-wrap; W3C: CSS Text Level 3

    Hidden, but still scrollable

    A box with overflow: hidden hides the extra content from the reader, but the content is still there. The CSS Overflow standard says it must still be scrollable by a program, so the box is still a scroll container. Another value, overflow: clip, forbids scrolling through any mechanism at all.

    Why it fits here: This topic's table says hidden cuts the extra off; this shows what hidden really does, and the stricter clip.

    Source: W3C: CSS Overflow Level 3

    In one line

    Content too big for a fixed-size box overflows: visible (the default) draws it over what follows, hidden cuts it off, auto adds a scrollbar when needed; often the better fix is no fixed height.

    Where it is used

    Every layout on the web is boxes inside boxes, spaced and sized by these properties.

    What you learnedWhere it is usedA real example
    paddingbuttons, cards, table cellsthe comfortable space inside a payment app's Pay button
    borderinputs, cards, dividersthe grey line around a search box
    marginspace between paragraphs, cards and sectionsthe gap between product cards on a shopping page
    box-sizing: border-boxnearly every modern sitethe first rule in most stylesheets, so widths mean the whole box
    max-width with margin: 0 autoreading columnsa news article's text centred in a column no wider than about 700px
    inline-blockbadges, tags, small buttons in texta "New" badge beside a product's name
    display: nonemenus, tabs, pop-upsa phone menu hidden until the menu button is pressed
    overflow: autochat windows, long lists in a fixed panela chat app's message list, scrolling inside its panel

    Mistakes and debugging

    These are the box-model slips everyone makes, each checked in the browser.

    1. Forgetting that padding and border add to the width

    <!DOCTYPE html>
    <style>
      .notice { width: 320px; padding: 16px; border: 4px solid teal; }
    </style>
    <div class="notice">Meant to be 320px</div>

    Checked in Chrome

    • (the slip: drawn 360px wide, 40px too wide for its 320px slot)

    The fix: box-sizing: border-box, or subtract the padding and border from the width.

    2. A border with no style

    border: 2px maroon draws nothing. The fix: add the style: border: 2px solid maroon.

    3. margin: auto on a box with no width

    <!DOCTYPE html>
    <style>
      body { margin: 0; }
      .card { margin: 0 auto; background-color: lightyellow; }
    </style>
    <div class="card">Not centred</div>

    Checked in Chrome

    • (the slip: a block with no width takes the full width, so there is nothing left to centre)

    The fix: give it a width or a max-width smaller than its parent: max-width: 600px; margin: 0 auto;.

    4. Width on an inline element

    A span or a link ignores width and height. The fix: display: inline-block (or block).

    5. Margins that do not add up

    Two stacked boxes with margins of 20px and 30px have a 30px gap, not 50px; a first child's top margin can push its parent down. The fix: expect the larger margin to win, and give the parent a little padding when its child's margin escapes.

    6. A fixed height with too much inside

    Text spills out and over the next element. The fix: remove the height, use min-height, or add overflow: auto.

    The debugging corner: see the boxes

    Hover over an element in DevTools' Elements panel, and the page shows its layers in colour: the content in blue, the padding in green, the margin in orange. The Computed tab draws the same box as a diagram with every number, from the margin outside to the content inside. Most "why is there a gap?" questions end the moment you hover.

    Quick card

    The lesson on one card.

    Every idea of the lesson

    <!DOCTYPE html>
    <style>
      *, *::before, *::after { box-sizing: border-box; }
      body { margin: 0; }
      .notice {
        width: 320px;
        max-width: 100%;
        margin: 24px auto;
        padding: 16px 20px;
        border: 4px solid maroon;
      }
      .badge { display: inline-block; padding: 4px 8px; background-color: gold; }
      .hours { height: 60px; overflow: auto; }
      .gone { display: none; }
    </style>
    <div class="notice">
      Lamplighters' Fair <span class="badge">New</span>
      <div class="hours">Monday: 9 to 9<br>Tuesday: 9 to 9<br>Wednesday: 9 to 9<br>Thursday: 9 to 9</div>
      <p class="gone">Hidden note</p>
    </div>

    Checked in Chrome

    • border-box: the notice is exactly 320px, padding and border included
    • margin auto centres it
    • the badge sits in the line and takes its padding
    • the long hours scroll inside their box
    • the hidden note takes no space
    IdeaIn one line
    the four layerscontent, padding (inside the colour), border (width style colour), margin (outside, see-through)
    short formsone value all round; two: up-down, sideways; four: top, right, bottom, left
    the real widthwidth + padding × 2 + border × 2, unless box-sizing: border-box
    min-, max-width, -heightlimits instead of fixed sizes
    marginsvertical ones collapse to the larger; horizontal ones add; margin: 0 auto centres a box with a width
    displayblock (full line), inline (in the text, ignores sizes), inline-block (in the line, obeys sizes)
    hidingdisplay: none (no space) or visibility: hidden (keeps a gap)
    overflowvisible (spills), hidden (cut), auto (scrolls when needed)

    The story: Box inside box inside box

    You measure the notice the tailors' way: the words, then the lining around them, then the stitched edge, then the gap to the next frame. You switch the guild's rule so that 320 means the whole notice, lining and edge included, and it slides into its slot exactly.

    The long list of hours gets its own small window that scrolls, so nothing spills onto the notice below. Between notices, you learn, the gap is the larger of the two, never both added together.

    Corin measures his own and laughs. "It was 370," he says. "I added the lining twice."

    Reagents gathered: content, padding, border, margin, box-sizing, overflow.

    Sources

    • MDN Web Docs (Prose CC BY-SA 2.5 or later; code samples added since 2010-08-20 CC0 (its licensing page, read 2026-10-26); reused with credit)
    • HTML Living Standard (WHATWG) (CC BY 4.0: "Copyright WHATWG (Apple, Google, Mozilla, Microsoft). This work is licensed under a Creative Commons Attribution 4.0 International License" (its acknowledgements page, read 2026-10-26); reused with credit)