【问题标题】:CSS vertical alignment within inline-block内联块内的 CSS 垂直对齐
【发布时间】:2012-02-16 15:19:13
【问题描述】:

我正在尝试使用 CSS 设置一些按钮的样式,并且我的按钮容器是一个内联块,其中文本使用行高在块内垂直居中。问题是当我使用另一个内联块向按钮添加图标时,它似乎调整了前面文本的垂直对齐方式,尽管图标在中间正确对齐。

<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>

当图标出现时,文本会向下移动。我可以帮助我理解为什么图标块会影响前面文本的位置。

【问题讨论】:

    标签: html css markup


    【解决方案1】:

    只需添加

      vertical-align: middle;
    

    到.icon 班级。小提琴:http://jsfiddle.net/JGJtH/。
    (如https://developer.mozilla.org/en/CSS/vertical-align 中解释的,此属性的默认值为baseline)

    【讨论】:

      猜你喜欢
      • 2012-03-29
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      相关资源
      最近更新 更多