代码之家  ›  专栏  ›  技术社区  ›  bananaboy

我的CSS背景不会显示在github页面上

  •  -2
  • bananaboy  · 技术社区  · 7 年前

    我在GitHub上创建了一个页面,但是通过CSS编码的背景不会出现在我的页面上。这个 CSS 代码如下所示。

    header {
      background-image: url("resources/img/antelope.JPG");
      background-size: cover;
      background-position: center;
      height: 100vh;
    }
    

    我的github repo是: https://github.com/person/person.github.io

    CSS文件存储在此处:

    https://github.com/person/person.github.io/blob/master/resources/css/style.css

    如果能帮我解释一下为什么我的背景不会出现,我将不胜感激。

    2 回复  |  直到 5 年前
        1
  •  1
  •   blurfus    7 年前

    您的图像不在 resources 文件夹它与 index.html 页面-您的URL应为 url("antelope.JPG");

    更新

    查看您的更改,URL现在应如下所示:

    如果不执行任何操作,则URL应为: resources/img/antelope.jpg

    如果将资源打包到根文件夹中,则: ./img/antelope.jpg

    但是,最终的URL将取决于您如何构建页面,并注意服务器可能区分大小写。

    确保指定了正确的文件名(包括区分大小写的扩展名)

    类似于

    header {
      background-image: url(./img/antelope.jpg); /* may need to be adjusted depending on how you build your page */
      background-size: cover;
      background-position: center;
      height: 100vh;
    }
    <header>
      <nav>
        Some navigation here
      </nav>
    </header>
        2
  •  0
  •   Alex    7 年前

    使用根目录中的一个( https://github.com/anhthyngo/anhthyngo.github.io )使用下一个CSS:

    header {
        background-image: url('../../antelope.JPG');
        background-size: cover;
        background-position: center;
        height: 100vh;
    }
    

    从中使用 img 目录使用下一个CSS:

    header {
        background-image: url(../img/antelope.jpg);
        background-size: cover;
        background-position: center;
        height: 100vh;
    }
    

    enter image description here