如何为不同的页面加载不同的CSS? [英] how to load different css for different pages?

查看:146
本文介绍了如何为不同的页面加载不同的CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个设置的头文件

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

然后我有我的主体页面,我将有 main.html, project.html,contact.html ...等等

  // php为我的身体加载不同的页面
< div> ...。

和页脚页。

 < footer> ... 

我的问题是如何交换css文件以适应我的主体页面的不同页面。页眉和页脚文件是模板,我不想在每次加载新页面时更改css文件。我该如何做到这一点?非常感谢!

解决方案

一个简单的方法是使用名为basename的php函数。



如果您有3页main.php,project.php和contact.php,您可以根据所查看页面的名称加载不同的资源。



例如
echo;

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


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>

这篇关于如何为不同的页面加载不同的CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