Tutorials CSS Display Property
Ch 20 / 60
Chapter 20 Beginner 7 min read CSS

CSS Display Property

Every CSS layout decision starts with display. It controls whether an element stacks vertically, flows with text, creates a flex container or grid container, or disappears entirely. Understanding it deeply is the key to mastering every CSS layout system.

What you'll learn
What display controls and why it matters
block vs inline — the two fundamental types
inline-block — best of both worlds
flex and grid — modern layout containers
none vs visibility: hidden
All display values and real-world use cases

What Does display Control?

The display property defines two things for an element: its outer display type (how it participates in the parent layout flow — block or inline) and its inner display type (what layout algorithm it creates for its children — normal flow, flex, or grid).

display: block
outer = block
stacks on its own line in flow
inner = flow (normal)
children flow normally
Used by: div, p, h1–h6, section, ul…
display: flex
outer = block
takes up full line in flow
inner = flex
children become flex items
Outer is still block (takes a full line). Inner changes — children become flex items.
display: inline
outer = inline
flows with text on same line
inner = flow
inline children only
Used by: span, a, strong, em, img…
display: grid
outer = block
takes up full line in flow
inner = grid
children become grid items
Like flex but in two dimensions. Children become grid items placed on a grid.
CSS — display syntax
.element { display: block; }
.element { display: inline; }
.element { display: inline-block; }
.element { display: flex; }
.element { display: inline-flex; }
.element { display: grid; }
.element { display: inline-grid; }
.element { display: none; }       /* removes from layout */
.element { display: contents; }    /* element's box disappears */
.element { display: table; }       /* behaves like <table> */
.element { display: list-item; }   /* like <li> with marker */

display: block

Block elements start on a new line, stretch to fill the full width of their parent, and respect all box model properties (width, height, margin, padding). The next element always starts below.

Live demo — each box starts on its own line
Block 1 — width: 90%
Block 2 — width: 70%
Block 3 — width: 50%
Block 4 — default full width
CSS — display: block
/* HTML block elements by default: */
/* div, p, h1–h6, section, article, header,
   footer, nav, main, ul, ol, li, form, hr… */

/* Making an inline element block */
span   { display: block; } /* span now acts like div */
a      { display: block; } /* link fills full width */
button { display: block; width: 100%; } /* full-width button */

/* Block element characteristics */
div {
  display:  block;
  width:    300px;    /* ✓ respected */
  height:   100px;    /* ✓ respected */
  margin:   16px 0;  /* ✓ all 4 sides work */
  padding:  12px;    /* ✓ all 4 sides work */
}

display: inline

Inline elements flow with the surrounding text. They only take up as much width as their content, do not start on a new line, and ignore width, height and top/bottom margins.

Live demo — inline elements flow within text
Normal text, then span 1 continues on the same line, and span 2 also stays inline with text, and span 3 wraps naturally like words do. Setting width/height on these has no effect.
CSS — display: inline
/* HTML inline elements by default: */
/* span, a, strong, em, b, i, u, code, small,
   label, button, img, input, select… */

/* Inline element behaviour */
span {
  display:       inline;
  width:         200px;  /* ✗ IGNORED */
  height:        50px;   /* ✗ IGNORED */
  margin-top:    20px;   /* ✗ IGNORED (vertical margins) */
  margin-left:   10px;   /* ✓ horizontal margin works */
  padding-left:  8px;    /* ✓ horizontal padding works */
  padding-top:   4px;    /* ⚠ visual only, no layout effect */
}

/* Making a block element inline */
div { display: inline; }  /* div now flows like span */
Inline limitations
width and height have no effect on inline elements. Vertical margin-top and margin-bottom are also ignored. If you need to set dimensions on an element that stays in the text flow, use display: inline-block instead.

display: inline-block

inline-block combines both worlds: it flows inline with text like inline elements, but respects width, height and all margins and padding like block elements.

Live demo — inline-block elements sit side by side and accept dimensions
80×40
60×60
120×36
50×50
auto width
nav links
buttons
Primary
Outline
Ghost
badges in text
CSS Tutorial Beginner —Chapter 20 New by K2infocom Free
CSS — display: inline-block
.badge {
  display:       inline-block;
  padding:       2px 10px;    /* ✓ works on all sides */
  border-radius: 999px;
  font-size:     11px;
  font-weight:   700;
  /* Flows inline with text but respects padding/size */
}

.nav a {
  display:     inline-block;
  padding:     8px 16px;  /* clickable area */
  font-weight: 600;
}

.icon-box {
  display:     inline-block;
  width:       48px;      /* ✓ width respected */
  height:      48px;      /* ✓ height respected */
  vertical-align:middle;  /* align with surrounding text */
}

/* Note: inline-block elements have a tiny whitespace
   gap between them (caused by HTML whitespace).
   Use flexbox or gap:0 on parent to remove it. */

