@import styles not working in a media query
Asked Answered
O

8

27

I am trying to import a style based off a media query but the import is not being triggered If i put styles directly in the media query they are rendered to the page.

Here is a link to the live site http://update.techbasics.ca

Here is my style.css with the media queries and the imports

I am using wordpress if that helps debug.

@import url('base.css');
/******************************************************************
Site Name: Tech Basics
Author: Anders Kitson

Stylesheet: Main Stylesheet

Here's where the magic happens. Here, you'll see we are calling in
the separate media queries. The base mobile goes outside any query
and is called at the beginning, after that we call the rest
of the styles inside media queries.
******************************************************************/
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box; }

/*****************************************************************
BASE CSS
*****************************************************************/

/*****************************************************************
MEDIA QUERIES
*****************************************************************/
@media only screen and (min-width: 480px) {
    @import url('min480.css');
  }
@media only screen and (min-width: 600px) {
     @import url('min600.css');
  }
 @media only screen and (min-width: 768px) {
  body {
    background: purple; } }
@media only screen and (min-width: 992px) {
  body {
    background: orange; } }
@media only screen and (min-width: 1382px) {
  body {
    background: url("../img/noisy_grid.png"); } }
/* iPhone 4 ----------- */
@media only screen and (-webkit-min-device-pixel-ratio : 1.5), only screen and (min-device-pixel-ratio : 1.5) {
  @import url('base.css'); 
}

and here is min600.css (located in the same directory as the style.css file)

header {
  text-align: left; }
  body{
    background: green;
  }
Olla answered 18/2, 2013 at 19:17 Comment(0)
B
26

try that kind of code

@import url("/inc/Styles/full.css") (min-width: 940px);
Botvinnik answered 5/9, 2013 at 12:13 Comment(1)
Any idea why this doesn't work for me? @import url('https://rawgit.com/jonsuh/hamburgers/master/dist/hamburgers.css') (screen and max-width: 574px);Marshmallow
C
13

Here is the solution:

/* Everything 700px and lower */
@import url("./700.css") only screen and (max-width: 700px);
Confusion answered 16/1, 2016 at 11:58 Comment(0)
U
10

Did you use like this?

You can write:

@import url('path.css') (screen and min/max-width: 600px);

You can add path as you use @import

or like:

@import url(style.css) (screen and min-width:600px);
Ubangi answered 15/7, 2013 at 8:48 Comment(1)
I don't know why this was down voted but I prefer this shorthand method, less characters.Marvismarwin
D
4

MDN states that @import cannot be nested and must come before all other declarations except @charset.

The syntax is as follows:

@import url;
@import url list-of-media-queries;

https://developer.mozilla.org/en-US/docs/Web/CSS/@import

Diplo answered 1/9, 2016 at 3:19 Comment(0)
M
0

It works fine, try to resize your window and you will see the colors changing As I can see in my main window on my screen (1920x1080) the CSS rule

body {
background: url("../img/noisy_grid.png");
}

Located in style.css , line 37-38 fires first, that's why you can't see the orange color. Try re arrange your css rules

Myrt answered 18/2, 2013 at 21:20 Comment(3)
its not the declarations in the media query thats not working I know they are working its the rules in the imported min600.css file thats not wirking the techbasics title should shift to the left when it is at minwidth 600px but it doesnt.Olla
Notice how the imported style in the minwidth 600px media query also does not change to color green. This is my problem.Olla
Try upgrading your browser or using another browser.Forkey
E
0

I had the same problem and after searching without finding a good solution, I ended up moving the media queries to the imported css instead. And in fact all the style sheets are downloaded even if they are not applied anyway (see CSS media queries).

Then what's the point of having separate files? For me, it keeps things organized. Using your example:

@import url(min480.css); /* min-width: 480px */
@import url(min600.css); /* min-width: 600px */

Then on the min600.css:

/* min600.css */
@media only screen and (min-width: 600px) {
   header {
      text-align: left; 
   }
   body {
      background: green;
   }
}
Encaustic answered 21/3, 2013 at 15:33 Comment(0)
P
0

true statement

@import ('filename.css') mediaType and (feature: value);

example:

@import (responsive.css) screen and (min-width: 320px) and (max-width: 480px);

notice: if use @import for CSS file, this file should not attachment in <link> tag. otherwise dose not work Styles.

Pham answered 24/7, 2020 at 11:4 Comment(0)
W
0

in my case I should first add this meta tag in my html

 <meta content="width=device-width, initial-scale=1" name="viewport" />
Windsucking answered 25/3, 2021 at 23:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.