开发者

Is it valid to give a style element an ID?

开发者 https://www.devze.com 2023-04-08 15:47 出处:网络
It says here that it is not within HTML4, though I don\'t really see where that\'s spelled out in the text.

It says here that it is not within HTML4, though I don't really see where that's spelled out in the text. From what I can tell, based on this, it is ok to do so in HTML5 but I'm not entirely sure (assuming style is an HTML element?)

I am using this to rotate out a stylesheet and want it to be as valid as possible acc开发者_高级运维ording to HTML5 specs, so wondering if I should rewrite it with a data-* element.


+1 Interesting question!

Instead of using a style block, you should consider linking (link) to your stylesheets and then switch them out by referencing an id or a class.


That said, title is perfectly acceptable for a style tag in HTML5. You can use this as a hook for your stylesheet switching.

http://www.w3.org/TR/html5/semantics.html#the-style-element

Fyi... this validates

<!DOCTYPE html>
<html>
  <head>
    <title>Title</title>
    <style title="whatever"></style>
  </head>
  <body>
    Test body
  </body>
</html>

http://validator.w3.org/#validate_by_input+with_options


I've just put the following code into the W3C validator and it has no errors :)

<!DOCTYPE html>
<html>
  <head>
    <title>Title</title>
    <style id="test"></style>
  </head>
  <body>
    Test body
  </body>
</html>

I think the W3C Validator is a good resource for this type of thing, it is marked as experimental but that's because the standard is yet to be be finalised.


It is not valid in HTML4 (as per the spec) and data-* attributes are not either. That is, the document will not validate against the Doctype spec if you use these attributes.

Regardless of whether the document validates or not, browsers will ignore elements that they do not recognize.

Style tags are DOM elements like any other tag, so you can add any attributes you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消