Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS How to Make a Website Adding Pages to a Website Add Iconography

clayton nicholson
PLUS
clayton nicholson
Courses Plus Student 1,229 Points

could you also use "li.phone a" instead of ".contact-info li.phone a" as the selector? is there a standard way to know?

I'm having trouble knowing how far back I need to specify when writing my selectors. I'm wondering if

li.phone a

would give the same result as

.contact-info li.phone a

Each of those would be targeting a very specific link nested inside of a list item with the class of "pone".

Is there a rule of thumb for how far back you need to specify the selector path?

2 Answers

Alex Heil
Alex Heil
53,547 Points

hey clayton nicholson , as a general rule of thumb you should only be as specific as needed.

as a basic example let's say you have a class phone and you only use it once - or whenever you use it it should look the same then in this case you only need to write styles like

.phone {
//your styles
}

no need to do something like this: (unfortunately I see this all over the place)

body .main .box .phone {
//styles for this
}

so going back to your original example it really depends on your structure. if you're about to style links inside the phone class differently, depending on other parent elements, you would go a level deeper. But if you only target the phone links in general than no need to be over-specific.

hope that helps and have a nice day ;)

clayton nicholson
PLUS
clayton nicholson
Courses Plus Student 1,229 Points

I appreciate the answer. The code I referenced above was from one of the video tutorials. The instructor used:

.contact-info li.phone a {
// styles used
}

I didn't know if this same would also work:

li.phone a {
// styles used
}

the part of the selector that would change according to the lesson is the:

li.phone a {
// styles
}

as the other ones that would be styled differently would be:

li.mail a {
//styles
}

and:

li.twitter a {
// styles
}
Edward Bryan Kene Morales
Edward Bryan Kene Morales
7,374 Points

Hi Clayton,

In most cases, you can get away by just using

li.phone a {
// styles used
}

or simplify it even further by using just

.phone a {
// styles used
}

as they both work the same way as

.contact-info li.phone a {
// styles used
}

As Alex pointed out above, try to avoid being too specific in targeting HTML tags as it would result to you writing more code than you actually need.

I guess it would be great if you could read this article from MDN. It talks about the subject that you are asking about which is CSS Specificity. But if you are taking the Web Design Path, Guil will explain this later down the path.

I hope this helps!