美文网首页
Sticky footer 页面底部

Sticky footer 页面底部

作者: mipaifu328 | 来源:发表于2018-07-12 16:19 被阅读0次

Sticky footer:页面内容不够长的时候,页脚块粘贴在视窗底部;如果内容足够长时,页脚块会被内容向下推送。

sticky-footer

实现的方式主要有以下几种:

1.Flexbox解决方案

核心代码:

<body>
  <div class="main">
    content
  </div>
  <div class="footer">footer</div>
 </body>
body{   
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.main{
  flex: 1;
}
.footer{
  height:50px;
}

100vh(相对于视口的高度。视口被均分为100单位的vh)这里就不需要设置html{height:100%;}

2.calc()解决方案

核心代码:

<body>
  <div class="main">
    content
  </div>
  <div class="footer">footer</div>
 </body>
.main{
  min-height: calc(100vh - 50px - 1em);  /* 1em为元素之间的间距 */
}
.footer{
  background: #21BAA8;
}

这个方法需要注意减去元素之间的边距。

3.absolute解决方案

<body>
  <div class="main">
    content
  </div>
  <div class="footer">footer</div>
 </body>
body{
  margin:0;
  position: relative;
  min-height: 100vh;
  padding-bottom: 50px;
  box-sizing: border-box;
}
.footer{
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 50px;
}

4.负margin解决方案

这个方法需要增加额外的html元素。

<body>
  <div class="main">
    content
    <div class="push"></div>
  </div>
  <div class="footer">footer</div>
 </body>
.main{
  min-height: 100%;
}
.push{
  height: 50px;
}
.footer{
  margin-top: -50px;
  height: 50px;
}

这里也可以设置.mian{margin-bottom: -50px;},思路一样。

5.Grid解决方案

<body>
  <div class="main">
    content
  </div>
  <div class="footer">footer</div>
 </body>
body{   
  min-height: 100vh;
  display: grid;
  grid-template-rows: 1fr auto;
}
.footer{
  grid-row-start: 2;
  grid-row-end: 3;
}

参考:

相关文章

  • Sticky footer 页面底部

    Sticky footer:页面内容不够长的时候,页脚块粘贴在视窗底部;如果内容足够长时,页脚块会被内容向下推送。...

  • CSS方案总结

    一、Sticky Footer效果 无论页面内容高度如何变化,footer始终在页面底部的固定位置 二、垂直居中 ...

  • flex常用布局

    Sticky Footer 当页面内容高度小于可视区域高度时,footer 吸附在底部;当页面内容高度大于可视区域...

  • web前端-Sticky Footer布局

    什么是Sticky Footer布局 Sticky Footer布局实现的效果是, 当页面中的内容高度小于屏幕高度...

  • Sticky footer布局

    Sticky Footer布局概括如下:如果页面内容不够长的时候,页脚粘贴在视窗底部;如果内容足够长,页脚会被内容...

  • sticky-footer布局的方法

    一、什么是sticky-footer布局?sticky-footer布局是一种经典的布局效果,可概况如下:如果页面...

  • css实现Sticky Footer

    所谓 “Sticky Footer”,指的就是一种网页效果: 如果页面内容不足够长时,页脚固定在浏览器窗口的底部;...

  • css sticky footer经典布局

    什么是sticky footer布局?一般指手机页面中,当内容高度撑不满一屏时,页脚紧贴屏幕底部;当内容高度超过一...

  • CSS处理常见问题文章收集

    1、布局类 Sticky Footer : 完美的绝对底部https://aotu.io/notes/2017/0...

  • 【译】CSS五种方式实现Footer置底

    页脚置底(Sticky footer)就是让网页的footer部分始终在浏览器窗口的底部。 当网页内容足够长以至超...

网友评论

      本文标题:Sticky footer 页面底部

      本文链接:https://www.haomeiwen.com/subject/upxrpftx.html