在样式的书写中,我们常常使用以下方式实现垂直居中,若span元素内例外,解决办法看文章最后
text
.parent{
display:flex;
align-items:center
}
.parent{
height:64px;
}
.child{
height:64px;
line-height:64px;
}
.parent{
position:relative;
}
.child{
positon:absolute;
top:50%;
transform:translateY(-50%)
}
实际应用中发现,span内的元素,并不能垂直居中对齐,查了原因发现是因为默认按照底部基线对齐,问题如左图,正确的应该如右图:
.parent {
line-height: 64px;
height: 64px;
background-color: antiquewhite;
}
.child {
background-color: black;
line-height: 1;
vertical-align: text-top;
}
.parent {
height: 100%;
line-height: 1;
background-color: antiquewhite;
}
.child {
background-color: black;
vertical-align: text-top;
}
参与评论
手机查看
返回顶部