Sub topic 1
What the display property does
Prerequisites
This chapter assumes you know the following:
- Basic HTML elements and how a document is structured.
- How to write a CSS rule with a selector and properties.
- The CSS box model: content, padding, border, and margin.
What display controls
Every element in a page generates zero or more boxes. The display property decides what kind of box the element generates and how that box takes part in layout. It answers two questions:
- How does the element's box behave among its siblings: does it start a new line, or does it sit inside a line of text?
- How does the element lay out its own children?
The first question is about the outer display type. The second is about the inner display type. The classic values this tutorial covers set mainly the outer type:
| Value | Outer behavior | Generates a box? |
|---|---|---|
| `block` | Starts on a new line and fills the available width | Yes |
| `inline` | Flows inside a line of text | Yes |
| `inline-block` | Flows inside a line, but its inside behaves like a block | Yes |
| `none` | Takes no part in layout | No |
Display is not semantics
HTML tells the browser what an element means. A <ul> is a list, and a <nav> is a navigation region. CSS display tells the browser how to draw the box. These are separate things.
You can change the display value without changing the meaning of the element. A <span> is still a generic inline wrapper in the document, even if you give it display: block. A <li> styled with display: inline-block is still a list item, so assistive technology may still announce it as part of a list. Some display changes can affect how browsers expose elements, particularly tables and lists, but the general rule holds: choose the HTML element for its meaning and the display value for its layout.
Display comes from CSS
Each element has a default display value. The browser's built-in stylesheet sets it, which is why <div> and <span> behave differently without any CSS from you. The two rules below restate those defaults.
div {
display: block;
margin: 1rem 0;
}
span {
display: inline;
}The behavior comes from the display value and not from the tag name. If you write span { display: block; }, every span stacks like a div.
Seeing the difference
The next markup has two div elements and two span elements.
<div>Block one</div>
<div>Block two</div>
<span>Inline one</span>
<span>Inline two</span>The two divs each start on a new line and span the available width. The two spans sit side by side in the same line, and they take only as much width as their content needs.
Changing the value
You change the value with an ordinary declaration. The cascade, specificity, and inheritance rules apply as they do for any other property. One detail matters here: display is not inherited. Setting display: none on a parent hides its children because the parent generates no box, not because the children inherited the value.
.card-title {
display: block;
}
.inline-note {
display: inline;
}Common errors
- Setting width on a span and seeing no change. The span is inline, so
widthandheightare ignored. Later chapters cover this and the fix. - Assuming the tag fixes the behavior. Any element can take any display value.
- Using display to change meaning. Turning a
<div>into something that looks like a button does not make it a button. Use a<button>element. - Expecting
display: noneto act like an invisible box. The element generates no box at all, so it leaves no empty space.
What comes next
The following chapters take each value in turn. They start with block and inline, then explain how width, height, margin, and padding apply to each. After that they cover inline-block and its quirks, then none and how it differs from other ways of hiding content.
Exercise
A <span> has display: block applied through CSS. Does it become a different HTML element, and how does it behave in the layout?
Show answer
It stays a <span> in the document, so its meaning does not change. In layout it now generates a block-level box, so it starts on a new line and fills the available width, like a <div> does by default.
Exercise
A parent div has display: none. Its child has no display rule of its own. Is the child shown, and why?
Show answer
The child is not shown. The parent generates no box, so none of its descendants are rendered. The child does not inherit display: none, because display is not an inherited property. The child is omitted because its parent has no box.