Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center text vertically in div? [duplicate]

I know you can do it with a single line by setting the line-height, but my data might take up more than one line. I tried doing something with setting display:table for parent and display:table-cell for the child, but I couldn't get it to work.

Here is my jsfiddle

like image 436
jas7457 Avatar asked Jan 27 '26 11:01

jas7457


2 Answers

Well, I played around with the jsFiddle for a bit, and this is what I came up with:

http://jsfiddle.net/rVgkJ/7/

The relevant CSS:

.small_wrapper_div{
    width:100%;
    display:table;
}

.small_wrapper_div > div{
    display:table-cell;
    vertical-align: middle; 
}

.small_picture_div{
    width:50px;
    height:50px;
}

.small_picture_div img {
    width:50px;
    display:block;
}

.small_text_div{
    padding-left:10px;
}
like image 176
Tieson T. Avatar answered Jan 30 '26 05:01

Tieson T.


In a nutshell you will need to add position: relative to the containing div (small_wrapper_div) and then you can add:

.small_text_div a {
    display: table-cell; 
    vertical-align: middle; 
    text-align: left;    
}

and change .small_text_div to be some combination of:

.small_text_div {
    height:100%;
    padding-left:10px;
    width:250px;
    position: absolute;
    left:50px;
    top: 2px;  
    display: table; 
}

And you will have the text vertically centered.

like image 42
rg88 Avatar answered Jan 30 '26 05:01

rg88



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!