Skip to content
tezvyn:

CSS 'display': How Elements Occupy Space

Source: developer.mozilla.orgMediumHow cards are made

CSS 'display': How Elements Occupy Space

The CSS display property defines an element's layout role: does it demand its own line (block) or share space (inline)? It's essential for everything from text flow to complex layouts with flex and grid.

Why it exists

By default, HTML elements have a specific way of occupying space—a takes a full line, while a sits within one. To build any non-trivial layout, we need to override these defaults. The display property is the primary tool for controlling an element's basic layout behavior.

The mental model

The display property sets two things: an element's outer type and its inner type. The outer type defines how the element interacts with its siblings: does it create a line break (block) or sit alongside them (inline)? The inner type defines how its own children are arranged, for example, using normal flow, a flexbox context, or a grid.

How it works

Different values for display produce distinct behaviors. The most common are: block: The element takes up the full width available to it and starts on a new line. You can set width and height. Examples: div, p, h1. inline: The element takes up only as much width as its content requires and flows with surrounding text. It ignores width, height, and vertical margin/padding. Examples: span, a, strong. inline-block: A hybrid. It flows alongside other elements like an inline element, but you can control its width, height, and margin like a block element. none: The element is completely removed from the document flow. It takes up no space and is not rendered. This is different from visibility: hidden, which just makes the element invisible but preserves its space. flex and grid: These values turn the element into a container, establishing a new layout context for its direct children. This unlocks powerful alignment and distribution capabilities defined in the Flexbox and Grid specifications.

When to use it

Use display to alter the default rendering of an element for layout purposes. For example, changing <li> elements from block to inline-block or flex items to create a horizontal navigation bar. It's fundamental for building component layouts and controlling visibility with display: none.

When not to use it

Avoid using display: none to hide content that should be accessible to screen readers. For that, use a dedicated 'visually-hidden' class that moves the content off-screen. Also, avoid legacy values like display: table for main page layout; Flexbox and Grid are the modern standards.

One canonical example

A classic task is creating a horizontal navigation menu from a list. An unordered list <ul> with list items <li> will stack vertically by default because <li> is display: block. By setting display: flex on the parent <ul>, the child <li> elements will automatically align horizontally, forming a navigation bar.

Interview question

Which CSS display value allows an element to flow horizontally with surrounding text while still permitting explicit control over its width and height?

  • a.inline
  • b.flex
  • c.block
  • d.inline-blockCorrect
Why?

Inline-block elements combine the characteristics of inline elements by flowing horizontally with text, and block elements by allowing explicit width, height, and vertical margin/padding. Inline elements flow horizontally but ignore width/height, while block elements take a new line.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles