Yogesh Chauhan's Blog

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

in PHP on July 19, 2019

Can you import an external CSS file into PHP using the same code as HTML? Try it.

I know you can’t use the same code but it’s a lot similar.

I had Googled the solution many times but I couldn’t find the exact answer and kept trying. At the end, I found on a reference book so I am sharing here. I thought someone might need it.

If you try the following code the the PHP page is going to print the whole CSS code in some cases and in others, it won’t do anything at all.

  php include 'my-blog-header.php';  include 'my-blog-main-style.css'; 
  //add opening and closing tags 

In above code, all you need to do is surround the .CSS file line with a style tag.

Look at the following code.


  php include 'my-blog-header.php'; ?> //INCLUDING EXTERNAL PHP FILE
  //add opening and closing tags 

There is one advantage as well. In the above way you can use your PHP file into your CSS file which can be really useful to organize it for e.g. colors as variables.

NOTE: style tag does’t need to be echoed so the following code can be used for better programmer readability.

<style> php include 'style.css'; </style> //add opening and closing tags <? code ?>

Also, all the HTML file system rules applied here as well. So you can use any file from any folder but you need to follow some rules.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
MySQL queries to get data rows from previous month as well as from last 30 daysSQL/MySQLFilling a button background from left to right using CSSCSSHow to avoid element shift on border change while hovering in CSS?CSSHow to apply style only to first child and/or only to children other than the first child?CSSfirst-of-type and last-of-type selectors in CSSCSSHow to list all PHP variables to debug the script?PHP