详解八种方法实现CSS页面底部固定

2020-04-27 07:18:19易采站长站整理

min-height: calc(100vh - 200px); /* 这个200px是header和footer的高度 */
}
header,footer{
height: 100px;
line-height: 100px;
}

查看效果

方法六: 通过设置flexbox,将主体main设置为flex

html


<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS代码


body{
display: flex;
min-height: 100vh;
flex-direction: column;
}
main{
flex: 1
}

查看效果

方法七: 使用grid布局

Html代码


<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS代码


html {
height: 100%;
}
body {
min-height: 100%;
display: grid;
grid-template-rows: auto 1fr auto;
}
.footer {
grid-row-start: 3;
grid-row-end: 4;
}

查看效果

方法八: display-*

html


<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS


body {
min-height: 100%;
display: table;
width: 100%;
}
main {
display: table-row;
height: 100%;
}

查看效果