Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css single or multiple line vertical align

Tags:

html

css

I have a title that can have one or more lines.

How can I align the text vertically? If it was always one line I could just set the line-height to the container height.

I can do it using JavaScript, but I don't really like it, I'm searching for a pure CSS way.

Also if the container could expand with the lines it would be perfect, so I can always have the same padding on the top and bottom.

enter image description here

like image 233
braindamage Avatar asked Feb 08 '12 11:02

braindamage


People also ask

How do you vertically align something in CSS?

Use the line-height Property to Align Text Vertically in CSS If we have single-line text, we can use the line-height property to align the text vertically within a div . The line-height CSS property sets the height of a line box. It is used to set the distance between lines of text.

Is vertical align deprecated?

Deprecated as an attribute Occasionally you will see “valign” used on table cells to accomplish vertical alignment. e.g. <td valign=top> . It should be noted that this attribute is deprecated and should not be used.

How do you make a paragraph vertical in CSS?

Use the CSS line-height property Add the line-height property to the element containing a text larger than its font size. By default, equal spaces will be added above and below the text, and you'll get a vertically centered text.

How do I align text with one line in CSS?

The text-align-last property specifies how to align the last line of a text. Notice that the text-align-last property sets the alignment for all last lines within the selected element. So, if you have a <div> with three paragraphs in it, text-align-last will apply to the last line of EACH of the paragraphs.


2 Answers

For this you can use display:table-cell property:

.inline {    display: inline-block;    margin: 1em;  }  .wrap {    display: table;    height:  100px;    width:   160px;    padding: 10px;    border:  thin solid darkgray;  }  .wrap p {    display:        table-cell;    vertical-align: middle;  }
<div class="inline">    <div class="wrap">      <p>Example of single line.</p>    </div>  </div>    <div class="inline">    <div class="wrap">      <p>To look best, text should really be centered inside.</p>    </div>  </div>

But it works IE8 & above. Read this article for more info: CSS Tricks: Vertically Center Multi-Lined Text.

like image 85
sandeep Avatar answered Oct 02 '22 19:10

sandeep


If you don't like the display:table trick (I know I don't) here's a solution without it:

.cen {   min-height:5em; width:12em; background:red; margin:1em 0; } .cen p {   display:inline-block; vertical-align:middle;   margin:0 0 0 1em; width:10em; } .cen::after {    display:inline-block; vertical-align:middle; line-height:5em;    width:0; content:"\00A0"; overflow:hidden; } 

with HTML

<div class="cen">  <p>Text in div 1</p> </div> 

This gives the div a height of 5em, unless the content is heigher, then it grows.
Live example here.

Edit: Oh, which browsers is this supposed to work on? IE8 won't cooperate.

(Later edit: updated CSS to handle issues in Chrome)

like image 41
Mr Lister Avatar answered Oct 02 '22 19:10

Mr Lister