Reputation: 170
I have a common HTML header file which I use in several PHP files. I have a general CSS file which I have included inside the <head>
tags in the header file. However, I want to include one additional CSS file only in one PHP file. Since I have common header file, do I have to include the additional CSS file in the common header or there is any way that I can include the additional CSS file only in the PHP file where it is required?
Thanks.
Upvotes: 1
Views: 3225
Reputation: 11148
In that common header include file, use a conditional statement (if) and depending on the condition, place the link to the stylesheet.
Example:
<?php
if($somevar){
echo '<link rel="stylesheet" type="text/css" href="stylesheet.css">';
}
?>
The $somevar
is your condition.
Upvotes: 1
Reputation: 6159
One other option is to set a special class on the body on your page and use CSS selectors accordingly in only ONE CSS file:
<body class="<?php echo $pageName; ?>">
If your special page is "special", then use in the CSS:
body.special h2 {...}
It will override your common h2 declaration.
Much more easier to maintain if the changes in this special page are minor.
Upvotes: 0
Reputation: 21466
This is exactly why a lot of people include a piece of code in their header files that allow you to add more stylesheets to it on a per-page basis.
Something like this (goes in <head>):
<?php
if (!empty($styles) && is_array($styles)) {
foreach ($styles AS $style) {
echo '<link rel="stylesheet" href="/assets/css/'. $style .'">';
}
}
?>
You could expand on that if you wanted to include media types, but that snippet allows you to put a variable at the top of an individual script if you need a specific stylesheet:
<?php
$styles = array('custom_style.css');
?>
Upvotes: 2
Reputation: 791
Just:
echo '<link rel="stylesheet" type="text/css" href="your-specified.css">';
in your desired PHP file.
Upvotes: 0