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 trialAristides Staffieri
14,346 PointsSass "&" selector
I've been stuck on this for a day or so, anyone see the problem?
We want to add an exception for <a> tags that are are in the <div> with a class of '.footer': those links should be purple. Use the post-selector ampersand to achieve this.
Code: /* Write your SCSS code below. */
p{ a { color: red; .footer & { color: purple; } } }
p {
a { color: blue; &:hover { opacity: 0.5; } } }
18 Answers
Tom Bedford
15,645 PointsI had problems submitting answers due to timeouts but when it was working I passed with:
p {
a {
color: red;
div.footer & {
color: purple;
}
}
> a {
color: blue;
&:hover {
opacity: 0.5;
}
}
}
This outputs the following css:
p a {
color: red;
}
div.footer p a {
color: purple;
}
p > a {
color: blue;
}
p > a:hover {
opacity: 0.5;
}
jesdavpet
21,489 PointsSame, same.
On question 4/4 I was also getting a timeout error, and couldn't pass the challenge even though the Sass code I was using did result in the correct style being applied to the HTML. Confusing!
I was selecting with .footer & {}
when I had the error, but tried div.footer & {}
instead (as suggested by Tom Bedford above) -- and it worked. Perhaps, I misunderstood the question as it was phrased.
Daniel Gregory
3,956 PointsI think I'm missing something, if the Sass code is:
a { color:red; .footer & {color: purple;} }
that makes the css:
a {color:red;} .footer p a {color:purple;}
Why should you have to add the div prefix to the .footer to pass the code challenge even though when the code is copied into sublime and viewed in a browser it works?
Jay Hori
554 PointsI really still do not understand why we needed to write div.footer instead of .footer. My understanding is that we can always ignore div when referring a class. Can someone explain this?
Tom Bedford
15,645 PointsHi Jay
The question specifically asks you to style divs with a class of footer. Not "anything" with a class of footer. It's all about specificity.
/* this styles anything with a class of footer */
.footer {
color: purple;
}
/* this will only style divs with a class of footer */
div.footer {
color: purple;
}
Steve N. Peralta R.
31,097 PointsReferencing Parent Selectors: &
Sometimes it’s useful to use a nested rule’s parent selector in other ways than the default. For instance, you might want to have special styles for when that selector is hovered over or for when the body element has a certain class. In these cases, you can explicitly specify where the parent selector should be inserted using the & character. For example:
a {
font-weight: bold;
text-decoration: none;
&:hover { text-decoration: underline; }
body.firefox & { font-weight: normal; }
}
is compiled to:
a {
font-weight: bold;
text-decoration: none; }
a:hover {
text-decoration: underline; }
body.firefox a {
font-weight: normal; }
From: Sass reference
Carlos E. Quiroz Rojas
15,278 Points:P yeah Thank Y try after I my post I pass allready and I see the error
Thank you Steve !!
Raymundo Figueroa
Front End Web Development Techdegree Student 25,606 PointsThe correct Code
p{a{color:red; div.footer & {color:purple;} }}
p{>a{color:blue; &:hover{opacity:0.5;} }}
Drew S
34,034 PointsI was just about to write in about this.
I have the same exact code, and confused to why it isn't passing.
Aristides Staffieri
14,346 PointsDo you also get an error instead of a fail when you submit Andrew?
Drew S
34,034 PointsDo you mean a Timeout Error? Yeah, that's been happening to me. On the last part of the code challenge I will try to submit the code, and it shows the error message saying my code is invalid the first time. If I try and send it through again I get a Timeout Error. So I'm having to redo the entire code challenge.
tji
1,707 PointsMe too.. Can't pass this one..
Nathan Smith
9,252 PointsI had the same time out issue, and had to redo the whole test a few times. I was able to pass by getting all the questions right on the first try.
Carlos E. Quiroz Rojas
15,278 PointsNope I have the same code and the same issue an I try also this
Code: /* Write your SCSS code below. */
p{ a { color: red; div.footer &{ color: purple; } } }
p { > a { color: blue; &:hover { opacity: 0.5; } } }
Code: /* Write your SCSS code below. */
p{ a { color: red; .footer & { color: purple; } } }
p { > a { color: blue; &:hover { opacity: 0.5; } } }
what is the errorr
Steve N. Peralta R.
31,097 PointsLook at the best answer at the top. From Tom. The code must be nested at the same way.
Dalibor Nikolic
5,283 PointsI still can't seem to pass that and I've followed what tom wrote...This is the error text i get "Within the '.footer' div, 'a' tags that are within 'p' tags should be purple."
.entry{ .content{ p{ a{ color: red; div .footer &{ color: purple;} } > a { color: blue; &:hover{ opacity: 0.5; } } } } }
Timothy Crandall
4,354 PointsYou have to remove .entry and .content classes, because they do not apply to the a's in the footer class.
Jay Hori
554 PointsHi Tom- That is precisely what I was missing. Thank you!
Brian Kerr
5,787 PointsThank you! So happy I wasn't the only person having trouble with this one.
Steve Baek
6,939 PointsThanks Tom. I was having the same problem as everyone else.
mark kling
5,504 Pointsthat was the worst piece since "functions."
Sophie Rapoport
Courses Plus Student 6,304 PointsOne other thing to pay attention to - div .footer and div.footer are not the same thing. My code didn't pass until I removed the space from the selector, and now that I think of it this makes sense. div .footer selects all elements with class .footer nested inside a div, but div.footer selects all divs with the class footer. I hope that helps others who get stuck on this question!
Tony Li
976 PointsTony Li
976 Pointswhy no need have ; after purple in sass file?
Tom Bedford
15,645 PointsTom Bedford
15,645 PointsIt is needed, I have corrected it. Congratulations no one found that for ~4 months.