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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里