The display
property in CSS is a fundamental property that specifies how an element is rendered in a document. The block
value for the display
property makes an element a block-level element. A block-level element always starts on a new line and takes up the full width available, pushing subsequent elements to the next line.
Example 1: Basic Block Behavior
/* Selects all <p> elements and makes them block-level */
p {
display: block; /* This is the default for paragraphs */
background-color: lightblue;
margin-bottom: 10px;
}
Explanation: This code explicitly sets paragraph elements to display: block
. Each paragraph will start on a new line and span the full width of its container, with a light blue background to visualize the space it occupies.
Example 2: Converting Inline to Block
/* Changes inline <span> elements to block-level */
span {
display: block;
background-color: lightgreen;
width: 200px; /* Now we can set width */
height: 50px; /* And height */
}
Explanation: By default, <span>
is an inline element. This example changes it to a block-level element, which allows us to define its width and height, properties that are not applicable to inline elements.
Example 3: Block-Level Links for Navigation
/* Styles navigation links to be block-level for easier clicking */
nav a {
display: block;
padding: 15px;
background-color: #333;
color: white;
text-decoration: none;
}
Explanation: This turns anchor tags within a <nav>
element into block-level elements. This makes the entire rectangular area of each link clickable, not just the text, improving user experience.
Example 4: Structuring a Header
/* Ensures header elements are treated as distinct blocks */
header h1, header p {
display: block;
text-align: center;
}
Explanation: Here, both the <h1>
and <p>
elements inside a <header>
are set to display: block
. This ensures they stack vertically and each takes up the full width, which is ideal for a centered header layout.
Example 5: Creating Vertical Button Groups
/* Styles buttons to appear one above the other */
.button-group button {
display: block;
width: 100%;
margin-bottom: 5px;
padding: 10px;
}
Explanation: This example styles buttons within a .button-group
to be block-level elements. Setting width: 100%
makes each button fill the horizontal space of its parent container, creating a stacked button layout.
Example 6: Using Block for Form Elements
/* Displays form labels and inputs on separate lines */
form label, form input {
display: block;
margin-bottom: 8px;
}
Explanation: By setting both <label>
and <input>
elements to display: block
, they each occupy their own line. This creates a clean, organized, and easy-to-read vertical form structure.
Example 7: Controlling List Item Display
/* Explicitly setting list items to block */
ul li {
display: block;
background-color: #f0f0f0;
border: 1px solid #ccc;
padding: 5px;
}
Explanation: While <li>
elements are block-level by default, this example explicitly sets display: block
. This can be useful to override other styles or to emphasize that each list item is a distinct block-level container.