4

I have a weird, annoying problem. I have a css/ folder and index.html at the root. I load css files in the header as follows:

<!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" xml:lang="en" lang="en">

<head>
    <title>blabla</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="keywords" content="keywords" />
    <meta name="description" content="desc" />

    <!-- style files -->
    <link rel="stylesheet" type="text/css" href="css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="css/layout.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="css/global.css" media="screen" />
</head>

but the css is not working: I see a plain index.html. I'm sure the css path is right; when I click "view source" and copy/paste the css files path, it shows the css files.

Also, when I copy the css directly into index.html, it works. What could be the problem?

2
  • 1
    Have you tried : "/css/reset.css" Commented Nov 27, 2009 at 18:09
  • 1
    Is there an error in the CSS? I'm guessing a single extra { would cause the entire/rest of the file to not load. Commented Feb 4, 2015 at 16:41

18 Answers 18

2

Probably not served as text/css. Did you check your server config?

Sign up to request clarification or add additional context in comments.

Comments

2

The relative URL is likely wrong. To help you further we need to know two things:

  1. What is the full (absolute) URL with which you open the HTML page? Check browser address bar.
  2. What is the full (absolute) URL with which you can open the CSS file individually in your browser?

Once you know both, you can do the math to get the right relative path for use in link tag.

Comments

1

Try Firefox and Firebug. Have a look in the error console; maybe it's just a small typo.

2 Comments

hmm, it says "304 Not Modified" for css files. at Firebug Net tab
304 means that the browser already has the CSS in the cache and thinks it's up-to-date. Try to clear the browser cache.
1

It's either the wrong content-type header as jensgram suggests, or the relative path you are giving (css/...) is wrong because the page itself is in a different folder.

Install Firebug and switch to the "net" tab. It will show you if the files failed to load due to a wrong URL.

2 Comments

hmm, it says "304 Not Modified" for css files. at Firebug Net tab
Hm. That means they get definitely loaded. Then go to the Firefox error console and have it show all messages. If the CSS files' content-type header is wrong (usually text/html instead of text/css), it will say so there. (This can probably be done somewhere in firebug as well but I don't know where.)
1

Normally for this kind of problem, I would do:

  • add a style like body {border:10px solid red !important;} into the css file and refresh the html file.
  • Check if there is any typo in the path and file names, double check again, use copy/paste the names if necessary.
  • Use versions, like ... href="css/reset.css?001" ...
  • Check the folder and files permissions on the server.
  • Try load it on a different browser or "private mode".

Hope it helps!

Comments

1
+50

Try to use Firebug/Chrome Web Inspector and see if the files load. It might be that the web user has no access rights to the CSS files, in this case you should see that the files can't be loaded and they return a 401 HTTP status. In this case try to set the correct permissions (through CHMOD if it's a Unix server, 0644 should be sufficient).

2 Comments

Thanks! When I've look in debug console : ressources aren't loaded. I saw this error for each ressources : ERR_BLOCKED_BY_CLIENT. Then I just disable AdBlock Plugin and it work fine.
If I have to guess, I'd say it probably has something to do with certain class/id names referenced in your CSS file. Try removing all style definitions and adding them one by one if you have time, so you can see what is causing AdBlock to block the file.
1

Try using the format of:
<link rel="stylesheet" href="/css/file_name_here.css" type="text/css" media="screen">
etc. Though it shouldn't, it may be the order of the parameters that is causing it. This is the order i use and it has always worked for me.    I also noticed that you have three <!DOCTYPE> declarations, is this necassery and could it be the cause of this?   I have researched and
found that only one is permitted...


Whether or not they are the causes or not, i hope you find the solution soon!

Comments

1

Try this

<link rel="stylesheet" type="text/css" href="../css/reset.css" media="screen"/>

or

  <link rel="stylesheet" type="text/css" href="~/css/reset.css" media="screen" />

1 Comment

If I understand you correctly, this is a trick for the OP to determine if his stylesheet is applied at all. Then please say so. And if this is your intention, it is not an answer, just a comment.
0

Try removing the media attribute. I dont know if that would affect it but its worth a try.

Comments

0

Look in debug console if ressources are loaded. If not and if you see this error for each ressources :

ERR_BLOCKED_BY_CLIENT

Just disable AdBlock Plugin.

Comments

0

If you use the whole URL as a path, does it works? If it does, there is something wrong with your path. Try make it absolute by adding a / href="/css/reset.css"

Comments

0

try it by removing media and type attributes ,May it will work

Comments

0

Try going directly to the css files. If your url is http://www.xxxx/com try http://www.xxxx/com/css/xxxxx.css. Check if your able to open the css. If not there is a issue with your path

Comments

0

enter image description here

Please create folder structure as per above screen shot.

I have updated all files and its working as expected now.

index.html
-----------------

    <!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" xml:lang="en" lang="en">

    <head>
        <title>blabla</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="keywords" content="keywords" />
        <meta name="description" content="desc" />

        <!-- style files -->
        <link rel="stylesheet" type="text/css" href="css/reset.css" media="screen" />
        <link rel="stylesheet" type="text/css" href="css/layout.css" media="screen" />
        <link rel="stylesheet" type="text/css" href="css/global.css" media="screen" />
    </head>
    <body>
    <div id="reset">
        Reset Div
    </div>

    <div id="layout">
        Layout Div
    </div>

    <div id="global">
        global Div

    </div>

    </body>
    </html>

----------
reset.css
----------
#reset{
color:green;
}

----------
global.css
----------

#global{
    color:red;
}

-----------
layout.css
------------

#layout{
color:blue;
}

We will get below output when we run index.html file.

enter image description here

Comments

0

Check the file permissions, you may not have the rights to read the css files. Give read access to everyone.

Comments

0

Some suggestions...

According to W3 :

Partial URLs are interpreted relative to the source of the style sheet, not relative to the document.

So I suggest using /css/reset.css as path.
<link rel="stylesheet" type="text/css" href="/css/reset.css" media="screen" />

Howewer if this does not work i would try to add whole address of your website.

Does http://www.yourweb.com/css/reset.css show you anything?

If you could use PHP I suggest add this code to your index file like this:

<?php echo realpath(dirname(__FILE__)); ?>

You will see your absolute path.

Also have you been able to duplicate your problem on external test sites like jsfiddle using url path to your css?

Comments

0

This solution will help only if you are trying to deploy on web, not on a local environment but since there is a lack of detail here, I wanted to specify.

I remember having the same problem and I fixed it adding // before the path to the file as the browser need to know it is loaded from web.

You can verify this easily by pasting the url with and without the // before the css path.

For example

xxx.xxx.xxx.xxx/path/to/css.css

Would not be loaded while

//xxx.xxx.xxx.xxx/path/to/css.css

Would be loaded.

Other possibilities would be

  • Incorrect permissions
  • Wrong path, maybe you are missing something ? Try with a slash at the begining or the absolute path.

Comments

0

For anyone experiencing this behavior in an environment that was published to the web, I would recommend attempting to view it in a different browser or in your phone. I experienced this behavior in Google Chrome through my personal computer and confirmed that my console was blocking the css elements from being viewed. I hosted my website in Filezilla to be specific.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.