Sub topic 2
Default display values: why div and span behave differently
This chapter assumes you can write HTML elements, write CSS rules with selectors and properties, and describe the box model (content, padding, border, margin). It explains where an element's default display value comes from and why div and span lay out so differently.
Layout comes from CSS, not from the tag
Every element has a display value. The browser reads that value to decide what kind of box the element generates. HTML tags carry meaning (a p is a paragraph, a span is a generic phrase). The layout behavior comes from CSS.
The browser's built-in style sheet, called the user agent stylesheet, assigns a default display to each element. The div and span elements are both generic containers with no meaning of their own. They differ in layout only because the user agent stylesheet gives them different defaults.
Seeing the difference
<div>Block one</div>
<div>Block two</div>
<span>Inline one</span>
<span>Inline two</span>The two div elements each start on a new line and span the available width. The two span elements sit side by side in the same line.
Now restate the defaults in your own CSS:
div {
display: block;
margin: 1rem 0;
}
span {
display: inline;
}Nothing changes on screen. The rules only repeat what the browser already applied. This shows that the behavior comes from the display value and not from the tag name.
Common defaults
You do not need to memorize every element. These groups cover most of what you will write.
| Default `display` | Common elements |
|---|---|
| `block` | `div`, `p`, `h1` to `h6`, `ul`, `ol`, `section`, `article`, `header`, `footer`, `form` |
| `inline` | `span`, `a`, `strong`, `em`, `code`, `label` |
| `list-item` | `li` |
| `inline-block` | `button`, `input`, `select`, and `textarea` behave this way in most browsers |
| `none` | `head`, `script`, `style`, `title`, `meta` |
Two entries deserve a note. The li default of list-item is a block-level box that also draws a marker. Form controls such as button and input are replaced or widget-like elements. They sit in a line of text like inline content, but they accept width and height like blocks. Exact defaults for these can vary between browsers, so check the computed style when it matters.
Checking the actual value
Do not guess. Open the browser developer tools, select the element, and look at the Computed tab for display. You can also read it from script:
<p id="note">Hello</p>
<script>
const note = document.getElementById("note");
console.log(getComputedStyle(note).display);
</script>This logs block. Change the element to a span and it logs inline.
Overriding the default
Any element can take any display value. A rule in your own CSS wins over the user agent stylesheet, so you can make a span behave like a block, or a div behave like inline content.
span.banner {
display: block;
}
div.chip {
display: inline;
}This does not change the element's meaning. A span set to display: block is still a generic phrase element for assistive technology. Choose the tag for its meaning. Choose display for its layout.
Common errors
- Putting
widthon aspanand seeing nothing happen. Thespanis inline by default, so the width is ignored. Later chapters cover this in detail. - Assuming a
divcan never sit beside anotherdiv. It stacks only because its default isblock. Changingdisplaychanges that. - Choosing a tag for its look. Use
spanordivfor what they mean, then setdisplayto get the layout.
Exercise
A colleague writes <span>One</span><span>Two</span> and wants each span on its own line. They plan to replace the spans with divs. Which single CSS rule achieves the same layout without changing the tags, and why does it work?
Show answer
span { display: block; }. The spans were side by side because the user agent stylesheet gives span a default of display: inline. Setting display: block makes each one generate a block-level box, which starts on a new line and spans the available width.
Exercise
Which tool shows you the display value the browser actually applied to an element, and what does it report for a p with no custom CSS?
Show answer
The Computed tab in the browser developer tools, or getComputedStyle(element).display in script. For a p with no custom CSS it reports block.