How to Use PHP Inside CSS File

How do i run PHP inside CSS

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;?>

How to include PHP code in CSS?

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.

how can I use of a php variable into a .css file

Actually you can.

1st Solution

Instead of using the .css file extension, use .php

Set up variables

<?php

header("Content-type: text/css; charset: UTF-8"); //look carefully to this line

$brandColor = "#990000";
$linkColor = "#555555";
?>

Use variables

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

...

ul#main-nav li a {
color: <?php echo $linkColor; ?>;
}

2nd and short solution

Create a file and name it like style.php, then in your style.php set your styles in tags like below

style.php

<style>
.blabla{
....
}

#heeeHoo{
...
}
</style>

then include style.php to your file (test.php) like

<html>
<head>
<?php include 'style.php'; ?>
</head>
<body>

</body>
</html>

That is the correct answer. Think like inline css but that is actually in external file

PHP inside CSS stylesheet

add this code in your header.php inside <style> </style>

div#cuzd-dispatch-general-v {
display: <?php echo do_shortcode('[dispatch_date_hide]'); ?> ;
}

How can a php include access the css files

You have correctly identified the problem, that the HTML cannot find the CSS. That is directly because of this tag:

<link rel="stylesheet" href="styles.css" type="text/css" />

Specifically, this part of the tag:

href="styles.css"

You have told your code to look for the styles.css file in the same directory as the index.php file. Is that correct?

Usually, the web site structure looks like this:

public_html
- css
- js
- includes
- img

Your website seems to be structured like this:

includes
public_html

Imagine yourself inside the index.php file. All files that are INCLUDEd become part of index.php, as if they were there originally. So, you are expecting to find the CSS file here (as per your <link rel="stylesheet" tag):

public_html/style.css

If they are really inside a CSS folder, then perhaps this will fix it:

<link rel="stylesheet" href="css/styles.css" type="text/css" />

Update:

No need to echo out your HTML in PHP, you can literally just do this:

header.php

<div class ="header">
<h1>The Best Sport</h1>
<h1 class="sitetitle">AllWaterPolo</h1>
<img src ="img/51wmckj8p1l__sx300__1.png" class="wpball" alt="Water Polo Ball" />
<h2 class="homeScreenLink"> <a href ="index.html">Water Polo!</a></h2>
</div>

If the other include files are similar, then it appears your rendered file will all be HTML. Therefore, the next step is: where is your style sheet? Try this. In the address bar of your browser, type:

localhost/styles.css

And see if your stylesheet appears. If not, try this:

localhost/css/styles.css

If the second one works, then change this:

<link rel="stylesheet" href="styles.css" type="text/css" />

to this:

<link rel="stylesheet" href="css/styles.css" type="text/css" />

Note: I might not have your path structure correct. Amend as required.

Include PHP inside CSS file without PHP extension

This is just for apache2 and php? Then one idea would be to interpret the .css as a .php which can be done by editing the .conf that includes the php extension or find this line somewhere in the config files:

AddType application/x-httpd-php .php

and edit to show

AddType application/x-httpd-php .php .css .phtml .etc .etc


Related Topics



Leave a reply



Submit