Jump to content

can i add php in a .css?


Orionsbelter

Recommended Posts

You can include your .css doc with the "include();" function like so;

 

<?php
include("style.css");
?>

 

or you can directly insert your css content into a .php file;

 

<?php
//php code goes here

//start your css

h2
{
color: black;
font-weight: bold;
font-size: 1.2em;
text-align: left;
}
// end of css

CSS;
?>

 

Hope that helps, not sure on how to directly insert PHP into a .css document but this way is probably more preferred by Developers.

Link to comment
Share on other sites

You can't insert php into a css document. Think about it.

 

PHP only works with a .php extension. Once you convert a .css file to a .php file, it is no longer a .css file.

 

A solution similar to Rhyboo's solution is best ... generate your entire inline php on the fly in the head tag.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>php in head</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
echo"
<style type=\"text/css\">
body
{
color: #000;
        background-color:#fff;
font-size: small;
margin:0;
}
etc.,
etc.,
<style>";
?>
<head>
<body>

Link to comment
Share on other sites

There are actually a couple ways to include php in css files.

 

Method 1)

Include your CSS, and change your filename from whatever.css to whatever.php. Then link to the file like this:

 

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

 

Method 2)

Use your .htaccess file to have .css files processed as php files. In this way you don't have to change any file names or links, but you will take a hit in that apparently your css sheets don't cache, which basically takes the functionality out of a css sheet, and as such this method probably shouldn't be used.

 

Method 3)

The third method is discussed here, although the article in question is actually about gzipping css sheets, rather than about using php in css. But in order to use this method, your css sheets all need to be php, so following this method will do what you want.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.