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
displayvalue decides. It’s the same property your base stylesheet sets on images. Click the paragraph’s row, and in the Computed tab, typedisplayinto the filter, and the paragraph’s isblock. Click the link’s row, and it’sinline. 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
displayline, 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,widthis grayed out, and its info icon says thedisplay: inlineproperty preventswidthfrom having an effect. Themargin-topisn’t grayed out, but the badge hasn’t moved down. - Watch out: on inline text, such as a
<span>or a link,width,heightand top and bottom margins do nothing, and the page shows no error. Its text decides its size. display: inline-blockkeeps 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
imgrule removed? An image is inline by default. Select the photo, and in the Styles pane, untickdisplay: block;in theimgrule, 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.