I have a script, and I will be needing to include PHP code in the CSS stylesheet. But when I try adding PHP code in my CSS file nothing occurs! Is it possible?
Putting PHP code in a CSS file will not work. You will have to create a PHP file first, and let that output CSS code. And also set the correct content type headers.
You can add css styling to a page generated by php just the same as you would an ordinary html page. Typically you would link to a style sheet in the head section, or put the styling in <style> tags within the head.
As you can see, you can use any HTML you want without doing anything special or extra in your PHP file, as long as it's outside and separate from the PHP tags. In other words, if you want to insert PHP code into an HTML file, just write the PHP anywhere you want (so long as they're inside the PHP tags).
Using these simple steps, we can easily add the PHP code. Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the <body> tag where we want to add the code of PHP.
Rename your CSS file to end in .php
,
styles.css -> styles.php
However, I doubt that you really need to have PHP code in a CSS file.
Good point from the comments below, place
<?php header("Content-type: text/css"); ?>
at the top of the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With