Font-size [7/10] Doubts regarding the CSS code


#1

https://www.codecademy.com/courses/make-a-website/lessons/closer-look-css/exercises/font-size?action=lesson_resume&link_content_target=interstitial_lesson

The code / lesson is working fine and there’s no problem per se. The only thing that I don’t understand is the use of ‘.hero a’ selector in the code. I mean when we are ought to use ‘a.hero’ and not ‘.hero a’?. When I remove the .hero in ‘.hero a’ it works just as fine, just the same. Is it something to be bothered about?


#2

the arrangement of your selectors can very much affect what elements you want to scope/zero-in on

so by saying “a.hero” and “.hero a”

is like saying for the latter, all elements/containers with the class .hero we want to select your anchor links…

so then the former is more likely target a link with the class of hero…


#3

To iluustrate what @tommygebru is referring to…

.hero a {
     color: green;
}
a.hero {
    color: orange;
}
<ul class='hero'>
  <li><a href="#">link a</a></li>
  <li><a href="#">link b</a></li>
  <li><a href="#">link c</a></li>
  <li><a class="hero" href="#">link d</a></li>
</ul>

All of the above links will be green, except the last one, which will be orange.


#4

haha thanks @mtf thats a pretty sweet example and includes both css declarations in one html structure!


#5

Well, many thanks @tommygebru and @mtf Wish I could mark both the answers as the solution. So this is to say, in this scenario, if there ain’t any other class that contains anchor element other than ‘hero’ can we use just ‘a’ as the selector?


#6

Because of its low specificity the selector will be universal and apply to the entire document. It’s okay for rules we wish to apply across all elements of the type. Eg.

a {
    text-decoration: none;
}
a:hover {
    text-decoration: underline;
}

#7

It depends on a case by case basis and how you choose to design your work, the level of specificity is something you will always be concerned with in css :smile:


#8

This topic was automatically closed after 7 days. New replies are no longer allowed.