Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import/include a CSS file using PHP code and not HTML code?

Tags:

html

import

css

php

I have googled a lot but it seems that I am doing something wrong.

I want to do this:

<?php include 'header.php'; include'CSS/main.css'; ... ?> 

However, my page prints the CSS code.

Note: I want to use PHP to include the CSS file, and not use I also do you want to rename my CSS file to a PHP file as some website mentioned.

Any clues?

Many thanks.

like image 205
Shailen TJ Avatar asked Jun 11 '11 12:06

Shailen TJ


People also ask

Can you include CSS in PHP?

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.

How do I import a file into CSS?

Note: There are two different ways to import a CSS file into another using @import url(“style2. css”); or @import “style2. css”; or directly import any CSS file or multiple CSS file in the HTML file directly within <style>@import “style1.

How does CSS work with PHP?

CSS represents the style and the appearance of content like font, color, margin, padding, etc. PHP is used for server-side programming which will interact with databases to retrieve information, storing, email sending, and provides content to HTML pages to display on the screen.


2 Answers

You have to surround the CSS with a <style> tag:

<?php include 'header.php'; ?> <style> <?php include 'CSS/main.css'; ?> </style> ... 

PHP include works fine with .css ending too. In this way you can even use PHP in your CSS file. That can be really helpful to organize e.g. colors as variables.

like image 163
flori Avatar answered Oct 03 '22 18:10

flori


You are including the CSS code as text in your PHP page. Why not just link it in the traditional fashion?

<link rel="stylesheet" href="CSS/main.css" type="text/css"> 
like image 40
Flash Avatar answered Oct 03 '22 17:10

Flash