方法一:全局增加一个负值下边距等于底部高度

<style>
html, body {
height: 100%;
margin: 0;
}
.content {
padding: 20px;
min-height: 100%;
margin: 0 auto -50px;
}
.footer,.push {
height: 50px;
}
</style>
<div class="content">
<h1>Sticky Footer with Negative Margin 1</h1>
<p><button id="add">Add Content</button></p>
<div class="push"></div>
</div>
<footer class="footer">Footer </footer>

方法二:底部元素增加负值上边距

<style>
html, body {
height: 100%;
margin: 0;
}
.content {
min-height: 100%;
}
.content-inside {
padding: 20px;
padding-bottom: 50px;
}
.footer {
height: 50px;
margin-top: -50px;
}
body {
font: 16px Sans-Serif;
}
h1 {
margin: 0 0 20px 0;
}
p {
margin: 20px 0 0 0;
}
footer {
background: #42A5F5;
color: white;
line-height: 50px;
padding: 0 20px;
}
</style>
<div class="content">
<div class="content-inside">
<h1>Sticky Footer with Negative Margin 2</h1>
<p><button id="add">Add Content</button></p>
</div>
</div>
<footer class="footer"> Footer </footer>

方法三:使用calc()计算内容的高度

<style>
.content {
min-height: calc(100vh - 70px);
padding: 40px 40px 0 40px;
}
.footer {
height: 50px;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
font: 16px Sans-Serif;
}
h1 {
margin: 0 0 20px 0;
}
p {
margin: 0 0 20px 0;
}
footer {
background: #42A5F5;
color: white;
line-height: 50px;
padding: 0 20px;
}
</style>
<div class="content">
<h1>Sticky Footer with calc()</h1>
<p><button id="add">Add Content</button></p>
</div>
<footer class="footer"> Footer </footer>

方法四:使用flexbox

<style>
html {
height: 100%;
}
body {
min-height: 100%;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
padding: 20px;
}
.footer {
padding: 20px;
}
</style>
<div class="content">
<h1>Sticky Footer with Flexbox</h1>
<p><button id="add">Add Content</button></p>
</div> <footer class="footer">Footer </footer>

方法五:使用grid布局

<style>
html {
height: 100%;
}
body {
min-height: 100%;
display: grid;
grid-template-rows: 1fr auto;
}
.content {
padding: 20px;
}
.footer {
grid-row-start: 2;
grid-row-end: 3;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
font: 16px Sans-Serif;
}
h1 {
margin: 0 0 20px 0;
}
p {
margin: 0 0 20px 0;
}
.footer {
background: #42A5F5;
color: white;
padding: 20px;
}
</style>
<div class="content">
<h1>Sticky Footer with Grid</h1>
<p><button id="add">Add Content</button></p>
</div>
<footer class="footer">Footer</footer>

本文转载于:猿2048→https://www.mk2048.com/blog/blog.php?id=hji2iaj0cbb

div 底部固定方法(不用position定位)的更多相关文章

  1. 保持在Div 底部的方法

    <!DOCTYPE> <html> <head> <meta http-equiv="content-type" content=&quo ...

  2. 一个宽度不确定的DIV里放三个水平对齐的DIV,左右两个DIV宽度固定为100px,中间那个DIV自适应宽度

    方法一:浮动  注意三个div的位置 <html><head> <meta charset="utf-8"> <style type=&q ...

  3. 盒子模型&position定位

    有时候深深的感觉语文这门课程其实很有用, 至少以前学的时候没有感觉到 直到现在阅读大量的别人的资料文章的时候或者是看一些题目....... 总之:认真阅读小心品味 当然,前面的孤言自语和本文无关,只是 ...

  4. 微信小程序之顶部固定和底部固定

    顶部固定 <view style="position:fixed;top:0;"> ...... </view> 底部固定 <view style=& ...

  5. <转载>DIV+CSS position定位方法总结

    如何学习DIV+CSS布局之position属性 如果用position来布局页面,父级元素的position属性必须为relative,而定位于父级内部某个位置的元素,最好用 absolute. 任 ...

  6. 将HTML页面页脚固定在页面底部(多种方法实现)

    当一个HTML页面中含有较少的内容时,Web页面的footer部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,接下来为大家介绍下如何将页脚固定在页面底部,感兴趣的朋友可以了解下 作为一个 ...

  7. div footer标签css实现位于页面底部固定

    Web页面的“footer”部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,让你的页面看上去很不好看,特别是现在宽屏越来越多,这种现象更是常见,本文将介绍两种解决方案,需要了解的朋友可以 ...

  8. 设置一个DIV块固定在屏幕中央(两种方法)

    设置一个DIV块固定在屏幕中央(两种方法) 方法一: 对一个div进行以下设置即可实现居中. <style> #a{ position: fixed; top: 0px; left: 0p ...

  9. 实现三个div,固定左右两边的div宽为200,中间的div宽度自适应的四种方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

随机推荐

  1. WIN10:字体文件路径

    比如编程时要用到Arial字体,Arial的字体绝对路径为 C:\Windows\Fonts\Arial.ttf 注意字体文件的后缀都是ttf

  2. k8s dashboard 安装和证书更新

    1.k8s 搭建   参见https://blog.51cto.com/lizhenliang/2325770 [root@VM_0_48_centos ~]# kubectl get cs NAME ...

  3. MySql日常解决错误

    MySql数据库导入sql错误 Unknown collation: 'utf8mb4_0900_ai_ci 导入语句:mysql -u root -p database < E:/SS/Tes ...

  4. django程序在windows服务器上发布

    django程序在windows服务器上发布 参考文献:https://www.cnblogs.com/djangocn/p/10227006.html 1.安装 IIS 和 GCI 打开服务器管理器 ...

  5. Dapr 弹性的策略

    云原生应用需要处理 云中很容易出现瞬时故障.原因在以下文档 暂时性故障处理[1] 中有具体说明. 任何环境.任何平台或操作系统以及任何类型的应用程序都会发生暂时性故障. 在本地基础结构上运行的解决方案 ...

  6. 微信小程序两点之间的距离

    1:申请key: https://lbs.qq.com/dev/console/application/mine 网址: https://note.youdao.com/ynoteshare/inde ...

  7. js数组和对象的区别,ajax传入多个参数值,ajax传多个数组数据

    数组分为索引数组和关联数组 js中先声明一个空数组 arr = [] 索引数组 索引是整数,如arr[0] = 'a' 关联数组 索引是自定义的字符串,如arr['a'] = 'a' js中的对象 你 ...

  8. tp5怎么防sql注入 xss跨站脚本攻击

    在 application/config.php 中有个配置选项 框架默认没有设置任何过滤规则,你可以是配置文件中设置全局的过滤规则 则会调用这些函数 自动过滤 // 默认全局过滤方法 用逗号分隔多个 ...

  9. OSPF协议原理及配置2-理解邻居和邻接关系

    OSPF是一个动态路由协议,运行OSPF的路由器之间需要交换链路状态信息和路由信息,在交换这些信息之前首先需要建立邻接关系.邻接关系用来交换链路状态及路由信息. 注意:并非所有的邻居关系都可以成为邻接 ...

  10. 1.1 STL基本概念

    文章目录 1 STL概述 1.1 STL基本概念 1.2 STL 六大组件 1.3 STL优点 2.1 容器 2.2 算法 2.3 迭代器 2.4 示例 1 STL概述 STL是StandardTem ...