fightstarr20 fightstarr20 - 3 months ago 8
CSS Question

Vertically align text next to image

I am trying to vertically align some text next to an image using Twitter Bootstrap 3

<div class="col-sm-4">
<img class="img-circle" src="http://dummyimage.com/100x100/000/fff">
<h2>
Short title
</h2>
</div>

<div class="col-sm-4">
<img class="img-circle" src="http://dummyimage.com/100x100/000/fff">
<h2>
A longer title
</h2>
</div>


https://jsfiddle.net/DTcHh/23814/

Sometimes my titles are on one line and sometimes they are on two. This means I am unable to set a static margin to get the text to be vertically centered.

Whats the best way to achieve this and does anybody have an example I can see?

Answer

You have options for this, but one of them is using flex:

/* Latest compiled and minified CSS included as External Resource*/

/* Optional theme */
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');

body{margin: 10px;}

img{float:left;margin-right:20px}
.col-sm-4 > div {
  display: flex;
  align-items: center;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-sm-4">
<div>
    <img class="img-circle" src="http://dummyimage.com/100x100/000/fff">
    <h2>
        Short title
    </h2>
    </div>
</div>

<div class="col-sm-4">
<div>
    <img class="img-circle" src="http://dummyimage.com/100x100/000/fff">
    <h2>
        A longer title
    </h2>
    </div>
</div>