简体   繁体   English

如何为不同的页面加载不同的CSS?

[英]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 然后我有我的主体页面,我将有main.html, project.html, contact.html …等等

//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. 我的问题是如何交换css文件以适应我的主体页面的不同页面。 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. 页眉和页脚文件是模板,每次加载新页面时我都不想更改css文件。 How do I accomplish this? 我该如何完成? Thanks a lot! 非常感谢!

One simple way of doing it is by using a php function called "basename". 一种简单的方法是使用名为“ basename”的php函数。

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. 如果您有3个页面main.php,project.php和contact.php,则可以根据正在查看的页面名称来加载不同的资源。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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