-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 52 题:怎么让一个 div 水平垂直居中 #92
Comments
楼上总结的差不多了。再加一种 div.parent{
display:flex;
}
div.child{
margin:auto;
} |
div.parent {
display: table;
}
div.child {
display: table-cell
vertical-align: middle;
text-align: center;
} |
补充一个,使用 table-cell 的方式: .parent {
display: table-cell;
height: 200px;
width: 200px;
background-color: orange;
text-align: center;
vertical-align: middle;
}
.child {
display: inline-block;
width: 100px;
height: 100px;
background-color: blue;
} <div class="parent">
<div class="child"></div>
</div> |
水平垂直居中有好多种实现方式,主要就分为两类不定宽高和定宽高
element.style {
position: absolute;
left: 50%;
top: 50%;
margin-left: -250px;
margin-top: -250px;
width: 500px;
height: 500px;
background: yellow;
z-index: 1;
} 使用定位+transfrom element.style {
position: absolute;
left: 50%;
top: 50%;
width: 500px;
height: 500px;
background: yellow;
z-index: 1;
transform: translate3d(-50%,-50%,0);
}
element.style {
position: absolute;
left: 50%;
top: 50%;
background: yellow;
z-index: 1;
transform: translate3d(-50%,-50%,0);
} 还有一些其他的方法比如使用父容器使用flex,grid,table |
同理: |
需要再补充一个@cleverboy32 div.grandson {
display:inline-block;
width: 10px;
height: 10px;
} <div class="parent"><div class="child"><div class="grandson"></div></div></div> |
|
table-cell 是不支持设置 width: 100%; 想让 <div class="parent-fix">
<div class="parent">
<div class="child">DEMO</div>
</div>
</div> .parent-fix {
display: table;
width: 100%;
}
.parent {
display: table-cell;
text-align: center;
vertical-align: middle;
height: 200px;
background-color: #ccc;
}
.child {
display: inline-block;
background-color: #000;
line-height: 50px;
color: #fff;
} |
@zeroone001 在 parent 有宽高的时候, child 已经垂直居中了 |
不行 |
<div class="box">
<div class="center">
</div> .box {
width: 500px;
height: 500px;
text-align: center; /*center*/
}
.box::after {
content: '';
display: inline-block;
width: 0;
height: 100%;
vertical-align: middle;
}
.center {
display: inline-block;
width: 100px;
height: 100px;
vertical-align: middle;/*center*/
} |
// 1. line-height + text-algin
.parent {
height: 100px;
line-height: 100px;
text-align: center;
}
.child {
}
// 2. text-align + vertail-align
.parent {
text-align: center;
}
.child {
display: inline-block;
vertical-align: middle;
}
// 3. table-cell + vertail-align
.parent {
display: table-cell;
vertical-align: middle;
}
.child {
}
// 4. position + margin
.parent {
position: relative;
}
.child {
width: 100px;
height: 100px;
position: absolute;
left: calc(50% - 50px);
top: calc(50% - 50px);
}
// 5.position + transform
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
// 6. flex
.parent {
display: flex;
}
.child {
align-items: center;
justify-content: center;
} |
楼上大神们指出的很全了, 我指出一点: 前端小白,有错勿怪,感谢指正 |
方法2 只能水平居中,方法3导致div.parent宽度和div.parent宽度一样,且设置width样式无效 |
这个设置table-cell 的盒子会占满父盒子,里面的文字才能居中。 |
能说一下这种实现方式的原理吗 |
这个我前几天刚在张鑫旭的《CSS世界》里面第一次看到,原理就是(基于我个人的理解):设置inline-block和vertical-align:middle后,里面的元素会基于中间的文字准线居中对齐(学生时代的英语本子里面写字母,都是4条线形成三个空白区域,文字的对齐就是根据这几条线的)vertical-align更多信息可以看看张鑫旭博文; |
|
提个建议,可以在每一种实现上写一下思想和场景 |
margin是给自己加个外边框,是相对自身原来的位置来变化吧 |
学无止境~ div.child { |
div.parent{
display:grid;
place-items: center;
} |
在73题的IFC中有人提到了IFC的应用场景:垂直居中项应该就是这种方式的实现原理 |
1、使用绝对定位
2、不使用绝对定位,使用flex
3、使用table-cell,display为table-cell同时使用verticalAlign: middle会让元素垂直居中
|
The text was updated successfully, but these errors were encountered: