Sub topic 2
Default display values: why div and span behave differently
This chapter assumes you can write basic HTML, write CSS rules with selectors and properties, and describe the box model (content, padding, border, margin). It covers where an element's default display value comes from, which elements get which value, and the mistakes that follow from misunderstanding this.
Behavior comes from CSS, not from the tag
Put two div elements and two span elements in a page and they behave differently.
<div>Block one</div>
<div>Block two</div>
<span>Inline one</span>
<span>Inline two</span>Each div starts on a new line and stretches across the available width. The two span elements sit side by side in the same line, and each is only as wide as its text.
Nothing in the HTML syntax causes this. The tag name div does not mean "block" by itself. Every browser ships with a user agent stylesheet, which is a built-in set of CSS rules applied before any author CSS. That stylesheet contains rules much like these:
div {
display: block;
margin: 1rem 0;
}
span {
display: inline;
}(The margin on div is only for illustration. Browsers do not give div a default margin, but they do give one to p, h1, ul, and others.) The important point is that the display value is a CSS declaration. You can override it, and the element then follows the new value.
Where the default comes from
The final display value of an element is decided by the cascade, in this order of increasing priority:
- The user agent stylesheet (browser defaults).
- Any user stylesheet (rare).
- Author styles, meaning your CSS.
If you write no display rule, the user agent value wins. If you write one, yours wins, subject to normal specificity and source-order rules. This is why span { display: block; } works and turns a span into a block box.
If no stylesheet sets display at all, the initial value of the property is inline. That is the fallback for elements the user agent stylesheet does not mention, including custom elements such as <my-card>.
Common defaults
The table lists the default display of elements you meet often.
| Element | Default display | Notes |
|---|---|---|
| `div`, `p`, `section`, `article`, `header`, `footer`, `main`, `nav` | `block` | Generic and structural containers |
| `h1` to `h6`, `ul`, `ol`, `form`, `blockquote` | `block` | Come with default margins |
| `li` | `list-item` | Block-like box plus a marker |
| `span`, `a`, `strong`, `em`, `code`, `label` | `inline` | Text-level elements |
| `img`, `input`, `button`, `select`, `textarea` | `inline-block` in practice | Replaced or form controls that accept width and height |
| `table` | `table` | With `tr` as `table-row` and `td` as `table-cell` |
| `head`, `script`, `style`, `title`, `meta` | `none` | Not rendered |
Two details in this table surprise people.
imgis described asinlinein some references, because it is placed in a line of text. It is a replaced element, which means its content comes from outside the CSS (the image file). Replaced inline elements acceptwidthandheight, so they behave like inline-block boxes for sizing. You will see how inline-block works in a later chapter.headand its children aredisplay: nonein the user agent stylesheet. That is why<title>text never appears on the page. If you sethead { display: block; }, you can make it visible, which shows that even this rule is plain CSS.
You can check the real value for any element in your browser. Open DevTools, select the element, and look at the Computed tab for display. You can also read it from script.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Default display values</title>
</head>
<body>
<div id="a">div</div>
<span id="b">span</span>
<ul><li id="c">li</li></ul>
<my-card id="d">custom element</my-card>
<script>
for (const id of ["a", "b", "c", "d"]) {
const el = document.getElementById(id);
console.log(id, getComputedStyle(el).display);
}
</script>
</body>
</html>The console prints block, inline, list-item, and inline for the four elements. Open the file in a browser and compare.
What the two basic types mean
A display value actually has two parts: how the element behaves toward its siblings and parent (the outer type), and how it lays out its own children (the inner type). For the values in this tutorial:
blockhas an outer type of block and an inner type of flow. The box takes a line of its own in the parent.inlinehas an outer type of inline and an inner type of flow. The box joins the current line of text.inline-blockhas an outer type of inline and an inner type of flow-root. The box joins the line but contains its children like a block.nonegenerates no box.
You do not need to memorize this model now. It helps to know it exists, because it explains why inline-block sits between the other two, and why newer values such as flex and grid can be paired with either outer type (inline-flex, inline-grid).
Changing the default
Because the default is only a starting point, you can change it for any element. Two rules turn the usual behavior around.
/* Make every span in a card act like a block */
.card span {
display: block;
}
/* Make list items flow in a line */
.menu li {
display: inline-block;
}After the first rule, each span inside .card takes its own line and accepts width, height, and vertical margin. After the second, the li elements sit in a row. A li that is changed to inline-block loses its list-item marker, because the marker is part of the list-item value. If you want bullets back, use display: list-item or add your own marker.
Common errors
These are the mistakes that come from not knowing the defaults.
- Setting
widthon aspanoraand seeing no change. Both are inline by default. Setdisplay: inline-blockordisplay: blockfirst. - Expecting a
divto shrink to its text. A block box fills the available width of its parent. Giving it a width, or changing it toinline-block, changes that. - Putting block elements inside inline elements and trusting the result. HTML allows an
ato wrap block content in modern HTML, but the layout of a block inside an inline box is complicated. Prefer making the outer elementdisplay: blockorinline-block. - Assuming
displayremoves semantics. Aulwithdisplay: blockandlist-style: noneis still a list. Some browsers have historically dropped list semantics from assistive technology whenlist-style: noneis applied to a list, so test with a screen reader or addrole="list"when the list meaning matters. - Forgetting custom elements are inline. Covered above, and a frequent source of "my component has no height" bugs.
- Overriding
displayon abuttonorimgand losing expected behavior. Changing these toinlinecan makewidthandheightstop working on elements that normally accept them. Check the Computed tab before assuming.
Reading defaults to predict layout
You can predict a lot with one question: is this element block-level or inline-level?
- Block-level elements stack vertically and fill the width available.
- Inline-level elements flow left to right (in a left-to-right language) and wrap at the edge of the container, as words do.
Given this markup, the output has four lines:
<div>
<p>Paragraph</p>
<span>One</span> <span>Two</span>
<p>Another paragraph</p>
</div>The first p takes a line. The two span elements share a line, with a space between them because of the space character in the source. The second p starts a new line. That space character matters later, when you see the whitespace gap between inline-block elements.
Exercise
A developer writes <a href="/docs" class="button">Docs</a> and adds .button { width: 8rem; height: 3rem; }. The link stays as wide as its text. Why, and what is the smallest change that fixes it?
Show answer
An a element is display: inline by default, and an inline box ignores width and height. The smallest fix is to add display: inline-block to .button, which keeps the link in the line of text and makes it accept the width and height. Using display: block also works, but then the link takes its own line.
Exercise
You add a custom element <user-card> to a page and give it height: 200px. Nothing changes. What default explains this, and how can you confirm it in the browser?
Show answer
Custom elements get the initial display value, which is inline, so height has no effect. To confirm it, select the element in DevTools and read display on the Computed tab, or run getComputedStyle(element).display in the console. Setting display: block on user-card fixes it.