I have two DIVs. They each contain a SPAN with the same 16x16 background image and another SPAN with the same text. The only difference is the font sizes. How do I vertically align the centers of the icon and text even when the font sizes are different?
I basically want an element with an icon and text that vertically aligns no matter the font size. If there's a better way to do it than the multiple spans I'm using, that's fine too.
Here's a 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;
}
Example 1
.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;}
<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>
Example 2
.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;}
<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>