Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i run PHP inside CSS

Tags:

css

php

i have a stylesheet link like so

<link href="css/<? echo $theme;?>/styles.css" rel="stylesheet" type="text/css" />

Inside the CSS I want to be able to echo a background image outputted by the db

body{ background-image:url(../../images/<?php echo $theme.'/'.$background;?>);}

I tried adding the following to the beginning of my page but it just output everything in html code

<? header ("Content-type: text/css");?>

Im sure there is a bit to this, can anyone advise the best course of action?

Thanks in advance

like image 235
Woz Avatar asked Sep 11 '12 09:09

Woz


People also ask

Can I run PHP in CSS?

Here is a Way: Use that part of the CSS inside the HTML file. Use double quotes " " and write the PHP file inside it. Here is an example to use PHP inside CSS file. This CSS is in the HTML file and this code is working fine on my project.

Can you put PHP inside 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).

How do I start PHP in HTML?

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. And, then we have to type the start and end tag of PHP.

What is HTML PHP and CSS?

HTML stands for Hyper Text Markup Language, CSS for Cascading Style Sheets, and PHP for PHP Hypertext Preprocessor. (Yes, the acronym is recursive. Computer nerds like to do silly things like this as a joke.) The 10-second explanation: HTML is the flesh and bones of a website, defining its structure.


1 Answers

Change the file ending to php so it gets run by the server, and once you've done that you can link to it as if it was a regular css file and make sure the header is correct set with that header code you have at the top of the file

Change:

<link href="css/<? echo $theme;?>/styles.css" rel="stylesheet" type="text/css" />

To:

<link href="css/<? echo $theme;?>/styles.php" rel="stylesheet" type="text/css" />

and top of that file you have

<? header ("Content-type: text/css");?>

And btw since you seem to have php shorttags already enabled you might aswell use instead of using

<? echo $var;?>

Use

<?=$var;?>
like image 80
Breezer Avatar answered Oct 18 '22 13:10

Breezer