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;
}
try that kind of code
#import url("/inc/Styles/full.css") (min-width: 940px);
Here is the solution:
/* Everything 700px and lower */
#import url("./700.css") only screen and (max-width: 700px);
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);
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
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
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;
}
}
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.
in my case I should first add this meta tag in my html
<meta content="width=device-width, initial-scale=1" name="viewport" />
Related
I have a question about a project with WordPress and the Avada theme. There is a boxed mode option with a dropshadow customization. But when we use the website on mobile, it's not friendly. I would like to disable it!
I think I have to use media queries with CSS properties, but I don't know how...
You can find the project with this link: https://lr-architectes.com
I have already tried something like that in WordPress CSS customizer
#media (max-width: 800px) {
.body .html {
width: 100% !important;
}
}
#media (max-width: 800px) {
.layout-boxed-mode {
display: none !important;
}
}
No results
It looks like there's an error in your CSS syntax. You're missing some vital keywords in your code:
// the keywords `screen` and `and` were missing
#media screen and (max-width: 800px) {
.body .html {
width: 100% !important;
}
}
#media screen and (max-width: 800px) {
.layout-boxed-mode {
display: none !important;
}
}
To learn more about CSS media queries refer to W3Schools's docs on it here
Due to my vague understanding of tools I am using I failed to identify the source of the problem. node-sass does not provide media-queries aggregation, but css-mqpacker does, that is where I had to look for the problem resolvation. https://github.com/hail2u/node-css-mqpacker/issues/49.
What would be your way out of the following situation.
I merge two partial .scss files by #importing them to base file. Each file has media queries, and they provide styles for respective element of a page.
/* contents of index.scss */
#import "_block.scss", "_block-2.scss";
First file introduces two breakpoints, and order of appearance for these breakpoints in sass compiled stylesheet is defined by order in this file.
/* contents of _block.scss */
.block {
#media (max-width: 500px) {...}
#media (max-width: 450px) {...}
}
The #import of the second file has the same set of breakpoints plus one for max-width: 550px.
/* contents of _block-2.scss */
.block-2 {
#media (max-width: 550px) {...}
#media (max-width: 500px) {...}
#media (max-width: 450px) {...}
}
Identical breakpoints are aggregated during compilation, but a new one is placed at the end of compiled stylesheet, overriding properties for all breakpoints for particular element, which is not desirable behavior.
/* stylesheet compiled by sass */
#media screen and (max-width: 500px) {
.block {...}
.block-2 {...}
}
#media screen and (max-width: 450px) {
.block {...}
.block-2 {...}
}
#media screen and (max-width: 550px) {
.block-2 {...}
}
What would be a right solution?
This example represents a project where I cannot import second file before the first one because it introduces another problems with overriding.
I ended up defining style specifically to order all existing breakpoints, and introducing it early, but it is a hack I do not like at all, so I still in need for elegant solution.
It concerns me whether there is a use for media queries nested in CSS rules if it leads to such implications. In desktop first and mobile first media queries order matters, but I do not have sufficient control of it even in this simple case.
.block {
background-color: lightblue;
}
#media screen and (max-width: 500px) {
.block {
background-color: lightgreen;
}
}
#media screen and (max-width: 450px) {
.block {
background-color: lavender;
}
}
Also see https://www.w3schools.com/css/css_rwd_mediaqueries.asp
I added your current setup & and a suggested solution.
This is assumed you really need to run the 2 css files in the order you explained. It is also assumed that you want to keep as-is but have the final result working.
Since you might (for some reason) not want to delete the original values in CSS, you will have to set the ones you do not want to use, to transparent. That is background-color original default.
As a second step you need to decide which max-width you really want. When you know that you can secure the system uses that by adding "!important".
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* Current setup */
#media (max-width: 500px) {body {background-color: red}}
#media (max-width: 450px) {body {background-color: blue}}
#media (max-width: 550px) {body {background-color: green}}
/* Solution */
#media (max-width: 500px) {body {background-color: transparent}}
#media (max-width: 450px) {body {background-color: yellow !important;}}
#media (max-width: 550px) {body {background-color: transparent}}
</style>
</head>
<body>
test
</body>
</html>
I've seen a lot of posts about nesting media queries in LESS so I dont want to repeat any of that or waste anyones time but my question is slightly different. I have a nested media query inside a .less file with this code:
#media only screen and (max-width: 420px), only screen and (max-device-width: 420px){}
So that is on my login.less so my login page will be more responsive. I want to make another page responsive as well so in my aboutMe.less I also added the same code:
#media only screen and (max-width: 420px), only screen and (max-device-width: 420px){}
but its not triggering at all. Can you not have two media queries of the same type in css? So I would need to make a .less file mediaqueries.less and only have one instance of this:
#media only screen and (max-width: 420px), only screen and (max-device-width: 420px){}
and put all the sites code that I want that query to trigger in there, or is it possible to add the same query anywhere you want inside nested less files and im just doing something wrong?
Thanks!
CSS supports multiple identical media queries, if you like, but CSS doesnt support nesting.
LESS, on the other hand, does support a few methods for nesting media queries. You can read about it here: http://lesscss.org/features/#extend-feature-scoping-extend-inside-media
Example:
#media screen {
#media (min-width: 1023px) {
.selector {
color: blue;
}
}
}
Compiles to:
#media screen and (min-width: 1023px) {
.selector {
color: blue;
}
}
LESS also supports nesting media queries below selectors like this:
footer {
width: 100%;
#media screen and (min-width: 1023px) {
width: 768px;
}
}
Compiles to:
footer {
width: 100%;
}
#media screen and (min-width: 1023px) {
footer {
width: 768px;
}
}
If this doesnt answer your question, then please post the relevant part of your LESS file(s).
For media rules on less my recommendation is use Escaping.
Sample
#min768: (min-width: 768px);
.element {
#media #min768 {
font-size: 1.2rem;
}
}
I have a media query and corresponding CSS that sets up a toolset on the right side of the screen in the case for desktop users and on the bottom for mobile. However, it overrides the CSS properties and assigns 0 to both "left" and "right". What I am doing wrong? Please see the screenshot from firebug below.
Here are my media tags:
/*
- Binds all layouts together with imports and mediaqueries.
*/
//#import "libs/reset.less";
#import "mediaqueries/global.less";
#import "mediaqueries/desktop.less";
#import "mediaqueries/mobile.less";
#media only screen and (min-width: 480px) {
.mqMobile; /* Use mix-in to make sure that the styles are expanded inside the mediaquery */
}
#media only screen and (min-width: 992px) {
.mqDesktop; /* Use mix-in to make sure that the styles are expanded inside the mediaquery */
}
For the mobile media query, use right: auto. This should override the normal CSS, and switch the right property from 0 without actually specifying a specific value. For example:
#media only screen and (min-width: 480px) {
.mqMobile {
left: 0;
right: auto;
}
}
U need to add media query in ur css file.
This will target all
#toolset{
float:left;
}
This will target mobile
#media only screen and (max-device-width: 480px) {
#toolset{
float:right;
}
}
Refer to this:
http://css-tricks.com/snippets/css/media-queries-for-standard-devices/
I'm using WinLESS, a LESS compiler for Windows. In style.less, I'm using #import directives to import my 768up.less and 1030up.less, etc.
The compiled style.css has the contents of 768up.less and 1030up.less parsed inline, for example:
style.less
#import "normalize.less";
/* base styling here */
#media only screen and (min-width: 768px) { #import "768up.less"; }
#media only screen and (min-width: 1030px) { #import "1030up.less"; }
style.css
/* imported normalize */
html { font-size: 100%; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; } /* etc */
/* base styling */
.wrap { margin: 0; padding: 0 5px; } /* etc */
/* imported 768up */
.wrap { padding: 0 20px; }
/* imported 1030up */
.wrap { padding: 0 30px; }
Doesn't this defeat the purpose of the #import which are mixed with #media? I mean, the filesize of style.css is now the sum of all imported + compiled files.
Even if the browser won't use 1030up because of small screen size, will it still have downloaded style.css in its entirety?
Isn't the compiled style.css supposed to contain the #import directives unchanged, so that style.css becomes more lightweight and simply instructs to browser to retrieve additional styling if the #media criteria is met?
Is WinLESS compiling these CSS files wrong for me?
Ideally, I'd like to see the following:
/* normalize */
#import "normalize.css";
/* base styling */
.wrap { margin: 0; padding: 0 5px; } /* etc */
/* progressively enhance styling to accomodate larger screens */
#media only screen and (min-width: 768px) { #import "768up.css"; }
#media only screen and (min-width: 1030px) { #import "1030up.css"; }
Please, if I'm misunderstanding the whole concept of #import, enlighten me!
Reducing round trips generally improves performance more than reducing sizes.
It's the same idea as using sprite sheets. Making 4 requests for 1kb is a lot worse than making 1 request for 20kb.
In this case, it can't even do the requests concurrently. It must get the first file, parse it, and only then does it realize it must go back to the server to get another file.
Also mind how gzip works. 1kb+1kb != 2kb.
If you suspect you're in a case where you'd rather keep the files split, LESS only inline includes #import if it's a .less, so try:
#media only screen and (min-width: 768px) { #import "768up.css"; }
#media only screen and (min-width: 1030px) { #import "1030up.css"; }
(note the .css instead of the .less)
More details can be found by doing a control+f, search for "Importing" on http://lesscss.org/
You can enforce the import type it's doing like:
#import (css) "foo.bar";
#import (less) "foo.bar";