13

In my first version of this question, I did not offer enough information, I am sorry. Here is a clearer version of what help I need. Many of your answers are close already, this will help:

Much More Specific Version of This Question:

I've decided to use php with css to make my pages work more efficiently than they would if I were to achieve the same outcome with javascript. This is not important. What's important is that the two methods that I have tried to do this (as shown in a tutorial) are:

adding

AddType application/x-httpd-php .css

to my .htaccess

Then, when the style sheet showed up in the page's sources but did not have an affect on the page I tried using

<link rel="stylesheet" href="style.php" media="screen">

to link my stylesheet as a php file, as suggested by this tutorial. http://www.phpro.org/articles/Embedding-PHP-In-CSS.html

Here was the css that I was testing with the php variables:

<?php
/*** set the content type header ***/
header("Content-type: text/css");

/** set the paragraph color ***/
$para_color = '#0000ff';

/*** set the heading size ***/
$heading_size = '2em';

/*** set the heading color ***/
$heading_color = '#c0c0c0';
?>

p{
    color: <?php echo $para_color;  ?>;
    font-weight: bold;
    font-size: 1.2em;
    text-align: left;
}       

h1{
    color: <?php echo $heading_color; ?>;
    font-size = <?php echo $heading_size; ?>;
    text-align: centre;
}

Both methods gave me the same outcome, with the css showing up exactly as is should as a stylesheet source, but not showing up on the page UNLESS I made a change to the style in the page's dev tools, after which the page would style just fine.

Question: Why is this happening, and how do I fix it?

Old Version:

The question here is a simple one:

How can I use PHP in CSS?

I am creating a website where it will be much more efficient both in time and in processing speed for me to use php within CSS to achieve various desired results, rather than using javascript.

