Sub topic 1
CSS display: block, inline, inline-block, and none
Prerequisites
Assume you know basic HTML structure, CSS selectors and properties, and the CSS box model.
What the `display` property does
The CSS display property controls how an element generates a box and participates in layout. It affects whether the element starts on a new line, shares a line with surrounding content, accepts dimensions, and takes up layout space.
The property changes presentation, not HTML meaning. A span remains phrasing content even when styled as display: block. Likewise, changing a div to display: inline does not make it semantically equivalent to a span.
flowchart TD
A[display property] --> B[block box]
A --> C[inline box]
A --> D[inline-block box]
A --> E[no box: display none]caption: Common display outcomes alt: The display property can create block, inline, or inline-block boxes, or remove the element from layout.
Default display values: why `div` and `span` behave differently
HTML elements have user-agent stylesheet defaults. A div defaults to display: block, while a span defaults to display: inline.
<div>Block one</div>
<div>Block two</div>
<span>Inline one</span>
<span>Inline two</span>The div elements stack because each block box starts on a new line and normally stretches across its container. The span elements share a line when enough horizontal space exists.
`display: block`
A block box starts on a new line and pushes following block-level siblings below it. Its width defaults to the available width, minus applicable margins, borders, and padding.
Block boxes accept width, height, and all four margins and padding values. Common block-level elements include div, p, section, and headings, although CSS can change any element's display value.
.notice {
display: block;
width: 20rem;
margin: 1rem 0;
padding: 1rem;
}`display: inline`
An inline box participates in the current line of text. It does not start a new line. Its width and height declarations are ignored. Horizontal margin and padding apply, but vertical margin does not change the line's layout in the same way as block margin.
Use inline display for content-level elements such as links or emphasized text when they should remain within a sentence.
Exercise
Why does setting width: 200px on an inline span usually have no effect?
Show answer
Inline boxes flow with text, and their width and height are determined by their content and line formatting. Use display: inline-block or display: block when the element must accept explicit dimensions.