What is display?

The CSS display property controls how an element is laid out on the page โ€” how much space it takes, how it flows with other elements, and whether it respects width/height settings.

Every HTML element has a default display value. Block elements like <div>, <p>, and <h1> stack vertically and take full width. Inline elements like <span>, <a>, and <strong> flow horizontally and only take the width they need.

Understanding display values is fundamental to CSS layout. It determines whether an element is a block, inline, or something in between โ€” and affects everything from navigation bars to cards and grids.

Key point:
The display property is one of the most important CSS properties for controlling layout. Master it, and you control how content flows on your page.

display: block

Block elements take the full available width and stack vertically, one below the other. They respect width, height, margin, and padding โ€” even if they're not explicitly set.

Default block elements: <div>, <p>, <h1> through <h6>, <ul>, <ol>, <section>, <article>, <header>, <footer>.

styles.css Block elements stack vertically

Use display: block when you want full-width elements that stack vertically. This is the default for most container elements.

display: inline

Inline elements flow horizontally, taking only the width they need. They ignore width, height, margin-top, and margin-bottom settings.

Default inline elements: <span>, <a>, <strong>, <em>, <img>, <button>.

styles.css Inline elements flow horizontally

Inline elements are useful for small pieces of text or buttons within a paragraph. But they have limitations โ€” you can't control their width or vertical margin.

display: inline-block

Inline-block is the best of both worlds. Elements flow horizontally like inline elements, but respect width, height, margin, and padding like block elements.

styles.css Inline-block: hybrid layout

Inline-block is ideal for buttons, navigation links, and small cards that should sit side-by-side but with full control over spacing and sizing.

Three ways to hide elements

In CSS, you have three main ways to hide an element: display: none, visibility: hidden, and opacity: 0. Each behaves differently.

display: none

Removes the element from the document flow entirely. It takes up no space, and surrounding elements collapse into the freed space.

visibility: hidden

Hides the element visually, but keeps its space in the layout. Surrounding elements don't move.

opacity: 0

Makes the element transparent (invisible), but it still occupies space and can be clicked. Useful for animations and keeping elements interactive while hidden.

styles.css Three ways to hide: comparison
Summary:
  • display: none = element gone, space freed (best for removing elements)
  • visibility: hidden = invisible but space kept (use when layout must stay stable)
  • opacity: 0 = transparent and clickable (use for animations and hover effects)

Try it yourself: Block vs Inline vs Inline-Block

Edit the code below to see how different display values affect layout. Notice how block takes full width, inline flows horizontally, and inline-block combines both.

โœ๏ธ Try It Yourself โ€” Display Values
Preview

Try it yourself: Three ways to hide

Edit the code below to see how display:none, visibility:hidden, and opacity:0 differ. Notice how neighboring elements react differently depending on which property you use.

โœ๏ธ Try It Yourself โ€” Hiding Elements
Preview

Practical use case: Navigation bar

Inline-block is perfect for building horizontal navigation bars. Each link is an inline-block element, so they flow horizontally but can have padding, background colors, and hover effects.

styles.css Building a navigation bar with inline-block

This pattern is used on thousands of websites. Inline-block lets you build clean, simple navigation without needing flexbox or grid.

When to use each display value

  • display: block โ€” Use for full-width elements that stack (containers, sections, paragraphs).
  • display: inline โ€” Use for text-like elements within a paragraph (bold, italics, links). Rarely explicitly set since it's the default for <span>, <a>, etc.
  • display: inline-block โ€” Use for buttons, navigation links, and small cards that should sit side-by-side with spacing control.
  • display: none โ€” Use to completely remove elements from the layout (mobile menu toggles, hidden content).
  • visibility: hidden โ€” Use when you need to hide an element but preserve its space in the layout.
  • opacity: 0 โ€” Use for animations, hover effects, and when you need the element to remain clickable while invisible.
Pro tip:
For modern layouts, consider using display: flex or display: grid instead of inline-block. They're more powerful and flexible. But inline-block is simpler for small, straightforward layouts like navigation bars.
Challenge: Build a Horizontal Navigation Bar

Task: Build a navigation bar with these requirements:

  • Use inline-block on navigation links
  • Add padding (12px 20px) to each link
  • Set a background color and text color
  • Add a hover effect that changes the background
  • Create a "show/hide" demo: add a button that toggles an element between visible and hidden using either display:none or visibility:hidden

Use the Try It Yourself editor above to build it. Think about which display property to use and why.

Hint: Start with a <nav> container and <a> tags inside. Set each <a> to display: inline-block. For the toggle, create a <button> that calls a JavaScript function to add/remove a class that sets display: none or visibility: hidden.

Key takeaways

  • display: block โ€” Full width, stacks vertically, respects sizing.
  • display: inline โ€” Flows horizontally, ignores width/height/vertical margin.
  • display: inline-block โ€” Flows horizontally but respects sizing. Perfect for buttons and navigation links.
  • Three ways to hide: display: none (removes space), visibility: hidden (keeps space), opacity: 0 (transparent and clickable).
  • Choose the right display value based on your layout needs.

Test your knowledge

Question 1 of 5
Which display value takes the full available width and stacks vertically?
Question 2 of 5
Can you set width and height on an inline element?
Question 3 of 5
What is the key difference between display:none and visibility:hidden?
Question 4 of 5
Which display value is best for building a horizontal navigation bar with buttons?
Question 5 of 5
Which property makes an element invisible but still clickable?
0
/5
Great start! You understand display and visibility.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered CSS display.
Imagine what you'll build next.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’