Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include: CSS with .php file extension?

Tags:

include

css

php

I'd like to wrap a CSS file in PHP... So I write the header for the CSS file and give it a .php file extension, thus... css.php.

Will this work if the page is already being used as an include? Or will this new header clash with the frame the page is being included into?

like image 636
user154107 Avatar asked Sep 18 '09 15:09

user154107


People also ask

Can you add CSS to PHP file?

You can add css styling to a page generated by php just the same as you would an ordinary html page. Typically you would link to a style sheet in the head section, or put the styling in <style> tags within the head.

What file extension is used for CSS?

. css is the extension for CSS file to save. The extension to save any css file is simple just like if you want to save any text file so you write filename.

How do I add CSS to a file?

CSS can be added to HTML documents in 3 ways: Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section. External - by using a <link> element to link to an external CSS file.

What are PHP file extensions?

php file extension refers to the name of a file with a PHP script or source code that has a ". PHP" extension at the end of it. It's similar to a Word file with a . doc file extension.


2 Answers

gnarf nailed it.

I do:

<link rel="stylesheet" type="text/css" media="screen" href="<? echo TMPL ?>css/name-of-file.css.php">

and then in top of your .css.php file:

<?
header('Content-Type: text/css');
// print out your php-driven css...
?>
like image 169
virtualeyes Avatar answered Oct 13 '22 20:10

virtualeyes


If you have a file called css.php just make sure the first lines set the proper content-type header. You may also want to split your session setup stuff (if there is any) into a bootstrap.php if you haven't already. A quick example of loading some style info from a databse:

<?php 
  header("Content-Type: text/css"); 
  include('bootstrap.php');
  // fetch some information to print out our styles
  $result = mysql_query("SELECT * FROM site_styles");
  while ($row = mysql_fetch_assoc($result)) {
    echo $row["selector"]." {\n".$row["css"]."\n}\n";
  }
?>

From your other php file, just output the tag to include the css.php, you do not want to use the php include() function for this task!

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

Although since most browsers will cache your css file pretty aggressively, you might find that dynamically changing the contents of that file doesn't do much good. You could force that to update by adding a get parameter to the href of the link like so:

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

Although this is going to completely reload your css file every time that parameter changes. It is generally better practice to serve up static css files for this reason. If you have some styles that need to be loaded from configuration parameters, etc, that don't change very often, the first example should work for you quite well.

like image 40
gnarf Avatar answered Oct 13 '22 18:10

gnarf