skip to Main Content

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?

2

Answers


  1. 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>
    Login or Signup to reply.
  2. You can simply do it by using the flexbox layout:

    /* Layout */
    
    .flex-container {
      display: -webkit-box;
      display: -moz-box;
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
      
      -webkit-flex-direction: row;
      -ms-flex-direction: row;
      flex-direction: row;
      
      -webkit-flex-wrap: wrap;
      -ms-flex-wrap: wrap;
      flex-wrap: wrap;
      
      -webkit-align-items: center;
      -ms-flex-align: center;
      align-items: center;
      
      padding: 0;
      margin: 0;
    }
    
    .flex-column {
      -webkit-flex-direction: column;
      -ms-flex-direction: column;
      flex-direction: column;
      
      -webkit-flex-grow: 0;
      flex-grow: 0;
      
      -webkit-flex-shrink: 0;
      flex-shrink: 0;
      
      -webkit-box-orient: vertical;
      -moz-box-orient: vertical;
      
      -webkit-box-flex: 0;
      -moz-box-flex: 0;
    }
    
    .flex-column-auto {
      -webkit-flex-grow: 1;
      flex-grow: 1;
      
      -webkit-flex-basis: 0;
      -ms-flex-preferred-size: 0;
      flex-basis: 0;
      
      width: auto;
      max-width: 100%;
    }
    
    /* Style */
    
    .image {
      border-radius: 50px;
    }
    
    .header {
      padding: 20px;
    }
    <div class="flex-container">
      <div class="flex-column flex-column-auto">
        <!-- Article -->
        <article>
          <div class="flex-container">
            <div class="flex-column">
              <img src="http://dummyimage.com/100x100/000/fff" alt="Image" class="image">
            </div>
            <div class="flex-column flex-column-auto">
              <header class="header">
                <h2>A short title</h2>
              </header>
            </div>
          </div>
        </article>
      </div>
      
      <div class="flex-column flex-column-auto">
        <!-- Article -->
        <article>
          <div class="flex-container">
            <div class="flex-column">
              <img src="http://dummyimage.com/100x100/000/fff" alt="Image" class="image">
            </div>
            <div class="flex-column flex-column-auto">
              <header class="header">
                <h2>A long long long long title</h2>
              </header>
            </div>
          </div>
        </article>
      </div>
      
      <div class="flex-column flex-column-auto">
        <!-- Article -->
        <article>
          <div class="flex-container">
            <div class="flex-column">
              <img src="http://dummyimage.com/100x100/000/fff" alt="Image" class="image">
            </div>
            <div class="flex-column flex-column-auto">
              <header class="header">
                <h2>A super long long long long long title</h2>
              </header>
            </div>
          </div>
        </article>
      </div>
    </div>

    Fiddle: https://jsfiddle.net/pavelrich/f5hyvbwq/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search