Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically align text and button inside Bootstrap 3 alert

I'm trying to vertically align text (pulled left) and a button (pulled right) inside a Bootstrap 3 alert. Somehow like this:

+------------------------------------------------+
| Some text                           [A button] |
+------------------------------------------------+

What I have so far is the following (Bootply):

<div class="alert alert-info" style="overflow:hidden">
    <p class="pull-left">Some text</p>
    <button class="btn btn-sm btn-default pull-right">A button</button>
</div>

The button is perfectly aligned (this was actually my first problem, solved here), but now the text is out of center.

I appreciate your help!

like image 836
muenchdo Avatar asked Feb 27 '14 14:02

muenchdo


People also ask

How do I vertically align text in Bootstrap 3?

row is now display:flex you can simply use align-self-center on any column to vertically center it... or, use align-items-center on the entire .

How do I vertically align an item in bootstrap?

Change the alignment of elements with the vertical-alignment utilities. Please note that vertical-align only affects inline, inline-block, inline-table, and table cell elements. Choose from .align-baseline , .align-top , .align-middle , .align-bottom , .align-text-bottom , and .align-text-top as needed.

How do I align text vertically and horizontally in bootstrap?

Bootstrap 4 use flexbox, so d-flex assign the property display: flex than align-items-center aligns it vertically and justify-content-center aligns it horizontally. Whilst code only answers may solve the problem, some explanation of your answer would help others understand what you have done and why you have done it.

Can we apply vertical alignment for 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.


2 Answers

Assuming you don't want to use line-height, you can also try:

div.alert-info {
    position: relative;
}

div.alert-info p.pull-left {
   -webkit-transform: translateY(-50%);
   -ms-transform: translateY(-50%);
   -o-transform: translateY(-50%);
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
}

http://www.bootply.com/117260

like image 106
Adrift Avatar answered Sep 17 '22 08:09

Adrift


use display:table; width:100%; for the alert div, and for the p tag remove the float and use display:table-cell; vertical-align:middle.
You can use the same rules for the button.

like image 23
n1kkou Avatar answered Sep 18 '22 08:09

n1kkou