【问题标题】:Why will the CSS file not be attached when I use absolute path?为什么我使用绝对路径时不会附加 CSS 文件?
【发布时间】:2018-02-18 07:21:25
【问题描述】:

这是我的代码:

<link href="<?= realpath(__DIR__ . '/..') ?>/css/styles-home.css" rel="stylesheet" type="text/css" />

结果是:

<link href="C:\xampp\htdocs\myweb\ltk/css/styles-home.css" rel="stylesheet" type="text/css" />

为什么那个 css 文件没有附加到 HTML 页面?我的意思是我在 HTML 页面中看不到样式。注意到路径是对的。自从我通过浏览器打开以下路径时:

file:///C:/xampp/htdocs/myweb/ltk/css/styles-home.css

我可以看到 CSS 代码。怎么了?

【问题讨论】:

  • 您是否在浏览器控制台中遇到任何错误。
  • href 属性包含 url,而不是文件路径。这不是一个有效的网址。
  • 您的结果 href 包含不同的斜杠(\ 和 /)
  • @Steve 好的,这有效吗? C:/xampp/htdocs/myweb/ltk/css/styles-home.css
  • 那么一个有效的 url 是 http://localhost/myweb/ltk/css/styles-home.css 或相对 url /myweb/ltk/css/styles-home.css

标签: php html css path routing


【解决方案1】:

如果我有一个 html 文件,其中包含一个带有 src 的图像元素,其文件路径与您上面的类似。

<img src="C:\foo\bar.jpg">

我通过 Google Chrome(一种网络浏览器)通过 http 访问该文件(这里我的本地网络服务器在文档根目录中有 html 文件)。浏览器将路径解析为:

http://localhost/C:/foo/bar.jpg

如果我在浏览器中打开文件(通过文件系统),它会尝试:

file:///var/www/C:/foo/bar.jpg

我的文档根是/var/www

您可以看到浏览器将此处的图像路径视为相对路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多