- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Display
If you want to master CSS layout, the display property is the first thing you need to grasp. It is arguably the most important CSS property for controlling how elements occupy space on your webpage. By changing the display value, you can turn a list into a navigation bar, hide elements entirely, or create complex multi-column grids.
Every element on a webpage is essentially a rectangular box. The display property determines the behavior of that box and how it interacts with its neighbors. Let's dive into the most common values you'll use in modern web development.
Block Level Elements (display: block;):
By default, elements like <div>, <p>, and <h1> are block-level. A block-level element always starts on a new line and takes up the full width available, stretching to the left and right as far as it can. Imagine these as "bricks" stacked on top of each other.
- Elements with display: block; create a block-level box that extends the full width of its containing element.
- Common block-level elements include <div>, <p>, <h1> to <h6>, <ul>, <ol>, and <li>.
- You can explicitly set the
width,height,margin, andpaddingon these elements.
div {
display: block;
width: 100%; /* Default behavior */
}
p {
display: block;
margin-bottom: 20px;
}
Inline Elements (display: inline;):
Inline elements are the opposite of block elements. They do not start on a new line; instead, they sit side-by-side with other elements, taking up only as much width as necessary for their content. Think of these like "words in a sentence."
- Elements with display: inline; generate an inline box and flow alongside adjacent elements.
- Common inline elements include <span>, <a>, <strong>, and <em>.
- Critical Note: Width and height properties have no effect on inline elements.
span {
display: inline;
color: red;
}
a {
display: inline;
text-decoration: underline;
}
width or height on a <span> or <a> tag and wondering why it isn't working. To apply dimensions, you must change the display to block or inline-block.
Inline-Block Elements (display: inline-block;):
display: inline-block; provides the "best of both worlds." It allows elements to sit next to each other on the same line (like inline elements), but it also allows you to set a width, height, and vertical margins/padding (like block elements).
- It's the perfect choice for creating horizontal navigation menus, button groups, or a grid of product cards.
- It respects the
widthandheightyou define while staying in the flow of the text.
.menu-item {
display: inline-block;
margin-right: 15px;
padding: 10px;
}
.btn {
display: inline-block;
width: 120px;
height: 45px;
background-color: #007bff;
color: white;
text-align: center;
line-height: 45px;
border-radius: 5px;
}
inline-block, you might notice a small mysterious gap between elements. This is caused by whitespace (spaces or line breaks) in your HTML code. You can fix this by removing the spaces between your HTML tags or by using Flexbox instead.
None (display: none;):
The display: none; value removes the element from the page entirely. It won't be visible, and it won't take up any space in the layout. It's as if the element never existed in the HTML.
- Commonly used for dropdown menus that only appear on hover or mobile navigation toggles controlled by JavaScript.
.hidden-element {
display: none;
}
display: none; is different from visibility: hidden;. While display: none removes the element's space, visibility: hidden hides the element but leaves a "ghost" gap where the element would have been.
Table-Related Values (display: table;, display: table-cell;, display: table-row;):
Before Flexbox and Grid became popular, developers used these values to mimic the behavior of HTML tables for layout purposes. Today, they are mostly used for specific styling needs, such as vertically centering content inside a div.
- display: table; makes the element behave like a
<table>. - display: table-row; makes the element behave like a
<tr>. - display: table-cell; makes the element behave like a
<td>, which is useful for usingvertical-align: middle;.
.table-container {
display: table;
width: 100%;
}
.table-cell {
display: table-cell;
vertical-align: middle;
text-align: center;
}
Flexbox (display: flex;):
display: flex; turns an element into a "flex container." This is the modern standard for creating one-dimensional layouts (either a row or a column). It makes aligning items and distributing space incredibly simple.
- Great for navigation bars, centering items, and sidebars.
- Child elements automatically become "flex items."
.navbar {
display: flex;
justify-content: space-between; /* Pushes items to the far left and right */
align-items: center; /* Vertically centers items */
}
.nav-link {
padding: 10px 20px;
}
Grid (display: grid;):
display: grid; enables CSS Grid, the most powerful layout system in CSS. Unlike Flexbox, which is one-dimensional, Grid is two-dimensional meaning it handles both rows and columns at the same time.
- Perfect for full-page layouts, complex galleries, or dashboard interfaces.
- Allows you to define exact tracks (columns and rows) for your content.
.main-layout {
display: grid;
grid-template-columns: 250px 1fr; /* A fixed sidebar and a flexible main area */
grid-template-rows: auto;
gap: 20px;
}
.sidebar {
background-color: #f4f4f4;
}
.content {
background-color: #ffffff;
}
These are the core values of the display property. Understanding when to use block vs. inline-block, and when to upgrade to flex or grid, is the key to building professional, responsive websites.