tags:

views:

87

answers:

5

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

+2  A: 

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%5FStyle%5FSheets#History

Daniel A. White
+1  A: 

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

uriel
next you're going to tell me that XSLT is an ugly mess
ʞɔıu
he's talking about it would have to be like this `<rule selector=".something .something2" />` I find that very ugly.
Daniel A. White
to characterize XSLT as "an ugly mess" would be so generous and charitable that Bill Gates and Warren Buffet giving away their whole fortunes to feed starving children in Africa would make them look like Ebenezer Scrooge by comparison.XSLT makes me yearn for COBOL.And for the record, I was already using XSLT in the late 90's even before the final spec had been adopted and over the years have built a couple of sizable apps with it, the experience has left me scarred for life and wont touch it ever again for fear of losing the little sanity I have left.tl;dr XSLT will give you nightmares.
uriel
A: 

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

codemeit
A: 

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

Chetan Sastry
+2  A: 

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

ja
I spent some time reviewing Mr. Lie thesis. Some day I hope to have the time to read it through. There is a good deal more there than just CSS.
Michael J
I was under the weather this past weekend and had a chance to read Mr. Lie's paper. I've had many opportunities to read similar papers. This one stands out is one of the better ones that I've come across. It is a great introduction to the subject of style sheets. Well worth the time if you're into this sort of thing.Thank you Ja
Michael J