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 trialPhuc Tran
6,542 PointsAn attribute selector problem
[id*="#"] { background-color: tomato; }
I was supposed to writ css code to target an elemeent ONLY IF its ID matches the HASH in a URL, and set its background-color to tomato.
So I used attribute selector like above, but apparently it didn't work.
Cheers, Phuc
4 Answers
Nick Nish
7,304 PointsHey @Phuc
There are many roads to success, in this case a lot of CSS selector solutions would work, including @Dustin's. If you want to use an attribute selector, you'll have to specify the type of element that you want to select: (or if you want to select all elements with an ID you can try the second line, but it's generally poor practice to overuse the (*))
div[id="id-name"] { background-color: tomato; }
*[id="id-name"] { background-color: tomato; }
Dustin Matlock
33,856 PointsHow about:
div:not([id="s2"]) {
background-color: tomato;
}
Phuc Tran
6,542 PointsIt worked as it selected the right one, however I don't think that was the purpose of the exercise. ( :not attribute selector was mentioned in the section).
Phuc Tran
6,542 PointsIt worked as it selected the right one, however I don't think that was the purpose of the exercise. ( :not attribute selector was mentioned in the section).