The `hidden` Attribute is Visibly Weak
The hidden
attribute: a semantic mirage? This article explores the effectiveness of HTML's hidden
attribute for concealing elements, revealing its surprising weakness and suggesting better alternatives.
The hidden
attribute, while seemingly straightforward, falls short of its intended purpose. Consider this example:
<div>I'm visible</div> <div hidden="">I'm hidden</div>
Despite its intuitive name and broad browser support, its effectiveness is questionable.
While Adam Laki appreciates its semantic clarity, Monica Dinculescu points out a critical flaw: hidden
is a user agent style, easily overridden by other CSS rules. A simple div { display: block; }
will render the supposedly "hidden" element visible:
div { display: block; } <div hidden=""> lol guess who's not hidden anymore hint: it's this thing </div>
This highlights two key problems:
-
Fragility: The
hidden
attribute is easily overridden by almost any CSSdisplay
property other thannone
, making its reliability questionable. This is particularly frustrating because the attribute is explicitly intended to hide elements. -
Overloaded
display
Property: Thedisplay
property's dual role (controlling visibility and element type) is inefficient. A dedicated CSS property for visibility would be preferable, but backward compatibility prevents this.
To leverage the semantic benefits of hidden
while ensuring reliable hiding, Monica suggests this CSS rule:
[hidden] { display: none !important; }
This can be included in a CSS reset or base stylesheet.
Alternatively, using CSS classes provides a more robust and flexible approach. A common utility class might look like this:
.hide, .hidden { display: none; }
However, context matters. For dynamic visibility changes with visual effects, a more tailored approach is often necessary:
.menu { opacity: 0; visibility: hidden; transition: 0.2s; transform: translateX(20px); &[data-open] { opacity: 1; visibility: visible; transform: translateX(0); } }
In conclusion, while the hidden
attribute offers semantic appeal, its inherent weakness necessitates careful consideration. For reliable element hiding, CSS classes or a targeted CSS rule overriding hidden
provide more dependable solutions. The best approach depends on the specific context and desired level of control.
The above is the detailed content of The `hidden` Attribute is Visibly Weak. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How to implement Windows-like in front-end development...

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan
