How to import a CSS file using PHP code and not HTML code?
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.

//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.


Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS Mar 2 WordPress: How to access first and random row values from a repeater field in ACF? Mar 1 The basics of CSS Box model Feb 27 WordPress: How to display slider repeater fields in ACF? Feb 26 Simple Page Hit Counter in PHP

You might also like these

How to get query string values in JavaScript? (URL Manipulation using URLSearchParams)JavaScriptUPDATE and DELETE Statements in PostgresPostgresHow to implement NSNumberFormatter in Swift for currency?SwiftCurrencyPipe in Angular 9 with ExamplesAngularHow does AdSense calculate page loading time?JavaScriptHow to get ACF values from custom post type?WordPress