Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically-align media-body content in Twitter Bootstrap?

I'd like media-body content to be vertically aligned 'middle'.

Here is the markup:

<div class="media">
  <a class="pull-left" href="#">
    <img class="media-object" src="..." alt="...">
  </a>
  <div class="media-body">
    I'd like this text to be vertically aligned 'middle'
  </div>
</div>

I've tried the following which doesn't work:

.media-body {
  vertical-align: middle;
}

Any idea how I can do this?

like image 760
henrywright Avatar asked Mar 17 '14 09:03

henrywright


People also ask

How do you vertically align elements in bootstrap?

Answer: Use the align-items-center Class In Bootstrap 4, if you want to vertically align a <div> element in the center or middle, you can simply apply the class align-items-center on the containing element.

How do I vertically align an image 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. Example: HTML.

How do I center a div vertically using bootstrap 5?

Aligning content to the center of the DIV is very simple in Bootstrap 5, You just have to convert the div into a flex box using d-flex class property and then use the property align-items-center to vertically align the content in the middle of the div.


2 Answers

Since Bootstrap v3.3.0, it's possible to vertically align both the media-object and the media-body, by adding the CSS class media-middle.

For example:

<div class="media">
  <div class="media-left media-middle">
    <a href="#">
      <img class="media-object" src="..." alt="...">
    </a>
  </div>
  <div class="media-body media-middle">
    <h4 class="media-heading">Media heading</h4>
    ...
  </div>
</div>

See: http://getbootstrap.com/components/#media

Incidentally, the docs currently don't make it clear that this works for the media-body as well. It does, because all media-middle does is to add a vertical-align: middle; style to an element, and media-body already has the style display: table-cell (as does media-left and media-right).

like image 77
sevenpointsix Avatar answered Nov 01 '22 17:11

sevenpointsix


Bootply : http://www.bootply.com/122430

HTML:

<div class="media">
  <a class="" href="#">
    <img class="media-object custom-media" src="..." alt="..." height="500px" width="500px">
  </a>
  <div class="media-body">
    I'd like this text to be vertically aligned 'middle'
  </div>
</div>

CSS:

.custom-media, .media-body{
  display:inline;
}
like image 32
BENARD Patrick Avatar answered Nov 01 '22 17:11

BENARD Patrick