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

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.

Related

What is difference between external and internal css?

I watched a flex tutorial and I found parts mentionning external and internal css. So what is the differences between the two ?
External CSS refers to a file location, ie
<link rel="stylesheet" href="your-file-here.css">
Internal CSS
Means that the CSS is included on the page, wrapped in style tags in the <head>:
So:
<style>
#wrapper { width:960px; margin:0 auto; }
</style>
When internally using CSS, styles can be used in what is called inline styles.
Which looks like:
<p style="color: #333; font-size: 22px;">Blah blah blah.</p>
The only real benefit to internal CSS, is that the browser doesn't need to make an additional GET request to download the .css file. But external is preffered. As it means you just need to modify the .css file, and it will be reflected in all pages which include a reference to that specific file.
Internal CSS
Defined inside <style> elements.
Embedded directly inside the page.
External CSS
Linked via the <link rel="stylesheet"> element.
Exists as a seperate file on the server.
The main advantage of an external CSS file, is that it can be cached independently from pages, meaning that the client only needs to download it once, which saves on loading times and bandwidth.
Also, by linking many pages to once CSS file, you only need to change one place, and have all of the site immediately affected (without having to go on every page and make the change).
An internal style sheet is a style tag in the head section of the page:
<style type="text/css">
body { margin: 0; padding; 10px; }
</style>
An external style sheet is a CSS file that is used by the page from a link tag in the head section:
<link rel="stylesheet" href="pagestyles.css" />
An external style sheet can also be specificed using the #import CSS rule, either from an internal style sheet or another external style sheet:
#import "otherstyles.css";
There is also a third type of css; inline styles that are specified on the element that they apply to:
<div style="background:#ccc;">

overriding css styles with #import not working

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.

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

Styles, using #import url() with the root of your application

I'm looking to use 'root' or '~' within a style tag to keep all my styles together and in one location.
Instead of having to rely on '../../' notation, can this be achieved?
My current code..
<style type="text/css">
#import url(../../../styles_dev.css);
#import url(../../styles/AssessHome.css);
li{font-size: 14px;}
</style>
Is there a way to convert this to something like this...
<style type="text/css">
#import url(~/styles_dev.css);
#import url(~/styles/AssessHome.css);
li{font-size: 14px;}
</style>
Thanks for any help!
No you can not use the ~ symbol there, is not going to be translate.
Alternative you can use the root of your server and run and debug your site on local iis, or use the relative paths as you all ready do.
Other solution is to render in code behind this lines using a custom handler for css.

CSS not working in stylesheet

I have a set of Styles that were first created inside the style attribute on a page.
I want to move it from being on the page itself into a stylesheet.
however, when I move it to a .css file, the page breaks, move the code back to the html doc and it works fine again.
This makes absolutely no sense, moving styles from a style to a css file shouldnt break the code should it?
Am I missing something? I am not changing any of the code, its simply a copy and paste.
This is just a shot in the dark as (at the time of this post) you haven't provided source code.
Make sure you're linking to your stylesheet using a link tag in the head of the HTML document.
If you had:
<style type="text/css">
/* <![CDATA[ */
#someid
{
margin: 0;
padding: 3px 12px;
}
/* ]]> */
</style>
You'll need to have
#someid
{
margin: 0;
padding: 3px 12px;
}
in your CSS file with:
<link rel="stylesheet" type="text/css" href="path/to/style.css" />
to link to the stylesheet.
Some common newbie mistakes include:
<style type="text/css" src="path/to/style.css">: because it's a similar syntax to the <script> tag, which would make sense, but is invalid
<link rel="stylesheet" src="path/to/style.css">: but link elements use href not src
placing link elements within the body: although browsers will tend to manage link elements in the body, there are likely going to be some errors, and it's not a defined behavior
not specifying a doctype declaration: allows the browser to go into quirks mode, which is never a good idea.
You should make sure the stylesheet is properly imported.
Sometimes the #import doesn't work well if not used accordingly, so always reference your stylesheet:
<link rel="stylesheet" type="text/css" href="name-of-stylesheet.css" />
Always remember to close the <link> tag as it's a self-close tag. I think #zzzzBov forgot to mention that.
Finally, if that doesn't work, try to override some of the styles by physically writing (above the </head> section) something like:
<style type="text/css">
body { background: blue; }
* { color: red; }
</style>
and see if that gives you a blue background and red colored text. It should. After that, try to implement the referencing method and make sure you reference the stylesheet file to the right directory.
Good luck!
I had the same problem, but the cause was not some mistake in the code but the fact that the .css file was loaded with some delay after making the modifications in it. The server needed 5 - 10 minutes to update the changes.
I had this problem as well, and the reason was that the path had to be updated for some url() references since the css file was in another folder than the html file it previously was called from.
So basically
background-image: url('patterns/debut_dark.png');
had to be changed to
background-image: url('../patterns/debut_dark.png');
Don't include <style type="text/css"></style> in your .css file.
I had the same issue and was quite frustrating. I had a css file that was properly referenced, however not all the elements were being loaded from it. As it turns out, it was a cache problem in Chrome. After clearing it and restarting the window, the css elements were working correctly.
Ran across same problem. Found there were lines in my css file that should have been commented out (a block of colour palette information that I had cut and paste to the top of the file for quick reference).
If all your syntax seems fine, then its most likely a browser cache problem that we can easily fix. In your html/php file, reference your new .css style sheet (e.g. styles.css) by adding an extra random parameter. This will force browsers visiting your page to fetch your latest styles.css.
In the of your html/php file, you should have something like this to load your new styles.css file:
<link rel="stylesheet" type="text/css" href="styles.css" />
simply change it to be like this:
<link rel="stylesheet" type="text/css" href="styles.css?ref=v1" />
that extra "?ref=v1" will prevent browsers from re-using the styles.css file they have cached, and will force browsers to get your very latest styles.css file. As you make updates to the styles.css file and upload them to your web server, just change the "v1" to "v2" etc. or whatever naming system you like so that browsers are forced to reload the latest styles.css. Note adding this "?ref=v1" to the link does not need you to change the name of your styles.css file (you can change the file name but i find that gets messy). This is a simple and clean way to force browsers into re-fetching your very latest .css file.

Resources