css - 内联 block 的中心背景

标签 css

给定 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 时,文本看起来乱七八糟:

http://jsfiddle.net/S329D/5/

为什么会这样?

最佳答案

因为行高还是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;
}

http://jsfiddle.net/S329D/7/

关于css - 内联 block 的中心背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23591203/

相关文章:

html - 如何通过内联CSS改变颜色

html - 按内容自动增长表格单元格元素

javascript - HTML5 视频时长

html - 使用 HTML 表格是否比使用 divs/CSS 的代码效率高一倍?

html - CSS 选择器 : Is there any way to select the last element which has the same class name in the whole html?

javascript - 如何使用一个按钮 Javascript 在两个图像之间切换

html - <td> 中的 DIV 向右浮动

javascript - 使用 CSS 或 JavaScript 水平对齐表格行

html - 登录模式框的模糊背景

javascript - 如何通过鼠标点击和拖动 Javascript 来滚动图像