在带有背景图像的元素上使用垂直对齐

发布于 2024-09-01 10:46:04 字数 575 浏览 1 评论 0原文

<style type='text/css'>
#span1{
background-image:url("http://www.reoiv.com/images/rss.jpg");
background-repeat:no-repeat;
cursor:pointer;
display:block;
float:left;
height:15px;
width:15px;
vertical-align:text-bottom;
}
</style>

<span id='span1'></span>觀看次數

我想做的是实现垂直对齐:文本底部效果,但我不是在图像元素上执行此操作。我正在设置背景图像的元素上执行此操作。 如果您将上述代码粘贴到此处: http://htmledit.squarefree.com/

您将看到文本无法垂直对齐到底部。

我想知道如果可能的话,如何在不添加额外的 html 元素的情况下完成它。

非常感谢大家。

<style type='text/css'>
#span1{
background-image:url("http://www.reoiv.com/images/rss.jpg");
background-repeat:no-repeat;
cursor:pointer;
display:block;
float:left;
height:15px;
width:15px;
vertical-align:text-bottom;
}
</style>

<span id='span1'></span>觀看次數

What I would like to do is to achieve the vertical align: text-bottom effect but I am not doing it on a image element. I am doing it on an element with background-image set.
If you paste the above codes here: http://htmledit.squarefree.com/

You will see that the text failed to vertically align to bottom.

I would like to know how it can be done without adding extra html element if possible.

Many thanks to you all.

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

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

发布评论

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

评论(3

故事和酒 2024-09-08 10:46:04

您在文本之前关闭了 span 标记:

<span id='span1'></span>觀看次數

使用此:

<span id='span1'>觀看次數</span>

希望有所帮助。

You closed the span tag before your text:

<span id='span1'></span>觀看次數

Use this:

<span id='span1'>觀看次數</span>

Hope that helps.

眼睛会笑 2024-09-08 10:46:04

你不能用浮动来做到这一点。您可以尝试使用 display: inline-block,但浏览器支持很粗略。

编辑: http://www.quirksmode.org/css/display.html

You can't do that with floats. You could try using display: inline-block, but browser support is sketchy.

EDIT: http://www.quirksmode.org/css/display.html

稚然 2024-09-08 10:46:04

您无法获得像素完美的结果。这是我一直在使用的解决方法:

<span style="background: url(http://www.reoiv.com/images/rss.jpg) no-repeat left center; padding-left: 20px;">觀看次數</span>

这可以使图标与文本很好地垂直居中对齐。 padding-left 与内联元素配合得很好。

You can't get pixel perfect results. Here is a work around I've been using:

<span style="background: url(http://www.reoiv.com/images/rss.jpg) no-repeat left center; padding-left: 20px;">觀看次數</span>

This vertically center-aligns the icon nicely with the text. padding-left works nicely with inline elements.

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