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 CSS Selectors Going Further with Attribute Selectors and Pseudo-Classes Substring Matching Attribute Selectors - "Begins With" and "Ends With"

stian wilks johansen
stian wilks johansen
11,053 Points

can't get the icons to show

a[href^="http://"] {
  color: #52bab3;
  text-decoration: none;
  background-repeat: no-repeat;
  background: 18px 18px;
  padding-left: 25px;
}

a[href$=".pdf"] {
  background-image: url('../img/icn-pdf.svg');
}

a[href$=".jpg"] {
  background-image: url('../img/icn-picture.svg');
}

a[href$=".zip"] {
  background-image: url('../img/icn-zip.svg');
}

3 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

Change "background: 18px 18px" to "background-size: 18px 18px" as this is a shorthand and not a specific value you are overwriting the background value when you define "background-image:" in the following CSS rules.

a[href^="http://"] {
  color: #52bab3;
  text-decoration: none;
  background-repeat: no-repeat;
  background-size: 18px 18px;
  padding-left: 25px;
}

a[href$=".pdf"] {
  background-image: url('../img/icn-pdf.svg');
}

a[href$=".jpg"] {
  background-image: url('../img/icn-picture.svg');
}

a[href$=".zip"] {
  background-image: url('../img/icn-zip.svg');
}

Also if your links contain no content or text in your HTML markup the background will not display because the elements width and height would = 0. So if there is no content in the link define the size manually.

Like so:

a[href^="http://"] {
  color: #52bab3;
  text-decoration: none;
  background-repeat: no-repeat;
  background-size: 18px 18px;
  padding-left: 25px;
  height: 18px;
  width: 18px;
}

a[href$=".pdf"] {
  background-image: url('../img/icn-pdf.svg');
}

a[href$=".jpg"] {
  background-image: url('../img/icn-picture.svg');
}

a[href$=".zip"] {
  background-image: url('../img/icn-zip.svg');
}

Codepen example: http://codepen.io/anon/pen/RWxqrZ

heather918
heather918
6,375 Points

Not that it was the problem here, but I made the mistake of putting a space between the 'a' and '['. I reviewed my code several times attempting to see where I went wrong, and why my icons wouldn't display. I just wanted to post here in case someone else has a similar issue.

Good: a[href$=".jpg"] {} Bad: a [href$=".jpg"] {}

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

It's a bit tricky to tell without the HTML code but it could well be you're targeting the wrong classes for the attribute selectors you're using.

$= searches for a string in your href attribute that ends in a particular substring. To get them to match you'd have to use ^=. An attribute selector that looks anywhere in the string for a match.

a[href^=".pdf"] {
  background-image: url('../img/icn-pdf.svg');
}

a[href^=".jpg"] {
  background-image: url('../img/icn-picture.svg');
}

a[href^=".zip"] {
  background-image: url('../img/icn-zip.svg');

Good luck. :-)