Phill
Phill

Reputation: 1051

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.

Upvotes: 3

Views: 6422

Answers (1)

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123367

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)

Upvotes: 6

Related Questions