Home 〉 CSS Tutorials 〉 CSS Display Property Explained with Examples | Block, Inline, Flex, Grid
CSS Display Property Explained with Examples | Block, Inline, Flex, Grid !
Not sure why your element won’t behave the way you want? The `display` property controls how elements behave in layout—whether as block, inline, flex, or grid. Learn how each value works and when to use them. This property is the foundation of any webpage layout.
The 'display' property in CSS defines how an HTML element is shown on the page. It is one of the most important layout tools in web design. Whether you're building a layout, hiding elements, or aligning content, 'display' plays a key role.
In this tutorial, you'll learn about different display values including 'block', 'inline', 'inline-block', 'flex', 'grid', 'none', and more—with clear examples for each.
Note: This post gives a quick overview of 'display: table', 'display: flex', and 'display:grid'. We have covered a separate blog post for the same.
HTML elements have default display behaviors:
CSS lets you override this with the 'display' property to control how elements behave and interact in layout.
Syntax ✍
selector {
display: value;
}
Example 📄
div {
display: flex;
}
<!DOCTYPE html>
<html>
<body>
<p style="background-color: aquamarine; padding: 10px; margin: 5px;">This is a Para 1</p>
<p style="background-color: aquamarine; padding: 10px; margin: 5px;">This is a Para 2</p>
<label style="background-color: aquamarine; padding: 10px; margin: 5px;">This is Label 1</label>
<label style="background-color: aquamarine; padding: 10px; margin: 5px;">This is Label 2</label>
<label style="display: block; background-color: aquamarine; padding: 10px; margin: 5px;">This is Label 3</label>
<label style="display: block; background-color: aquamarine; padding: 10px; margin: 5px;">This is Label 4</label>
</body>
</html>
output 📌
This is a Para 1
This is a Para 2
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
<!DOCTYPE html>
<html>
<body>
<p style="background-color: aquamarine; padding: 10px; margin: 5px;">This is a Para 1</p>
<p style="background-color: aquamarine; padding: 10px; margin: 5px;">This is a Para 2 </p>
<p style="display: inline; background-color: aquamarine; padding: 10px; margin: 5px;">This is a Para 3 </p>
<p style="display: inline; background-color: aquamarine; padding: 10px; margin: 5px;">This is a Para 4</p>
</body>
</html>
output 📌
This is a Para 1
This is a Para 2
This is a Para 3
This is a Para 4
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Like inline, but you can set width/height.
<!DOCTYPE html>
<html>
<body>
<p>This is a Para 1</p>
<p>This is a Para 2</p>
<p>This is a <span>Span 1</span></p>
<p>This is a <span style="background-color: aquamarine; padding: 10px; margin: 10px;">Span 2</span></p>
<p>This is a <span style="background-color: aquamarine; padding: 10px; margin: 10px;">Span 3</span></p>
<p>This is a <span style="display: inline-block; background-color: aquamarine; padding: 10px; margin: 10px;">Span 4</span></p>
<p>This is a Para 3</p>
</body>
</html>
output 📌
This is a Para 1
This is a Para 2
This is a Span 1
This is a Span 2
This is a Span 3
This is a Span 4
This is a Para 3
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
<!DOCTYPE html>
<html>
<body>
<p>You can see me!</p>
<p style="display: none;">You can't see me!</p>
<p>You can see me!</p>
</body>
</html>
output 📌
You can see me!
You can see me!
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Useful for toggling visibility with JavaScript.
<!DOCTYPE html>
<html>
<body>
<div style="display: flex;">
<div style="background: lightcoral; padding: 10px;">Item 1</div>
<div style="background: lightblue; padding: 10px;">Item 2</div>
<div style="background: lightgreen; padding: 10px;">Item 3</div>
<div style="background: lightsalmon; padding: 10px;">Item 4</div>
</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Great for horizontal/vertical layouts, centering. Refer to our detailed tutorial on CSS Flexbox Tutorial: Master CSS Display Flex Property with Examples
<!DOCTYPE html>
<html>
<body>
<div style="display: grid; grid-template-columns: 1fr 1fr;">
<div style="background: lightblue;">Column 1</div>
<div style="background: lightcoral;">Column 2</div>
<div style="background: lightblue;">Column 3</div>
</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Useful for creating complex layouts with rows and columns. Refer to our complete blog post on CSS Grid Layout Tutorial for Beginners | Complete Guide with Examples
Example 📄
display: table;
display: table-row;
display: table-cell;
We have covered display: table' and related values in a separate dedicated blog post.
The 'display' property is your first step in creating layouts in CSS. It helps you define how elements appear and interact. From simple block vs inline behaviors to modern layouts using Flexbox and Grid, mastering 'display' is essential for every web developer.
Start small—experiment with block, inline, and inline-block. Then move to 'flex' and 'grid' for powerful, responsive layouts.
It's 'block'.
'inline-block' allows setting width and height, unlike 'inline'.
Use 'display: none' to hide it and remove it from layout.
Use Flexbox for one-dimensional layouts, Grid for two-dimensional layouts.
Yes, but overuse can lead to content being ignored by search engines.