Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<style> tag inside any elements will still work?

Tags:

html

css

I see something like this:

<div>
<style type="text/css">
...
</style>
</div>

It's very strange,but still work.

Is this against the standard?

like image 831
omg Avatar asked Sep 06 '09 10:09

omg


4 Answers

It's worth pointing out that although it's invalid HTML, it's also extremely common, and any browser that didn't support it would fail to render properly a significant portion of the web.

Mash-ups in particular, need use of this feature, and HTML 5 defines <style scoped> to deal with this use case. <style scoped> can appear in the body, though styles so defined do not apply to the whole document, only to the section in which <style scoped> appears.

WARNING: HTML 5 is a draft, and there is no guarantee that <style scoped> or any other HTML 5 feature that is not already implemented will ever be implemented.

like image 76
Alohci Avatar answered Nov 17 '22 04:11

Alohci


Yes, it violates the HTML specification.

<!ELEMENT DIV - - (%flow;)*            -- generic language/style container -->

(from the div section of the specification)

Follow the hyperlinks in the live version if you want to see exactly how %flow; expands (it doesn't include style).

Browsers just tend to do huge amounts of error recovery because so many authors do stupid things.

Don't depend on error recovery — there are lots of browsers out there, and they don't all behave the same way when the HTML doesn't conform to spec.

like image 27
Quentin Avatar answered Nov 17 '22 03:11

Quentin


The STYLE element is only allowed as child of the HEAD element. See this explanation for further details.

like image 4
Gumbo Avatar answered Nov 17 '22 03:11

Gumbo


In HTML5, a <style> tag is allowed to appear under anything in <body> or <head>.

Mostly you are not allowed to put blocking elements into inline elements but meta elements like style, script, meta may appear wherever you want.

like image 4
Daniel W. Avatar answered Nov 17 '22 04:11

Daniel W.