CSS `display` Property: Mastering HTML Element Layout and Rendering
Learn how to control the layout and rendering of HTML elements using the CSS `display` property. This tutorial covers key `display` values (inline, block, inline-block, none, etc.), explaining their effects on element dimensions, flow, and how they impact overall page layout and design.
Controlling Element Display with CSS `display`
Understanding the `display` Property
The CSS `display` property is fundamental for controlling the layout of HTML elements. It determines how an element is displayed and how it interacts with other elements on the page. Every HTML element has a default display value (either `inline`, `block`, or `inline-block`), which determines its initial rendering behavior. In essence, `display` controls the type of box an element creates and how it participates in the page's layout.
Common `display` Values
Several key values for the `display` property control how elements are rendered:
1. `inline`
Elements rendered inline with surrounding text; they don't start on a new line and only take up the width necessary for their content. Examples of inline elements include spans, anchor links, and strong emphasis tags. They don't create line breaks before or after themselves.
2. `inline-block`
Similar to `inline`, but you can set the width and height of the element. This allows more control over the element's dimensions while maintaining inline flow.
3. `block`
Elements rendered as block-level elements; they always start on a new line and take up the full width available to them. Examples of block-level elements include divs, paragraphs, and headings. They create line breaks before and after themselves.
4. `run-in`
This value's behavior is context-dependent and is not fully supported by all browsers (it's not reliably rendered in Firefox). It tries to integrate the element into the flow of surrounding text. If it's followed by a block-level element, it acts like an inline element; if followed by an inline element, it acts like a block-level element.
5. `none`
The element is completely hidden and doesn't take up any space in the layout.
Examples of `display` Values
These examples illustrate the different `display` values. You'll need the corresponding HTML elements (e.g., paragraphs or divs) to see the effects in a browser. The `display` property is applied directly to the HTML element in the CSS.
Example 1: `display: inline;`
Shows how inline elements flow with text.
Example 2: `display: inline-block;`
Demonstrates inline-block elements with custom width and height.
Example 3: `display: block;`
Illustrates how block elements start on new lines and take full width.
Example 4: `display: run-in;`
Shows the conditional behavior of `run-in`. Note that browser support can vary.
Example 5: `display: none;`
Demonstrates how `display: none;` completely removes an element from the layout.
Additional `display` Values
CSS3 introduced additional `display` values for more advanced layout control, like:
flex
andinline-flex
(for flexbox layouts).grid
andinline-grid
(for grid layouts).- Table-related values (
table
,table-row
,table-cell
, etc.) list-item