Block vs Inline vs Inline-Block — Comparison

Property block inline inline-block
Starts on new line Yes No No
Default width 100% of parent Content width Content width
Respects width/height Yes No Yes
Horizontal margin/padding Yes Yes Yes
Vertical margin (top/bottom) Yes No Yes
Vertical padding (top/bottom) Yes Visual only Yes
Flows with text No Yes Yes
Typical elements div, p, h1, section span, a, strong, em img, button, input

display: flex & display: grid

flex and grid create layout containers for their children. The container itself still behaves as a block element in the outer flow — the magic happens inside:

flex
Modern
One-dimensional layout. Children become flex items arranged in a row or column. Perfect for nav bars, button groups, centring, and component layouts.
inline-flex
Modern
Like flex but the container flows inline with surrounding text. Used for icon + label combos in running text.
grid
Modern
Two-dimensional layout. Children become grid items placed in rows and columns simultaneously. Perfect for page layouts, card grids and complex UIs.
inline-grid
Modern
Like grid but the container flows inline. Rare in practice but useful for inline grid components.
CSS — display: flex & grid
/* ── Flex container ── */
.nav {
  display:         flex;
  align-items:     center;
  gap:             16px;
}

.card {
  display:         flex;
  flex-direction:  column;
  justify-content: space-between;
}

/* Centre content — classic flex trick */
.centered {
  display:          flex;
  align-items:      center;
  justify-content:  center;
}

/* ── Grid container ── */
.card-grid {
  display:              grid;
  grid-template-columns:repeat(auto-fill, minmax(250px, 1fr));
  gap:                  20px;
}

.page-layout {
  display:              grid;
  grid-template-columns:260px 1fr; /* sidebar + main */
  grid-template-rows:   auto 1fr auto; /* header + content + footer */
  min-height:           100vh;
}
Flex and Grid get their own chapters
Flexbox (Chapter 25–28) and CSS Grid (Chapter 29–33) are powerful enough to deserve full chapters each. This chapter introduces them as display values. The detailed guide to all their sub-properties comes later.

display: none — Hiding Elements

display: none completely removes an element from the document flow. It takes up no space, is invisible, and its position is given to the elements around it. Compare with visibility: hidden which hides visually but keeps the space:

display: none
Box 1
Box 3
Box 2 is gone. Box 3 moves up to fill the space. Zero space reserved.
visibility: hidden
Box 1
Box 3
Box 2 is invisible but its space is reserved. Box 3 stays in the same position.
CSS — display: none and alternatives
/* ── display: none — removes from flow completely ── */
.hidden      { display: none; }

/* ── visibility: hidden — invisible, space preserved ── */
.invisible   { visibility: hidden; }

/* ── opacity: 0 — invisible, space preserved, events fire ── */
.transparent { opacity: 0; }

/* ── Comparison summary ──
  display:none     → invisible, no space, no events
  visibility:hidden→ invisible, space kept, no events
  opacity:0        → invisible, space kept, events fire ── */

/* ── Common toggle pattern (with JavaScript) ── */
.menu         { display: none; }
.menu.is-open { display: block; }

/* ── Responsive show/hide ── */
.mobile-only  { display: none; }
@media (max-width: 640px) {
  .mobile-only{ display: block; }
  .desktop-only{ display: none; }
}

All display Values — Quick Reference

CSS has many display values. Here are all the ones you need to know:

block
Layout
Full-width, starts on new line. Respects all box model props. Default for div, p, h1–h6, section.
inline
Flow
Flows with text. Ignores width/height/vertical margin. Default for span, a, strong, em.
inline-block
Flow
Flows inline but respects all box model props. Default for img, button, input, select.
flex
Modern
Block-level flex container. One-dimensional. Children become flex items.
inline-flex
Modern
Inline-level flex container. Flows with text, children are flex items.
grid
Modern
Block-level grid container. Two-dimensional. Children become grid items.
inline-grid
Modern
Inline-level grid container. Rare in practice.
none
Hide
Removes element from layout entirely — no space, no visibility, no events.
contents
Special
The element's own box disappears — its children participate in the parent layout directly. Useful for unwrapping elements.
table
Special
Behaves like <table>. Paired with table-row and table-cell. Rarely needed — use grid instead.
list-item
Special
Block-level with a list marker (bullet/number). Default for <li>.
flow-root
Layout
Creates a new block formatting context — clears floats, contains margins. Modern alternative to overflow:hidden.

display: contents — The "Vanishing Box"

display: contents makes the element's own box disappear while its children participate in the parent's layout as if the wrapper never existed. Useful for semantic wrappers in flex/grid layouts:

CSS & HTML — display: contents use case
<!-- Grid parent with a semantic wrapper -->
<div class="grid-parent">
  <div class="item">Item 1</div>
  <div class="wrapper">   <!-- semantic grouping -->
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
  </div>
  <div class="item">Item 4</div>
