使用CSS,即使字体大小发生变化,如何垂直对齐此图标和文本?

时间:2015-05-28 04:03:56

标签: html css vertical-alignment

我有两个DIV。它们每个都包含一个具有相同16x16背景图像的SPAN和另一个具有相同文本的SPAN。唯一的区别是字体大小。即使字体大小不同,如何垂直对齐图标和文本的中心?

我基本上想要一个带有图标和文字的元素,无论字体大小如何,它都会垂直对齐。如果有一个比我正在使用的多个跨度更好的方法,那也没关系。

这是一个jsfiddle:https://jsfiddle.net/nup2pwqz/

<div class='divcontainer'>
    <span class='span1'> <span>
    <span class='span2 font1'>Header<span>
</div>
<div class='divcontainer'>
    <span class='span1'> <span>
    <span class='span2 font2'>Header<span>
</div>

.font1{
        font-size: 12px;
}
.font2{
        font-size: 18px;
}
.divcontainer{
    display:inline-block;
    position:relative;
    text-align:center;
    width:200px;
    border:1px solid red;
}
.span1{
    display:inline-block;
    vertical-align:middle; 
    width:16px; 
    height:16px; 
    background: url('http://www.albany.edu/gsa/FileZilla-3.7.0.2/resources/16x16/cancel.png') no-repeat;
}
.span2{
    padding-left:20px;
}

3 个答案:

答案 0 :(得分:2)

示例1

&#13;
&#13;
.divcontainer{
    display: inline-block;
    position: relative;
    text-align: center;
    width: 200px;
    border: 1px solid red;
}
.divcontainer span{
    padding-left: 20px;
    position: relative;
}
.divcontainer span:before{
    content: '';
    position: absolute; top: 50%; left: 0;
    width:16px; 
    height:16px; 
    background: url('http://www.albany.edu/gsa/FileZilla-3.7.0.2/resources/16x16/cancel.png') no-repeat;
    margin-top: -8px;
}
.font1{font-size: 12px;}
.font2{font-size: 18px;}
.font3{font-size: 28px;}
.font4{font-size: 48px;}
&#13;
<div class='divcontainer'>    
    <span class='font1'>Header</span>
</div>
<div class='divcontainer'>   
    <span class='font2'>Header</span>
</div>
<div class='divcontainer'>   
    <span class='font3'>Header</span>
</div>
<div class='divcontainer'>   
    <span class='font4'>Header</span>
</div>
&#13;
&#13;
&#13;

示例2

&#13;
&#13;
.divcontainer{
    display: inline-block;
    position: relative;
    text-align: center;
    width: 200px;
    border: 1px solid red;    
}
.divcontainer > span{
    display:inline-block;
    vertical-align:middle;
}

.span1{    
    width:16px; 
    height:16px; 
    background: url('http://www.albany.edu/gsa/FileZilla-3.7.0.2/resources/16x16/cancel.png') no-repeat center center;
}
.span2{
    padding-left:5px;
}

.font1{font-size: 12px;}
.font2{font-size: 18px;}
.font3{font-size: 28px;}
.font4{font-size: 38px;}
&#13;
<div class='divcontainer'>
    <span class='span1'></span>
    <span class='span2 font1'>Header</span>
</div>
<div class='divcontainer'>
    <span class='span1'></span>
    <span class='span2 font2'>Header</span>
</div>
<div class='divcontainer'>
    <span class='span1'></span>
    <span class='span2 font3'>Header</span>
</div>
<div class='divcontainer'>
    <span class='span1'></span>
    <span class='span2 font4'>Header</span>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用line-height。将以下内容添加到.divcontainer块:

.divcontainer {
    line-height: 16px;
}

答案 2 :(得分:0)

您可以使用line-height代替vertical-align,如下所示(您还需要设置background-image-postion的值)

.span1{
    display:inline-block;
    line-height:30px; 
    width:16px; 
    height:30px; 
    background: url('http://www.albany.edu/gsa/FileZilla-3.7.0.2/resources/16x16/cancel.png') no-repeat center;
}