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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题