居中文本(垂直对齐或填充)?

时间:2016-03-27 03:57:23

标签: html css html5

所以我在将一些文字与图像对中时遇到了一些麻烦,我把脚本放在下面:

<li>
    <div class="member">
        <div class="image">
            <img src="http://i.imgur.com/vSZyVru.jpg" alt="">
        </div>
        <div class="info">
            <h4>       Bill Nye</h4>
            <p>
                    Researcher and journalist
            </p>
            <ol class="social">
                <li><a href="#"><i class="fa fa-facebook"></i></a></li>
                <li><a href="#"><i class="fa fa-linkedin"></i></a></li>
            </ol>
        </div>
    </div>
</li>

我不确定是使用纵向对齐还是填充?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

两种方法

CSS表格(如果愿意,可以是实际的表格)

html {
  box-sizing: border-box;
}
*,
*::before,
*::after {
  box-sizing: inherit;
  margin: 0;
  padding: 0;
}
ol {
  list-style: none;
}
.member {
  display: table;
  margin: 1em;
}
.member .image,
.member .info {
  display: table-cell;
  vertical-align: middle;
  border: 1px solid grey;
  padding: 1em;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />

<div class="member">
  <div class="image">
    <img src="http://i.imgur.com/vSZyVru.jpg" alt="">
  </div>
  <div class="info">
    <h4>       Bill Nye</h4>
    <p>
      Researcher and journalist
    </p>
    <ol class="social">
      <li><a href="#"><i class="fa fa-facebook"></i></a>
      </li>
      <li><a href="#"><i class="fa fa-linkedin"></i></a>
      </li>
    </ol>
  </div>
</div>

<强> Flexbox的

html {
  box-sizing: border-box;
}
*,
*::before,
*::after {
  box-sizing: inherit;
  margin: 0;
  padding: 0;
}
ol {
  list-style: none;
}
.member {
  display: inline-flex;
  margin: 1em;
}
.member .image,
.member .info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  border: 1px solid grey;
  padding: 1em;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />

<div class="member">
  <div class="image">
    <img src="http://i.imgur.com/vSZyVru.jpg" alt="">
  </div>
  <div class="info">
    <h4>       Bill Nye</h4>
    <p>
      Researcher and journalist
    </p>
    <ol class="social">
      <li><a href="#"><i class="fa fa-facebook"></i></a>
      </li>
      <li><a href="#"><i class="fa fa-linkedin"></i></a>
      </li>
    </ol>
  </div>
</div>