在表格中垂直对齐图像

时间:2019-05-25 13:31:21

标签: html css

我正在创建“为什么选择我们”初始页面,我想创建表,其中表的一侧是图像,另一侧是原因。我需要图像在中心垂直对齐。

到目前为止,我已经尝试在表内制作另一个表,并通过包装在其周围的表格单元格将图像与“ vertical-align:middle”垂直对齐。

<section class="reason">
        <div class="content">
            <div class="float-left table">
                <div class="table-cell">
                    <img class="thumbnail" src="https://1eican1u2cuw7g1kw7btt2xy-wpengine.netdna-ssl.com/wp-content/uploads/personal-attention-vamospanish-85x85.png">
                </div>
            </div>
            <div class="float-right">
                <h3>
                    Lorem Ipsum Dolor Sit Amet
                </h3>
                <p class="appeal">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
            </div>
        </div>
    </section>

<style>
.reason {
  padding: 100px;
  background-color: #fff;
}

.content {
  display: table;
  clear: both;
  max-width: 935px;
  margin: 0 auto;
}

.float-left {
  float: left;
  width: 50%;
}

.float-right {
  float: right;
  width: 50%;
}

.table {
  display: table;
}

.table-cell {
  display: table-cell;
  vertical-align: middle;
}

.thumbnail {
  display: block;
  margin: 0 auto;
}
</style>

这是一个概述问题的代码笔:https://codepen.io/AlecAcres/pen/NVMRoY

4 个答案:

答案 0 :(得分:0)

可能是<td>的风格吗?

<style>
td img { 
  vertical-align: top;
}
</style>

答案 1 :(得分:0)

我更改了代码(基本上使用flex代替了table)-: Working demo here

HTML

<section class="reason">
    <div class="content">
        <div class="float-left">
            <div class="table-cell">
                <img class="thumbnail" src="https://1eican1u2cuw7g1kw7btt2xy-wpengine.netdna-ssl.com/wp-content/uploads/personal-attention-vamospanish-85x85.png">
            </div>
        </div>
        <div class="float-right">
            <h3>
                            Lorem Ipsum Dolor Sit Amet
                    </h3>
            <p class="appeal">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        </div>
    </div>
</section>

CSS

.reason {
  padding: 100px;
  background-color: #fff;
}

.content {
  display: flex;
  justify-content: center;
  max-width: 935px;
  margin: 0 auto;
}

.float-left {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.float-right {
  flex: 1
}

.table-cell {
  display: table-cell;
  vertical-align: middle;
}

.thumbnail {
  display: block;
  margin: 0 auto;
}

答案 2 :(得分:0)

.flex-container {
  display: flex;
}

.flex-image {
  display: flex;
  flex-flow: column wrap;
  justify-content: space-evenly;
}
 <div class="flex-container">
    <div class="flex-image">
      <img
        src="https://1eican1u2cuw7g1kw7btt2xy-wpengine.netdna-ssl.com/wp-content/uploads/personal-attention-vamospanish-85x85.png">
    </div>
    <div class="flex-content">
      <h3>
        Lorem Ipsum Dolor Sit Amet
      </h3>
      <p class="appeal">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
        proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
  </div>

答案 3 :(得分:0)

这是使用FlexBox的更现代的方法!

演示: https://codepen.io/anon/pen/WBJoxY

HTML:

<section class="reason">
  <div class="content">
    <div class="thumbnail">
      <img src="https://1eican1u2cuw7g1kw7btt2xy-wpengine.netdna-ssl.com/wp-content/uploads/personal-attention-vamospanish-85x85.png">
    </div>
    <div class="text">
      <h3>
        Lorem Ipsum Dolor Sit Amet
      </h3>
      <p class="appeal">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
        proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      </p>
    </div>
  </div>
</section>

CSS:

.reason {
  padding: 100px;
  background-color: #fff;
}

.content {
  display: flex;
  flex-direction: row;
  max-width: 935px;
  margin: 0 auto;
}

.text {
  max-width: 50%;
}

.thumbnail {
  display: flex;
  margin: 0 auto;
  justify-content: center;
  align-items: center;
}