Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically align text within a div [duplicate]

The code below (also available as a demo on JS Fiddle) does not position the text in the middle, as I ideally would like it to. I cannot find any way to vertically centre text in a div, even using the margin-top attribute. How can I do this?

<div id="column-content">     <img src="http://i.stack.imgur.com/12qzO.png">     <strong>1234</strong>     yet another text content that should be centered vertically </div> 
#column-content {     display: inline-block;     border: 1px solid red;     position:relative; }      #column-content strong {     color: #592102;     font-size: 18px; }  img {     margin-top:-7px;     vertical-align: middle;         } 
like image 715
Cyclone Avatar asked Feb 12 '12 14:02

Cyclone


People also ask

How do I align text vertically inside a div?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .

How vertically align div in another div?

Vertically centering div items inside another div Just set the container to display:table and then the inner items to display:table-cell . Set a height on the container, and then set vertical-align:middle on the inner items.

How do you vertically align text?

Center the text vertically between the top and bottom margins. Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center.

How do I vertically center a div element?

You can use the vertical-align property, which commonly applies to inline, inline-block, and table-cell elements. But it cannot be used to align block-level elements vertically. Earlier, it was used with the valign attribute, but now this attribute is deprecated. Instead, you can use vertical-align: middle.


2 Answers

Andres Ilich has it right. Just in case someone misses his comment...

A.) If you only have one line of text:

div  {    height: 200px;    line-height: 200px; /* <-- this is what you must define */  }
<div>vertically centered text</div>

B.) If you have multiple lines of text:

div  {    height: 200px;    line-height: 200px;  }    span  {    display: inline-block;    vertical-align: middle;    line-height: 18px; /* <-- adjust this */  }
<div><span>vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text</span></div>
like image 154
Krisztián Balla Avatar answered Oct 08 '22 02:10

Krisztián Balla


Create a container for your text content, a span perhaps.

#column-content {    display: inline-block;  }  img {    vertical-align: middle;  }  span {    display: inline-block;    vertical-align: middle;  }    /* for visual purposes */  #column-content {    border: 1px solid red;    position: relative;  }
<div id="column-content">      <img src="http://i.imgur.com/WxW4B.png">    <span><strong>1234</strong>      yet another text content that should be centered vertically</span>  </div>

JSFiddle

like image 28
Andres Ilich Avatar answered Oct 08 '22 02:10

Andres Ilich