duanliusong6395 2015-09-25 09:08
浏览 64
已采纳

包括来自header.php的样式表和相对路径

i am working on a website and i am looking for a way to include all my stylesheets and scripts relatively. Including the php-files is easy:

<?php include 'header.php'; ?>  

or

<?php include '../header.php'; ?>  

or by referencing the root-path:

<?php include '/header.php'; ?> 

But: All the stylesheets and scripts are not loaded on pages located in ‘/products‘, because they are located relatively to the root-directory and not to root/products.

For example:

<link href="css/styles.css" rel="stylesheet">

Is there a way to include stylesheets and scripts relativlely, without taking care of where the page is located, that is calling header.php?

I am working on a local server with mamp and there are many websites in the mamp-directory.

Thanks in advance!

Tim

  • 写回答

2条回答 默认 最新

  • douxie3625 2015-09-25 09:14
    关注

    It's all about the HTML. The following will load a stylesheet relative to the page you are currently on.

    <link href="css/styles.css" rel="stylesheet">
    

    For example, if your page is /products/page1.html. The browser will try to load /products/css/styles.css

    An absolute path would look like:

     <link href="/css/styles.css" rel="stylesheet">
    

    Note the additional forward slash. In this case although your page is /products/page1.html. The browser will try to load /css/styles.css

    You can also use the base tag to change the relative urls as in the first example.

    <base href="/assets/" />
    <link href="css/styles.css" rel="stylesheet">
    

    This time the browser will try to load /assets/css/styles.css

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!