Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to give a style element an ID?

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 according to HTML5 specs, so wondering if I should rewrite it with a data-* element.

like image 841
Damon Avatar asked Sep 27 '11 19:09

Damon


3 Answers

+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

like image 165
Jason Gennaro Avatar answered Sep 20 '22 17:09

Jason Gennaro


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.

like image 22
Clive Avatar answered Sep 19 '22 17:09

Clive


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.

like image 31
Richard Hulse Avatar answered Sep 22 '22 17:09

Richard Hulse