Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I align text to the middle of an element with CSS in Twitter Bootstrap?

I am using Bootstrap alerts and this is my success alert message:

<div class="alert alert-success" id="UploadSuccess">
    <a class="close" data-dismiss="alert">×</a>
    <strong>Congrats!</strong> You have successfully did it!.
</div>

And the result is:

Resulting image of above HTML

As you'll can see that the text alignment is at the top of the <div>. So, how would II align it to the middle?

I have tried using padding-top and vertical-align:middle but neither works (I end up with the same result).

What do I need to do to change the vertical alignment of the text?

like image 382
coder Avatar asked Apr 08 '12 08:04

coder


People also ask

How do I center align text in bootstrap?

Add class . text-center to the parent div to align text or any item inside to the center. You can also decide how the alignment should look like on the specific screen sizes.

How do you center an element in CSS with bootstrap?

Just add the class . text-center to the parent element of the text to center content horizontally.

How do you align text to the center in CSS?

To center text in CSS, use the text-align property and define it with the value 'center. ' You can use this technique inside block elements, such as divs. You can also center text in HTML, which is useful if you only want to center individual elements on the page on a case-by-case basis.

How do I center align an item in bootstrap?

Use the . align-items-center class in Bootstrap to align single rows of items in the center.


3 Answers

Trying to vertically center text text is a common issue. Normally this wouldn't work on normal boxes, but you can force it to work with vertical align:

vertical-align: middle;
display: table-cell;

However this will not work in IE7 and lower.

If you are sure the text you want to display you could use line-height to fake the effect like this:

height: 40px;
line-height: 40px; /* same as height */

This way works cross browser and has support up to IE5.5 I believe. If this is not an option I'm afraid you're out of luck (it can't be done).

As a side note that error message suffers from bad grammar, it should be "Congratulations! You have successfully done it.".

like image 44
sg3s Avatar answered Sep 30 '22 21:09

sg3s


Make the line-height of the div the same as the height of the div, eg:

#UploadSuccess { height: 20px; line-height: 20px; }

This will only work if you have one line of text.

like image 76
James Zaghini Avatar answered Sep 30 '22 20:09

James Zaghini


Make it easier ;) ! Try to use alert-block ! That works fine with latest Bootstrap version !

<div class="alert alert-block alert-success" id="UploadSuccess">
    <a class="close" data-dismiss="alert">×</a>
    <strong>Congrats!</strong> You have successfully did it!.
</div>

enter image description here

like image 33
Alexandre Tranchant Avatar answered Sep 30 '22 21:09

Alexandre Tranchant