View Full Version : Applying an id AND a class to an element.

Feb 18th, 2007, 02:12 AM

While working on some CSS tonight, I had one of those "what the heck, why not" moments. I applied an ID and a CLASS to a DIV to see if the DIV would take on both rule sets. I didn't expect this to work since I never tried it before. But to my surprise, it worked:

<div class="component" id="operatingsystem" >

blah blah blah blah blah


I apologize if I'm showing my "newbieness" to CSS.
Now my question: is this considered 100% valid markup?
It definitely reduced a whole bunch of redundant markup for me.
I'm just hoping my technique is considered valid.



Feb 18th, 2007, 03:03 AM
Yes its valid. You can even add more classes if you like but not IDs to the same element. To add more classes you simply use a space in between the classes. For example.

<div class="component class2 class3" id="operatingsystem">

blah blah blah blah blah


Feb 18th, 2007, 03:03 AM
Certainly it works, as you can see.
"Cascading Style Sheets" has an order of being applied to a document, so read up on that aspect of the CSS Specifications and the w3c specifications have a formula for resolving conflicts between competing styles. For the full explanation, go to their site and look at the specs or google on "CSS specificity" to see what you come up with.