Hide div but keep the empty space
Preserve the space of a hidden <div>
with the CSS visibility
property:
Apply this class to the <div>
:
For interactivity, toggle visibility with jQuery:
Another crafty trick: opacity over visibility
Use the opacity
property to make a div invisible, yet keep the text selectable:
But wait, we don't want visible text-highlighting, no problem. Use user-select: none;
:
Combine both for an ultra-sneaky div:
A deep dive into layout dynamics
The great debate: visibility
vs display
In the CSS world, visibility: hidden
hides an element but keeps its place in the layout. display: none
, on the other hand, erase the element from the document flow, causing the space to collapse.
Play nice with others: interaction with sibling elements
An element with visibility: hidden
still plays along with other elements — margins, padding, borders are all respected. Ergo, the space it occupies is fully preserved.
Smoother than a fresh jar of Skippy: transitions and animations
You can then toggle the classes via JavaScript for a cool fade effect:
Practical implementations
Dynamic content loaders
While loading dynamic content, use visibility: hidden
to keep the layout stable. No more UI jumps as content populates.
Hidden triggers for user interactions
In gamified interfaces, hidden elements can act as secret triggers — the user triggers an interaction without even knowing it, magical!
Non-interactive info display
Use opacity: 0
if you need to keep informative text on the screen that's accessible to screen readers, but not visible to users.
Accessibility considerations
Combining visibility
with opacity
helps maintain decent accessibility standards while managing the visual presentation.
Navigating by keyboard
Preserve the tab order for accessibility by hiding non-relevant sections with visibility: hidden
, without removing them completely from the document flow.
Beware of the gotchas!
SEO implications
Search engines might interpret visibility: hidden
as an attempt to hide text — spammy spammy! Use sparingly, and watch out for SEO repercussions.
Performance blues
Don't overuse opacity
, especially for large hidden areas. They can still be rendered by the browser, which can slow things down.
User experience trips
Avoid hiding critical information from users. It can very quickly turn from a cool user interface trick to a frustrating user experience roadblock.
Was this article helpful?