Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vertical-align: middle with Bootstrap 2

I use the twitter bootstrap and I wanted to align verticaly a div block with a picture and the text at the right.

Here is the code:

<ol class="row" id="possibilities">      <li class="span6">          <div class="row">              <div class="span3">                  <p>some text here</p>                  <p>Text Here too</p>              </div>              <figure class="span3"><img src="img/screenshots/options.png" alt="Some text" /></figure>          </div>      </li>      <li class="span6">          <div class="row">              <figure class="span3"><img src="img/qrcode.png" alt="Some text" /></figure>              <div class="span3">                  <p>Some text</p>                  <p>Some text here too.</p>              </div>          </div>      </li> </ol> 

I tried this but not wortks:

.span6 .row{display: table;} .span6 .row .span3, .span6 .row figure{display:table-cell; vertical-align: middle;} 

I tried this too:

.span6 .row .span3{display: inline-block; vertical-align: middle;} 

None is working. Does somebody have an idea? Thanks in advance.

like image 204
bgondy Avatar asked Jul 21 '12 12:07

bgondy


People also ask

How do I center align vertically in bootstrap?

In Bootstrap 5, if we want to vertically align an <div> element in the middle of a containing element, we can do this by applying the class align-items-center and d-flex on the containing element of that div. Here, the d-flex class has the same effect as that of the display: flex; property in CSS.

How do I vertically align a div in the middle?

The CSS just sizes the div, vertically center aligns the span by setting the div's line-height equal to its height, and makes the span an inline-block with vertical-align: middle. Then it sets the line-height back to normal for the span, so its contents will flow naturally inside the block.

How do I align my cards vertically center in CSS?

1 — Vertical Center Using Auto Margins One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.


2 Answers

Try this:

.row > .span3 {     display: inline-block !important;     vertical-align: middle !important; } 

Edit:

Fiddle: http://jsfiddle.net/EexYE/

You may need to add Diego's float: none !important; also if span3 is floating and it interferes.

Edit:

Fiddle: http://jsfiddle.net/D8McR/

In response to Alberto: if you fix the height of the row div, then to continue the vertical center alignment you'll need to set the line-height of the row to be the same as the pixel height of the row (ie. both to 300px in your case). If you'll do that you will notice that the child elements inherit the line-height, which is a problem in this case, so you will then need to set your line height for the span3s to whatever it should actually be (1.5 is the example value in the fiddle, or 1.5 x the font-size, which we did not change when we changed the line-height).

like image 156
Skrivener Avatar answered Oct 08 '22 14:10

Skrivener


Try removing the float attribute from span6:

{ float:none !important; } 
like image 33
Diego Sanches Avatar answered Oct 08 '22 13:10

Diego Sanches