html - 2x2 网格等高,尽管盒子内容量

标签 html css reactjs flexbox

我正在尝试创建一个 2x2 网格,其中四个单独的框(红色、蓝色、绿色、黄色)的高度都相等,无论它们包含多少内容。

#rowTwo {
  background-color: pink;
  min-height: 25%;
  display: flex;
  flex-direction: column; 
  align-items: center;
}

#grid2x2 {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  background-color: orange;
  width: 80%;
  justify-content: center;
}

.box {
  display: flex;
  flex-basis: calc(50% - 20px);
  margin: 5px;
  justify-content: center;
  padding-top: 5%;
  overflow: hidden;
}
<div id={styles.rowTwo}>
  <Heading title="My Title"/>
  <div id={styles.grid2x2}>
    <div id={styles.boxOne} className={styles.box}>
        <DescriptionBoxItemWithHeading item={this.state.sectionTwoItems[0]}/>
    </div>
    <div id={styles.boxTwo} className={styles.box}>
        <DescriptionBoxItemWithHeading item={this.state.sectionTwoItems[1]}/>
    </div>
    <div id={styles.boxThree} className={styles.box}>
        <DescriptionBoxItemWithHeading item={this.state.sectionTwoItems[2]}/>
    </div>
    <div id={styles.boxFour} className={styles.box}>
        <DescriptionBoxItemWithHeading item={this.state.sectionTwoItems[3]}/>
    </div>
  </div>
</div>

我面临的问题是一个盒子扩展到一行中最大内容的高度。

这是一张概述我的问题的图片:

enter image description here

有谁知道我怎样才能做到这一点?

最佳答案

您可以在 CSS 中执行此操作,方法是使用 padding-top 添加高度,然后使用绝对定位将您的内容绘制在此填充之上。

这是一个例子:https://codepen.io/anon/pen/xpwooz

#grid2x2 {
    display: flex;
    flex-wrap: wrap;
    background-color: orange;
}

.box {
    display: flex;
    flex-basis: calc(50% - 20px);
    margin: 5px;
    justify-content: center;
    overflow: hidden;
    background: red;
    padding-top: 100px;
    position: relative;
}

.content {
  position: absolute;
  background: green;
  top: 0;
  width: 100%;
}    

问题是您不能根据内容高度动态更改此高度。您可以将其设置为固定大小或将其设置为百分比并根据其父宽度更改大小。如果您希望根据最大的内容元素更改高度,我认为您需要使用 javascript。

关于html - 2x2 网格等高,尽管盒子内容量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47854087/

相关文章:

javascript - ReactJS 示例失败

jquery - 用 jquery 在表格旁边添加 div 元素

css - 将 HTML 输入日期选择器右对齐

html - Bootstrap 主题在页面中心对齐文本点

javascript - 如何在 react.js 中将数据从父级传递给子级

javascript - 更改颜色 InputLabel Material UI

php - 使用 location.replace 使用相同 URL 时,Javascript 不会重新加载页面

html - 在同一个 html 页面中使用两个不同的 css

html - 如何在 CSS 中选择文本而不是图像

html - 强制内容水平流动