Size Boxes With border-box

Your event cards have room inside them and space between them, and in the last lesson, you used the box diagram to find out which box made a gap. So far, though, every card has been as wide as the page allows. The moment you give a box a width of your own, the box model has one more surprise for you.

In this lesson, you’ll predict how wide a box will be, find out why it’s wider than the width you wrote, and set up a short base stylesheet that makes widths mean what they say.

Give the Cards a Width

In a wide window, a card that stretches across the whole page reads more like a banner than a notice. Rosa would like to try narrower cards, 300 pixels wide, like the notices pinned to the garden’s board. The width property sets how wide a box is, so add it to the card rule in events.css:

Language: CSS Filename: events/events.css
.event-card {
  background-color: honeydew;
  padding: 16px 24px;
  border: 2px solid var(--green);
  margin-bottom: 24px;
  width: 300px;
}

You’d expect a card 300 pixels wide, from the outer edge of its left border to the outer edge of its right border. Save, and measure what the browser made of it. Open DevTools, and in the Elements panel, expand <main> and <section>. Hover over the first <article> line, and the label on the page shows the card’s size:

DevTools highlights the work day card, and its label reads article.event-card 352 by 130.72, although the rule sets a width of 300 pixels.
A 300-Pixel Width That Comes Out 352 Pixels Wide

The label says the card is 352 pixels wide, not 300. As far as the browser is concerned, nothing went wrong. There’s no error in the Console and no warning in the Styles pane. The card is 52 pixels wider than you asked for, and CSS doesn’t tell you.

You’ll also see the seed swap’s photo poke out past its card’s right edge. The photo is 800 pixels wide, and nothing tells it to shrink. Fitting photos into their boxes comes later in the course, so keep your eyes on the work day card for now.

Find the Extra Pixels

Fifty-two pixels have to come from somewhere, and the box diagram shows where. Click the first <article> line to select the card, and open the Computed tab:

The work day card's box diagram shows a content width of 300, padding of 24 on the left and right, and a border of 2 on every side.
The Width Sets the Content Alone

The content in the middle of the diagram is exactly 300 wide. So width didn’t set the size of the card. It set the size of its content box, the content layer alone. The padding and the border are added around it, on both sides:

  • Content: 300
  • Padding: 24 on the left and 24 on the right, 48 in all
  • Border: 2 on the left and 2 on the right, 4 in all

That’s 300 + 48 + 4 = 352, the number in the label. The margin isn’t in the sum because it’s outside the border. It still takes up room on the page, but it isn’t part of the card you see.

This is the browser’s default behavior. The box-sizing property decides what width and height measure, and its default value, content-box, makes them measure the content only. The other value is border-box. With it, width measures the whole box out to the outer edge of the border, and the browser takes the padding and border out of that width, so the content gets what’s left.

Start a Base Stylesheet

You could add box-sizing: border-box to the card rule. But every box you give a width later in the course would need the same line, and each one you forget would quietly come out too wide. So set it once, for every element, in a base stylesheet: a few rules at the top of a stylesheet that replace the browser defaults you don’t want.

Add this rule at the top of events.css, right under the comment on its first line:

Language: CSS Filename: events/events.css
/* Practice styles for the garden's event pages */

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

The * is the universal selector, and it matches every element on the page. Pseudo-elements, such as the flower your h2::before rule adds before each <h2>, aren’t elements, so * doesn’t match them. The selectors *::before and *::after cover those too.

The rule goes at the top because it’s the starting point that every rule after it builds on. The universal selector also adds nothing to a selector’s specificity, the weight used to choose between competing rules, so a later rule that needs content-box for one particular box still wins.

Save, and hover over the first <article> line again:

DevTools highlights the work day card, and its label now reads article.event-card 300 by 130.72.
With border-box, the Card Is as Wide as Its Width

The card is 300 pixels wide now, the width you wrote. Select it and look at the box diagram again. The content in the middle is 248 wide. The browser took the 48 pixels of padding and the 4 of border out of the 300, instead of adding them on.

