PDA

View Full Version : Can a css file have another suffix than .css?


nassau
2006-10-29, 12:58
I'm planning to serve different Style Sheets as external included css files, like this:<link rel="stylesheet" href="http://www.example.com/css/main.css" />


nothing special about that, but - i want to serve different scripts depending on a number of factors, such as theme selection etc. i'm planning to store the different scripts in the database instead of actually storing the different files on disk.

my solution for this would be to have a php file figure out what code to serve as css. this would ulimately mean i would have a css file ending in .php, like this:<link rel="stylesheet" href="http://www.example.com/css/main.php" />





Question: will all browsers recognize a css file regardless of its suffix? can i have a css file named main.php?



.

chucker
2006-10-29, 13:03
Browsers usually* don't care about the suffix; they care about the content-type headers the server gives. So, if you plan on serving it with a .php suffix, make sure your web server is configured to send it with the right content-type.

I.e., send this header:
Content-Type: text/css

*) IE occasionally looks at the suffix first and the content-type second, due to many mis-configured web servers, including the default configuration of the most popular web server, Apache.

Koodari
2006-10-29, 13:10
Why is only IE affected?

Does it just work harder at guessing what faulty servers/pages want to say, while other browsers say, "not my problem if someone else does not conform to standards" and not even try to display?

I'd love to hear the background here.

nassau
2006-10-29, 13:33
...if you plan on serving it with a .php suffix, make sure your web server is configured to send it with the right content-type.



thanks! how would i do that? would it be enough to send the headers with the file or do i have to manually configure the whole server?

rollercoaster375
2006-10-29, 13:43
Just send the header with the file.


<?php
header ( "Content-Type: text/css" );
// Print some dynamic CSS!
?>

chucker
2006-10-29, 13:58
Why is only IE affected?

Does it just work harder at guessing what faulty servers/pages want to say, while other browsers say, "not my problem if someone else does not conform to standards" and not even try to display?

I'd love to hear the background here.

It's not quite that simple. You see, IE became so popular that, by correcting this common mistake, many admins running web servers assumed they did nothing wrong — since the browser they used (IE) handled things just fine.

Of course, Apache should have fixed this junk long ago, but that's a whole other issue.

The end result is that, these days, it's not uncommon to have some download in say .dmg format and the browser displays it as plain text, because that's what the server says it is. IE ignores that, in a "yeah, right, plain text, haha" manner (and in most cases, rightly so).

I believe these days, some engines have actually started doing content sniffing to try and second-guess whether the content-type the server claims could be remotely correct. That's certainly better than trusting the file extension, and also better than trusting the content-type header.

Kraetos
2006-10-29, 14:10
Just send the header with the file.


<?php
header ( "Content-Type: text/css" );
// Print some dynamic CSS!
?>


What he said. Alternatively, use a control structure to call different style sheets. Example:


<?php
switch ($theme) {
case "blue":
print '<link href="http://www.example.com/css/bluetheme.css" rel="stylesheet" type="text/css" />';
break;
case "red":
print '<link href="http://www.example.com/css/redtheme.css" rel="stylesheet" type="text/css" />';
break;
deafult:
print '<link href="http://www.example.com/css/notheme.css" rel="stylesheet" type="text/css" />';
break;
}
?>


If you call your theme style sheets after you call your other style sheets, then the theme style sheets need only contain styling that is changed by the theme. The theme styling will override the initial styling, if there is initial styling present.

This as the added advantage of keeping PHP out of your CSS, which makes the CSS easier to work with.

Hope this helps! :)

nassau
2006-10-29, 16:08
thanks, but i'm storing the css content in the database so it has to be brought out via php, unless i'm missing a much simpler solution. i'm allowing users to save their own css scripts.

nassau
2006-10-29, 16:52
deleted

Kraetos
2006-10-29, 17:30
thanks, but i'm storing the css content in the database so it has to be brought out via php, unless i'm missing a much simpler solution. i'm allowing users to save their own css scripts.

Ah. In that case, rollercoaster was spot on. Easiest thing to do is embed the style sheet.

nassau
2006-10-30, 02:40
thanks guys!