html - 固定背景和 div 从页面下方开始

标签 html css

我想创建一个简单的 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>

这里的问题是我在“查看更多”按钮下方添加的 html 标签将“查看更多”按钮向上推,而不是停留在其下方

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

最佳答案

对于page-body,使用top: 100% & position:absolute。另外,删除之前的 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 {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  top: 100%;
}

.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>

关于html - 固定背景和 div 从页面下方开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50828331/

相关文章:

html - 切换背景图片 - Angular 2

html - 不要在悬停时设置父元素的样式

html - 在 CSS 中以不同文本大小居中 HTML 正文元素

html - 仅使用 CSS 在悬停 Div B 时更改 Div A 的背景图像

javascript - 防止拖动不可拖动元素时出现重影?

html - float 权利不起作用?

javascript - 在 JS 中交换 id - 这只能使用一次。为什么?

css - 继承元素背景的反渐变

html - 将HTML元素(固定)放置在另一个元素的顶部,该元素不重叠,并且在窗口调整大小时具有恒定的间距

javascript - XHTML 模式下 MathOverflow 的宝贵天赋