html - 粘性页脚弄乱了部分的高度

标签 html css

我正在尝试在具有固定高度的部分的网页中创建粘性页脚。当我尝试这个 trick ,我得到了粘性页脚,但这些部分没有显示它们的固定高度。我该如何解决?

Ps:当我将 min-height 更改为 height 时,它解决了部分高度的问题,但我没有看到粘性页脚。

html,body {
width:100%;
height:100%;
position:relative;
}

.wrapper {
position:relative;
width:100%;
min-height:100%;
margin-bottom:200px;
}

.section1{
width:100%;
height:100%;
background-color:grey;
}

.section2{
width:100%;
height:50%;
background-color:orange;
}

.wrapper:after {
  content: "";
  display: block;
}
.site-footer, .wrapper:after {
height:200px ;
}

.site-footer {
    background-color:red ;
}
<main class="wrapper">
<section class="section1">
<h2>header1</h2>
</section>
<section class="section2"><h2>header2</h2></section>
</main>
<footer class="site-footer">
<p>footer</p>
</footer>

最佳答案

您可以为部分使用“vh”值而不是百分比值(1vh = 视口(viewport)高度的 1%):

html,body {
width:100%;
height:100%;
position:relative;
}

.wrapper {
position:relative;
width:100%;
min-height:100%;
margin-bottom:200px;
}

.section1{
width:100%;
height:100vh;
background-color:grey;
}

.section2{
width:100%;
height:50vh;
background-color:orange;
}

.wrapper:after {
  content: "";
  display: block;
}
.site-footer, .wrapper:after {
height:200px ;
}

.site-footer {
    background-color:red ;
}
<main class="wrapper">
<section class="section1">
<h2>header1</h2>
</section>
<section class="section2"><h2>header2</h2></section>
</main>
<footer class="site-footer">
<p>footer</p>
</footer>

关于html - 粘性页脚弄乱了部分的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42636444/

相关文章:

html - 如何在Chrome开发者工具中查看范围输入的拇指样式?

html - 子 div 仅在右侧扩展和脱离父 div

twitter-bootstrap - 中屏幕的 Bootstrap 位置布局错误

html - 基于嵌套元素宽度的 CSS Flex 动态水平宽度

javascript - 在 svg 上右键单击菜单

javascript - Canvas Javascript 绘图功能不起作用

javascript - reveal.js:幻灯片编号的可见性

javascript - 通过ID获取元素时,如何获取该部分中的内容?

HTML 视频无法正常工作

php - 如何使内容适合表格大小