【问题标题】:Jekyll _post not using the css when the site is built into the _siteJekyll _post 在站点内置到 _site 时不使用 css
【发布时间】:2020-06-05 09:15:40
【问题描述】:

我在 _post 中的帖子在使用:bundle exec jekyll serve 时应用了 css。当我去构建 jekyll 站点时: bundle exec jekyll build; _post 中的帖子不再应用 css,即使在构建站点的 html 源代码中,它们具有相同的 <link rel="stylesheet" href="/assets/css/styles.css">

这里是文章_layout:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900" 
rel="stylesheet">
    <title>{{ page.title }}</title>
    <link rel="stylesheet" href="/assets/css/styles.css">
    <link rel="shortcut icon" type="svg/svg" href="assets/svg/users-dmitri13.svg">
  </head>
  <body>
   {% include navigation.html %}
   {{ content }}
   {% include footer--articles.html %}
  </body>

如果我去掉&lt;link rel="stylesheet" href="/assets/css/styles.css"&gt; 中的开头/。 css 会显示在构建的_site 的文章页面上,但随后帖子将无法找到 css。

帖子_layout:

---
layout: articles
---


<article class="article">
  <div class="header--large header--large--gradient">
      <div class="heading-primary--main-static header__text-box">
          <h1>{{ page.title }}</h1>
      </div>
  </div>

  <div class="article__content">
      <p>{{ page.date | date_to_string }} - {{ page.author }}</p>
      <div class="paragraph--big">
          {{ content }}
      </div>
  </div>

【问题讨论】:

    标签: html css jekyll


    【解决方案1】:

    您应该使用link 标签。 Jekyll 会在 _site 文件夹中为你生成正确的输出 URL。

    &lt;link rel="stylesheet" href="{% link /assets/css/styles.css %}"&gt;

    Docs for the link tag.

    【讨论】:

    • 在使用 &lt;link rel="stylesheet" href="{% link /assets/css/styles.css %}"&gt; 时,我现在从终端收到此错误:Jekyll Feed: Generating feed for posts Liquid Exception: Could not find document '/assets/css/styles.css' in tag 'link'. Make sure the document exists and the path is correct. in /_layouts/articles.html Error: Could not find document '/assets/css/styles.css' in tag 'link'. Make sure the document exists and the path is correct. Error: Run jekyll build --trace for more information. 此外,它没有解决原始问题。
    • 错误是说您的 CSS 文件的路径需要修复。使用存储库中的文件路径,而不是_site 文件夹中的路径。输出应该是&lt;link rel="stylesheet" href="{% link the/path/to/styles.css %}"&gt;
    【解决方案2】:

    当存放所有文件的文件夹是根目录时,&lt;link rel="stylesheet" href="/assets/css/styles.css"&gt; 起作用。

    使用bundle exec jekyll serve 或在网络服务器上托管会生成包含所有文件的文件夹,即根目录。 /assets/css/styles.css 中的开头 / 告诉 &lt;link rel="stylesheet" href="/assets/css/styles.css"&gt; 开始查找文件夹的根目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 2021-07-05
      • 2016-07-31
      相关资源
      最近更新 更多