Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include PHP code in CSS?

Tags:

css

php

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?

like image 841
Jack Billy Avatar asked Mar 17 '11 12:03

Jack Billy


People also ask

Can we add PHP code in CSS?

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.

Can you style PHP with CSS?

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.

Can I include PHP in HTML?

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).

Where do I put PHP code in HTML?

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.


1 Answers

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.

like image 61
Your Common Sense Avatar answered Nov 15 '22 20:11

Your Common Sense