gpt4 book ai didi

css - 垂直居中跨度

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:28 24 4
gpt4 key购买 nike

我有这段代码:

a {
background: #A9A9A9;
display: block;
position: absolute;
right: 0;
top: 0;
height: 100%;
width: 20%;
vertical-align: middle;
float: right;
text-align: center;
}
a span {
display: inline-block;
vertical-align: middle;
}
<a href="#"><span>Center</span></a>

我想将 span 垂直居中到 a 中,他位于 position: absolute;

最佳答案

使用position:relative进行跨越并给予top:50%

a {
background: #A9A9A9;
display: block;
position: absolute;
right: 0;
top: 0;
height: 100%;
width: 20%;
vertical-align: middle;
float: right;
text-align: center;
}

a span {
display: inline-block;
position: relative;
top: 50%;
transform: translate(0px, -50%);
}
<a href="#"><span>Center</span></a>

解决方案 2:

使用display:tabledisplay:table-cell

a {
background: #A9A9A9;
display: block;
position: absolute;
right: 0;
top: 0;
height: 100%;
width: 20%;
vertical-align: middle;
float: right;
text-align: center;
display:table;
}
a span {
display: table-cell;
vertical-align: middle;
}
<a href="#"><span>Center</span></a>

关于css - 垂直居中跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35288480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com