Skip to content Skip to sidebar Skip to footer

Vertically Align Text Within Div Element

I know this question has been asked to death but nothing through searching has worked for me. You know the deal, I have a div element that I need to vertically align text in but no

Solution 1:

One other thing you can do. If it's only one line of text in the div you can use line-height

example

div {
    line-height:40px;
}

Solution 2:

The idea is from here and should work for all browsers.

<divstyle="margin: 0 0 10px 0; padding: 10px; border: 2px solid #606060; background-color: #2b2b2b;
    -webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px;"><divstyle="float: left; width: 55px; height: 40px;"><ahref="link"><imgstyle="max-width: 50px; border: 1px solid #ffb92c;"src="image.jpg"alt="" /></a></div><divstyle="float: right; width: 155px; font-size: 0.7em; height: 40px; display: table; #position: relative; overflow: hidden;"><divstyle="#position: absolute; #top: 50%; display: table-cell; vertical-align: middle;"><divstyle="#position: relative; #top: -50%;"><ahref="link">This is the text to vertically align</a></div></div></div><divstyle="clear: both"></div></div>

Solution 3:

You need to do like this:

http://jsfiddle.net/rathoreahsan/5u9HY/

Use fixed height instead of padding in main div. and use line height for left & right Divs

Solution 4:

Here is a clean version of the solution

<divstyle="background: yellow"><divstyle="width: 155px; font-size: 0.7em; height: 40px; display: table; overflow: hidden;"><divstyle="display: table-cell; vertical-align: middle;"><divstyle=""><ahref="link">This is the text to vertically align</a></div></div></div><divstyle="clear: both"></div>

http://jsfiddle.net/5y4Nb/

Solution 5:

Seems like a common float issue which can be fixed with a clearfix or, like i did in the following code snippet, with a fixed height of the wrapper.

I also sat an line-height of the floating divs and made it a little wider.

Take a look at this:

<divstyle="margin:0 0 10px 0;padding:10px;border:2px solid #606060;background-color:#2b2b2b;-webkit-border-radius:8px;-moz-border-radius:8px;border-radius:8px;height:40px"><divstyle="float:left;width:55px;height:40px;"><ahref="link"><imgstyle="max-width:50px;border:1px solid #ffb92c;"src="image.jpg"alt="" /></a></div><divstyle="float:right;width:185px;font-size:0.7em;height:40px;line-height:40px"><ahref="link">This is the text to vertically align</a></div></div>

http://jsfiddle.net/YqxPZ/3/

Post a Comment for "Vertically Align Text Within Div Element"