修复了从页面下方开始的背景和div

时间:2018-06-13 02:35:40

标签: html css css3

我想创建一个简单的CSS样式但似乎很难实现

我想要实现的是将背景图像固定以适合整个页面,并且我希望网站的内容在"下面"图像,这意味着当用户访问页面时他只会看到背景,向下滚动内容后会显示在下面

我试过这个



.content {
  background-image: url('../img/header-bg.jpg');
  background-position: center;
  background-size: cover;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 100%;
}

.content-title {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fed136;
}

.page-body::before {
  position: relative;
}

.page-body {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}

.view-more {
  width: 100%;
  height: 125px;
  border: none;
  background: rgba(0, 0, 0, .3);
}

<div class="content">
  <div class="content-title">
    <h1>Hello World</h1>
  </div>
</div>

<div class="page-body">
  <button class="view-more">View More</button>
  <div class="page-holder">
    <button>Click Me</button>
  </div>
</div>
&#13;
&#13;
&#13;

这里的问题是我在&#34; view-more&#34;下面添加了html标签。按钮按下&#34;查看更多&#34;按钮向上而不是停留在它下面

我怎样才能达到这样的效果?

2 个答案:

答案 0 :(得分:0)

对于page-body,请使用top: 100%&amp; position:absolute。另外,删除之前的css

&#13;
&#13;
.content {
  background-image: url('../img/header-bg.jpg');
  background-position: center;
  background-size: cover;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 100%;
}

.content-title {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fed136;
}

.page-body {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  top: 100%;
}

.view-more {
  width: 100%;
  height: 125px;
  border: none;
  background: rgba(0, 0, 0, .3);
}
&#13;
<div class="content">
  <div class="content-title">
    <h1>Hello World</h1>
  </div>
</div>

<div class="page-body">
  <button class="view-more">View More</button>
  <div class="page-holder">
    <button>Click Me</button>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我改变了你的CSS,因为我需要从头开始,但我认为这是你想要完成的。我使用z-index来指定哪些元素位于顶部。我还添加了.page-body {top:100%;像@nikhil提到的那样。

<div class="content">
  <div class="content-title">
    <h1>Hello World</h1>
  </div>
</div>

<div class="page-body">
  <button class="view-more">View More</button>
  <div class="page-holder">
    <button>Click Me</button>
  </div>
</div>

CSS

.content {
  background: rgba(0,0,0,0.4);
  background-position: center;
  background-size: cover;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
}

.content-title {
  display: table;
  z-index: 1000;
  width: 100%;
  height: 100%;
  color: yellow;
}

h1 {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

.page-body {
  display: block;
  position: absolute;
  text-align: center;
  top: 100%;
}