开发者

Why is the HTML style element content not standard XML?

开发者 https://www.devze.com 2022-12-10 07:10 出处:网络
I\'m new to HTML, but have a long history with XML.It strikes me as odd that an HTML style element looks like this.

I'm new to HTML, but have a long history with XML. It strikes me as odd that an HTML style element looks like this.

<style type="text/css">
    .style1 { width: 250px; }
    .style2 { width: 20px; }
</style>

I would have thought the same information could have been captured in a more XML friendly format. For example maybe this

<style type="text/css">
    <style1 width="250px"/>
    <style2 width="25px"/>
</style>

It seems to me that the latter would be parsed by the XML parser, whereas the former would require custom parsing code. It seems so uncharacteristic that I am wondering if there is actually a good reason.

Tha开发者_如何学Pythonnks,

Michael


Mainly because it predates XML. See section 7.2.3.3 of Håkon Wium Lie's thesis: http://people.opera.com/howcome/2006/phd/#h-275


CSS is a different language. XHTML just allows it for embedding since it is content. Just think of like JavaScript being in an XML document.

Not to mention using XML would cause problems with DTD and would be very verbose where CSS has simplicity.

Here is some history of CSS: http://en.wikipedia.org/wiki/Cascading_Style_Sheets#History


Because the CSS syntax is much cleaner, concise and more expressive. specially having multiple selectors in a single rule would be extremely tedious in an XML based language. (It could be put on an attribute, but that would be just an ugly hack of embedding a huge part of the existing CSS syntax into the XML tag attribute or value).


In this case

<style type="text/css">
    .style1 { width: 250px; }
    .style2 { width: 20px; }
</style>

the CSS styles are the content of element style. CSS itself is not HTML


As others have said, CSS is an entirely different language. It does not conform to XML syntax. CSS parsers are not XML parsers. To embed Javascript and CSS in XHTML documents correctly, enclose them in a CDATA section. CSS can also be enclosed in XML comments since it does not generally have non XML-compliant characters like Javascript does.

Here is a good article on the topic. https://developer.mozilla.org/en/Properly_Using_CSS_and_JavaScript_in_XHTML_Documents

0

精彩评论

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