html - Flexbox 行/列等高

标签 html css flexbox

我有一行有两列,都是 50%。左边是图片,右边是文字。我的 html:

.row{
  display:flex;
  flex-direction:row;
  align-items:center;
}

.cell1, .cell2{
  width:50%;
}

.cell2 {
  background: green;
}
<div class="row">
    <div class="cell1"><img src="https://via.placeholder.com/450x450" ></div>
    <div class="cell2">Cell 2</div>
</div>

现在我希望单元格 2 的背景为绿色并且需要单元格 2 与单元格 1 的高度相同,我知道我可以通过为该行提供此背景来实现它,但是我如何只使单元格 2 的背景为绿色(并且与 cell1 具有相同的高度)?

最佳答案

在主容器 stretch 上保持默认对齐方式,以便两者具有相同的高度,然后对齐 cell2 中的内容:

.row {
  display: flex;
}

.cell1,
.cell2 {
  width: 50%;
}

.cell2 {
  display: flex;
  flex-direction: row;
  align-items: center;
  background:green;
}
<div class="row">
  <div class="cell1"><img src="https://picsum.photos/300/200?image=0" ></div>
  <div class="cell2">some text here</div>
</div>

关于html - Flexbox 行/列等高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53915379/

相关文章:

javascript - res.render() 和 res.redirect() 的行为类似于 res.send()

jquery - 当鼠标悬停在使用 jQuery 的其中一个元素上时,如何为共享同一类的所有元素添加边框?

css - 如何使具有自定义宽度的 flexbox 元素跳转到可用空间

css - 调整 div 大小的调整大小事件

html - CSS 可见性无与折叠

HTML 和 CSS 问题 : Element between two absolute-positioned elements needs to resize correctly

html - 如何让左边的菜单高度根据右边的内容增长

html - CSS图片div的定位

css - 根据内容或父元素拉伸(stretch)元素

reactjs - 3 :1 对齐标签和输入