DIV居中方案
如有错误或者更好的方法,欢迎指正或建议。
DIV垂直居中方案
-
绝对定位+宽高不确定(top、transform)
/* <div>垂直居中</div> */ div{ position: absolute; top:50%; transform: translateY(-50%); } -
绝对定位+宽高确定(top、margin-top)
/* <div>垂直居中</div> */ div{ width:100px; height: 100px; background-color: hsla(10,100%,50%,0.5); position: absolute; top:50%; margin-top: -50px;/* 容器高度的1/2 */ text-align: center; line-height: 100px; } -
绝对定位+宽高确定(top、bottom、margin: auto)
/* <div>垂直居中</div> */ div{ width:100px; height: 100px; background-color: hsla(10,100%,50%,0.5); position: absolute; top:0; bottom: 0; margin: auto 0; /*或者margin: auto*/ /* margin 是复合属性 margin: auto => margin: auto auto auto auto margin:0 auto => margin: 0 auto 0 auto writing-mode: horizontal-tb; 和 direction: ltr; 的情况下 margin-top: auto; 和 margin-bottom: auto;,计算值为0 => margin: auto; 等同 margin: 0 auto;。 */ text-align: center; line-height: 100px; } -
box布局(box-align:center)
/* <div class="fa"> <div class="son">水平居中</div> </div> */ div.fa { height: 500px; width: 500px; /* Internet Explorer 10 */ display:-ms-flexbox; -ms-flex-align:center; /* Firefox */ display:-moz-box; -moz-box-align:center; /* Safari, Chrome, and Opera */ display:-webkit-box; -webkit-box-align:center; /* W3C */ display:box; box-align:center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
Flex布局 (align-items: center)
/* <div class="fa"> <div class="son">垂直居中</div> </div> */ div.fa { display: flex; -webkit-display: flex; height: 500px; width: 500px; align-items: center; -webkit-align-items: center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
Flex布局+margin(margin: auto 0)
/* <div class="fa"> <div class="son">垂直居中</div> </div> */ div.fa { display: flex; -webkit-display: flex; height: 500px; width: 500px; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; margin: auto 0; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
Grid布局(align-items: center)
/* <div class="fa"> <div class="son">垂直居中</div> </div> */ div.fa { height: 500px; width: 500px; display: grid; align-items: center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
table-cell布局 (vertical-align: middle)参考 display: table-cell知识点总结
/* <div class="fa"> <div class="son">垂直居中</div> </div> */ div.fa { height: 500px; width: 500px; display: table-cell; vertical-align: middle;/*主要作用于内联元素,亦可作用于table-cell*/ background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
绝对定位+calc() (calc() 函数动态计算)
/* <div class="fa"> <div class="son">垂直居中</div> </div> */ div.fa { position: relative; height: 70vh; width: 70vw; background-color: hsla(0, 100%, 50%, 0.5); } div.son { position: absolute; width: 10vw; height: 10vh; top: -moz-calc((70vh - 10vh)/2); top: -webkit-calc((70vh - 10vh)/2); top: calc((70vh - 10vh)/2); background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 10vh; }
DIV水平居中方案
-
绝对定位+宽高不确定(top、ltransform)
/* <div>水平居中</div> */ div{ position: absolute; left:50%; transform: translateX(-50%); } -
绝对定位+宽高确定(top、margin-top)
/* <div>水平居中</div> */ div{ width:100px; height: 100px; background-color: hsla(10,100%,50%,0.5); position: absolute; left:50%; margin-left: -50px;/* 容器宽度的1/2 */ text-align: center; line-height: 100px; } -
绝对定位+宽高确定(top、bottom、margin: auto)
/* <div>水平居中</div> \*/ div{ width:100px; height: 100px; background-color: hsla(10,100%,50%,0.5); position: absolute; left:0; right: 0; margin: 0 auto; /*或者margin: auto*/ /* margin 是复合属性 margin: auto => margin: auto auto auto auto margin:0 auto => margin: 0 auto 0 auto writing-mode: horizontal-tb; 和 direction: ltr; 的情况下 margin-top: auto; 和 margin-bottom: auto;,计算值为0 => margin: auto; 等同 margin: 0 auto;。 */ text-align: center; line-height: 100px; } -
box布局(box-pack:center)
/* <div class="fa"> <div class="son">水平居中</div> </div> */ div.fa { height: 500px; width: 500px; /* Internet Explorer 10 */ display:-ms-flexbox; -ms-flex-pack:center; /* Firefox */ display:-moz-box; -moz-box-pack:center; /* Safari, Chrome, and Opera */ display:-webkit-box; -webkit-box-pack:center; /* W3C */ display:box; box-pack:center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
Flex布局 (justify-content: center)
/* <div class="fa"> <div class="son">水平居中</div> </div> */ div.fa { display: flex; -webkit-display: flex; height: 500px; width: 500px; justify-content: center; -webkit-justify-content: center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
Flex布局+margin(margin: 0 auto)
/* <div class="fa"> <div class="son">水平居中</div> </div> */ div.fa { display: flex; -webkit-display: flex; height: 500px; width: 500px; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; margin: 0 auto; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
Grid布局(justify-items: center)
/* <div class="fa"> <div class="son">水平居中</div> </div> */ div.fa { height: 500px; width: 500px; display: grid; justify-items: center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
table-cell布局 (text-align: center)参考 display: table-cell知识点总结
/* <div class="fa"> <div class="son">水平居中</div> </div> */ div.fa { height: 500px; width: 500px; display: table-cell; text-align: center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; display: inline-block;/*必须为行内元素*/ background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
绝对定位+calc() (calc() 函数动态计算)
/* <div class="fa"> <div class="son">水平居中</div> </div> */ div.fa { position: relative; height: 70vh; width: 70vw; background-color: hsla(0, 100%, 50%, 0.5); } div.son { position: absolute; width: 10vw; height: 10vh; left: -moz-calc((70vw - 10vw)/2); left: -webkit-calc((70vw - 10vw)/2); left: calc((70vw - 10vw)/2); background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 10vh; }
DIV水平垂直居中方案
-
绝对定位+宽高不确定(top、ltransform)
/* <div>水平垂直居中</div> */ div{ position: absolute; left:50%; top:50%; transform: translate(-50%,-50%); } -
绝对定位+宽高确定(top、margin-top)
/* <div>水平垂直居中</div> */ div{ width:100px; height: 100px; background-color: hsla(10,100%,50%,0.5); position: absolute; left:50%; top:50%; margin-top: -50px;/* 容器高度的1/2 */ margin-left: -50px;/* 容器宽度的1/2 */ text-align: center; line-height: 100px; } -
绝对定位+宽高确定(top、bottom、margin: auto)
/* <div>水平垂直居中</div> \*/ div{ width:100px; height: 100px; background-color: hsla(10,100%,50%,0.5); position: absolute; left:0; right: 0; top:0; bottom:0; margin: auto; /* margin 是复合属性 margin: auto => margin: auto auto auto auto margin:0 auto => margin: 0 auto 0 auto writing-mode: horizontal-tb; 和 direction: ltr; 的情况下 margin-top: auto; 和 margin-bottom: auto;,计算值为0 => margin: auto; 等同 margin: 0 auto;。 */ text-align: center; line-height: 100px; } -
box布局(box-pack:center;box-align:center)
/* <div class="fa"> <div class="son">水平垂直居中</div> </div> */ div.fa { height: 500px; width: 500px; /* Internet Explorer 10 */ display:-ms-flexbox; -ms-flex-pack:center; -ms-flex-align:center; /* Firefox */ display:-moz-box; -moz-box-pack:center; -moz-box-align:center; /* Safari, Chrome, and Opera */ display:-webkit-box; -webkit-box-pack:center; -webkit-box-align:center; /* W3C */ display:box; box-pack:center; box-align:center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
Flex布局 (justify-content: center;align-items: center)
/* <div class="fa"> <div class="son">水平垂直居中</div> </div> */ div.fa { display: flex; -webkit-display: flex; height: 500px; width: 500px; align-items: center; -webkit-align-items: center; justify-content: center; -webkit-justify-content: center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
Flex布局+margin(margin: auto)
/* <div class="fa"> <div class="son">水平垂直居中</div> </div> */ div.fa { display: flex; -webkit-display: flex; height: 500px; width: 500px; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; margin: auto; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
Grid布局(justify-items: center;align-items: center)
/* <div class="fa"> <div class="son">水平垂直居中</div> </div> */ div.fa { height: 500px; width: 500px; display: grid; align-items: center; justify-items: center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
table-cell布局 (text-align: center)参考 display: table-cell知识点总结
/* <div class="fa"> <div class="son">水平垂直居中</div> </div> */ div.fa { height: 500px; width: 500px; display: table-cell; vertical-align: middle; text-align: center; background-color: hsla(0, 100%, 50%, 0.5); } div.son { width: 100px; height: 100px; display: inline-block;/*必须为行内元素才能水平居中*/ background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 100px; } -
绝对定位+calc() (calc() 函数动态计算)
/* <div class="fa"> <div class="son">水平垂直居中</div> </div> */ div.fa { position: relative; height: 70vh; width: 70vw; background-color: hsla(0, 100%, 50%, 0.5); } div.son { position: absolute; width: 10vw; height: 10vh; top: -moz-calc((70vh - 10vh)/2); top: -webkit-calc((70vh - 10vh)/2); top: calc((70vh - 10vh)/2); left: -moz-calc((70vw - 10vw)/2); left: -webkit-calc((70vw - 10vw)/2); left: calc((70vw - 10vw)/2); background-color: hsla(20, 100%, 50%, 0.5); text-align: center; line-height: 10vh; }
参考
https://blog.youkuaiyun.com/qq_31947477/article/details/107053142
https://www.jianshu.com/p/52b0c2a7ea29
本文详细列举了多种使用CSS实现元素垂直居中、水平居中以及水平垂直居中的方法,包括绝对定位、Flex布局、Grid布局、Table-cell布局以及calc()函数动态计算等。这些方案覆盖了不同场景下的布局需求,适用于现代浏览器。
478

被折叠的 条评论
为什么被折叠?



