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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to Add, Edit and Delete a Workflow in Etrieve?Miscellaneoususer_can vs current_user_can in WordPressWordPressWindow setInterval() Method in JavaScriptJavaScriptControl Scrolling with CSS Scroll SnapCSSLearn to make a responsive gallery using CSS Grid and without media queriesCSSHow to make flexbox items of the same size?CSS