Explain Codes LogoExplain Codes Logo

How to hide elements without having them take space on the page?

html
responsive-design
performance
css-transitions
Alex KataevbyAlex Kataev·Oct 6, 2024
TLDR

To completely hide an element and release its used space, apply display: none; in CSS:

.hide { display: none; /* playing hide and seek, and winning */ }

Apply .hide class to the required element:

<div class="hide">Gone with the wind</div>

This CSS removes the element not only from the rendering tree but also from the flow of the page.

Visibility control methods

Let's move further and discover more methods and implications of hiding elements:

Flip your visibility cap

JavaScript provides an interactive solution to toggle the display property, making the element appear and disappear on cue:

function toggleVisibilityMagic(elementId) { var magicalElement = document.getElementById(elementId); // Let's play peekaboo! magicalElement.style.display = (magicalElement.style.display === 'none') ? 'block' : 'none'; }

Vanishing Act in slow motion

A little CSS magic can make elements disappear smoothly, as if they were part of a Houdini's act:

.smooth-fade { visibility: hidden; opacity: 0; transition: visibility 0s 0.5s, opacity 0.5s linear; } .smooth-fade.show { visibility: visible; opacity: 1; transition: opacity 0.5s linear; }

The SEO and accessibility considerations

Remember, with great power of coding, comes great responsibility — accessibility and SEO! Search engines and screen readers might not appreciate your magic tricks with display: none;.

Offscreen trick

Absolute positioning, is another trick in the bag. It keeps content away from the viewport, albeit available for screen readers:

.offscreen { position: absolute; left: -999em; /* Off on a hasty vacation */ }

Keep the performance in check

Improving user experience is also about maintaining good performance. Complex hiding methods might trigger reflows and repaints, affecting page load time, especially on mobile devices.

Diving deeper into concealment

Smoother transitions

Use CSS transitions to soften the effect of an element disappearing, enhancing user experience by making the change less abrupt:

.transition-hide { transition: max-height 0.5s ease-out, visibility 0s 0.5s; // a smooth farewell max-height: 0; visibility: hidden; }

This creates a seamless reduction in the element's height before it slips out of sight completely.

Inline styles - the higher order

An inline style can be an easy solution to ensure an element is hidden, despite conflicting CSS rules. That's because they always call the shots:

<div style="display: none;">Out of sight, out of layout.</div>

Pros and cons masterlist

Every technique has its own trade-offs. Here's a quick recap of the different methods and their implications:

  • visibility: hidden — The element becomes invisible but continues to squat on page real estate.
  • opacity: 0 — It's there, yet not there. The 'ghost' element can still intercept user interactions.
  • position: absolute; left: -9999px; — Element is sent on a permanent vacation, but could be a performance buzzkill.
  • aria-hidden="true" — Makes content invisible to screen readers only. Useful for accessibility customisation.