内联块内的 CSS 垂直对齐

发布于 2024-12-28 13:28:41 字数 855 浏览 1 评论 0原文

我正在尝试使用 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>

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凯凯我们等你回来 2025-01-04 13:28:41

只需添加

  vertical-align: middle;

.icon 类即可。小提琴:http://jsfiddle.net/JGJtH/
(如 https://developer.mozilla.org/en/CSS/ 中所述Vertical-align 该属性默认值为基线)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文