【问题标题】:CSS internal links not relative to css file?CSS内部链接与css文件无关?
【发布时间】:2018-09-21 15:34:04
【问题描述】:

我一定遗漏了一些东西,css 内部文件链接是否与 css 文件相关?搜索并且到目前为止每个来源都说是,但我的文件似乎不想加载字体(fontawesome)。 index.html 文件正确加载并显示字体,因此我知道它正在工作;但是,加载 css 相关文件的 myfile.html 页面不起作用。为了使 myfile.html 工作,我必须更改 css 文件以加载相对于 myfile.html 页面(或者看起来如此)所以 ../fonts/ 但随后会破坏 index.html 页面。

由于事情的托管和完成方式,我必须保持一切相对,否则我只会使用/fonts/,但目前这不是一个选项。

为了保持简短,我只是列出了所列示例所需的 fontawesome 部分

可能遗漏了一些简单的东西,但我提取的所有参考资料都说它应该相对于 .css 文件加载。

这是文件夹/文件结构

- home
    - inc
            myfile.html
    - fontawesome
            fontawesome.min.css
            - fonts
    index.html

---- index.html ----

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="fontawesome/fontawesome.min.css" rel="stylesheet" type="text/css">
    <title>Home Page</title>
</head>
<body>
    <div><i class="fas fa-bars"></i> fun stuff here</div>
</body>
</html>

---- myfile.html ----

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="../fontawesome/fontawesome.min.css" rel="stylesheet" type="text/css">
    <title>myfile</title>
</head>
<body>
    <div><i class="fas fa-bars"></i> fun stuff here</div>
</body>
</html>

---- fontawesome.min.css ----

.fa,.fab,.fal,.far,.fas{
    -moz-osx-font-smoothing:grayscale;
    -webkit-font-smoothing:antialiased;
    display:inline-block;
    font-style:normal;
    font-variant:normal;
    text-rendering:auto;
    line-height:1
}
.fas.fa-pull-left{
    margin-right:.3em
}
.fa.fa-pull-right,.fab.fa-pull-right,.fal.fa-pull-right,.far.fa-pull-right,.fas.fa-pull-right{
    margin-left:.3em
}

@font-face{
    font-family:"Font Awesome 5 Free";
    font-style:normal;
    font-weight:400;
    src:url(fonts/fa-regular-400.eot);
    src:url(fonts/fa-regular-400.eot?#iefix) format("embedded-opentype"),
            url(fonts/fa-regular-400.woff2) format("woff2"),
            url(fonts/fa-regular-400.woff) format("woff"),
            url(fonts/fa-regular-400.ttf) format("truetype"),
            url(fonts/fa-regular-400.svg#fontawesome) format("svg")
}
@font-face{
    font-family:"Font Awesome 5 Free";
    font-style:normal;
    font-weight:900;
    src:url(fonts/fa-solid-900.eot);
    src:url(fonts/fa-solid-900.eot?#iefix) format("embedded-opentype"),
            url(fonts/fa-solid-900.woff2) format("woff2"),
            url(fonts/fa-solid-900.woff) format("woff"),
            url(fonts/fa-solid-900.ttf) format("truetype"),
            url(fonts/fa-solid-900.svg#fontawesome) format("svg")}

.fa,.far,.fas{
    font-family:"Font Awesome 5 Free"
}
.fa,.fas{
    font-weight:900
}

【问题讨论】:

  • 在浏览器中使用 devtools(网络)并检查每个请求的去向。
  • 这似乎是 Firefox 的问题(?),chrome 和 IE 工作正常。我用devtools加载它,网络是空的。

标签: html css relative-path


【解决方案1】:

此时我只是从臀部射击。 :-) 尝试编辑 fontawesome.min.css 中的路径。只需像这样将 ../ 添加到每个 URL:

@font-face{
    font-family:"Font Awesome 5 Free";
    font-style:normal;
    font-weight:400;
    src:url(../fonts/fa-regular-400.eot);
    src:url(../fonts/fa-regular-400.eot?#iefix) format("embedded-opentype"),
            url(../fonts/fa-regular-400.woff2) format("woff2"),
            url(../fonts/fa-regular-400.woff) format("woff"),
            url(../fonts/fa-regular-400.ttf) format("truetype"),
            url(../fonts/fa-regular-400.svg#fontawesome) format("svg")
}
@font-face{
    font-family:"Font Awesome 5 Free";
    font-style:normal;
    font-weight:900;
    src:url(../fonts/fa-solid-900.eot);
    src:url(../fonts/fa-solid-900.eot?#iefix) format("embedded-opentype"),
            url(../fonts/fa-solid-900.woff2) format("woff2"),
            url(../fonts/fa-solid-900.woff) format("woff"),
            url(../fonts/fa-solid-900.ttf) format("truetype"),
            url(../fonts/fa-solid-900.svg#fontawesome) format("svg")}

【讨论】:

  • 它在上述两种情况下都加载了字体,问题在于字体本身被加载到 CSS 中。它似乎不像它应该那样与 CSS 文件相关。双点 '../' 表示上一个目录,所以 '../fontawesome/fontawesome.min.css' 在上面的 'inc' 目录中实际上意味着 '/home/fontawesome/fontawesome.min.css'...你的链接实际上会寻找'/home/home/fontawesome/fontawesome.min.css'...如果我错了请纠正我:)
  • 我的错误,我想我误解了这个问题(并且应该排除领先的'..')。那么,您是在告诉我它正在加载 fontawesome.min.css,但无法显示您要使用的图标/字体?
  • 您能否提供相关页面的链接?
  • 是的,对于“inc”文件夹中的页面。主索引页面可以正常使用字体,只是当我通过子目录中的相对链接加载 fontawesome 时。目前它托管在本地服务器上:(
  • 那么,如果您将 myfile.html 移动到 home/ 目录,它会按预期加载字体吗?如果是这样,我猜它没有从子目录加载 fontawesome.min.css。
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 2020-08-31
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 2014-09-06
  • 1970-01-01
相关资源
最近更新 更多