Display Property

What You'll Learn

  • How the display property controls box generation and layout behavior
  • The difference between block, inline, and inline-block display types
  • When to use display: none vs visibility: hidden
  • Understanding display: contents for removing wrapper elements
  • How display values affect width, height, and margin/padding behavior

Introduction to Display

The display property is fundamental to CSS layout. It controls how an element generates boxes and participates in the layout system. Every HTML element has a default display value, but you can change it to achieve different layout behaviors.

Understanding display is crucial because it affects not just the element itself, but how it interacts with surrounding elements. Different display values determine whether an element takes up the full width available, flows inline with text, or is removed from the layout entirely.

display: block - Full-Width Stacking

Block-level elements start on a new line and stretch to fill the full available width of their container. They stack vertically and respect all box model properties (width, height, margin, padding).

Syntax

display: block; .container { display: block; width: 600px; margin: 0 auto; }

Example

<div class="block-demo"> <p>Block Element 1</p> <p>Block Element 2</p> <p>Block Element 3</p> </div>

Rendered Result:

display: inline - Text Flow

Inline elements flow with the text content and only take up as much width as their content requires. They do not start on a new line and sit on the same line as adjacent inline elements. Importantly, inline elements ignore width and height properties, and vertical margins/padding may not behave as expected.

Syntax

display: inline; .tag { display: inline; padding: 0.25rem 0.5rem; background: lightgray; }

Example

<p> This is a paragraph with <span class="tag">inline</span> elements that <span class="tag">flow</span> with the text. </p>

Rendered Result:

display: inline-block - Best of Both Worlds

Inline-block elements combine characteristics of both inline and block elements. They flow inline like inline elements (don't force line breaks) but accept width, height, and all margin/padding like block elements. This makes them perfect for creating button groups, navigation items, and grid-like layouts without using actual grid or flexbox.

Syntax

display: inline-block; .button { display: inline-block; width: 150px; padding: 0.75rem; text-align: center; }

Example

<div class="button-group"> <button class="button">Save</button> <button class="button">Cancel</button> <button class="button">Delete</button> </div>

Rendered Result:

display: none - Removing from Layout

Elements with display: none are completely removed from the document flow and layout. They take up no space and are not rendered at all. This is different from visibility: hidden, which hides the element but still reserves its space in the layout.

Syntax

display: none; .modal { display: none; } .modal.active { display: block; }

Example

<div class="demo"> <p>Visible paragraph</p> <p class="hidden">Hidden paragraph (display: none)</p> <p>Another visible paragraph</p> </div>

Rendered Result:

display: none vs visibility: hidden

/* Completely removes element from layout */ .gone { display: none; } /* Hides element but keeps its space */ .invisible { visibility: hidden; }

display: contents - Removing the Box

The display: contents value causes an element's box to disappear, promoting its children to take its place in the layout. This is useful when you need a wrapper element in HTML for semantic or JavaScript purposes, but don't want it to affect the CSS layout.

Syntax

display: contents; .wrapper { display: contents; }

Example

<div class="grid"> <div>Grid Item 1</div> <div class="wrapper"> <!-- These children become direct grid items --> <div>Grid Item 2</div> <div>Grid Item 3</div> </div> <div>Grid Item 4</div> </div>

Rendered Result:

Display Value Comparison

Here's a quick reference showing how different display values affect element behavior:

Display Value Line Breaks Width/Height Margin/Padding Takes Space
block Forces new line Respected All directions Yes
inline No line break Ignored Horizontal only Yes
inline-block No line break Respected All directions Yes
none Removed N/A N/A No
contents Box removed N/A (box gone) N/A (box gone) Children do

Key Takeaways