Skip to main content

The CSS display Property

The display property is one of the most important properties for controlling layout. It sets whether an element is treated as a block or an inline element.

Every element has a default display value based on its type, usually block or inline. The display property lets you change that default behavior.


Block-level Elements

A block-level element always starts on a new line and takes up the full width available.

The <div> is a block-level element. Other examples include <h1>–<h6>, <p>, <form>, <header>, <footer>, and <section>.


Inline Elements

An inline element does not start on a new line and only takes up as much width as it needs.

Examples of inline elements include <span>, <a>, and <img>.


Display Values

The display property has many values. The most common are:

  • inline — shows the element as an inline element.
  • block — shows the element as a block element.
  • contents — makes the container disappear, so its children move up a level in the DOM.
  • flex — shows the element as a block-level flex container.
  • grid — shows the element as a block-level grid container.
  • inline-block — formats the element as inline, but lets you set height, width, padding, and margin.
  • none — hides the element completely, so it takes up no space.

Override the Default Display Value

The display property changes the default behavior of an element. Changing an inline element to block, or the other way around, can help the page look the way you want while still following web standards.

Note: Setting display only changes how the element is shown, not what kind of element it is. So an inline element with display: block still cannot contain other block elements.

Span as block

The inline <span> elements are shown as block elements, each on its own line.

Prompt
An HTML page that displays inline span elements as block elements. In an internal <style> block, set span { display: block; }. Add a few <span> elements so they each appear on their own line instead of inline.
Output: complete HTML page

Link as block

The inline <a> elements are shown as block elements, each on its own line.

Prompt
An HTML page that displays inline link elements as block elements. In an internal <style> block, set a { display: block; }. Add a few <a> links so each appears on its own line.
Output: complete HTML page

Example of More Display Values

You can apply several different display values to elements.

Example

Six paragraphs use none, inline, block, inline-block, flex, and grid.

Prompt
An HTML page that demonstrates several display values. In an internal <style> block, make six paragraph classes: ex1 none, ex2 inline, ex3 block, ex4 inline-block, ex5 flex, ex6 grid. Add a labeled paragraph for each so the different display behaviors show (the "none" one will be hidden).
Output: complete HTML page