Sub topic 3
display: block
This chapter assumes you can write HTML elements and CSS rules, and that you know the box model: content, padding, border, and margin.
What a block box does
An element with display: block generates a block-level box. Three rules describe its behavior:
- It starts on a new line, and the content after it starts on another new line.
- With
width: auto, which is the default, it stretches to fill the width of its container. - It accepts
width,height,margin, andpaddingon all four sides.
Browsers give div, p, h1 to h6, ul, ol, li, section, header, footer, and form a block display value by default. The value comes from the browser's default stylesheet, not from the tag. The rule below restates what the browser already applies to every div.
div {
display: block;
margin: 1rem 0;
}How blocks stack
Block boxes stack vertically in the order they appear in the HTML. Each one sits below the previous one, whatever its width.
<div>Block one</div>
<div>Block two</div>
<span>Inline one</span>
<span>Inline two</span>The two div elements each take a full line. The two span elements share one line, because span is inline by default. Inline boxes get their own chapter.
Width and height
A block with width: auto takes the container's width and subtracts its own margin, border, and padding. Padding therefore does not make an auto-width block overflow its parent.
Setting width stops this filling behavior. The block then keeps that width, and the leftover space stays empty. That leftover space is still the block's. Another block does not move up beside it.
Height works differently. By default, height: auto makes the block as tall as its content. You rarely need to set a height on a block, and a fixed height can cause overflow when the content grows.
To center a block horizontally, give it a width and set the horizontal margins to auto:
.card {
width: 30rem;
max-width: 100%;
margin: 0 auto;
padding: 1rem;
}The max-width: 100% stops the card from overflowing on narrow screens.
Vertical margins collapse
Vertical margins between adjacent blocks collapse. Two margins combine into one, and the larger value wins. They do not add up.
.a { margin-bottom: 30px; }
.b { margin-top: 20px; }If .a sits directly above .b, the gap between them is 30px, not 50px. Horizontal margins never collapse.
A parent's margin can also collapse with its first or last child's margin. This happens when nothing separates them, such as padding, a border, or a new formatting context. The child's margin then appears to push the parent away from its neighbors.
Turning other elements into blocks
You can set display: block on elements that are not block by default. A common case is an anchor, which is inline by default:
.card-link {
display: block;
padding: 1rem;
}The whole padded rectangle becomes clickable, and the link takes its own line. Changing display does not change the element's meaning. A link is still a link for keyboard and screen reader users.
Common errors
- Setting
widthon aspanand seeing no change. Aspanis inline, and inline boxes ignorewidthandheight. Setdisplay: blockorinline-blockfirst. - Expecting a narrow block to let its neighbor sit beside it. A block with a set width still occupies its whole line. To place blocks side by side, you need
inline-block, flex, or grid. - Adding vertical margins and getting less space than expected. Check whether the margins are collapsing.
- Giving a block
width: 100%and also padding. With the defaultbox-sizing: content-box, the padding is added on top of the width and the block overflows. Usewidth: auto, or setbox-sizing: border-box.
Exercise
A div has width: 400px and margin: 0 auto, inside a 1000px-wide container. A second div follows it. Where does the first div sit, and can the second div appear on the same line beside it?
Show answer
The first div is centered, with 300px of empty space on each side, because the auto horizontal margins share the leftover 600px equally. The second div cannot sit beside it. A block box always occupies its own line, so the second div starts below the first.