douya5331 2014-09-12 06:16
浏览 10
已采纳

PHP网站 - 在子目录中加载样式表

I'm building a PHP based site with this directory structure

index.php

css

  • style.css
  • bootstrap.css

includes

  • header.php
  • footer.php

bikes

  • road.php
  • mountain.php

The Problem

So I'm working on road.php and I obviously need to be able to link to both style.css and bootstrap.css, but when I declare at the start of road.php to include the header.php and footer.php it is like as if it cannot find the stylesheets and the site reverts back to the default 1990s look.

I have also found that any form of link on the page loads a 404. I'm only just starting out with PHP because I need some more power in my sites, but I just can't seem to get my head around the super basic things.

I just don't know what to do and I'm finding myself turning my back on the whole PHP language.

Thanking you in advance,

Stu :)

  • 写回答

1条回答 默认 最新

  • dtmu88600 2014-09-12 06:22
    关注

    I can't be certain without seeing the actual content of header.php (in perticular the part where you import the stylesheets), but it sounds like you are using a relative path to your stylesheets. Something like <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />. This works fine for index.php, but since the other pages are inside the subfolder bikes, they will be looking for the CSS files in yoursite.com/bikes/css.

    The solution is to provide an absolute path. Something like this:

    <link rel="stylesheet" type="text/css" href="http://yoursite.com/css/style.css" media="screen" />
    

    This way, it doesn't matter if the page is inside a subfolder (or a subfolder of a subfolder) - it will allways look for the CSS file in the right location.

    If you are using multiple domain names, or for some other reason you cannot hardcode the domain name, you can prepend a slash (/) to the path as well:

    <link rel="stylesheet" type="text/css" href="/css/style.css" media="screen" />
    

    This path is relative to the root of the website, not to the current directory.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图