A card without a width doesn’t change at all with the rules it has now, because a card that fills the page fills it with either value. For these cards, border-box matters once you set a width or a height.

From then on, the number you write is the number you get, as long as it leaves room for the padding and border. The content can’t shrink below zero, so a width: 40px card with these 48 pixels of padding and 4 of border would still come out 52 wide. Even so, border-box makes sizes far easier to predict, which is why so many stylesheets set it on every element.

The 300-pixel width has done its job, but it doesn’t suit these cards, since the seed swap’s photo still sticks out of its card. Delete the width: 300px; line from the card rule, and save. The cards fill the page again:

The events page with the work day card stretching across the white main area, which stops a few pixels short of the window's left and right edges.
The Cards Fill the Page Again

A later lesson gives the whole column of cards a width that suits it. When you predict a box’s size from now on, ask which layer its width measures: the content alone with content-box, or out to the border with border-box.

Height follows the same rule, though a text card rarely gets one, since its text decides how tall it is. Picture a box with height: 100px and the cards’ 16 pixels of padding at the top and bottom and 2-pixel border. With content-box, it’s 100 + 32 + 4 = 136 pixels tall. With border-box, it’s 100 tall, and its content gets 64.

Margins sit outside both numbers. If the 300-pixel border-box card had a margin of 20 pixels on the left and right, the card you see would still be 300 wide, but it would take up 340 pixels across the page. Use the button below to ask the mentor for a few practice boxes and check that you can do this arithmetic both ways:

Finish the Base Stylesheet

Two more browser defaults get in the way of almost every design, so they belong in the base stylesheet too. Add a rule for <body> and one for <img> under the box-sizing rule, so the top of events.css reads like this:

Language: CSS Filename: events/events.css
/* Practice styles for the garden's event pages */

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

body {
  margin: 0;
}

img {
  display: block;
}

The body rule removes the 8-pixel margin that the browser’s default stylesheet gives <body>. You met this margin in See Every Element as a Box. It’s what keeps the white <main> off the window’s edges, but it’s a value you didn’t choose. With it gone, any room around the page is room you set yourself.

Your file already has a body rule further down, with the page’s colors and font. Both rules match <body>, and they set different properties, so both apply.

The img rule is about where an image sits. By default, an <img> sits in a line, like a word in a sentence. The browser leaves a little room under every line for the tails of letters such as g and p, and a line that holds only an image keeps that room too. That’s a gap of a few pixels between the seed swap photo and its caption.

Before you save, scroll down to the seed swap photo and look at its bottom edge. Between the photo and its caption, there’s a strip of honeydew about 6 pixels tall:

The bottom of the seed swap photo, with a thin strip of the card's honeydew background between the photo and its caption.
A Few Pixels of Line Space Under the Photo

The display property with the value block makes the image stack like a paragraph instead, so the gap goes. That’s all you need to know about inline boxes for now. A later lesson in this course shows that gap in DevTools and comes back to this rule.

Save, and the white background of <main> now runs to both edges of the window:

The events page with the white main area and the work day card running to the window's left and right edges, with no honeydew strip beside them.
The Events Page With Its Base Stylesheet

Scroll down to the seed swap photo again. The strip is gone, and the caption moves up a few pixels to sit against the bottom of the photo:

The bottom of the seed swap photo, with its caption right against it and no strip of honeydew between them.
The Caption Now Sits Against the Photo

Those three rules are the base stylesheet. Each one replaces a browser default with one you chose: every box measures its width out to the border, the page has no margin you didn’t set, and images stack like blocks. Try saying what each rule changes in a sentence of your own, and then use the button below to check your explanation with the mentor:

A width you give a box now measures it out to the border, as long as it leaves room for the padding and border, and the page starts from defaults you chose. The edge of each card is still a plain 2-pixel line, though. In the next lesson, Draw Borders, Corners and Shadows, you’ll style that border, round the cards’ corners, and give them a soft shadow.

Become a Member to join the conversation.