html - CSS - 使父子div的高度相等

标签 html css

我正在尝试创建的内容如下所示:

 --------------------------------------------------
 |                        |           |           |
 |                        | .box1     | .box2     |
 |                        |           |           |
 |   .image               -------------------------
 |                        |                       |
 |                        |     .description      |
 |                        |                       |
 |                        |                       |
 --------------------------------------------------   

与彼此相比,.box1 和 .box2 的高度相同(例如 flex: 1;),并且 .box1、.box2 和 .description 周围的图像和外壳大小相同

我似乎找不到一种方法可以做到这一点,我认为 Flexbox 示例在这种情况下很少见。

我认为表格可以用于此示例,具有如下所示的嵌套表格:

<table class="parent-wrapper">
  <tr>
    <td class="image">
      <img src="...."/>
    </td>
    <td class="parent-box">
      <table class="child-wrapper">
        <tr>
          <td class="box1">.box1</td>
          <td class="box2">.box2</td>
        </tr>
        <tr>
          <!-- I don't know a workaround for this as well -->
          <td class="description" colspan="2">.description</td>
        </tr>
      </table>
    </td>
 </tr>
</table>

但结果如下:

enter image description here

我想要的是 'image' td 的大小与 'child-wrapper' td 的高度相同

所以这个:(用硬编码高度制作)

enter image description here

最佳答案

flexbox 示例:

不需要表格!

编辑:已更新以满足要求。在 上使用(简写的 flex-basis)20%min-width250px。 image 以符合您的口味。请注意,我通过样式设置背景图像,因为我假设它将来自后端。

也许您还想在主容器上设置最小高度。

.block {
  display: flex;
}

.image {
  flex: 0 1 20%;
  min-width: 250px;
  background: #f00;
  color: #fff;
  
  background-size: cover;
  background-position: 50% 50%;
}

.image img {
  display: block;
  width: 100%;
  height: auto;
}

.content {
  display: flex;
  flex-direction: column;
  flex: 1;
}

.boxes {
  display: flex;
  flex: 1;
}

.box1 {
  background: cyan;
  flex: 1;
}

.box2 {
  background: yellow;
  flex: 1;
}

.description {
  flex: 1;
  background: #531777;
  color: #fff;
}
<div class="block">
  <div class="image" style="background-image: url('https://s-media-cache-ak0.pinimg.com/236x/c8/e8/cc/c8e8cc83e6eeb60061ba11c9d8ba9a11.jpg')">
  </div>
  <div class="content">
    <div class="boxes">
      <div class="box box1">
        .box1
      </div>
      <div class="box box2">
        .box2
      </div>
    </div>
    <div class="description">
      .description
      <br>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut mollis sed augue eu pulvinar. Cras sodales tortor ac mauris bibendum, quis sagittis quam viverra. Aliquam erat volutpat. Pellentesque ullamcorper porta metus, nec efficitur lorem vulputate quis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean vel dui et eros gravida bibendum. Nunc pulvinar commodo facilisis.
    </div>
  </div>
</div>

代码笔:http://codepen.io/veksen/pen/LRgwOy

关于html - CSS - 使父子div的高度相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40125083/

相关文章:

jquery - 为 child 添加类(class) - jquery

html - 如何在 ng-blur 之前调用 ng-click?

html - 使用 flexbox 的响应式表格

html - 将包含动态调整大小的输入元素的内联标签定位到表单中

html - 如何在 HTML CSS 标签上添加工具提示

html - 修复菜单导航中的宽度和其他 CSS 问题

javascript - 单击超链接不会触发旅行

css关键帧动画图像循环

css - 如何设置 Gtk::ComboBox 的样式

javascript - 除一个 PHP 包含文件外,所有其他都可以工作