给定 HTML:
<div id="title">Text</div>
<div class="circlecontainer">
<div class="circle">Hello</div>
</div>
和 CSS:
div {
display: inline-block;
}
.circlecontainer {
background: #E0E;
text-align:center;
line-height:200px;
width:200px;
height:200px;
}
.circle {
width:100px;
height:100px;
border-radius: 50px;
font-size:20px;
color:#fff;
background:#000
}
.circle:hover {
width:200px;
height:200px;
border-radius: 100px;
}
JSFiddle:http://jsfiddle.net/S329D/4/
我希望黑色圆圈在没有悬停时垂直居中。
但是当我设置 vertical-align:middle
时,文本看起来乱七八糟:
为什么会这样?
最佳答案
因为行高还是200px。试试这个:
div {
display: inline-block;
}
.circlecontainer {
background: #E0E;
text-align:center;
line-height:200px;
width:200px;
height:200px;
}
.circle {
line-height: 100px;
position: relative;
vertical-align: middle;
width:100px;
height:100px;
border-radius: 50px;
font-size:20px;
color:#fff;
background:#000
}
.circle:hover {
width:200px;
line-height: 200px;
height:200px;
border-radius: 100px;
}
关于css - 内联 block 的中心背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23591203/