Open ZoeBijl opened 6 years ago
<w3c hat off>
It seems very confusing to not allow it, especially, as outlined in the tweets above, when SVG is used inline in HTML.
Found the tweet in which @AmeliaBR explained why it's not in the spec.
This would be another case of synchronizing with HTMLElement
.
One complications, is that we would need to decide precedence relative to regular presentation attributes. (CSS rules would override, same as for HTML elements.)
As I mentioned to Michiel before, I really don't see a need for a new attribute in SVG. And I find it confusing that an attribute named hidden
sets display: none
in the UA stylesheet, not visibility: hidden
.
But it is clearly a confusion for authors who expect it to be a universal attribute.
This is especially likely if they have in the past used a compatibility rule in their stylesheet (for older browsers) that didn't distinguish by namespace: [hidden] {display: none}
will affect all elements with the attribute, even SVG. Native support for the HTML attribute is now good enough that devs might be using hidden
without that extra rule, and so are only now discovering that the native implementations don't include SVG.
Thank you for your comments Amelia. Like you said, if nothing else, this will prevent a lot of confusion.
The HTML standard suggests that [hidden] { display: none }
should be included in the user agent style sheet, so why aren’t browsers doing this? If they did, we wouldn’t have this problem to begin with, if I understand correctly.
@simevidas
Because that HTML user-agent stylesheet is restricted to HTML-namespaced elements using a CSS namespace rule:
@namespace url(http://www.w3.org/1999/xhtml);
[hidden], area, base, basefont, datalist, head, link, meta, noembed,
noframes, param, rp, script, source, style, template, track, title {
display: none;
}
One outcome of this issue would be to add a matching rule for SVG-namespaced elements, along with the global attribute and IDL property.
display
works as an SVG presentation attribute, right? So the question of precedence is interesting:
<g hidden display="inline">
…
</g>
The straightforward answer is probably(?) that hidden
overrides display="inline"
, since CSS is more specific than presentation attributes. I think this is simpler from an author perspective, since if they use a library like Normalize.css or their own styles for [hidden] { display: none }
, that could produce a weird effect where the order-of-precedence differs depending on native support, especially if the author opts not to serve certain “polyfills” to newer browsers.
hidden
is mostly useful for DOM scripting in my experience, where you don’t care what an element’s display
value should be — you just want to toggle it on and off. This especially seems to catch SVG authors — technically display: block
should result in a shown element, but but I keep getting bit by edge cases with stuff like CSS animations where it expects display: inline
instead. Most show/hide snippets on StackOverflow (et al.) toggle elements between display:block/none
, so I’d would like to see hidden
parity between SVG and HTML to simplify future show/hide scripts.
With an appropriate rule in the UA stylesheet (as "suggested" by HTML), display="inline"
would override hidden
in the example above because of the cascading rules [1]. It appears that current UAs [2] actually treat hidden
(in HTML) as a "presentational hint" [3] though, and if hidden
(in SVG) were to be implemented the same way, the above example would be sensitive to in which order the attributes were mapped (and in this particular example I believe that would lead display="inline"
winning.)
[1] https://svgwg.org/svg2-draft/styling.html#PresentationAttributes [2] Blink (so quite likely WebKit too), and it looked like Gecko did. [3] https://html.spec.whatwg.org/multipage/rendering.html#the-css-user-agent-style-sheet-and-presentational-hints
Since HTML elements work with this attribute, I find it confusing that SVG elements in HTML documents don't. I personally think this attribute is useful and hope SVG elements would work with it, too.
The SVG Working Group just discussed Add support for the `hidden`-attribute
, and agreed to the following:
RESOLUTION: Add a global hidden attribute to SVG with the same semantics as HTML that is expected to be implemented using UA stylesheets. Iff there is implementation commitment
@ZoeBijl, would you be interested in starting a PR for this? (It would follow an existing pattern used for e.g. tabindex, where we just reference HTML for the detailed definitions.) I'd be happy to help as needed.
Following the painful discussion in https://github.com/whatwg/html/issues/4904 I’m not so sure. To rephrase: I’m not sure this should be supported given the definition for the hidden
attribute in the HTML spec with all the limitations that imposes.
I’ve gone from fan of the hidden
attribute to thinking it’s the most useless attribute we have 🤷🏻♀️.
Hmm… hadn't seen that discussion on WHATWG HTML.
I'd still say there is a use, as an authoring convenience, to support <svg hidden>
as a styling shorthand that behaves the exact same as in HTML. But if the HTML definition is under dispute, that does make it messier.
One use case I can think of is having a hidden SVG
element with defs
for icons that you link to with use
somewhere down the page. I think that follows the HTML definition. But at this point I’m really not sure any more.
Under the (now defunct) W3C HTML 5.2 spec definition I would be totes in favour of this.
My aim with this issue was to reduce the confusion for authors. With the WHATWG definition I’m not sure allowing the hidden
attribute will achieve that. The least we as spec editors could do is be consistent in advice. Which is also why I have already removed the hidden
attribute from most APG code examples.
This hasn’t been discussed in a while but I thought it’d be worth following up on this thread based on a possible issue I foresee with hidden
usage in CSS resets:
[hidden] { display: none; }
(see for example normalize.css). This means developers working with such resets are none the wiser that SVG elements lack support.hidden="until-found"
(spec, Chrome Developers explainer) will likely cause a lot of those resets to reconsider the appropriateness of this now dated override, which breaks the until-found
value.If those types of widely-used resets remove their [hidden] { display: none; }
overrides to support hidden="until-found"
, a lot more people will find out that SVG elements inline in HTML unexpectedly don’t support hidden
.
This will be be pretty straightforward for maintainers of CSS resets and authors generally to resolve, but I thought it worth mentioning here nonetheless. I personally found out about this because Tailwind CSS removed this override from their reset for the reasons I mention above.
For context, I use the hidden
attribute on SVG elements exactly as described above – to hide a "sprite" of icons as symbols, which only works thanks to the CSS resets:
<svg hidden>
<defs>
<symbol id="circle"><circle cx="8" cy="8" r="7" /></symbol>
</defs>
</svg>
<svg class="inline" width="16" height="16"><use href="#circle"></use></svg>Test
In the past I've run into the lack of support for the
hidden
-attribute in SVG a few times. Today I saw a tweet that mentions this lack too. Were there ever plans to include support? And if not, are there good reasons not to include it?