Michael J
Michael J

Reputation: 2545

Why is the HTML style element content not standard XML?

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.

Thanks,

Michael

Upvotes: 2

Views: 203

Answers (6)

Andhavarapu Balu
Andhavarapu Balu

Reputation: 109

As far as I have understood,

Initially, styling was meant to be in the control of the user (via the browser), and not the web developer. So it did make sense to have a completely different syntax for it, as users don't need to learn HTML either ways

The separation of document structure from the document's layout had been a goal of HTML from its inception in 1990. Tim Berners-Lee wrote his NeXT browser/editor in such a way that he could determine the style with a simple style sheet. However, he didn't publish the syntax for the style sheets, considering it a matter for each browser to decide how to best display pages to its users.

https://www.w3.org/Style/CSS20/history.html

Additional reference: What is the difference between default, user and author style sheets?

Upvotes: 0

ja.
ja.

Reputation: 4249

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

Upvotes: 2

Chetan S
Chetan S

Reputation: 23803

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

Upvotes: 0

Ray Lu
Ray Lu

Reputation: 26648

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

Upvotes: 0

uriel
uriel

Reputation: 1473

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).

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 190907

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

Upvotes: 3

Related Questions