简体   繁体   中英

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. 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. Fiddle : http://jsfiddle.net/JGJtH/ .
(As explained in https://developer.mozilla.org/en/CSS/vertical-align default value of this property is baseline)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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