Controlling the Flow: CSS Display

Master the most fundamental CSS properties for building any layout, from simple stacking to complex responsive grids.

Lesson ProgressStep 1 of 9
Box 1
Box 2
Span 1Span 2
0 EXP

Welcome! Let's explore how CSS controls where elements appear on a page.

/* CSS layout journey begins! */

What is the `display` property?

The `display` property is the most important CSS property for controlling layout. It specifies how an element should be shown on the page and how it interacts with other elements.

Every HTML element has a default `display` value. For example, a `<div>` is `display: block` by default, while a `<span>` is `display: inline`. Changing this value can fundamentally alter the structure of your page.

System Check

What does the `display` property control?

Advanced Holo-Simulations

0 EXP

Apply your knowledge and earn achievements.


Achievements

🧱
Layout Shifter

Correctly use `block`, `inline`, and `inline-block`.

👻
Invisibility Cloak

Master the difference between `display: none` and `visibility: hidden`.

🧘
Flexbox Initiate

Build a basic layout using the power of `display: flex`.

Mission: Build a Flexible Layout

Use `display: flex` on the `.parent` to align the children. Then, completely hide the `.child-3` element. Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order by Space

Drag the `display` types into order from **most space** taken on the page to **least space** taken.

inline (Takes only content width)
block (Takes full width, new line)
none (Takes no space)

Challenge: Complete the Syntax

Fill in the missing properties and values to hide the elements as described.

.hide-but-keep-space {:;}
.remove-completely {:;}

Consult A.D.A.

Community Holo-Net

Beyond the Box: A Deep Dive into CSS Display

The CSS `display` property is arguably the most important and powerful property for controlling layout on the web. It doesn't just change one thing; it fundamentally alters an element's formatting context, dictating how it interacts with its siblings, its children, and the space it occupies.

The Two Worlds: Block vs. Inline

Every element in HTML has a default `display` value. They generally fall into two categories:

  • display: block: These elements are layout "bricks." They start on a new line and stretch to fill 100% of their parent's width by default. You can control their `width`, `height`, `margin`, and `padding`. Common examples include `<div>`, `<p>`, `<h1>`, and `<section>`.
  • display: inline: These elements are like "words" in a sentence. They flow together on the same line and only take up as much width as their content needs. Crucially, you **cannot** set a `width` or `height` on them, and vertical `margin` or `padding` will not affect surrounding elements. Examples include `<span>`, `<a>`, and `<strong>`.

This is where display: inline-block saves the day. It's the perfect hybrid: the element flows horizontally like an inline element, but you get to control its `width`, `height`, and vertical margins just like a block element. This was the standard for creating grids of cards or navigation buttons for many years.

Hiding Elements: The Accessibility Trap

You have two primary ways to hide an element, and their difference is critical.

`display: none` (Removes)

The element is completely **removed** from the document. It takes up no space, and the layout reflows as if it never existed. Importantly, it is also **hidden from assistive technologies** like screen readers. This is good for modals or dropdowns that aren't active.

`visibility: hidden` (Hides)

The element is made **invisible**, but it **still occupies its original space**. This leaves a "ghost" or an empty gap in your * 1 layout. A screen reader may or may not announce it, depending on the browser. It's useful for animations where you want to fade an element in without the layout "jumping."

Warning: Never use `display: none` or `visibility: hidden` to hide content that should *only* be for screen readers. For that, use a dedicated accessibility class (often called `.sr-only` or `.visually-hidden`) that moves the content off-screen.

The Modern Era: Flex & Grid

While `inline-block` was useful, it was a hack. Today, we have two powerful `display` values that create new "formatting contexts" for their children.

  • display: flex: Turns an element into a flex container. Its direct children (flex items) can be easily aligned, centered, spaced, and re-ordered along a single axis (either a row or a column). It's perfect for navbars, form controls, and centering things.
  • display: grid: Turns an element into a grid container. This is for two-dimensional layout. You can define explicit rows and columns, creating complex "magazine-like" layouts with incredible control. It's the go-to for overall page structure and card grids.
Key Takeaway:Use `display` to define the *type* of layout you want. Use `block` and `inline` for simple document flow. Use `flex` for one-dimensional alignment and `grid` for two-dimensional layouts. Use `display: none` and `visibility: hidden` with a clear understanding of their impact on layout and accessibility.

The New & Niche Values

Keep an eye on these other `display` values you may encounter:

  • display: contents: A strange but powerful value. It "evaporates" the element's own box, making its children act as direct children of the element's parent. This can be great for semantics (e.g., a `<div>` wrapper inside a flex container) but can have accessibility quirks.
  • display: flow-root: The modern, simple way to fix the old "clearfix" hack. If you have floating elements inside a container, setting `display: flow-root` on the container will force it to expand and contain them.

CSS Display & Visibility Glossary

display
The fundamental CSS property that specifies an element's rendering box type. It determines how an element is laid out (its formatting context) and how it interacts with other elements.
visibility
A CSS property that controls whether an element is visible or hidden. Unlike `display: none`, a hidden element still occupies its full space in the layout.
display: block
Makes an element a block-level element. It starts on a new line and takes up the full width available by default. You can control its `width`, `height`, `margin`, and `padding`. (e.g., `<div>`, `<p>`)
display: inline
Makes an element an inline-level element. It flows with surrounding text, does not start on a new line, and only takes up as much width as its content. `width` and `height` properties do not apply. (e.g., `<span>`, `<a>`)
display: inline-block
A hybrid value. The element flows horizontally (`inline`), but you can set its `width`, `height`, `margin`, and `padding` (`block`).
display: none
Completely removes the element from the document's layout. It takes up no space, and the layout "reflows" as if it never existed. It is also hidden from assistive technologies.
visibility: hidden
Makes the element invisible, but it **still occupies its full space** in the layout, leaving an empty gap. The browser does not need to recalculate the layout (reflow).
display: flex
Creates a "flex container," enabling a modern, one-dimensional layout system. Its direct children become "flex items" that can be easily aligned, spaced, and ordered along a single row or column.
display: grid
Creates a "grid container," enabling a modern, two-dimensional layout system of rows and columns. Ideal for complex page structures, forms, and card galleries.
Formatting Context
The environment in which an element's children are laid out. `display: block` creates a Block Formatting Context (BFC), while `display: flex` creates a Flex Formatting Context (FFC).
Reflow (Layout)
The browser process of recalculating element positions and geometry. Using `display: none` triggers a reflow, while `visibility: hidden` does not, which can be better for performance.
display: contents
A special value that "evaporates" the element's own box, making its children behave as direct children of the element's parent in the layout.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!