Display
Control how elements appear and take space on the page.
When building a website, it is important to control how elements appear on the page. Sometimes you want elements to appear on separate lines, sometimes side by side, and sometimes you may want to hide them completely. CSS provides the display and visibility properties to manage these behaviors.
Understanding these properties is essential because they affect how elements are displayed, arranged, and shown to users.
Why Display and Visibility Matter
Display and visibility help developers:
- Control page layouts
- Show or hide content
- Create responsive designs
- Improve user experience
- Organize webpage elements effectively
For example, an online store might hide certain promotional banners on mobile devices while displaying them on larger screens.
Understanding the Display Property
The display property determines how an HTML element is rendered on the page.
Different elements have different display behaviors by default.
Common Display Values
- block
- inline
- inline-block
- none
Let's explore each one.
Block Elements
Block elements take up the full available width and always start on a new line.
Example
div {
display: block;
}Examples of block elements:
- div
- h1
- p
- section
These elements naturally stack on top of each other.
Inline Elements
Inline elements only take up as much width as needed and stay on the same line.
Example
span {
display: inline;
}Examples of inline elements:
- span
- a
- strong
Multiple inline elements can appear next to each other.
Inline-Block Elements
The inline-block value combines features of both block and inline elements.
Example
.button {
display: inline-block;
width: 150px;
}Unlike inline elements, inline-block elements can have width and height while still appearing side by side.
This is commonly used for buttons and navigation menus.
Display None
The display: none value completely removes an element from the page.
Example
.banner {
display: none;
}The element becomes invisible and no longer occupies any space.
This is useful for hiding content that should not be shown under certain conditions.
Understanding Visibility
The visibility property controls whether an element is visible while keeping its position on the page.
Visible
.box {
visibility: visible;
}This is the default behavior.
Hidden
.box {
visibility: hidden;
}The element becomes invisible, but the space it occupies remains reserved.
Display None vs Visibility Hidden
Although both can hide elements, they behave differently.
Display None
.box {
display: none;
}- Element is hidden.
- Space is removed.
- Other elements move into its place.
Visibility Hidden
.box {
visibility: hidden;
}- Element is hidden.
- Space remains reserved.
- Layout does not change.
This difference is important when designing user interfaces.
Real-World Example
Imagine a website with a notification banner:
- Use display: none when the banner is dismissed completely.
- Use visibility: hidden when you want the banner hidden temporarily but still maintain the page layout.
Understanding which option to use helps create smoother user experiences.
Conclusion
The CSS display and visibility properties give developers control over how elements appear and behave on a webpage. The display property determines an element's layout behavior, while the visibility property controls whether it can be seen. By mastering values such as block, inline, inline-block, none, and hidden, you can build cleaner layouts and create more flexible, user-friendly websites.