How can I use php to generate css?

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

A much more specific version of this question:

I decided to use php with css so that my pages work more efficiently than if I could achieve the same result using javascript. It does not matter. The important thing is that the two methods that I tried to do this (as shown in the tutorial):

adding

AddType application/x-httpd-php .css 

in my .htaccess

Then, when the stylesheet appeared in the sources of the page, but did not affect the page I tried to use

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

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

Here was the css that I tested with 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 result: css, displayed exactly as it should, as the source of the stylesheet, but not displayed on the UNLESS page, I made changes to the style in dev dev tools, after which the page will be just fine .

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

Old version:

The question here is simple:

How can I use PHP in CSS?

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

I am told that this can be done. How?

+7
css php
source share
7 answers

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

 <?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 will use something like this.

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

Edit: Added a hypothetical MySQL query on how to get user preference. It may not be thorough due to the uncertainty of the issue.

+12
source share

Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation semantics (appearance and formatting) of a document written in a markup language. Additional information: http://en.wikipedia.org/wiki/Cascading_Style_Sheets CSS is not a programming language and does not have tools that come with a server-side language, such as PHP. However, we can use server-side languages ​​to create 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
source share

PHP is a server-side language, while CSS is a stylesheet, 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 } 

This way you can apply css to any element based on conditional statements

+1
source share

How about the following in the file header:

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

This will include a β€œbuilt-in” style (in this case, the margin of the table element is determined programmatically by choosing a random number between 5 and 10). This is a trivial example, but I believe that this is what you need. Without additional information, it would be difficult 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.

+1
source share

You can make your php css file one.

For example,

 default.css.php 

And there you will have

 <?php header("Content-type: text/css; charset: UTF-8"); ?> body { background: <?=$color?>; } 

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

+1
source share

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 a simple example. You can always complicate the way you create a stylesheet.

+1
source share

It would be possible if you wrote the css part directly in your php file in a style tag, instead of including a stylesheet from the outside

-one
source share

All Articles