CSS Display
The CSS display property controls how an element behaves in the layout.
It decides whether an element is block, inline, hidden, flexible, grid-based,
table-like, or removed from visual flow. Understanding display is essential for
building layouts, components, navigation menus, cards, forms, and responsive designs.
What Is CSS Display?
CSS display defines how an HTML element is rendered and how it interacts
with other elements around it. It affects box behavior, layout flow, width, height,
spacing, alignment, and visibility.
.box {
display: block;
}
.nav-link {
display: inline-block;
}
.card-grid {
display: grid;
}
Every HTML element has a default display value. For example, <div>
is block by default, while <span> is inline by default.
Why CSS Display Is Important Controls how elements appear in the page layout. Defines whether elements stack or flow inline. Enables modern layouts with Flexbox and Grid. Helps build navigation menus, cards, buttons, forms, and dashboards. Controls whether elements are visible or removed from layout. Improves responsive design when used with media queries. Helps avoid layout bugs caused by wrong element behavior. CSS Display Cheatsheet
The following table explains the most commonly used display values.
Display Value Example Purpose block display: block; Element starts on a new line and takes available width. inline display: inline; Element flows with text and does not start a new line. inline-block display: inline-block; Flows inline but supports width, height, padding, and margin. none display: none; Removes element from visual layout and accessibility tree. flex display: flex; Creates a one-dimensional flexible layout. inline-flex display: inline-flex; Creates an inline-level flex container. grid display: grid; Creates a two-dimensional grid layout. inline-grid display: inline-grid; Creates an inline-level grid container. contents display: contents; Makes the wrapper box disappear while children remain rendered. flow-root display: flow-root; Creates a new block formatting context. table display: table; Makes an element behave like a table. list-item display: list-item; Makes an element behave like a list item with marker support.
Default Display Values
HTML elements have default display values provided by the browser stylesheet.
HTML Element Default Display Behavior <div> block Starts on a new line and takes full available width. <p> block Paragraph block. <h1> to <h6> block Heading blocks. <span> inline Flows inside text. <a> inline Inline link text. <img> inline Inline replaced element. <li> list-item List item with marker behavior. <table> table Table layout behavior.
display: block
A block element starts on a new line and usually takes the full available width.
Width, height, margin, and padding work as expected.
.section {
display: block;
padding: 2rem;
margin-bottom: 1rem;
}
Common block elements include div, p, section,
article, header, footer, and headings.
display: inline
Inline elements flow with surrounding text and do not start on a new line.
Width and height do not apply the same way as block elements.
.highlight {
display: inline;
color: #0d6efd;
}
Common inline elements include span, a, strong,
em, and code.
display: inline-block inline-block combines inline flow with block-like sizing.
The element stays inline but supports width, height, padding, and margin.
.tag {
display: inline-block;
padding: 0.35rem 0.75rem;
border-radius: 999px;
background-color: #e7f1ff;
color: #0d6efd;
}
Use inline-block for badges, tags, buttons, chips, and small labels.
display: none display: none; removes an element from the page layout.
The element takes no space and is usually unavailable to screen readers.
.hidden {
display: none;
}
Use it when content should be completely hidden. Do not use it for content that
should remain accessible to screen readers.
display: flex
Flexbox creates one-dimensional layouts. It is useful for rows, columns, alignment,
spacing, navigation bars, buttons, cards, and form groups.
.nav {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
}
Use Flexbox when you need alignment and spacing in one direction.
display: inline-flex inline-flex creates a flex container that behaves like an inline element.
It is useful for buttons, icon text, badges, and compact controls.
.button {
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 0.75rem 1rem;
} display: grid
CSS Grid creates two-dimensional layouts with rows and columns.
It is useful for page layouts, card grids, galleries, dashboards, and complex UI sections.
.card-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
Use Grid when you need control over rows and columns together.
display: contents display: contents; removes the element’s own box while keeping its children
rendered as if they were direct children of the parent.
.wrapper {
display: contents;
}
Use it carefully because it may affect accessibility semantics in some cases and can make
layout debugging harder.
display: flow-root flow-root creates a new block formatting context. It is useful for clearing floats
without clearfix hacks.
.clearfix-container {
display: flow-root;
}
This can prevent floated children from escaping their parent container.
Table Display Values
CSS can make elements behave like table elements using display values such as
table, table-row, and table-cell.
.fake-table {
display: table;
width: 100%;
}
.fake-row {
display: table-row;
}
.fake-cell {
display: table-cell;
padding: 0.75rem;
}
Modern layouts usually prefer Flexbox or Grid, but table display values can be useful
for specific legacy layout patterns.
display: none vs visibility: hidden vs opacity: 0
These properties can hide elements, but they behave differently.
Method Takes Layout Space? Visible? Common Use display: none; No No Hide content completely. visibility: hidden; Yes No Hide visually while preserving layout space. opacity: 0; Yes No, but still visually transparent. Animation transitions; use carefully with interactions.
.hidden-complete {
display: none;
}
.hidden-space {
visibility: hidden;
}
.transparent {
opacity: 0;
} Responsive Display with Media Queries
The display property is often changed across screen sizes for responsive layouts.
.mobile-menu {
display: block;
}
.desktop-menu {
display: none;
}
@media (min-width: 768px) {
.mobile-menu {
display: none;
}
.desktop-menu {
display: flex;
}
}
Use responsive display changes carefully so important content remains accessible and usable.
CSS Display and Accessibility
Display values can affect accessibility, especially when hiding content or changing
layout order.
display: none; hides content from visual users and usually screen readers. Do not hide important text that users need to understand the page. Use visually hidden utilities for screen-reader-only text instead of display: none;. Be careful with visual reordering in Flexbox or Grid because keyboard order follows HTML source order. Use visible focus states for displayed interactive elements. .visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
} CSS Display and SEO
The display property does not directly create SEO metadata, but it affects
how users experience content. It can also hide or reveal important content.
Do not hide important SEO content only to manipulate rankings. Keep primary content visible and easy to read. Use responsive display changes to improve mobile usability. Avoid layout shifts caused by late display changes. Use semantic HTML even when CSS changes visual layout. Common CSS Display Mistakes Using display: none; for content that should remain accessible. Expecting width and height to work normally on inline elements. Using Flexbox when Grid is better for rows and columns. Using Grid when simple Flexbox would be easier. Changing visual order without considering keyboard and screen reader order. Using display: contents; without testing accessibility. Creating responsive menus that hide navigation from keyboard users. Using opacity: 0; while leaving invisible elements clickable. CSS Display Best Practices Use block for sections and structural containers. Use inline for text-level elements. Use inline-block for badges, chips, and button-like inline elements. Use flex for one-dimensional alignment and spacing. Use grid for two-dimensional layouts. Use display: none; only when content should be fully hidden. Use visually hidden CSS for screen-reader-only text. Keep DOM order logical even when using visual layout changes. Test layouts with keyboard navigation and screen readers. Test display changes across mobile, tablet, and desktop breakpoints. Key Takeaways The CSS display property controls how elements participate in layout. block elements stack and take available width. inline elements flow with text. inline-block combines inline flow with block sizing. flex is useful for one-dimensional alignment. grid is useful for two-dimensional layouts. display: none; removes content from layout and should be used carefully.
Pro Tip
Use this simple rule: block for structure, inline
for text, inline-block for small UI pieces, flex
for alignment, and grid for full layout systems.
CSS Icons Go to next item