Welcome to The Coding College! In this post, we’ll delve into W3.CSS Display, a collection of utility classes designed to control the visibility and layout behavior of HTML elements. Whether you’re hiding elements, creating inline layouts, or working with flexbox-style designs, W3.CSS makes managing display properties simple and intuitive.
Let’s explore how W3.CSS display classes can help you create responsive and visually stunning websites!
What is Display in W3.CSS?
The display property defines how an element is rendered on a webpage. With W3.CSS, you can easily toggle between display modes, hide elements, or control their visibility across different screen sizes—all without writing custom CSS.
W3.CSS Display Classes
W3.CSS provides a range of pre-built classes for controlling display behavior.
General Display Classes
Class | Description |
---|---|
w3-display-container | Creates a container for positioned elements. |
w3-display-top | Positions content at the top of a container. |
w3-display-middle | Positions content at the middle of a container. |
w3-display-bottom | Positions content at the bottom of a container. |
w3-hide | Hides the element. |
w3-show | Shows a hidden element. |
w3-block | Makes the element a block-level element. |
w3-inline | Makes the element an inline element. |
w3-inline-block | Makes the element an inline-block element. |
Responsive Display Classes
These classes allow you to hide or show elements on specific screen sizes.
Class | Description |
---|---|
w3-hide-small | Hides the element on small screens. |
w3-hide-medium | Hides the element on medium screens. |
w3-hide-large | Hides the element on large screens. |
w3-show-small | Shows the element only on small screens. |
w3-show-medium | Shows the element only on medium screens. |
w3-show-large | Shows the element only on large screens. |
How to Use W3.CSS Display Classes
1. Hiding and Showing Elements
Example: Hiding Elements
<div class="w3-hide">
This text is hidden by default.
</div>
Example: Showing Hidden Elements
<div class="w3-hide" id="myElement">
This text is initially hidden.
</div>
<button class="w3-button w3-blue" onclick="document.getElementById('myElement').classList.remove('w3-hide')">
Show Text
</button>
2. Responsive Visibility
Hide or show elements based on screen size.
<div class="w3-hide-small w3-light-grey" style="padding: 10px;">
This text is hidden on small screens.
</div>
<div class="w3-show-small w3-light-blue" style="padding: 10px;">
This text is only visible on small screens.
</div>
3. Block and Inline Display
Example: Block Display
<span class="w3-block w3-light-grey" style="margin-bottom: 10px;">
This span behaves like a block element.
</span>
Example: Inline Display
<div class="w3-inline w3-light-blue" style="margin-right: 10px;">Inline Item 1</div>
<div class="w3-inline w3-light-green">Inline Item 2</div>
4. Inline-Block Display
<div class="w3-inline-block w3-border w3-padding" style="margin-right: 10px;">
Inline-Block Item 1
</div>
<div class="w3-inline-block w3-border w3-padding">
Inline-Block Item 2
</div>
5. Display Containers for Layouts
W3.CSS provides display containers to control element positioning.
Example: Display Container
<div class="w3-display-container w3-light-grey" style="height: 200px; width: 100%;">
<div class="w3-display-top w3-padding">Top Content</div>
<div class="w3-display-middle w3-padding">Middle Content</div>
<div class="w3-display-bottom w3-padding">Bottom Content</div>
</div>
Best Practices
- Use Responsive Classes for Adaptive Design
Always test visibility across different devices for a seamless user experience. - Keep the Layout Simple
Usew3-display-container
sparingly to avoid over-complicating layouts. - Combine with Other Classes
Mix display classes with W3.CSS padding, margins, and colors for enhanced visuals. - Fallback for JavaScript
Ensure that dynamically hidden content (e.g.,w3-hide
) is still accessible to search engines and users without JavaScript enabled.
Practical Use Cases
1. Responsive Navigation Menus
<nav class="w3-bar">
<a href="#" class="w3-bar-item w3-button w3-hide-small">Home</a>
<a href="#" class="w3-bar-item w3-button w3-hide-small">About</a>
<a href="#" class="w3-bar-item w3-button w3-hide-small">Contact</a>
<a href="#" class="w3-bar-item w3-button w3-show-small w3-light-blue">Menu</a>
</nav>
2. Image Gallery with Inline-Block
<div class="w3-inline-block w3-border w3-padding w3-margin">
<img src="https://via.placeholder.com/150" alt="Image 1">
</div>
<div class="w3-inline-block w3-border w3-padding w3-margin">
<img src="https://via.placeholder.com/150" alt="Image 2">
</div>
3. Centered Modals or Dialog Boxes
<div class="w3-display-container" style="height: 100vh; background-color: rgba(0,0,0,0.5);">
<div class="w3-display-middle w3-padding w3-card w3-white">
<h3>Centered Modal</h3>
<p>This modal is perfectly centered!</p>
</div>
</div>
Conclusion
The W3.CSS Display Classes simplify the process of managing element visibility and layout. From responsive designs to inline-block galleries, these classes provide a robust toolkit for modern web development.
Explore more tutorials at The Coding College to enhance your W3.CSS skills and build stunning web projects.