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 trialVamsi Pavan Mahesh Gunturu
4,541 PointsThe media query does not activated
I am trying to remove the parallax effect(position:fixed for the back ground image) in the mobile view. But the media query is not getting activated. Can any one help me on that <pre> .girl-cover-photo{ height: 730px; background: url('../img/girl-cover.jpg') fixed; background-repeat: no-repeat; background-size: cover; @media screen and (max-width: 768px){ .girl-cover-photo{ background: url('../img/girl-cover.jpg') relative; } } } </pre>
3 Answers
jag
18,266 Points.girl-cover-photo{
height: 730px;
background: url('../img/girl-cover.jpg') fixed;
background-repeat: no-repeat; background-size: cover;
}
@media screen and (max-width: 768px){
.girl-cover-photo{ background: url('../img/girl-cover.jpg') relative; }
}
You accidentally included the media query within a class style.
Vamsi Pavan Mahesh Gunturu
4,541 PointsActually apart from the css i have mentioned in the question. I have the plain html like this <section class=".girl-cover-photo"></section>. So instead of making background relative .. I solved by making background: scroll. ps: I did not solve it http://stackoverflow.com/questions/34260116/how-to-make-the-position-static-in-mobile-view/34260491#34260491 :p
Eric Butler
33,512 PointsYes, good catch: here's the documentation on the background-attachment property. "Scroll" is the default, "relative" isn't a valid value.
Eric Butler
33,512 PointsHi Vamsi,
If you are using Sass, you don't need to re-specify the element inside the media query. You should write:
.girl-cover-photo {
height: 730px;
background: url('../img/girl-cover.jpg') fixed;
background-repeat: no-repeat;
background-size: cover;
@media screen and (max-width: 768px) {
background-attachment: scroll; /* UPDATED TO USE "SCROLL" VALUE */
}
}
Hope that solves it!
Vamsi Pavan Mahesh Gunturu
4,541 PointsThe thing is it's not about nesting. It's about the keyword relative. For background the relative does not get applied, to fix we need to make the background scroll! And BTW how did you format your snippet like that? I could not do it properly in the question.
Eric Butler
33,512 PointsYou're right, I updated my answer. For formatting, click the "Markdown Cheatsheet" link below the textarea to see how to format code. You wrap your code in new lines with 3 backticks ( ` ).
Cool!
Vamsi Pavan Mahesh Gunturu
4,541 PointsBut, why the problem appears when I specify the class explicitly inside the media query
jag
18,266 PointsDRY - Don't Repeat Yourself
SCSS wasn't designed to be used in such a way. Specifying it would make it redundant & since sass is a css complier it's all about making the most work with the least code.
Eric Butler
33,512 PointsIt's just not how Sass is written. You could write this, using plain CSS:
.girl-cover-photo {
height: 730px;
background: url('../img/girl-cover.jpg') fixed;
background-repeat: no-repeat;
background-size: cover;
}
@media screen and (max-width: 768px) {
.girl-cover-photo {
background-attachment: scroll;
}
}
Or you could write this same exact thing, but in Sass syntax:
.girl-cover-photo {
height: 730px;
background: url('../img/girl-cover.jpg') fixed;
background-repeat: no-repeat;
background-size: cover;
@media screen and (max-width: 768px) {
background-attachment: scroll;
}
}
It's just the syntax rules for each language.
Vamsi Pavan Mahesh Gunturu
4,541 PointsYea, cool! Just fiddling with sass recently. They say, you don't write css anymore :D
Vamsi Pavan Mahesh Gunturu
4,541 PointsVamsi Pavan Mahesh Gunturu
4,541 PointsStill it does not work. Did you test it? And sass allows media queries inside right?
jag
18,266 Pointsjag
18,266 PointsI wasn't aware you were working with sass. The code works when I apply it with a different picture here:
https://jsfiddle.net/7sx12m3m/
Does sass allow media queries? I'm not sure I don't work with sass but from what I see yes, nesting is allowed for media queries.
Providing your code will help see what the issue might be.