将使用 CSS 背景 属性 包含的图像与 Span 标记垂直对齐
Vertically align an image included using CSS background property to a Span tag
我有一个跨度标签。 我正在使用 css 在此 span 标签内加载图像。 现在我想将此图像在 span 标记内垂直居中。我无法将此图像垂直居中,因为我正在使用 CSS 将此图像应用于跨度。在这里,我使用了一个图标精灵并仅从图标精灵中提取相关部分。有人可以帮忙吗?
HTML
<span class="icon"></span>
CSS
.icon{
background: url(../images/icon-sprite.png) no-repeat -328px 0;
width: 60px;
height: 40px;
position: absolute;
}
我尝试将行高添加到 .icon
class。但是没有快乐。
您需要更改数字才能使其适用于您的图标,但这应该可以解决问题。
.icon {
position: relative;
border:1px solid #FF0000;
white-space: nowrap;
}
.icon:before{
content: "";
position: relative;
width:15px;
display:inline-block;
}
.icon:after{
content: "";
position: absolute;
top: 50%;
left: 0;
margin-top: -8.5px;
width: 15px;
height: 18px;
background: url('https://www.google.com/images/nav_logo127.png') no-repeat -0px -327px;
}
<span class="icon" style="font-size:20px;">whatever</span>
<span class="icon" style="font-size:25px;">whatever</span>
<span class="icon" style="font-size:30px;">whatever</span>
<span class="icon" style="font-size:35px;">whatever</span>
<span class="icon" style="font-size:40px;">whatever</span>
<span class="icon" style="font-size:45px;">whatever</span>
边框和字体大小仅为示例。
可以使用任何人:
.icon{
background: url(../images/icon-sprite.png) no-repeat;
background-position:left center;
width: 60px;
height: 40px;
position: absolute;
}
或
.icon{
background: url(../images/icon-sprite.png) no-repeat left center;
width: 60px;
height: 40px;
position: absolute;
}
理想情况下,您应该有一个父级 class 并尝试将 .icon
垂直对齐到父级。
.parent {
position: relative;
width: 120px;
height: 80px;
}
.icon {
background: url(../images/icon-sprite.png) no-repeat -328px 0;
width: 60px;
height: 40px;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
text-align: center;
}
<div class="parent">
<span class="icon"></span>
</div>
我有一个跨度标签。 我正在使用 css 在此 span 标签内加载图像。 现在我想将此图像在 span 标记内垂直居中。我无法将此图像垂直居中,因为我正在使用 CSS 将此图像应用于跨度。在这里,我使用了一个图标精灵并仅从图标精灵中提取相关部分。有人可以帮忙吗?
HTML
<span class="icon"></span>
CSS
.icon{
background: url(../images/icon-sprite.png) no-repeat -328px 0;
width: 60px;
height: 40px;
position: absolute;
}
我尝试将行高添加到 .icon
class。但是没有快乐。
您需要更改数字才能使其适用于您的图标,但这应该可以解决问题。
.icon {
position: relative;
border:1px solid #FF0000;
white-space: nowrap;
}
.icon:before{
content: "";
position: relative;
width:15px;
display:inline-block;
}
.icon:after{
content: "";
position: absolute;
top: 50%;
left: 0;
margin-top: -8.5px;
width: 15px;
height: 18px;
background: url('https://www.google.com/images/nav_logo127.png') no-repeat -0px -327px;
}
<span class="icon" style="font-size:20px;">whatever</span>
<span class="icon" style="font-size:25px;">whatever</span>
<span class="icon" style="font-size:30px;">whatever</span>
<span class="icon" style="font-size:35px;">whatever</span>
<span class="icon" style="font-size:40px;">whatever</span>
<span class="icon" style="font-size:45px;">whatever</span>
边框和字体大小仅为示例。
可以使用任何人:
.icon{
background: url(../images/icon-sprite.png) no-repeat;
background-position:left center;
width: 60px;
height: 40px;
position: absolute;
}
或
.icon{
background: url(../images/icon-sprite.png) no-repeat left center;
width: 60px;
height: 40px;
position: absolute;
}
理想情况下,您应该有一个父级 class 并尝试将 .icon
垂直对齐到父级。
.parent {
position: relative;
width: 120px;
height: 80px;
}
.icon {
background: url(../images/icon-sprite.png) no-repeat -328px 0;
width: 60px;
height: 40px;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
text-align: center;
}
<div class="parent">
<span class="icon"></span>
</div>