Benefit of stated #import vs. compiled #import in CSS - css

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";

Related

how can i make media query work with min-with? [duplicate]

This question already has answers here:
#import styles not working in a media query
(8 answers)
Closed 3 months ago.
#charset "UTF-8";
/* import the basis style page */
#import url("body.css");
/* why is this not working? */
/* import alternative style 500px */
#media (min-width: 500px){
#import url("screen_layout_small.css");
}
The screen_layout_small.css file contains :
#charset "UTF-8";
body {
background-color: red;
}
The url "screen_layout_small.css" works when it is not in a #media (works when it is not in a responsive command ?)
I tryed to load it when width >= 500px but it doesn't work.
By the way it does not mather if I use min-with or max-with, the file does not load in the #media.
I think you are using a wrong syntax. It should be:
#import url|string list-of-mediaqueries;
So in your case it should be:
#import "screen_layout_small.css" screen and (min-width: 500px);
I think the mistake is in the syntax. Try the code that I mentioned below
#charset "UTF-8";
/* inport the basis style page */
#import url("body.css");
/* why is this not working???? */
/* import alternative style 500px*/
#media screen and (min-width: 500px){
#import url("screen_layout_small.css");
}
#charset "UTF-8";
/* inport the basis style page */
#import url("body.css");
/* import alternative style 500px*/
#import "screen_layout_small.css" screen and (max-width: 500px);
this is the working code thanks to maria romano

How to mark third-party styles as #media screen {}

Is there any way to inject (not import) css files into another css file?
I am using less and trying this:
#media screen {
#import (less) url("../Content/components/bootstrap/dist/css/bootstrap.min.css") screen;
#import "fanoe.css" screen;
#import "../Content/components/font-awesome/css/font-awesome.min.css" screen;
#import "../Content/components/jquery-ui/themes/smoothness/jquery-ui.min.css" screen;
#import "style.base.less" screen;
}
However the above does not work - the third party styles does not render and my styles are broken because of missing classes.
(The files locations are correct)
The idea is to have all styles markes as media=screen so I want to have all classes within #media screen {}.
So I want to have something like this in the style.css file:
#media screen {
.bootstrap-class { }
.font-awesome-class { }
.custom-class { }
}
etc.
Or any othe idea how to mark third-party styles as #media screen {} is welcome.

Is it possible to have different CSS for different screen sizes within a class

Currently, I am using #media to define different CSS for different screen sizes
#media (max-width: 1800){
body{
font-size: 14px;
}
}
#media (min-width: 1800){
body{
font-size: 16px;
}
}
I am having trouble making sure to update both resolution's CSS when I make a change because they are located far from each other.
Is there a way to internalize the screen size to inside the class?
ie:
body{
#media (max-width: 1800){
font-size: 14px;
}
#media (min-width: 1800){
font-size: 16px;
}
}
No, you can't include media queries inside the declaration block of a css rule.
However, it sounds like your issue may be primarily related to organizing quite a bit more css than you included in your example to simplify the process of making changes to specific selectors. If that is the case, then it may help you to use more than one media query for the same breakpoint. This may help you organize your css for simpler maintenance (locate related css rules closer together), but it does add bloat to your code due to the repeating #media rules (whether the bloat is a reasonable tradeoff for simplifying the maintenance process is up to you).
For example:
/* body styles */
body {
font-size: 14px;
}
#media all and (min-width: 1800px) {
/* body styles for 1800px and above */
body {
font-size: 16px;
}
}
/* h1 styles */
h1 {
font-size: 24px;
}
#media all and (min-width: 1800px) {
/* h1 styles for the same media breakpoint as above */
h1 {
font-size: 36px;
}
}

Performance impact of multiple responsive declarations

I am not entirely sure of the best way to place declarations such as
#media screen and (max-width: 600px) {
//
}
in my stylesheet. If, for example, I have a block of rules pertaining to some element (say, the sidebar) and I want to include some responsive rules with it, then it is tempting to insert the above code along with all the other rules for the sidebar. But then I might have some other element (say, the header) that also needs to change in some way when the screen width is below 600px. Then I'll end up with several #media screen and (max-width: 600px) declarations scattered up and down my CSS file. But it makes more sense- to me- to prioritize grouping together CSS rules according to the HTML elements they control.
So can I do this? Is there a negative performance impact from having
#media screen and (max-width: 600px) {
.sidebar {
font-size: 12px;
}
}
#media screen and (max-width: 600px) {
.header {
font-size: 16px;
}
}
rather than
#media screen and (max-width: 600px) {
.sidebar {
font-size: 12px;
}
.header {
font-size: 16px;
}
}
?
There is no notable loss of performance using several media queries instead of only one. However, if
you resize or zoom-in/out your browser, there can be a peak of memory and CPU load.
You will not resize your browser, but partially-sighted users needs to zoom your website, etc.
You should consider using a CSS Preprocessor like Less, SASS, or Stylus. A media query can be placed as a CSS property in your rule:
// app.less
#max-width: 600px;
.sidebar {
background: #2c2c2c;
#media screen and (max-width: #max-width) {
font-size: 12px;
}
}
If you can't use a CSS Preprocessor, then don't duplicate your media queries because of maintenance nightmare.
I think it would just bulk up your css file size, but if you minify it, you should be fine. It is best practice though to accomplish as much as you can in as little code as possible.

#import styles not working in a media query

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" />

Resources