Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Follow Normal Flow

In this lesson, you saw the two kinds of box in normal flow, added a “Needs water” badge to the work day card, and found out which of its sizes the browser uses. You also saw why your base stylesheet makes images blocks.

Follow Along

The video starts from events/upcoming.html and events/events.css as you left them in Fill Boxes With Images and Gradients, with the banner at the top of the page and the base stylesheet at the top of events.css. Open the page in Chrome or Edge, with DevTools docked beside it. Rosa would like a badge on the work day card that says Needs water, so volunteers know plot 7 needs them.

Key Points

  • Two kinds of box. Hover over the work day’s date paragraph in the Elements panel, and its box runs the full width of the card, although the text stops halfway. Hover over the link at its end, and its box is only as wide as its words.
  • The display value decides. It’s the same property your base stylesheet sets on images. Click the paragraph’s row, and in the Computed tab, type display into the filter, and the paragraph’s is block. Click the link’s row, and it’s inline. You didn’t set either one. They come from the browser’s default styles.
  • Normal flow. A block box starts on a new line, which is why your cards stack. Unless you give it a width, like the 300-pixel cards you tried, it fills its parent’s width. An inline box sits in a line of text, among the words around it. Together, these two rules are normal flow, the layout every page gets before you change it.
  • Inline boxes wrap like words. When the window gets narrower, the link at the end of the date line wraps like the words around it. Here, it breaks across two lines. The cards still fill their parent, which here spans the window, and grow taller to fit.
  • A <span> has no meaning of its own. It marks a piece of text, so CSS can select it.
  • An inline box takes its size from its text. Rosa’s badge rule below asks for a width of 120 pixels, so every badge is the same size, and a top margin of 16 pixels, for some room above the badge’s line. Without its display line, the gold shows, but the badge is only as wide as its words, and it doesn’t move down.
  • DevTools shows what was ignored. The badge’s hover label reads 89.19 × 16. In the Styles pane, width is grayed out, and its info icon says the display: inline property prevents width from having an effect. The margin-top isn’t grayed out, but the badge hasn’t moved down.
  • Watch out: on inline text, such as a <span> or a link, width, height and top and bottom margins do nothing, and the page shows no error. Its text decides its size.
  • display: inline-block keeps a box in the line but lets it take sizes. The badge is 120 pixels wide, and its top margin pushes the whole line down by 16 pixels, the room Rosa asked for. An inline-block box sits in a line like a word, but its width, height and every margin apply, as on a block.
  • Why images are blocks in your base stylesheet. Remember the gap under the seed swap photo that your img rule removed? An image is inline by default. Select the photo, and in the Styles pane, untick display: block; in the img rule, and a strip of the card’s pale green background, 6 pixels tall, opens under the photo.
  • The strip is room for letters’ tails. The inline photo sits in the line like a word, and the line keeps room below it for the tails of letters such as g and p. Tick the box again, and the strip goes, because a block isn’t in a line at all. Unlike inline text, an inline image does take a width, a height and margins, even while it’s inline. That’s all you need about images in a line for now.
  • The rule: before you size a box, check its display. Blocks stack and, without a width of their own, fill their parent. Inline text flows in lines and takes its size from the text. Inline-block boxes sit in a line and take every size you give them.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Rosa would like a badge on the work day card that says Needs water, so volunteers know plot 7 needs them. A badge sits inside a line of text, and that’s a different kind of box from your cards.

00:12 In this video, you’ll see how the browser places both kinds.

00:18 Open DevTools, and hover over the work day’s date paragraph. Its box runs the full width of the card, although the text stops halfway. Now hover over the link at its end.

00:29 Its box is only as wide as its words.

00:33 The difference is each element’s display value, the same property your base stylesheet sets on images. Click the paragraph’s row, and in the Computed tab, type display into the filter.

00:44 Its display is block. Click the link’s row: inline. You didn’t set either one. They’re the browser’s default styles.

00:55 A block box starts on a new line. That’s why your cards stack. Unless you give it a width, like the 300-pixel cards you tried, it fills its parent’s width.

01:05 An inline box sits in a line of text, among the words around it. Together, these two rules are normal flow, the layout every page gets before you change it.

01:16 Your turn: pause the video and predict. When the window gets narrower, what happens to the link at the end of the date line? The link wraps like the words around it.

01:28 Here, it breaks across two lines.

01:31 The cards still fill their parent, which here spans the window, and grow taller to fit.

01:38 Now add the badge. In the work day card, add a paragraph: Plot 7, then a span around Needs water. A span has no meaning of its own. It marks text, so CSS can select it.

01:52 In events.css, Rosa’s badge rule asks for a gold background and dark text. It also sets a width of 120 pixels, so every badge is the same size, and a top margin of 16, for room above its line.

02:07 Save. The gold shows. But the badge is only as wide as its words, and it hasn’t moved down.

02:16 Hover over the span, and its label reads 89 by 16. In the Styles pane, the width is grayed out, and its info icon says the inline display prevents width from having an effect.

02:28 The margin isn’t grayed out, but the badge hasn’t moved either. Watch out: on inline text, such as this span or a link, width, height and top and bottom margins do nothing, and the page shows no error.

02:43 To keep the badge in the line but let it take sizes, add display: inline-block to the rule. Save. The badge is 120 pixels wide now, and its top margin pushes the whole line down by 16 pixels, the room Rosa asked for.

03:00 An inline-block box sits in a line like a word, but takes every size, as a block does.

03:08 Remember the gap under the seed swap photo that your img rule removed? An image is inline by default, and the rule makes it a block. Select the photo, and in the Styles pane, untick its display declaration.

03:21 A strip of the card’s pale green, 6 pixels tall, opens under the photo.

03:27 The inline photo sits in the line like a word, with room below for the tails of letters. Tick the box again. As a block, the photo isn’t in a line, so the strip goes.

03:39 Inline images, unlike text, do take every size. That’s all you need on them for now.

03:46 So before you size a box, check its display. Blocks stack and, without a width, fill their parent. Inline text flows in lines and takes its size from the text.

03:57 Inline-block boxes sit in a line and take every size you give them. Next is the lesson Choose How to Hide Content, where you’ll take boxes off the page on purpose.

Become a Member to join the conversation.