dots Created with Sketch.
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.


  // USE THIS CODE TO ADD EXTERNAL CSS FILE INTO PHP

  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.

amazon

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

#Jul 22 Is there a CSS parent selector? #Jul 22 Difference between :where and :is in CSS #Jul 22 Does :is() pseudo selector hint at CSS preprocessing in the future? #Jul 22 Control Scrolling with CSS Scroll Snap #Jul 21 Control rendering using CSS content-visibility property #Jul 21 How to use @supports rule in CSS?
You might also like these
WordPress: How to setup and get values from an ACF options page?WordPressHow to install Gulp with WordPress?WordPressObservation of Human Behavior [Shopping Observation Example]MiscellaneousSafe Practice to add Links to cross-origin destinationUI/UXHow to Validate User Name, Email Address and URL in PHP?PHPHow to add Go Back button in Swift 5?Swift