1

I have embedded my CSS file as PHP to make it more dynamic as shown in various examples online but I can't seem to get it working

<link rel='stylesheet' href='myphpstylesheet.php'>

My PHP file which I added in the link

<?php
 $bgcolor = '#FF00FF';
 ?>

 <style>
 Div #container {
  Background-color: <?php echo $bgcolor ; ?>
 }
 </style>

My HTML file

<html>
<head>
 <link rel='stylesheet' href='myphpstylesheet.php'>
 </head>
 <body>
   <div id="container ">
     I am a div with lorem ipusum 
   </div>
  </body>
  </html>

But the color does not apply please help

12
  • 1
    what exactly are you trying to archive? Commented Aug 18, 2018 at 14:33
  • 1
    Where are you adding Myphp file into Myhtml file I dont see anything that would achieve that. <link here> does not tell us enough Commented Aug 18, 2018 at 14:34
  • I want to like change my css with php like embedded php css Commented Aug 18, 2018 at 14:35
  • Can you copy your embedding link ? What is the computed CSS file ? Did you looked it up in your browser ? Commented Aug 18, 2018 at 14:35
  • 1
    herf !== href Commented Aug 18, 2018 at 14:40

2 Answers 2

1

I did some search on embedding css as php file, I don't really see the reason for this just use css as intended.

Anyway you can try these methods hope it fixes you problem also take note of herf and hrefas pointed out by Riggs Folly as they are not the same.

METHOD 1 You can edit your httpd.conf or .htaccess with this line

AddType application/x-httpd-php .css

the web server will now parse PHP code that is within the CSS files but i would avoid messing with those at all costs.

METHOD 2 You can also include a PHP file, in the same manner as you include a CSS file

<link rel="stylesheet" href="style.php" media="screen">

this should be done within the head of the HTML document that is the <head></head> tags

Then your style.php file should look something like this:

<?php
   header("Content-type: text/css; charset: UTF-8");

   $brandColor = "#990000";
   $linkColor = "#555555";
   $CDNURL = "http://cdn.blahblah.net";
?>

Then your css file should look somthing like this:

#header {
   background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
  color: <?php echo $linkColor; ?>;
}

ul li a {
  color: <?php echo $linkColor; ?>;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Do i need another css file i thought that the point of this was not to use a css file there by hiding more of your codes sever side
if your going through all this stress to avoid your css code being visible one can still see your css properties with F12 or inspect element
Thank you it's working now i was missing a few things which i took from your answer
0

You should use PHP's include command to inject the external PHP:

<?php include 'myphpstylesheet.php'; ?>

Inside that PHP file, you have a missing PHP closing tag, and the CSS needs to be declared properly within <style> tags:

<?php
 $bgcolor = '#FF00FF';
?>

<style>
 div #container {
  background-color: <?php echo $bgcolor ; ?>
 }
</style>

There's many ways to have dynamic control over CSS.

If you wanted to maintain the method, and truly feed it a dynamic PHP file, you have to make sure the PHP output is ONLY css. Here's an article on that.

4 Comments

Yes i have that i missed it while typing let me edit it
Sorry where is the herf errors is that whats making it not to work
Yea, I saw the herf error, but that line is not needed anyhow.
Thank you sir, i am going through the links in you're answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.