Using CSS with PHP Tutorial

Published January 12, 2015 by CSS Newbies.

Using CSS with PHP
PHP is a server side language and CSS and PHP do not interact with each other. Though you can output CSS to interact with your HTML, using PHP. Using CSS with PHP is even more simple that you might think. Similar to echoing out a text string in PHP, CSS is echoed out the same way. You can use CSS echoed out through PHP just like you would in HTML.

If you want to include an entire stylesheet in the HTML <head> tag the following code will work.

<PHP> echo '<link rel="stylesheet" type="text/css" href="style.css"></head>'; <PHP>

If you’d rather include some CSS through <style> tags the following code will allow you to echo it

<PHP> echo '<link rel="stylesheet" type="text/css" href="style.css"></head>'; <PHP>

Finally, you can also include a CSS file via PHP. This will essentially insert the file into this position on the page when it’s executed just like copy and paste.

<PHP> include('style.css'); <PHP>

NOTE: You will have to include the

<style>

and

</style>

tags at the beginning and end of the css file if using this method.

There are many uses to having PHP echo out CSS for you. A few examples are users customizing their theme and pulling CSS values from a database, having a different style for different user agents, and pulling customised styling from a cookie.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>