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