Is it possible to write something like
.global-container margin-top: 60px background-image: $image-bg @media(max-width: 767px) margin-top: 0 background-image: none
So we can define the desktop and mobile css within a class
I've tried this, but it seems not working
UPDATE: This is actually working: http://css-tricks.com/media-queries-sass-3-2-and-codekit/
You can nest media queries in native CSS, as long as you're doing it from the root.
Solution with the internal styleIt is not possible to use CSS @media rules and media queries in the inline style attribute as it can only contain property: value pairs. According to the W3 specification, the style attribute's value should match the syntax of contents of a CSS declaration block.
You may use as many media queries as you would like in a CSS file. Note that you may use the and operator to require multiple queries to be true, but you have to use the comma (,) as the or operator to separate groups of multiple queries. The not keyword can be used to alter the logic as well.
If you want to ignore the media queries (and don't want to just comment them out -for some reason) then an easy way to do this is to move all the media queries to the top of the CSS file. Now the . class selecter will override the media query and the media query will be ignored.
You should do like this:
@media all and (max-width: 767px) { .global-container { margin-top: 0; background-image: none; } }
If you want to target desktop, you can use:
@media (min-width:1025px) { .global-container { margin-top: 0; background-image: none; } }
I just notice you're using SASS
, you can do like this:
.global-container { margin-top: 60px; background-image: $image-bg; @media (max-width: 767px) { /* Your mobile styles here */ } @media (min-width:1025px) { /* Your desktop styles here */ } }
In plain CSS, the answer is no. You can't. Media queries should always be an outer shell, encasing the inside to be applied under that condition.
In SASS and LESS, however, it's fully valid. Any converter that supports nested curly brackets should be able to move the media query to the outside, therefore allowing CSS to work as said above. (You can check the output CSS after compiling sass/less file and see them do it.)
So, if you have something like this:
body { background:green; @media (min-width:1000px) {background:red;} }
The screen will be green in CSS (because it ignores that weird @media thing inside body style definition block) and red in SASS/LESS (because it gets what you'd actually like to see).
To be exact, preprocessors will understand the snippet above as:
body { background: green; } @media (min-width: 1000px) { body { background: red; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With