8

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.

Ken Redler
  • 23,863
  • 8
  • 57
  • 69
Lawrence Cooke
  • 1,567
  • 3
  • 26
  • 52
  • This might seem a dumb question, but do you reference the CSS file from inside your page? – Mihai Jan 22 '11 at 22:01
  • 2
    You are clearly missing "enough information for somebody to help you solve your problem". Post details. for example, how are you including the css file in your HTML file? – DwB Jan 22 '11 at 22:03
  • We can't help you without an example.. – Yorian Jan 22 '11 at 22:25

8 Answers8

24

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.
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • +1 for showing how to do it. I doubt you can't go wrong now ;) – Blender Jan 23 '11 at 01:15
  • 1
    Thank you for your help, it is very much appreciated, however I think the problem I am having, after a bit more testing seems to be more browser related, as in, on IE8/7 and FF4 & safari it works ok, but in Chrome, when I run the files as local files, its fine also, but after uploading to my server, some elements are shifting down instead of been next to. Which makes no sense, but if I move the styles back to being in the HTML , then it works just fine. Is there a bug in Chrome that would be doing this? or something else? – Lawrence Cooke Jan 23 '11 at 07:18
4

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!

Zsolt
  • 3,263
  • 3
  • 33
  • 48
Amit
  • 7,688
  • 17
  • 53
  • 68
  • you only *need* to close (`/>`) solo tags in XML/XHTML, and even then, browsers are very forgiving. – zzzzBov Jan 22 '11 at 23:06
1

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.

Mihai
  • 13
  • 3
1

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');
Jocke
  • 414
  • 3
  • 6
0

Don't include <style type="text/css"></style> in your .css file.

Philip Rego
  • 552
  • 4
  • 20
  • 35
0

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.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
steven
  • 508
  • 1
  • 8
  • 23
0

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

chixi77
  • 1
  • 1
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
carrabino
  • 1,742
  • 1
  • 14
  • 17