I'm told that this can be done. How?

  • you can use css in php, not php in css – Mohsen Safari Oct 20 '13 at 05:48
  • 1
    Stack Overflow shouldn't be the very first place you go when you have a problem... it shouldn't replace research, it should be an option after research has failed. – Amal Murali Oct 20 '13 at 05:49
  • Its not possible, because one is server side and other one is client side and @Mohsen he wants to use PHP in CSS not vice versa. – Kundan Singh Chouhan Oct 20 '13 at 05:49
  • php runs server side to generate the content of the web page - so you could inline you css code by `echo`-ing the relevant lines of code from php. Give an example of what you want your html to look like if you need more help. – Floris Oct 20 '13 at 05:50
  • 2
    possible duplicate of [How do i run PHP inside CSS](http://stackoverflow.com/questions/12367134/how-do-i-run-php-inside-css) – zzlalani Oct 20 '13 at 05:50
  • @AmalMurali PHP CAN be effectively used to provide variables for a page's CSS. And I did, look around, I came to this tutorial http://www.phpro.org/articles/Embedding-PHP-In-CSS.html but have had issues with it. I asked a question detailing these issues, but no one, out of multiple viewers of the question, had an answer. So I decided to ask the question in a ay that I might engage answerers, and not scare them away ;) –  Oct 20 '13 at 05:57
  • That comment goes for the rest of you as well, or suits most of your comments. –  Oct 20 '13 at 05:58
  • @jt0dd The link explains how to use a PHP file to generate CSS, not a CSS file using PHP. Read zzlalani's comment. – allejo Oct 20 '13 at 05:58
  • @Floris Ok Floris, let's say that I've saved a user's preferred background color in a php variable. By parsing the css as php on the server's side, I can set the background to be the correct color without using any further scripts, thus, however small the effect might be, I have saved myself time and have saved my viewer unnecessary client-side processing. In fact, let's assume that my goal is to do everything possible on the server side, simply as a test. –  Oct 20 '13 at 06:01
  • @Mohsen not true. http://www.phpro.org/articles/Embedding-PHP-In-CSS.html –  Oct 20 '13 at 06:07
  • @jt0dd: if you see `` this is using CSS in php, not php in CSS – Mohsen Safari Oct 20 '13 at 06:08
  • @jt0dd, see if my edit reflects what you are actually trying to ask. Revert/edit otherwise. – Alexei Levenkov Oct 20 '13 at 06:12
  • @KundanSinghChouhan you're incorrect, or else you're not understanding. I want to use php to control my css. –  Oct 20 '13 at 06:18
  • @Mohsen There are two ways of doing it, they both achieve the same outcome, but there is a problem. Look at www.mys3.me I have changed the .htaccess file to parse css files as php before sending them to the client. (Same effect as using the other method). But, while the file gets parsed and sent as a perfect css file, exactly as it should be, there is no effect on the page from the styles. However the stylesheet is there in the sources. And if you make an edit in the dev tools of the page, to that style source, the page updates and gets the styles. –  Oct 20 '13 at 06:22
  • That page is a blank page with a stylesheet that shows it should be a black background page. The css has parsed my php variable "Black" but the css is for some reason not applied to the page. –  Oct 20 '13 at 06:23
  • @jt0dd, how do I or anyother understand with your older version of post. – Kundan Singh Chouhan Oct 20 '13 at 06:27
  • @KundanSinghChouhan the problem, Kundan, was that I aksed the older version of the post, but no one who looked knew how to argue with or, more importantly, answer the question. So I decided to ask a more basic question so people would understand what my goal is here, and then elaborate to make this easier. Instead, I seem to have confused people. –  Oct 20 '13 at 06:30
  • 1
    You wouldn't happen to be using both the .htaccess rewrite AND the php code, would you? Only one of them should be used – allejo Oct 20 '13 at 06:32
  • Your "new" question is much, much clearer than the "old" one - but leaving the old one there is now almost more confusing than just getting rid of it... I notice you went from a net-downvote to a net-upvote after the edit! – Floris Oct 20 '13 at 06:34
  • @allejo No, I am using them separately. That's a good idea though, it might cause problems. Is this case though, I have tried both methods separately, and in both instances, the css stylesheet is being sent to the page perfectly, with the php parsed all fine. The ONLY problem is that the CSS will not show up to the page unless I force the page to update by editing the CSS in the dev tools. Unusual, to say the least. Maybe someone will have an explanation. –  Oct 20 '13 at 06:34
  • @Floris Yes, haha, do you think I should remove the old question? I didnt want to confuse the people who'd already been working on the question once I realized the problem with my old one being far too basic. –  Oct 20 '13 at 06:36
  • Currently, I am under the impression your web host is parsing your CSS file as an HTML document. Your .htaccess isn't breaking that by any chance, is it? By default, CSS files are shown in `
    ` in Firefox yet with your website, there are no `
    ` tags surrounding your CSS
    – allejo Oct 20 '13 at 06:44
  • original link to article is dead: https://phpro.orgarticles/Embedding-PHP-In-CSS.html – Flummox - don't be evil SE Nov 07 '17 at 08:53

7 Answers7

18

Your question is worded incorrectly and has confused a lot of people. What you want is a PHP file that will "generate" a CSS file so to speak. So instead of using <link> for a CSS file, you will use it to link to a PHP file which outputs CSS. As shown in the tutorial you posted.

<?php
/*** set the content type header ***/
header("Content-type: text/css");

/***
query the database for the background color
provided you have a valid MySQL connection or whatever you are using.
***/
$background_color = mysqli_query($dbc, "SELECT `background_color` FROM user_settings WHERE user_id = 1");

/** set the paragraph color ***/
$para_color = '#0000ff';

/*** set the heading size ***/
$heading_size = '2em';

/*** set the heading color ***/
$heading_color = '#c0c0c0';
?>

html{
    background: <?php echo $background_color; ?>
}

p{
        color: <?php echo $para_color;  ?>;
        font-weight: bold;
        font-size: 1.2em;
        text-align: left;
}       

h1{
        color: <?php echo $heading_color; ?>;
        font-size = <?php echo $heading_size; ?>;
        text-align: centre;
}

Now in your PHP file, you would use something like this.

<link rel="stylesheet" href="style.php" media="screen">

Edit: Added hypothetical MySQL query on how to retrieve a user's preference. Cannot be thorough due to the vagueness of the question.

allejo
  • 2,076
  • 4
  • 25
  • 40
  • I think this is as close to the right answer as you can get with the information given in the question. You might show how you change background color based on a stored user profile (add a database query) if you wanted to really knock it out of the park... – Floris Oct 20 '13 at 06:08
  • +1. The other answer could be to configure whatever is used to serve content to redirect .css files to PHP too (unfortunately I have no idea how to do that, trivial in .Net land where I live :) ) – Alexei Levenkov Oct 20 '13 at 06:08
  • Yes, this answer is close, as are a few of the others, now, I realized that I'd not provided enough information to explain my question. SO I have reworded it. THis answer, edited, would have the potential to answer this more detailed version, which I have edited once I realized the problem with my question. –  Oct 20 '13 at 06:15
  • Is there any way I could dynamically change the class name or id? e.g `h1` to `h(x)` where value of `x` based on condition? – aaa Apr 26 '20 at 15:13
2

You can make your css file a php one.

For example,

default.css.php

And in there, you'd have

<?php header("Content-type: text/css; charset: UTF-8"); ?>  


body {
 background: <?=$color?>;
}

More detailed info here: http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/

sqram
  • 7,069
  • 8
  • 48
  • 66
2

in your html file:

<link href="testCSS.php" rel="stylesheet">

in the testCSS.php file:

<?php
header("Content-type: text/css");
echo "body,html{background-color:silver; height:100%;color:white}";
?>

This is just a simple example. You can always complicate the way of generating the stylesheet.

someone
  • 21
  • 2
1

Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation semantics (the look and formatting) of a document written in a markup language. more info : http://en.wikipedia.org/wiki/Cascading_Style_Sheets CSS is not a programming language, and does not have the tools that come with a server side language like PHP. However, we can use Server-side languages to generate style sheets.

<html>
<head>
<title>...</title>
<style type=”text/css”>
table {
margin: 8px;
}

th {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
background: #666;
color: #FFF;
padding: 2px 6px;
border-collapse: separate;
border: 1px solid #000;
}

td {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
border: 1px solid #DDD;
}
</style>
</head>
<body>
<?php>
echo "<table>";
echo "<tr><th>ID</th><th>hashtag</th></tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr onmouseover=\"hilite(this)\" onmouseout=\"lowlite(this)\"><td>$row[0]</td>                <td>$row[1]</td></tr>\n";
}
echo "</table>";
?>
</body>
</html>
  • 1
    Now if only you have used the `php` to generate some of the ` – Floris Oct 20 '13 at 05:53
  • Sorry, you're misunderstanding the concept here. Php CAN be used in css. And by the way. That code looks really old school. I've been developing websites for 2 years and have never even seen the tag lol –  Oct 20 '13 at 05:53
  • @Floris Right. His answer was nice an thorough, but off concept –  Oct 20 '13 at 05:54
  • Lol I was just showing him an example of a basic page. @jt0dd –  Oct 20 '13 at 05:54
  • You can save your .css file as a .php file to run whatever function then output it as css. You can also make a php script to output an image file and set it as a background image with css. – TURTLE Oct 20 '13 at 06:09
  • 1
    TH is a valid HTML5 tag. Read more. – Jelle Ferwerda Oct 20 '13 at 06:16
  • @JakeMclaughlin right, and that's precisely what I'm trying to do, however, if you'll look at the question update, you'll see Im experiencing a problem with this process. –  Oct 20 '13 at 06:16
1

PHP is a server side language whereas CSS is style sheet but you can create some conditional checks and play with classes.

Just an example:

<span <?php if($a == 1) { echo "class='left'" ; } ?>This is some text </span>

In your CSS:

.left{
//some css work here
}

So in this way you can apply css to any element on the basis of conditional statements

Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
  • Actually, PHP can in fact be used in an external CSS stylesheet. I provided a much clearer question if you'd like to have another look. –  Oct 20 '13 at 06:37
1

How about the following in the header of your file:

<style type=”text/css”>
   table {
   margin: <?php echo rand(1,5);?>px;
   }

This would include "inline" styling (in this case, the margin of the table element is determine programmatically by choosing a random number between 5 and 10.). It's a trivial example but I believe this is what you are after. Without more information provided it's hard to be more helpful.

Obviously you want your php to do something more sensible - but the idea (that php generates some of the css instructions) seems to be what you are after.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • Ok, right, exactly, you and Moeed and Alejo are all on the right track. I'm sorry about the basics of my question. I will reword it. And add information. –  Oct 20 '13 at 06:10
-1

It would be possible if you wrote the css part directly on your php file in a style tag instead of including the style sheet externally

hussainb
  • 1,218
  • 2
  • 15
  • 33
  • I have tried this, and it doesn't work like you think it does, for some reason or another. The php doesn't get parsed inside of that style tag somehow or another. –  Oct 20 '13 at 06:03