简体   繁体   中英

how to load different css for different pages?

I am trying to create different style sheet for my pages.

I have a set header file

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
       <link href="/includes/main.css" rel="stylesheet" media="all"/>
</head>
<body>

then I have my main body page, I will have main.html, project.html, contact.html …etc

//php load different pages for my body
<div>….

and a footer page.

<footer>…

My question is how to swap the css file to adapt different pages for my main body page. The header and footer files are the template and I don't want to change the css file every time I load a new page. How do I accomplish this? Thanks a lot!

One simple way of doing it is by using a php function called "basename".

If you have 3 pages main.php, project.php and contact.php, you can load different resources depending upon the name of the page you are viewing.

For example echo ;

<!DOCTYPE html>
 <html>
  <head>
  <meta charset="utf-8"/>
   <?php if(basename($_SERVER['PHP_SELF']) == 'main.php'){ ?>
     <link href="/includes/main.css" rel="stylesheet" media="all"/>
   <?php }
   elseif(basename($_SERVER['PHP_SELF']) == 'project.php'){
   ?>
    <link href="/includes/project.css" rel="stylesheet" media="all"/>
   <?php } ?>
 </head>
<body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM