Search code examples
phphtmlcssincludestylesheet

How to link different style sheets for different php include files


So I'm dividing my index.php page into three sections: top, middle, bottom. The middle section will have different html php inlude pages and therefore will require different style sheets. Do I link the specific stylesheets in the individual php include pages, or in the index page? Because in the index page, the different style sheets don't seem to take effect, why is that?


Solution

  • Say your about page has a custom css file it needs you could do something like this:

    about.php

    <?
    $css = array('path_to_css_file', 'path_to_another_css_file');
    require_once('header.php'); // aka the top
    ?>
    
    [about page content goes here]
    
    <?
    require_once('footer.php'); // aka the bottom
    ?>
    

    The in your header.php file you could do this:

    header.php

    <html>
    <head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="main_style_sheet.css" />
    <?
    if (isset($css) && is_array($css))
      foreach ($css as $path)
        printf('<link rel="stylesheet" type="text/css" href="%s" />', $path);
    ?>
    </head>
    <body>
    

    This way you only load what you need for the given page.