views:

82

answers:

2

Hello, thank you for viewing.

My website includes the same header and footer for each page using PHP.

I wanted a style sheet that only applied specifically for a certain page, so put the style in using the appropriate tag.

...<body><style type="text/css"> /* what ever */ </style></body>...

The style sheet is processed correctly in all browsers I tested, however it is not validated correctly by W3C because it's located inside the body tag instead of the head.

My question is:
If I can't put the style sheet in the body tag, what is the best way to include it? I can reference the style sheet in the PHP header, but I'd rather not have another HTTP Request for such a small file. How would you do it? What is the least sloppy way to do it? Although the style tag shouldn't be in <body>, it is still processed correctly by browsers.

+5  A: 

What about giving the body an id and then just including the page specific files in the general CSS but with the styles prefixed by the id selector? Something like this:

On page

<body id="pageSpecificId">......</body>

In CSS file:

#pageSpecificId p {
   ... paragraph specific styles ...
}

#pageSpecificId li {
   ... list item specific styles ...
}
Dante617
This is assuming that the `body` tag is not included in his header/footer
Petah
Well, the body tag was in the code sample, but you're right, I did make that assumption.
Dante617
@Petah You're right, the body tag is in my header. Thanks for all the answers, everyone! This is my first question at Stack Overflow.
Nathan
@Nathan Putting the body tag in the header file is quite an unwise choice.. Please reconsider the way you are using your include files: the method mentioned by @Dante617 is far more flexible and scalable
Lucius
@Lucius Would it be a better idea to include two separate headers at the beginning of each page? One header for PHP code that executes before the HTML, and another header that contains the page layout?
Nathan
Actually, I cannnot see a real need for removing the body tag from the content, since you need to close it afterwards, at the end of the page.. anyways, whatever you choose to do, in each page you should have the option to set an id and some classes for the body, so that you can manage different site sections simply by specifying some rules inside the same css file. This approch gives you many advantages, for example, with the appropriate rules you can set the selected state of a section in your main menu only via css, without changing the page code
Lucius
A: 

The best way would be to use a MVC framework that buffers your view file, and allow tag to be dynamically added to the head before output.

Here is a ultra simple way of doing it:

index.php:

<?php
class Page {
    private static $head = array();
    private static $content = '';
    static function add_head($tag) {
        self::$head[] = $tag;
    }
    static function render_head() {
        foreach (self::$head as $tag) echo $tag;
    }
    static function render_content() {
        echo self::$content;
    }
    static function read_content($file) {
        ob_start();
        require $file;
        self::$content = ob_get_clean();
    }
    static function render_layout($file) {
        require $file;
    }
}

Page::read_content('view.php');
Page::render_layout('layout.php');
?>

layout.php:

<html>
    <head><?php Page::render_head(); ?></head>
    <body>
        <div id="header"></div>

        <div id="content"><?php Page::render_content(); ?></div>

        <div id="footer"></div>
    </body>
</html>

view.php:

<?php Page::add_head('<title>Hello World!</title>'); ?>
<h1>Hello</h1>
<p>World</p>
Petah
Thank you, I haven't heard of this before. It looks like I'm going to have to do some reading. This should work since my site is still in early development.
Nathan