👁️ Bootstrap 5 – Visibility Utilities: Show, Hide & Control Element Display
🧲 Introduction – Why Use Bootstrap 5 Visibility Utilities?
Bootstrap 5 includes simple yet powerful visibility utility classes that allow you to show or hide elements without modifying display or layout flow. Unlike .d-none
, which removes an element from layout entirely, .visible
and .invisible
preserve spacing—offering non-destructive control over element visibility.
🎯 In this guide, you’ll learn:
- How to use
.visible
and.invisible
effectively - Differences between visibility and display utilities
- Best use cases for toggling UI elements without layout shifts
- Real-world patterns like loading spinners, tooltips, and banners
✅ Example 1 – Using .invisible
to Hide Without Removing Space
<div class="invisible bg-warning p-3">
This content is hidden but still takes up space.
</div>
Class | Description |
---|---|
invisible | Hides element visually but keeps layout |
visible | Restores the element’s visibility |
📌 Use this to hide tooltips, loaders, or text while maintaining container size.
✅ Example 2 – Compare .invisible
vs .d-none
<div class="d-none bg-danger p-3">
This is fully removed from layout
</div>
<div class="invisible bg-primary p-3">
This is invisible but layout space is preserved
</div>
Class | Behavior |
---|---|
d-none | Removes element from layout flow |
invisible | Keeps layout but hides element visually |
🧠 Choose .invisible
when spacing is important, and .d-none
for full removal.
✅ Example 3 – Responsive Visibility Control
<p class="invisible invisible-sm visible-md visible-lg">
Only visible on md and lg screens
</p>
Class | Effect |
---|---|
invisible-sm | Invisible on small screens |
visible-md | Visible from medium (≥768px) and above |
✅ Customize what’s shown or hidden at each breakpoint with responsive variants.
✅ Example 4 – Toggle Visibility with JavaScript
<div id="loader" class="invisible bg-secondary text-white p-3">Loading...</div>
<script>
// Show loader after 1 second
setTimeout(() => {
document.getElementById("loader").classList.remove("invisible");
}, 1000);
</script>
📌 visible
/invisible
are perfect for toggling alerts, messages, spinners, and banners.
📘 Bootstrap 5 Visibility Utility Reference
Class | Description |
---|---|
visible | Shows the element (default state) |
invisible | Hides the element visually, retains layout |
d-none | Hides the element completely (no layout space) |
d-block / d-inline | Controls layout display (not visibility) |
🛠️ Best Practices for Visibility Utilities
Best Practice | Why It Matters |
---|---|
Use .invisible to toggle visibility without affecting layout | Ideal for spinners, loading states, animations |
Avoid toggling between .d-none and .d-block unnecessarily | Can cause layout shifts or flickering in UI |
Combine visibility with opacity | For fade effects via CSS transitions |
Use JavaScript to toggle .invisible | Avoid direct display: none for smoother animations |
Always test on all screen sizes | Responsive visibility is crucial for adaptive design |
📌 Summary – Recap & Next Steps
Bootstrap 5’s visibility utilities let you hide or reveal elements without breaking layout flow. With responsive support and simple syntax, .invisible
and .visible
help manage interactive UI states effectively.
🔍 Key Takeaways:
.invisible
hides elements but keeps them in the layout.d-none
hides and removes elements entirely from layout- Visibility utilities are non-destructive, perfect for dynamic UIs
- Use responsive visibility for device-specific control
- Combine with spacing and animation classes for smoother UX
⚙️ Great for loaders, badges, alerts, tooltips, modals, and dynamic content toggles.
❓ FAQs – Bootstrap 5 Visibility Utilities
❓ What’s the difference between .invisible
and .d-none
?
✅ .invisible
hides the element visually but keeps its layout space intact. .d-none
removes the element from the layout completely.
❓ Can I make an element visible again after using .invisible
?
✅ Yes, use .visible
or remove the invisible
class via JavaScript or manually.
❓ Are visibility utilities responsive?
✅ Yes, use suffixes like .invisible-sm
, .visible-lg
to control visibility at specific breakpoints.
❓ Does invisible
affect accessibility?
✅ Yes. Even though the element is hidden visually, it remains accessible to screen readers. Use aria-hidden="true"
if you want to hide it from assistive technologies.
Share Now :