Sub topic 1
What the display property does
Prerequisites
This chapter assumes you already know three things:
- Basic HTML elements and how a document is structured.
- How to write CSS rules with selectors and properties.
- The CSS box model: content, padding, border, and margin.
You do not need to know anything about flexbox or grid. They appear only briefly at the end.
The question display answers
Every element in an HTML document is a node in a tree. The browser must decide what to draw for each node. It might draw a box that starts on its own line, a box that sits inside a line of text, or nothing at all. The display property is how CSS answers that question.
display controls two things:
- How the element's box takes part in its parent's layout. Does it stack vertically with its siblings, or flow along a line with text?
- How the element lays out its own children. Do they flow normally, or are they arranged by another system such as flex or grid?
The CSS Display specification calls these the outer display type and the inner display type. This chapter focuses on the outer type, because the four values in this tutorial (block, inline, inline-block, none) mainly differ in how they behave among their neighbours.
Display is not semantics
An HTML tag describes what content means. A <p> is a paragraph, a <nav> is a navigation region, and a <span> has no special meaning. display describes how that content is laid out. The two are separate.
The browser connects them only through its default stylesheet, which gives each element a starting display value. A <div> is not block because it is a <div>. It is block because the browser's stylesheet contains a rule much like div { display: block; }. You can override that rule like any other.
div {
display: block;
margin: 1rem 0;
}
span {
display: inline;
}These rules restate the defaults. Writing them changes nothing. They show that the behavior comes from the CSS value, not from the tag name. The next chapter lists the defaults for common elements.
You can swap the behavior. This markup:
<div>Block one</div>
<div>Block two</div>
<span>Inline one</span>
<span>Inline two</span>renders the two div elements on separate lines, each spanning the available width. The two span elements sit side by side in one line. If you add div { display: inline; } and span { display: block; }, the behavior reverses, and the HTML does not change at all.
What a value actually decides
At the intermediate level, it helps to hold a precise model. When the browser builds the layout, it generates boxes from elements. The display value determines which kind of box, if any.
blockgenerates a block-level box. It participates in a block formatting context, where boxes stack vertically, and by default it fills the width of its container.inlinegenerates an inline-level box that flows inside a line box alongside text. It wraps across lines when needed.inline-blockgenerates an inline-level box that is itself a block container. It flows like text from the outside and behaves like a block from the inside.nonegenerates no box. The element and all its descendants are skipped during layout.
A short comparison:
| Value | Outside behavior | Inside behavior | Starts on a new line | Accepts width and height |
|---|---|---|---|---|
| `block` | Block-level | Flow layout | Yes | Yes |
| `inline` | Inline-level | Inline content | No | No |
| `inline-block` | Inline-level | Flow layout | No | Yes |
| `none` | No box | No box | Not applicable | Not applicable |
The last column for inline is the source of many beginner mistakes. The following rule has no visible effect on width or height:
span.tag {
display: inline;
width: 200px;
height: 50px;
margin: 20px;
padding: 10px;
}On an inline box, width and height are ignored. The vertical margin does not change line spacing. Horizontal margin and horizontal padding do take effect. Vertical padding is painted but does not push neighbouring lines away. A later chapter works through this in detail.
The diagram below shows how the value feeds into box generation.
The two-value syntax
Modern CSS lets you write display with two keywords, one for the outer type and one for the inner type. The familiar single keywords are shorthand for those pairs.
| Single keyword | Two-value form |
|---|---|
| `block` | `block flow` |
| `inline` | `inline flow` |
| `inline-block` | `inline flow-root` |
| `flex` | `block flex` |
| `inline-flex` | `inline flex` |
| `grid` | `block grid` |
This explains why inline-block has its name. It is an inline outer type wrapped around a block-like inner type (flow-root, which creates a self-contained block container). You can write the single keywords everywhere. They have the widest support. The two-value form is useful for understanding the model and may appear in newer code.
Display is set per element and is not inherited
Two properties of display surprise people:
- It is not inherited. Setting
display: inline-blockon a<ul>does not make its<li>children inline-block. Each element gets its own value from the cascade, or from the browser default. - It participates in the cascade like any other property. A class selector overrides the browser default. A more specific selector overrides the class. A later equal-specificity rule wins.
.row li {
display: inline-block;
}
.row li.wide {
display: block;
}Here, every li inside .row is inline-block, except the one with class wide. That li matches a more specific selector and becomes block.
You can inspect the result with your browser's developer tools. Open the Computed tab for the element and look at display. This tells you the value that actually applies, after all rules are resolved. When layout looks wrong, check this value first.
Changing display can change more than layout
Because display removes or reshapes boxes, it can have side effects beyond appearance.
display: noneremoves the element from the accessibility tree. Screen readers will not announce it, and it cannot receive keyboard focus. This is useful for content that should be unavailable, and harmful if you hide something users need.- Some elements carry special layout rules. Table elements such as
<table>,<tr>, and<td>have table display values by default. If you set them toblock, you lose table layout. Some browsers also lose the table semantics for assistive technology, though recent versions have improved this. - Replaced elements such as
<img>are inline by default but behave in some ways like inline-block. They acceptwidthandheighteven though they are inline-level. This is a special case, not a general rule. - Some HTML features rely on a box. An element with
display: noneis not rendered, so its dimensions cannot be measured from layout. Scripts that readoffsetWidthget0for it.
Common errors at this stage
These mistakes show up repeatedly when people first work with display:
- Expecting width on an inline element. The rule is ignored, and nothing warns you. Change the value to
inline-blockorblock. - Assuming
displayis inherited. Set it on the element you want to change, not on its parent. - Confusing
display: nonewith a style that only hides the element. It removes the box, so the layout closes up around the gap. Other hiding techniques leave a gap, and a later chapter compares them. - Overriding the tag's meaning with CSS and forgetting accessibility. Layout changes are cosmetic. Meaning comes from the HTML.
- Setting
displayand then wondering why a later rule has no effect. Check specificity and source order. Another rule may be settingdisplayafter yours.
Where this chapter fits
This chapter has set up the central idea: display decides the type of box an element generates, and the HTML tag only supplies a default. The following chapters build on this. They cover the default values for common elements, then the detailed behavior of block, inline, and inline-block, and finally display: none and how it differs from other ways of hiding content.
Exercise
A colleague writes span.label { width: 120px; height: 40px; } and reports that the span ignores both values. Using what you know about display, explain why this happens and give two different ways to fix it.
Show answer
A span has a default display of inline, and inline boxes ignore width and height. The tag name does not matter, only the computed display value. Fix 1: add display: inline-block; so the span stays in the line of text but accepts width and height. Fix 2: add display: block; so the span starts on its own line and accepts width and height, which is appropriate if you do not need it to sit beside other content.
Exercise
You set display: inline-block; on a <ul> element, hoping its <li> children will line up in a row. They still stack vertically. What is the reason, and what should you change?
Show answer
display is not inherited, so the <li> elements keep their own default value (list-item, which is block-level). Setting the value on the <ul> only changes how the list as a whole sits among its siblings. To put the items in a row, apply the value to the children, for example ul li { display: inline-block; }, or make the <ul> a flex container with display: flex;.