Can I use DIV class and ID together in CSS?
Can I use DIV Class and ID together in CSS? For example:
<div class="x" id="y">
--
</div>
Solution 1:
Yes, yes you can.
#y.x {
/* will select element of id="y" that also has class="x" */
}
Similarly:
.x#y {
/* will select elements of class="x" that also have an id="y" */
}
Incidentally this might be useful in some use cases (wherein classes are used to represent some form of event or interaction), but for the most part it's not necessarily that useful, since id
s are unique in the document anyway. But if you're using classes for user-interaction then it can be useful to know.
Solution 2:
You can also use as many classes as needed on a tag, but an id must be unique to the document. Also be careful of using too many divs, when another more semantic tag can do the job.
<p id="unique" class="x y z">Styled paragraph</p>