Using CSS with PHP Tutorial

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.

Join us in our newest publication:
<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




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.

Share and Enjoy !

0 0