overriding css styles with #import not working - css

I'v seen some similar questions about overriding styles with #import, people suggest to put #import at the bottom, but that does not seem to work here.
--- index.html ---
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body>
This text should be green.
</body>
--- style.css ---
body {color: red;}
#import url('style-override.css');
--- style-override.css ---
body {color: green;}
The example above will output red text, while green is expected.
Declaring style-override.css after style.css inside head will solve the problem, but I want to use #import inside a css file.
Adding !important in style-override.css will also get the expected result, but that is not the way it should work.
Can anyone explain this?

That isn't working because any import rule declared inside of a stylesheet must come before everything else - otherwise, ...well, it doesn't work ;) .
So, what you should have in your style.css stylesheet is:
#import url('style-override.css');
body {color: red;}

#import rules must be at the top. This is what the CSS spec. says about it:
Any #import rules must precede all other at-rules and style rules in a style sheet (besides #charset, which must be the first thing in the style sheet if it exists), or else the #import rule is invalid.

Related

Will a failed #import url() request block the rest of the CSS in the file from being applied

If a CSS file that starts with a #import url() containing a link to a stylesheet hosted on a external server and the request to load that file fails, will the subsequent CSS rules in the rest of the file be applied?
For example:
/* main-styles.css */
#import url("//external-url.com/styles.css")
h1 {
color: red;
}
Will the <h1> element be colored red?
use this in your head section..
<link rel="stylesheet" type="text/css" href="external-url.com/styles.css">
instead of using import..
After testing in both Chrome and Firefox browsers it appears that having the #import url() request fail does not impact any styles contained in the rest of the CSS file from being applied correctly.

Is the #import call supported within <style> tags?

