CSS让DIV上下左右居中的方法
示例: 一个父div(w:100%;h:400px)中有一个子div(w:100px;h:100px;)。让其上下左右居中。
1、使用varticle-align属性
理念:
利用表格单元格的居中属性。
步骤:
(1)父div外层配置一个div,同时设置为表格元素 (display: table),宽度为100%。
(2)父div配置为表格单元格元素 (display: table-cell)。
(3)父div配置居中属性(vertical-align: middle),使子div上下居中。
(4)子div通过margin配置左右居中(margin-left:auto; margin-right:auto)。
例子:
<style>* {margin: 0; padding: 0; box-sizing: border-box;}.table {display: table; width: 100%;}.father {display: table-cell; vertical-align: middle;}.son {margin: auto;}
</style>
<body><div class="table" ><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div></div>
</body>
备注:
(1)表格单元格比较特殊,如果只有一个单元格时,它的宽度默认会占父级(table|tr)宽度的100%。
(2)table默认宽度不会撑开,需要手动配置width:100%。
2、使用line-height属性
理念:
当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。通过文本居中属性text-align:center,可以使内部行内元素或行内块元素左右居中显示。
步骤:
(1)子div设定为行内块元素(display:inline-block)。
(2)父div设置行高(line-height)使子div上下居中。
(3)父div设置文本居中(text-align:center)使子div左右居中。
例子:
<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {line-height: 500px; text-align: center; font-size: 0;}.son { display: inline-block;/* display: inline-flex;display: inline-grid;display: inline-table; */}
</style>
<body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div>
</body>
备注:
行高如果设置为当前父div的高度(400px)的话,有固定高度的子div并不会居中显示的,问题出在浏览器默认将其当做文本居中的,即把它当做了一段文本(chrome默认font-size:16px;hight:21px)进行居中,没把它当做高度100px进行居中。所以需要对父div的line-height进行调整。以font-size:0(对应的字体高度为0)为例子,则需要line-height增加一个子div的高度(400px + 100px;)。
3、绝对定位
3.1 绝对定位(方式1)
理念:
利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。
步骤:
(1)父div标记下定位(position:relative|absolute|fixed)。
(2)子div绝对定位(position:absolute)。
(3)子div上下居中:top:50%;margin-top:-h/2 或是 bottom:50%;margin-bottom:-h/2。
(4)子div左右居中: left:50%;margin-left:-w/2 或是 right:50%;margin-right:-w/2。
例子:
<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {position: relative;}.son {position: absolute;bottom:50%;margin-bottom: -50px;left: 50%;margin-left: -50px;}
</style>
<body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div>
</body>
来自网友[盼先生]的建议:
针对方法三(绝对定位),还可以这样处理:
子元素:{position: absolute;
bottom:50%;
left: 50%;
transform: translate(-50% -50%);
}
这样处理的话,就无需关心子元素的宽高了。而且transform的兼容性还不错。
3.2 绝对定位(方式2)
理念:
定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置margin:auto会使它居中显示。
步骤:
父div标记下定位(position:relative|absolute|fixed|sticky)。
子div绝对定位(position:absolute)。
子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto。
子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto。
例子:
<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {position: relative;}.son {position: absolute; top: 0; bottom:0; left: 0; right: 0; margin: auto}
</style>
<body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div>
</body>
4、使用flex属性
理念:
弹性盒子中只要给弹性子元素设置 margin: auto; 可以使得弹性子元素上下左右居中。
例子:
<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {display: flex;}.son {margin: auto}
</style>
<body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div>
</body>
备注:
flex存在兼容性问题。
5、相对定位
理念:
利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。
步骤:
(1)父div标记下定位(position:relative|absolute|fixed)。
(2)子div相对定位(position:relative)。
(3)子div上下居中:top:50%;margin-top:-h/2 或是 bottom:-50%;margin-top:-h/2。
(4)子div左右居中: left:50%;margin-left:-w/2 或是 right:-50%;margin-left:-w/2。
例子:
<style>* {margin: 0; padding: 0; box-sizing: border-box;}.father {position: relative;}.son {position: relative; top:50%;margin-top:-50px;left:50%;margin-left:-50px}
</style>
<body><div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"><div class="son" style="width: 100px; height: 100px;background: palegreen;"></div></div>
</body>
备注:
(1)与绝对定位不同的是,定位属性相对的点是自身元素的左上角(所以bottom是负值,和方法三绝对定位写法有不一样的地方)。
(2)如果有同级元素,可能会受到影响。
(3)如果子div是浮动元素,也有居中效果。
结尾
方法二和方法三兼容性要比其它好些。最简单的是弹性盒子。
补充
margin赋值可以用2D转换代替。优点是不用计算子div的宽高。
margin-top: -h/2 => transform: translateY(-50%)
margin-bottom: -h/2 => transform: translateY(50%)
margin-left: -h/2 => transform: translateX(-50%)
margin-right: -h/2 => transform: translateX(50%)
原文作者:严俊东
原文地址:https://www.cnblogs.com/jundong/p/8969622.html
CSS让DIV上下左右居中的方法相关推荐
- 用CSS让DIV上下左右居中的方法
例如 一个父div(w:100%;h:400px)中有一个子div(w:100px;100px;).让其上下左右居中. 方法一(varticle-align) 理念 利用表格单元格的居中属性. 步骤 ...
- div内上下左右距离_用CSS让DIV上下左右居中的方法
当父div的行高等于自身高度时,内部的行内元素会上下居中显示.行内块没有固定高度时也会上下居中显示.所以需要对父div的 line-height 进行调整.利用定位属性(top.left.right. ...
- html中div不在火狐居中,Firefox嵌套CSS中div标签居中问题解决方法
本文和大家重点讨论一下Firefox嵌套CSS中div标签的居中问题的解决方法,主要包括使用line-height垂直居中,清除容器浮动,不让链接折行,始终让Firefox显示滚动条等内容. Fire ...
- 元素div 上下左右居中方法总结
元素div 上下左右居中方法总结 情景一:一个浏览器页面中,只有一个div模块,让其上下左右居中 解决方案: { position:fixed; left:0; right:0; top:0; b ...
- css文字在div中,[html][css]让文字在div中居中的方法[转]
转至:http://dreamweaver.abang.com/od/divcss/a/vertical-align.htm 一.行高(line-height)法 如果要垂直居中的只有一行或几个文字, ...
- html中div内容居中的方法
一.div内容 居中的方法: 方法1:table-cell div中的内容居中:不改变盒子尺寸. <!DOCTYPE html> <html lang="en"& ...
- html怎么上下左右分div分,超级简单div上下左右居中
下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. .juzhong { width:200px; height:200px; top:50% ...
- css:子元素div 上下左右居中方法总结
情景一:一个浏览器页面中,只有一个div模块,让其上下左右居中 解决方案: { position:fixed; left:0; right:0; top:0; bottom:0; margin:a ...
- css设置div上下左右均居中 、底部居中
css设置div或盒子居中 #垂直居中 #左右居中 #底部居中 类型一:固定宽度高度 html代码 <div class="login_container"><d ...
- 原生 js 让div上下左右居中
html 里写出一个div, css给div 定宽高和背景色: <div class="boxes"> </div> <style type=&quo ...
最新文章
- tensorflow LSTM
- Linux Kernel中的同步机制的介绍
- 如何在graphpad表示出正负误差_GraphPad Prism 8.0绘制误差连线并填充颜色图
- 我的Spring 之旅---Spring实战
- 访问者模式 php,php设计模式 Visitor 访问者模式
- vagrant网站中box下载方法
- linux+last命令菜鸟,Linux基本命令。。。菜鸟保留
- IO模型(epoll)--详解-03
- 重磅!2020中国高校毕业生月薪排名:清华第1,24高校过万,你呢?
- 一道面试题及其扩展,求好解法
- 设置指定打印机端口打印
- OpenCasCade拓扑几何(拉伸,扫略,旋转)
- PRINCE2和PMP体系架构有何区别
- 分享可用的谷歌学术(google scholar) hosts
- 618,拼多多玩起流量没阿里、京东啥事了
- python键盘上下左右控制_【322】python控制键盘鼠标:pynput
- opencv图像处理02-图像矩阵掩模操作
- C++ 域名转IP地址
- JavaScript - 四舍五入
- python多窗口传递信息_PyQT5 中两个界面之间数据传递