Hi guys, I have a question about selectors in css.
Let's take this html excerpt but before looking at the code let's assume that the html is part of a very very large page, and the same with the css. In addition the css rule below is called several times for different elements and therefore needs to be slightly different each time.
...
<div id="div_1" style="display:none">
<div id="div_2">
<div class="div_3">
<div class="div_4_button"><a href="#" onclick="closeAlertSimple()">Continue</a></div>
<a href="#" class="div_4_plain">Find out more</a>
</div>
<h1>Cookies on this Toshiba website</h1>
<p>bla bla <a href = "#">and more bla.</a></p>
</div>
</div>
...
Say i want to target and style the link in the div_4_button div, I would do it this way:
.div_4_button a {
height:16px;
display:block;
white-space:nowrap;
text-transform:uppercase;
}
But instead, I have been told that there is also another method, better than this especially if you want to use the above css rule with more than an item requiring slightly different styles. Here's what I have been shown:
#div_1 #div_2 .div_3 .div_4_button a {
height:20px;
display:block;
white-space:nowrap;
text-transform:lowercase;
}
You'll notice the css declarations are slightly different. SO tu summarize say I have this situation:
#div_1 #div_2 .div_3 .div_4_button a {
height:20px;
display:block;
white-space:nowrap;
text-transform:lowercase;
}
#other_Selector_somewhere_else #another_selector .a_different_class .div_4_plain a {
height:16px;
display:block;
white-space:nowrap;
text-transform:uppercase;
}
everything is fine.
If instead I have this situation
.div_4_button a {
height:20px;
display:block;
white-space:nowrap;
text-transform:lowercase;
}
.div_4_button a {
height:16px;
display:block;
white-space:nowrap;
text-transform:uppercase;
}
it's not good because the second rule will effectively change the first
Hope I am making sense
thanks