简体   繁体   English

内联块内的CSS垂直对齐

[英]CSS vertical alignment within inline-block

I'm trying to style some buttons with CSS and my button container is an inline-block with the text vertically centred within the block using line-height. 我正在尝试使用CSS设置一些按钮的样式,而我的按钮容器是一个内联块,文本使用line-height在块内垂直居中。 The problem is when I add an icon to the button using another inline-block it seems to adjust the vertical alignment of the preceding text although the icon is correctly aligned in the middle. 问题是当我使用另一个内联块向按钮添加图标时,它似乎调整了前面文本的垂直对齐,尽管图标在中间正确对齐。

<style>
.button {
    background: #ccc;
    display: inline-block;
    padding: 6px 6px 5px 6px;
    line-height: 16px;
    height: 16px;
    position: relative;
    vertical-align: text-bottom;
}
.button .icon {
    background-image: url("../images/button-down.png");
    display: inline-block;
    height: 16px;
    width: 16px;
    margin: 0;
    text-indent: -99999px;
    overflow: hidden;
    background-repeat: no-repeat;
    position: relative;
}
</style>

<!-- with icon -->
<a href="#" class="button">Save <span class="icon"></span></a>
<!-- without icon -->
<a href="#" class="button">Save</a>

When the icon is present the text is shifted down. 当图标出现时,文本向下移动。 I could do with some help understanding why the icon block effects the position of the preceding text. 我可以帮助理解为什么图标块会影响前面文本的位置。

Just add 只需添加

  vertical-align: middle;

to .icon class. .icon课。 Fiddle : http://jsfiddle.net/JGJtH/ . 小提琴: http//jsfiddle.net/JGJtH/
(As explained in https://developer.mozilla.org/en/CSS/vertical-align default value of this property is baseline) (如https://developer.mozilla.org/en/CSS/vertical-align中所述 ,此属性的默认值为基线)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM