CSS Display
Introduction
CSS Display is a powerful and versatile property that allows you to control how an element is rendered within the document. It’s far more than just “block” or “inline”; it offers a range of options to manipulate element placement, sizing, and how they interact with other elements on the page. Understanding Display is fundamental to building responsive and visually appealing web layouts. It’s a core concept that will significantly enhance your CSS skills.
Understanding the Basics
The display property has several values, each affecting how an element is rendered:
block: The element takes up the full width available and starts on a new line. It’s the default value for most elements.inline: The element flows within the text flow of the document. It doesn’t start on a new line and doesn’t take up the full width.inline-block: A hybrid ofinlineandblock. It flows within the text flow but allows you to set width and height, similar toblockelements.flex: This is a powerful display property that allows you to create flexible layouts using flexbox. It’s a cornerstone of modern web development.grid: This property allows you to create two-dimensional layouts using a grid system. It’s a more advanced option but extremely useful for complex page structures.
Practical Code Examples
Let's explore some practical examples to illustrate how these values work:
1. display: block;
<div style="display: block;">
This is a block-level element.
</div>
.my-block {
width: 200px;
height: 100px;
background-color: lightblue;
border: 1px solid black;
}
In this example, the div will take up the full width of its parent and start on a new line. The background-color and border styles will be applied to the block element.
2. display: inline;
<p style="display: inline;">
This is an inline element.
</p>
.my-inline {
width: 100px;
height: 50px;
background-color: lightgreen;
border: 1px solid black;
}
The p element will flow within the text flow. It won’t start on a new line.
3. display: inline-block;
<div style="display: inline-block;">
This is an inline-block element.
</div>
.my-inline-block {
width: 150px;
height: 75px;
background-color: orange;
border: 1px solid black;
}
The div will flow within the text flow, allowing you to set width and height.
4. display: flex;
<div style="display: flex;">
<div style="width: 50px; height: 50px; background-color: yellow;"></div>
<div style="width: 25px; height: 25px; background-color: pink;"></div>
</div>
.my-flex {
display: flex;
justify-content: center; /* Center the content horizontally */
align-items: center; /* Center the content vertically */
background-color: purple;
}
This example demonstrates a flexbox layout. The div elements will be arranged horizontally and vertically within the container.
5. display: grid;
<div style="display: grid;">
<div style="width: 30px; height: 30px; background-color: cyan;"></div>
<div style="width: 10px; height: 10px; background-color: red;"></div>
</div>
.my-grid {
display: grid;
grid-template-columns: repeat(2, 1fr); /* Creates two columns of equal width */
grid-template-rows: repeat(2, 1fr); /* Creates two rows of equal height */
background-color: lime;
}
This example creates a grid layout. The grid-template-columns and grid-template-rows properties define the columns and rows of the grid.
💡 Tip: Experiment with different display values to see how they affect the layout of your elements. Don't be afraid to try out different combinations to achieve the desired visual effect.
Summary
The display property is a fundamental tool for controlling how elements are rendered in CSS. Understanding the different values – block, inline, inline-block, flex, and grid – is crucial for creating flexible and responsive web layouts. Flexbox and grid are particularly powerful for more complex layouts. Practice with these examples to solidify your understanding.
🖥️ Try It Yourself
- Simple Box: Create a
divwithdisplay: block;and add some text inside it. - Inline Element: Create a
<p>element withdisplay: inline;and add some text inside it. - Inline-Block Element: Create a
divwithdisplay: inline-block;and add some width and height to it. - Flexbox Layout: Create a container with
display: flex;and add some content inside it. - Grid Layout: Create a container with
display: grid;and add some content inside it.

