CSS Display Get Certified
What is CSS Display?
The CSS display property controls how an HTML element is shown on the page. It decides whether an element appears as a block, stays on the same line, is arranged with Flexbox or Grid, or is hidden completely. Understanding display values is essential for building modern webpage layouts.
Why Use CSS Display?
Display values let you control layout behavior without extra markup. You can stack sections, place items side by side, create flexible rows and columns, or hide content when needed.
Basic Display Syntax
What this code does: Shows the general form of the display property applied to a selector.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-box {
display: block;
background: #e0f2fe;
padding: 16px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="demo-box">This uses display: block</div>
<div class="demo-box">Each box starts on a new line</div>
</body>
</html>
Step 1: Using display: block
Block elements start on a new line and take the full available width by default.
What this code does: Forces two boxes to stack vertically, each taking the full width with a blue background.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-box {
display: block;
background: #007BFF;
color: white;
padding: 15px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="demo-box">First Box</div>
<div class="demo-box">Second Box</div>
</body>
</html>
Step 2: Using display: inline
Inline elements stay on the same line and only use the width required for their content.
What this code does: Places three green labels side by side on one line because they use display: inline.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-box {
display: inline;
background: #28a745;
color: white;
padding: 8px;
margin-right: 6px;
}
</style>
</head>
<body>
<div class="demo-box">HTML</div>
<div class="demo-box">CSS</div>
<div class="demo-box">JavaScript</div>
</body>
</html>
Step 3: Using display: inline-block
Inline-block elements remain on the same line while allowing custom width and height.
What this code does: Creates three orange boxes that sit side by side with fixed width and height using inline-block.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-box {
display: inline-block;
width: 120px;
height: 80px;
background: #ff9800;
color: white;
text-align: center;
line-height: 80px;
margin: 5px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-box">Box 1</div>
<div class="demo-box">Box 2</div>
<div class="demo-box">Box 3</div>
</body>
</html>
Step 4: Using display: flex
Flexbox arranges child elements in rows or columns, making layouts easier to create and responsive.
What this code does: Places three purple boxes in a horizontal row that share available space equally with display: flex.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-container {
display: flex;
gap: 10px;
}
.demo-box {
background: #6f42c1;
color: white;
padding: 20px;
flex: 1;
text-align: center;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-container">
<div class="demo-box">1</div>
<div class="demo-box">2</div>
<div class="demo-box">3</div>
</div>
</body>
</html>
Step 5: Using display: grid
CSS Grid creates rows and columns for arranging elements in a structured layout.
What this code does: Builds a 3-column grid of red boxes using display: grid and grid-template-columns.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.demo-box {
background: #dc3545;
color: white;
padding: 20px;
text-align: center;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-container">
<div class="demo-box">A</div>
<div class="demo-box">B</div>
<div class="demo-box">C</div>
</div>
</body>
</html>
Step 6: Using display: none
The display:none value completely removes an element from the webpage layout.
What this code does: Hides one element with display: none while a second blue box remains visible.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-hide {
display: none;
}
.demo-show {
background: #007BFF;
color: white;
padding: 15px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-hide">You cannot see this.</div>
<div class="demo-show">This element is visible.</div>
</body>
</html>
Complete CSS Display Example
What this code does: Builds a simple navigation bar with display: flex so menu items sit in a row with hover styles.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-menu {
display: flex;
gap: 10px;
}
.demo-item {
background: #007BFF;
color: white;
padding: 15px 20px;
border-radius: 5px;
}
.demo-item:hover {
background: #0056b3;
}
</style>
</head>
<body>
<h2>Navigation Menu</h2>
<div class="demo-menu">
<div class="demo-item">Home</div>
<div class="demo-item">About</div>
<div class="demo-item">Services</div>
<div class="demo-item">Contact</div>
</div>
</body>
</html>
Advantages of CSS Display
Best Practices
- Use block for sections and containers.
- Use inline for short text elements.
- Use inline-block when width and height are needed.
- Use flex for one-dimensional layouts.
- Use grid for two-dimensional layouts.
- Use display:none only when an element should be removed from the layout.
- Choose the display value based on the layout requirement.
- Test layouts on desktop, tablet, and mobile devices.
Web Resources on CSS Display
1. MDN Web Docs : display CSS property
2. MDN Web Docs : CSS display
3. Dev.to : CSS Display property
Click “Run Code” to see the result here.
CSS Syntax Example
The CSS syntax consists of a selector and a declaration block.
h1 { color: blue; font-size: 36px;}Selector: h1 – targets all <h1> elements
Property: color and font-size
Value: blue and 36px
Your CSS Applied Below:
This is an H1 heading with your CSS
This is an H2 heading (not affected)
This paragraph is not affected by the h1 selector.
Welcome to CSS
This page is styled using CSS.
The heading and paragraph use your CSS rules.
- List item with your styles
- Another list item
๐งช Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.