1

我刚刚开始学习 HTML 和 CSS(以及一点 Bootstrap),我正在尝试制作一个简单的滚动网页。我的问题是我无法让 div 背景图像覆盖页面的整个高度——它只覆盖 h2 文本,就像它认为该元素的结尾是页面的结尾一样。我不知道它为什么会这样,而且我无法修复它。这真的难倒我,我敢肯定它有一个我错过的非常简单的解决方案。任何建议将不胜感激。

这是代码笔

body {
  background-color: rgb(255, 255, 255);
}

#front {
  background: url("https://image.ibb.co/mjTmwv/port34.png") no-repeat;
  overflow: hidden;
  background-size: cover;
  width: 100%;
  height: 100%;
}

#h1 {
  color: white;
  font-size: 50px;
  margin-top: 40vh;
  padding: 10px;
  background: rgba(43, 142, 255, 0.4);
}

#pt {
  color: white;
  font-size: 30px;
  background: rgba(39, 220, 130, 0.4);
}
<ul style="z-index:9;">
  <li>
    <a class="active" href="#"><img class="img-fluid" style="width:auto;height:auto;max-height:26px;max-width:20px;" src="https://image.ibb.co/mg1s3a/lyreimage27.png" alt="website icon"></img> placeholder inc.</a>
  </li>
  <li><a href="#">About
      </a>
  </li>
  <li><a style="text-decoration:none" href="#">Portfolio</a>
  </li>
  <li><a href="#">Contact</a>
  </li>
</ul>

<header>
  <div id="front">
    <div class="container-fluid">
      <h1 id="h1" class="text-center">Placeholder</h1>
      <h2 id="pt" class="text-center">Placeholder Text</h2>
</header>

4

4 回答 4

2

您的#frontdiv 没有足够的内容显示在整个页面上。您应该有足够的内容来覆盖整个页面或background-imagebody元素设置。否则改变你的#frontCSS如下。

#front {
  background: url("https://image.ibb.co/mjTmwv/port34.png") no-repeat;
  overflow: hidden;
  background-size: cover;
  width: 100%;
  height: 100vh;
}

body
    {
     background-color: rgb(255, 255, 255);
    }

#front {
    background:       url("https://image.ibb.co/mjTmwv/port34.png") no-repeat;
  overflow: hidden;
  background-size: cover;
  width: 100%;
  height: 100vh;
}

#h1 {
  color: white;
  font-size: 50px;
  margin-top: 40vh;
  padding: 10px;
  background: rgba(43, 142, 255, 0.4);
}

#pt {
  color: white;
  font-size: 30px;
  background: rgba(39, 220, 130, 0.4);
}
<ul style="z-index:9;">
  <li>
    <a class="active" href="#"><img class="img-fluid" style="width:auto;height:auto;max-height:26px;max-width:20px;" src="https://image.ibb.co/mg1s3a/lyreimage27.png" alt="website icon"></img> placeholder inc.</a>
  </li>
  <li><a href="#">About
      </a>
  </li>
  <li><a style="text-decoration:none" href="#">Portfolio</a>
  </li>
  <li><a href="#">Contact</a>
  </li>
</ul>

<header>
  <div id="front">
    <div class="container-fluid">
      <h1 id="h1" class="text-center">Placeholder</h1>
      <h2 id="pt" class="text-center">Placeholder Text</h2>
</header>

于 2017-09-12T17:09:22.833 回答
2

我稍微清理了您的标记(尽可能不要使用内联样式)并认为我达到了您正在寻找的效果。

#front {
  background: url("https://image.ibb.co/mjTmwv/port34.png") no-repeat;
  overflow: hidden;
  background-size: cover;
  width: 100%;
  min-height: 100vh;
}

.text-center {
  text-align: center;
}

h1 {
  color: white;
  font-size: 50px;
  margin-top: 40vh;
  padding: 10px;
  background: rgba(43, 142, 255, 0.4);
}

h2 {
  color: white;
  font-size: 30px;
  background: rgba(39, 220, 130, 0.4);
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: rgba(0, 0, 0, 0.7);
  position: fixed;
  top: 0;
  width: 100%;
  font-size: 16px;
  z-index: 9;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover:not(.active) {
  color: white;
  background-color: rgba(39, 220, 130, 0.4);
  transition: 0.5s;
  text-decoration: none;
}

.active {
  color: white;
  background-color: rgba(43, 142, 255, 0.4);
}

.active:hover {
  color: white;
  text-decoration: none;
}

.active img {
  width:auto;
  height:auto;
  max-height:26px;
  max-width:20px;
}
<ul>
  <li><a class="active" href="#"><img class="img-fluid" src="https://image.ibb.co/mg1s3a/lyreimage27.png" alt="website icon" />placeholder inc.</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Portfolio</a></li>
  <li><a href="#">Contact</a>
  </li>
</ul>

<header id="front">
  <div>
    <div class="container-fluid text-center">
      <h1>Placeholder</h1>
      <h2>Placeholder Text</h2> 
    </div>
  </div>
</header>

于 2017-09-12T17:11:54.147 回答
1

将标题元素的高度设置为 100vh;

所以在你的css中添加:

header {
height: 100vh;
}
于 2017-09-12T17:24:52.633 回答
0

#front是 的 100% 的大小header。尝试将标题设置为height: 100%. 或者(更好的解决方案),如果您希望它覆盖整个页面,请在正文上设置背景。这是更简单的选择,更有意义。

于 2017-09-12T17:11:12.720 回答