</div>

/* Without display:contents — wrapper breaks the grid */
.grid-parent { display: grid; grid-template-columns: repeat(4,1fr); }
.wrapper     { } /* wrapper = one grid cell, items 2&3 inside it */

/* With display:contents — wrapper disappears, 4 items in grid */
.wrapper     { display: contents; }
/* Now all 4 items are direct grid children */
Accessibility note
display: contents removes the element's box from the accessibility tree in some browsers — interactive elements like <button> and <a> may lose their role. Use it only on non-interactive wrapper divs.
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all CSS display values, layout properties and box model rules.
Download PDF

Try It Yourself

Change the display value in the editor. Notice how switching from block to inline removes the ability to set width, and how none removes elements from flow entirely.

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 20

Questions & Answers
Block elements: start on a new line, take the full width of their parent by default, and respect all box model properties — width, height, margin (all four sides), and padding.

Inline elements: flow with the surrounding text on the same line, only take as much width as their content, and ignore width, height, and vertical margins (top/bottom). Horizontal padding and margin work, but vertical padding is visual-only (doesn't push layout).

Examples: div, p, h1 are block by default. span, a, strong are inline by default.

You can override the default: span { display: block; } makes a span behave like a div, and div { display: inline; } makes a div flow like a span.
inline-block solves the limitations of both block and inline:

— Like inline: stays in the text flow, does not start on a new line, multiple elements sit side by side
— Like block: respects width, height, and all four sides of margin and padding

Common uses:
— Navigation links with explicit padding (clickable area)
— Badge/tag/chip components inside text
— Button groups that flow inline
— Icon boxes with fixed dimensions in text

Note: inline-block elements have a subtle whitespace gap between them (caused by HTML newlines/spaces between tags). You can eliminate this by using flexbox on the parent, setting font-size: 0 on the parent (and resetting on children), or keeping HTML tags on the same line.
Both hide an element visually, but they have different effects on layout and interactivity:

display: none:
— Completely removes the element from the document flow
— Takes up zero space — surrounding elements move to fill the gap
— Invisible, not focusable, events do not fire
— The element is also removed from the accessibility tree

visibility: hidden:
— Makes the element invisible but preserves its space in the layout
— Surrounding elements stay in position — a blank gap remains
— Not visible, not interactive, but the space is still there

A third option is opacity: 0 — invisible and space preserved, but events still fire (clicks work on transparent elements).

Use display: none when you want to toggle layout (menus, modals). Use visibility: hidden when you need a placeholder space.
Setting display: flex or display: grid changes two things:

1. The element itself still behaves as a block-level element in the outer flow — it takes a full line (just like display: block).

2. Its children are now controlled by a new layout algorithm — either flex formatting context or grid formatting context. They become flex items or grid items.

This is the "outer/inner display" model. flex is short for "block-level container with a flex inner layout". Use inline-flex to make the container itself flow inline while still creating a flex context for children.
display: contents makes the element's own box "disappear" — its children participate in the parent layout context as if the element itself were not there.

Practical use: you have a semantic wrapper <div class="group"> inside a flex or grid container. Without display: contents, the wrapper becomes one flex/grid item and the items inside it are children of the wrapper, not the container. With display: contents, the wrapper disappears from layout while keeping the semantic grouping in HTML — its children become direct flex/grid items of the outer container.

Accessibility warning: in some browsers, display: contents on interactive elements (<button>, <a>) removes them from the accessibility tree. Use only on non-interactive wrapper divs.
Multiple Choice Questions
Question 1 of 5
Which display value makes an element flow with text but still respect width and height?
Question 2 of 5
What happens to surrounding elements when you apply display: none?
Question 3 of 5
Which property does a display: inline element ignore?
Question 4 of 5
When you set display: flex on an element, what changes about its children?
Question 5 of 5
Which display value hides an element while preserving its space in the layout?
Chapter Summary
  • display controls an element's outer type (block/inline in the parent flow) and inner type (how it lays out its children).
  • block — starts on a new line, full width, respects all box model properties. Default for div, p, h1–h6, section.
  • inline — flows with text, content width only, ignores width/height and vertical margins. Default for span, a, strong.
  • inline-block — flows inline but respects all dimensions and margins. Default for img, button, input. Great for badges, nav links, icon boxes.
  • flex — block-level container, one-dimensional flex layout for children. inline-flex flows inline.
  • grid — block-level container, two-dimensional grid layout for children. inline-grid flows inline.
  • none — removes from flow: no space, no visibility, no events. visibility: hidden — invisible but space preserved. opacity: 0 — invisible, space preserved, events still fire.
  • contents — element's box disappears; children join parent layout directly. flow-root — creates a new block formatting context (modern float-clearing).
~7 min read
Beginner level
Chapter 20 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel