垂直对齐图像旁边的文本

时间:2016-08-17 15:18:43

标签: html css twitter-bootstrap twitter-bootstrap-3

我正在尝试使用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/

有时候我的头衔在一条线上,有时它们在两条线上。这意味着我无法设置静态边距以使文本垂直居中。

什么是实现这一目标的最好方法,有没有人能看到我能看到的例子?

2 个答案:

答案 0 :(得分:6)

你有这方面的选择,但其中一个是使用flex:

&#13;
&#13;
/* 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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您只需使用flexbox布局即可:

/* 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>

小提琴:https://jsfiddle.net/pavelrich/f5hyvbwq/