Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align text within a responsive box doesn't work

Tags:

html

css

How do I vertical align the text within a responsive box? I did for the image but it doesn't work for span, not sure why. Below is my half way done demo :

img{
    width:20px;
}

div{
    width:100px;
    height:100px;
    background:#ddd;
    padding:10px;
}

img{
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}
<div>
    <img src="https://cdn2.iconfinder.com/data/icons/windows-8-metro-style/256/football.png"/>
    <span>football</span>
</div>
like image 947
Alicia Avatar asked Sep 01 '26 22:09

Alicia


1 Answers

Try This

img{
    width:20px;
    padding:2px;
}

div{
    width:100px;
    height:100px;
    background:#ddd;
    padding:10px;
}

img,span{
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
    float: left;
}

Demo Here

like image 169
Shrinivas Pai Avatar answered Sep 03 '26 12:09

Shrinivas Pai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!