I’m just getting started with CSS. From what I’ve seen, it makes more sense to use a class in a div tag instead of an id. Is this correct?
<div class=“container”>
<div id=“container”>
I’m just getting started with CSS. From what I’ve seen, it makes more sense to use a class in a div tag instead of an id. Is this correct?
<div class=“container”>
<div id=“container”>
Generally speaking, a class is probably better, as it is reusable, and is not so hard to override. These days I tend to save IDs for JS hooks and on-page links.
When you say reusable, you mean multiple times on a page, right? Can you give an example of a time you would want to over-ride an id ?
Thanks.
Actually, it’s like a wrench and a hammer. You should use the right tool for the right job.
IDs:
#anID .rule3 { color: red}
.rule1 .rule2 .rule3{color:green}
<div id=“anID”>
<div class="rule1>
<p class=“rule2”><span class=“rule3”>text</span></p>
</div></div>
The text will be red. Even tho " .rule1 .rule2 .rule3{color:green}" has more classes list and seem more innermost in the markup… throwing just ONE ID makes the other rule more specific than using several , or more classes.
That being said classes are used for the opposite reasons:
Hope that helps to clear up things
BTW
To override an ID you either need to write it over in the code:
#MyId tag{a rule…}
#MyId tag{the same rule, different value}
this just seems kinda dumb many times.
OR you need TWO IDs ( see how using IDs willy nilly can become a mess?
#MyId tag{a rule…}
#anotherID #MyID tag{the same rule, different value}