So it's not supported to have:
...
<body>
<link rel="stylesheet" type="text/css" href="theme.css">
...
but what's the consensus with being able to do:
...
<body>
<style>
#import '/custom.css';
</style>
...
style tags and link rel should be within the head tags but that does not mean it won`t work inside body or divs. its just not good practice
#import '/custom.css';
should be placed only inside css files
Yes, you can have an #import rule at the start of a style sheet even when the style sheet appears as the content of a style element.
However, neither style nor link elements are allowed within body, only within body, according to the formal rules of HTML. These rules are not enforced in practice; the elements work just as they do inside head. (Division into body and head is really just formal.)

What is the correct link sequence when replacing #import from within a css file

I have an html web design made by someone else; they have used #import inside the css files. I will for several reasons (primarily: http://www.stevesouders.com/blog/2009/04/09/dont-use-import/) to remove the #import inside the css files and instead include directly it in the html page with: <link rel="stylesheet"… >
My question is now: what will be the correct way to include the files via link to maintain the current rule validation sequence
Current #import sequence, eg.:
a.css
#import b.css
b.css
#import c.css
#import d.css
Will the correct link order be:
a.css
b.css
c.css
d.css
or is it something like:
c.css
d.css
b.css
a.css
or???
Each file contains, besides one or more import tags also own css rules.
For the record: it is not an option for me to gather all the files in one file – the different css files are used I different context alone and/or together
An #import rule includes all stylerules as if they were on that position. Example (default.css):
#import "first.css";
#import "second.css";
#import "third.css";
body {
color: #333333;
}
In this order, all css rules of first.css will be applied first, then the second.css, then the third.css. So if you want to remove the inclusion, just follow the import links and add them in this order in your html head section:
<head>
<title>Whatever</title>
<link href="first.css" rel="stylesheet" type="text/css">
<link href="second.css" rel="stylesheet" type="text/css">
<link href="third.css" rel="stylesheet" type="text/css">
<link href="default.css" rel="stylesheet" type="text/css">
</head>
So, in your case it will be:
c.css
d.css
b.css
a.css
I believe they will be parsed in the order they are presented to the browser, in linked style sheets the import rules must be before any other rules in the file or errors will occur so again I am assuming that they are parsed in order.
a.css
b.css
c.css
d.css
Post back if you find this not to be true.

How do I get my #import stylesheet to override the main stylesheet?

I imported another stylesheet using #import in the main style sheet file. I would like the changes I have made in the #import stylesheet to override the main style sheet. Is this possible?
If your goal is to override styles by importing another stylesheet, you should use the order of precedence.
<head>
<title>Title</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="style-override.css" rel="stylesheet" type="text/css" />
</head>
Here the style.css is the original and style-override.css would contain your new custom css. These styles will override the styles from style.css. This means you won't need to use !important because the style is overwritten.
Avoid !important whenever you can.
To do #import
<style type="text/css">
#import url("style.css");
#import url("style-override.css");
</style>
Also as a side note if you would rather remove all styles from the page, use a css reset.
<style type="text/css">
#import url("style.css");
#import url("reset.css");
#import url("style-override.css");
</style>
Check out a CSS reset at http://meyerweb.com/eric/tools/css/reset/ and add it to reset.css.
#import the second stylesheet at the end of the first.
You're confusing !important and #import
This solution working perfect for me.
Make copy of your main.css and rename it to style.css.
In main.css delete all and past :
#import url("style.css");
#import url("style-override.css");
Thats all.
If your second stylesheet uses the same selectors, then it should override the first without any problem.
CSS has a very strict order of precedence for determining which one should be used, but if all else is equal and two styles have exactly the same precedence level, then it will use the one which was specified last. This allows you to override a style simply by repeating the same selector later on.
The only exception to this is if the first style was specified as !important. In this case, it is much harder to override it. Even specifying another style as !important may not always work (I've seen cases where it worked in some browsers but not others).
So if the previous stylesheet used !important then you may have problems overriding it. But if not, it should be fairly simple.
You can also use more specific class name - for example if you want to change
div#sample {
max-width: 75%;
}
on new css use
body div#sample {
max-width: 75%;
}
Just keep in mind, that overqualified selectors are not the best idea ;)

Is it possible to include one CSS file in another?

Is it possible to include one CSS file in another?
Yes:
#import url("base.css");
Note:
The #import rule must precede all other rules (except #charset).
Additional #import statements require additional server requests. As an alternative, concatenate all CSS into one file to avoid multiple HTTP requests. For example, copy the contents of base.css and special.css into base-special.css and reference only base-special.css.
Yes. Importing CSS file into another CSS file is possible.
It must be the first rule in the style sheet using the #import rule.
#import "mystyle.css";
#import url("mystyle.css");
The only caveat is that older web browsers will not support it. In fact, this is one of the CSS 'hack' to hide CSS styles from older browsers.
Refer to this list for browser support.
The #import url("base.css"); works fine but bear in mind that every #import statement is a new request to the server. This might not be a problem for you, but when optimal performance is required you should avoid the #import.
The CSS #import rule does just that. E.g.,
#import url('/css/common.css');
#import url('/css/colors.css');
Yes.
#import "your.css";
The rule is documented here.
In some cases it is possible using #import "file.css", and most modern browsers should support this, older browsers such as NN4, will go slightly nuts.
Note: the import statement must precede all other declarations in the file, and test it on all your target browsers before using it in production.
Yes, use #import
detailed info easily googled for, a good one at http://webdesign.about.com/od/beginningcss/f/css_import_link.htm
yes it is possible using #import and providing the path of css file
e.g.
#import url("mycssfile.css");
or
#import "mycssfile.css";
#import("/path-to-your-styles.css");
That is the best way to include a css stylesheet within a css stylesheet using css.
The "#import" rule could calls in multiple styles files. These files are called by the browser or User Agent when needed e.g. HTML tags call the CSS.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="EN" dir="ltr">
<head>
<title>Using #import</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
#import url("main.css");
</style>
</head>
<body>
</body>
</html>
CSS File "main.css" Contains The Following Syntax:
#import url("fineprint.css") print;
#import url("bluish.css") projection, tv;
#import 'custom.css';
#import url("chrome://communicator/skin/");
#import "common.css" screen, projection;
#import url('landscape.css') screen and (orientation:landscape);
To insert in style element use createTexNode don't use innerHTML but:
<script>
var style = document.createElement('style');
style.setAttribute("type", "text/css");
var textNode = document.createTextNode("
#import 'fineprint.css' print;
#import 'bluish.css' projection, tv;
#import 'custom.css';
#import 'chrome://communicator/skin/';
#import 'common.css' screen, projection;
#import 'landscape.css' screen and (orientation:landscape);
");
style.appendChild(textNode);
</script>
Import bootstrap with altervista and wordpress
I use this to import bootstrap.css in altervista with wordpress
#import url("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css");
and it works fine, as it would delete the html link rel code if I put it into a page
#import url('style.css');
As opposed to the best answer, it is not recommended to aggregate all CSS files into one chunk when using HTTP/2.0
I have created main.css file and included all css files in it.
We can include only one main.css file
#import url('style.css');
#import url('platforms.css');
Yes You can import easily one css to another (any where in website)
You have to use like:
#import url("url_path");
sing the CSS #import Rule
here
#import url('/css/header.css') screen;
#import url('/css/content.css') screen;
#import url('/css/sidebar.css') screen;
#import url('/css/print.css') print;
For whatever reason, #import didn't work for me, but it's not really necessary is it?
Here's what I did instead, within the html:
<link rel="stylesheet" media="print" href="myap-print.css">
<link rel="stylesheet" media="print" href="myap-screen.css">
<link rel="stylesheet" media="screen" href="myap-screen.css">
Notice that media="print" has 2 stylesheets: myap-print.css and myap-screen.css. It's the same effect as including myap-screen.css within myap-print.css.
I stumbled upon this and I just wanted to say PLEASE DON'T USE #IMPORT IN CSS!!!! The import statement is sent to the client and the client does another request. If you want to divide your CSS between various files use Less. In Less the import statement happens on the server and the output is cached and does not create a performance penalty by forcing the client to make another connection. Sass is also an option another not one I have explored. Frankly, if you are not using Less or Sass then you should start. http://willseitz-code.blogspot.com/2013/01/using-less-to-manage-css-files.html

Resources