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

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

Why not to add images to <li> using css: li.class {list-style-image: url('...');} ?

In the video "Add Iconography" why not to add images to <li> using:

li.class {list-style-image: url('...');}

Why Nick added images as <a> bg?

Also will it be the same if I declare selector .class without li before it?

.class {
...
}
Devron Baldwin
Devron Baldwin
3,508 Points

Hi Edgars,

Excellent questions.

Why Nick added images as bg?

One of the main reasons to use a background image instead of the list-style-image is the level of control you get over the position of the image. If you use background-image you can also use background-position to move the bullet. You can also do other things with a background image, such as resizing the image or repeating the image vertically or horizontally.

Resizing the bullet is particularly useful when creating responsive sites.

Also will it be the same if I declare selector .class without li before it?

Yes it will work. I find it's generally better practice to write the css with the li before it. This is for two reasons:

  1. It's easier for you to see what kind of element you are styling if you're scanning through the code. Also if someone else looks at your code then it's also easier for the second person to understand what you're doing.
  2. It stops you from accidentally applying the styling to another element. For example you may have a paragraph tag with the same class name.

I hope this helps.

3 Answers

Devron Baldwin
Devron Baldwin
3,508 Points

In short, it's so you can make the background change when you hover over a link.

Often you'll add the image to an anchor so you can control the background on hover or on other states for an anchor, such as visited or focus.

These states happen when you move your cursor over the link or tab through the page to get to the link.

All browsers support the various states of an anchor element so it's good practice to do this.

Armin Naderi
Armin Naderi
18,713 Points

@Devron Baldwin - thank you for all the great answers! I am confused on which selector to specify first within my css file. For example, should I specify ".contact-info li.phone a" or "ul.contact-info li.phone a" or even "section ul.contact-info li.phone a"? Where should I stop being more specific? I understand that I could simply do ".phone a," but as you stated it can cause bugs if two elements have the same class.

Devron Baldwin
Devron Baldwin
3,508 Points

Armin Naderi - that is a good question.

This is something you'll learn naturally by experience and there isn't really a correct answer that applies to all situations. One of the main reasons CSS is so useful is that you can style a lot of elements on your page with minimal effort. If you start being too specific then you can't reuse a class on another element.

Lets look at your two examples. Firstly:

.contact-info li.phone a {
    color: #f00;
}

The CSS above will make the phone number red in all of the following cases

<div class="contact-info">
    <ul>
      <li class="phone"><a href="#something">012345689</a></li>
    </ul>
</div>


<section>
    <ul class="contact-info">
      <li class="phone"><a href="#something">012345689</a></li>
    </ul>
</section>


<nav>
    <ul class="contact-info">
      <li class="phone"><a href="#something">012345689</a></li>
    </ul>
</nav>

So the first CSS example is quite flexible. However, If we think about your second example:

section ul.contact-info li.phone a {
    color: #f00;
}

The above CSS will only make the second phone number red in the examples I gave. This might be your intention, you may wish to only make phone numbers within the section red.

You generally only want to be more specific if you want your style to be exclusive to certain parts of your web page.

Another reason to be more specific is to override less specific styles, this is a great way to avoid the dreaded !important; (which you should avoid using at all costs!)

I hope that's helpful and makes sense.

Andrew Winkler
Andrew Winkler
37,739 Points

To answer your first question, Nick's using .class1 li.class2 {} notation to specifically designate the icon css within the contact-info class. Nick is likely formatting ccs for a specific contact list, and he anticipates reusing the icons in a different context in the future which may require different styling. for example class3 li.class2 {} where class 3 indicates the class of the new context.

To answer your second question: if you remove the li notation, you will be broadening the scope even more so. Nick's code in comparison is much more concise and reusable. Hypothetically he could reuse and restyle the same icons differently on the same webpage. Your purposed method does not allow for such flexibility.

Mind you, your css load time may be a fraction of a millisecond faster, but that difference is negligible.

Thank you Devron Baldwin! This helps a lot!

One more question:

Why Nick added bg-img to an "a - anchor" but not to a "li - list item"? It is made for the bg-img to be a link?

Devron Baldwin
Devron Baldwin
3,508 Points

Hi Edgars Grundmanis

The reason the styling is on the anchor tag is because of the interactivity it provides. Anchor tags are unique because they interact with the mouse cursor (or the tab key) to give different "states". These states are things we can use in CSS. Other elements can do hover states but it's only anchor tags that are well supported across all the different web browsers.

An example of a hover CSS style is below:

a {
    color: #00F;
}
a:hover {
    color: #F00;
}

The code above will make all links on a site blue and when you hover over the link the text will turn red.

You can do the same with a background image, you could swap it out like so:

a {
    background-image: url(images/mybackground.png);
}
a:hover {
    background-image: url(images/myotherbackground.png);
}

This isn't the best way to do this because some browsers will load the hover image when you hover, this can give a brief moment with no background (looks bad).

Generally you use a large image that may contain one or more icons and their hover counterparts. These large images are called 'sprites'. Read this article on CSS Sprites for more information.

I hope that helps you out.