YogeshChauhan . com

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.

dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 5 Use inline if to make a shorter conditional syntax in React May 4 What is Conditional Rendering in React? May 3 How does @extend rule work in SCSS (Sass)? May 2 How different is Handling Events in React vs HTML DOM? May 2 How to create bouncing balls using HTML canvas and JavaScript? Apr 30 HTML canvas methods

You might also like these

Essential SQL Commands We Need to KnowSQL/MySQLHow to add a ribbon inside a container using CSS?CSSWhat are components in Angular?AngularHow to create a smooth scrolling effect with CSS?CSSHow to Use Aggregate Functions (MIN, MAX, SUM, AVG, COUNT) to Summarize Data in SQL?SQL/MySQLCurrencyPipe in Angular 9 with ExamplesAngular