Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: define media query within one class [closed]

Tags:

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/

like image 519
cqcn1991 Avatar asked Jan 24 '14 03:01

cqcn1991


People also ask

Can you have nested media queries?

You can nest media queries in native CSS, as long as you're doing it from the root.

Does media query working in internal CSS?

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.

Can you use multiple media query settings in a stylesheet?

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.

How do I ignore media queries in CSS?

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.


2 Answers

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 */     }  } 
like image 181
Felix Avatar answered Oct 22 '22 00:10

Felix


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;   } } 
like image 20
dkellner Avatar answered Oct 22 '22 01:10

dkellner