External stylesheets

Discussion in 'HTML' started by SeeSchloss, Sep 30, 2003.

  1. SeeSchloss

    SeeSchloss Guest

    Hi,

    I'd like to dynamically generate an external stylesheet
    with PHP. Therefore, I replaced the
    <link rel="stylesheet" type="text/css" href="style.css" />
    with
    <link rel="stylesheet" type="text/css" href="style.php" />
    (replaced .css with .php).
    The problem is that the browser only seems to use the
    stylesheet when the extension is '.css', I also tried
    '.inc' and a few others, and only '.css' worked (the files
    were exactly the same, of course). And since .css files are
    not interpreted by PHP (without modifying the config
    file)...
    Is it normal ? And could you suggest any other way to do it
    ?
    I could make it internal, but since it is about 5KB, it
    would almost double the weight of my pages.

    Thanks.


    --
    SeeSchloß - http://www.seeschloss.net
     
    SeeSchloss, Sep 30, 2003
    #1
    1. Advertising

  2. SeeSchloss wrote:
    > I'd like to dynamically generate an external stylesheet
    > with PHP. Therefore, I replaced the
    > <link rel="stylesheet" type="text/css" href="style.css" />
    > with
    > <link rel="stylesheet" type="text/css" href="style.php" />
    > The problem is that the browser only seems to use the
    > stylesheet when the extension is '.css'


    File extensions are irrelevant, its the content type in the http header that
    browsers (should[1]) respect for deciding what type of file it is. By
    default PHP outputs files with a text/html header (which is sensible given
    that almost all PHP files are designed to output HTML documents). You have
    to explicitly tell it to send a different content type header if you don't
    want that.

    <?php header('Content-type: text/css'); ?>


    [1] Internet Explorer sometimes second guesses the author, I don't know if
    file extension plays a role in this.

    --
    David Dorward http://dorward.me.uk/
     
    David Dorward, Sep 30, 2003
    #2
    1. Advertising

  3. SeeSchloss

    SeeSchloss Guest

    David Dorward a écrit :
    > SeeSchloss wrote:
    >> I'd like to dynamically generate an external stylesheet
    >> with PHP. Therefore, I replaced the
    >> <link rel="stylesheet" type="text/css" href="style.css" />
    >> with
    >> <link rel="stylesheet" type="text/css" href="style.php" />
    >> The problem is that the browser only seems to use the
    >> stylesheet when the extension is '.css'

    >
    > File extensions are irrelevant, its the content type in the http
    > header that browsers (should[1]) respect for deciding what type
    > of file it is. By default PHP outputs files with a text/html
    > header (which is sensible given that almost all PHP files are
    > designed to output HTML documents). You have to explicitly tell
    > it to send a different content type header if you don't want
    > that.
    >
    > <?php header('Content-type: text/css'); ?>
    >
    >
    > [1] Internet Explorer sometimes second guesses the author, I
    > don't know if file extension plays a role in this.


    Ok ok, it works now. Since all I did was changing the
    extension without adding any code to it, I thought it
    should have worked to same, but the file goes through php
    anyway.

    By the way, I forgot to say it but the problem was with
    Mozilla Firebird, I just tried with Internet Explorer and
    it worked even without the text/css header.

    Thanks for the quick answer :)


    --
    SeeSchloß - http://www.seeschloss.net
     
    SeeSchloss, Sep 30, 2003
    #3
  4. SeeSchloss wrote:

    > By the way, I forgot to say it but the problem was with
    > Mozilla Firebird, I just tried with Internet Explorer and
    > it worked even without the text/css header.


    Internet Explorer: This style sheet is written in text/html, bah I'll try to
    treat it as CSS.

    Firebird: This style sheet is written in text/html, I don't know how to
    handle this, so I'll ignore it.

    I much prefer Firebirds approach - if at some stage a different style sheet
    language was created, that had similar syntax to CSS, IE would probably try
    to treat it as CSS and get things very very wrong.

    --
    David Dorward http://dorward.me.uk/
     
    David Dorward, Sep 30, 2003
    #4
  5. SeeSchloss

    brucie Guest

    brucie, Oct 1, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adrian

    Changing stylesheets

    Adrian, Oct 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    338
    Esteban Felipe
    Oct 27, 2003
  2. Rod  Snyder
    Replies:
    3
    Views:
    2,609
    =?Utf-8?B?RGF2aWQgQ29l?=
    Feb 2, 2004
  3. Scott Allen
    Replies:
    8
    Views:
    10,987
    Scott Allen
    May 2, 2004
  4. BJF

    External stylesheets and custom controls

    BJF, Jun 28, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    126
  5. Mellow Crow
    Replies:
    6
    Views:
    426
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page