具有 Flexbox 和嵌套 Div 的等高列

发布于 2025-01-10 09:40:30 字数 3059 浏览 0 评论 0原文

我目前正在尝试创建一行三个等高列,其中所有 div 都与最高的 div 相同,即文本最多的 div。我尝试添加 flex: 1 和 height: 100% 但我似乎无法让这些家伙的高度相等。这是我当前正在尝试的代码。您可能需要扩大宽度才能使它们排成一行。

* {
  box-sizing: border-box;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 15px;
  line-height: 1.42857143;
  color: #565757;
}

.internal-icons-icon {
  margin: 20px 0;
   height: 100%;
}

.internal-icons-layout {
  display: flex;
  flex-wrap: wrap;
  height: 100%;
}



.internal-icons-icon > div {
  padding: 30px;
  text-align: center;
  border: 1px solid #575859;
  color: #565757;
  text-decoration: none;
  box-shadow: 2px 4px 10px 8px rgb(0 0 0 / 10%);
}

.internal-icons-icon div {
  margin: 20px 0;

}

.internal-icons-content {
  font-size: 16px;
}

.internal-icons-title {
  font-size: 22px;
  font-weight: 700;
  text-transform: uppercase;
}

@media (min-width: 990px) {
  .internal-icons {
    display: flex;
    flex-direction: row;
  }

  .internal-icons-layout {
    display: flex;
    flex-wrap: wrap;
  }

  .internal-icons-icon {
    margin: 0;
    width: 33.333%;
    height: 100%;
    padding: 15px;
  }

  .internal-icons-content {
    flex: 1;
  }

}
<div class="internal-icons">
  <div class="internal-icons-layout">
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem Ipsum Dolor
        </div>
        <div class="internal-icons-content">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </div>
      </div>
    </div>
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem ipsum dolor
        </div>
        <div class="internal-icons-content">
          Fusce cursus, risus eu rhoncus varius, ipsum massa eleifend libero, non accumsan elit felis et augue.
        </div>
      </div>
    </div>
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem ipsum dolor
        </div>
        <div class="internal-icons-content">
          Fusce cursus, risus eu rhoncus varius, ipsum massa eleifend libero, non accumsan elit felis et augue.
          <br />
          <strong>LOREM IPSUM DOLOR >></strong>
        </div>
      </div>
    </div>
  </div>
</div>

I am currently trying to create a row of three equal height columns where all divs would be the same as the tallest div i.e. the div with the most text. I have tried adding flex: 1 and height: 100% but I can't seem to get these guys to be equal height. Here is the code I'm currently trying. You might have to expand the width to get them to line up in a row.

* {
  box-sizing: border-box;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 15px;
  line-height: 1.42857143;
  color: #565757;
}

.internal-icons-icon {
  margin: 20px 0;
   height: 100%;
}

.internal-icons-layout {
  display: flex;
  flex-wrap: wrap;
  height: 100%;
}



.internal-icons-icon > div {
  padding: 30px;
  text-align: center;
  border: 1px solid #575859;
  color: #565757;
  text-decoration: none;
  box-shadow: 2px 4px 10px 8px rgb(0 0 0 / 10%);
}

.internal-icons-icon div {
  margin: 20px 0;

}

.internal-icons-content {
  font-size: 16px;
}

.internal-icons-title {
  font-size: 22px;
  font-weight: 700;
  text-transform: uppercase;
}

@media (min-width: 990px) {
  .internal-icons {
    display: flex;
    flex-direction: row;
  }

  .internal-icons-layout {
    display: flex;
    flex-wrap: wrap;
  }

  .internal-icons-icon {
    margin: 0;
    width: 33.333%;
    height: 100%;
    padding: 15px;
  }

  .internal-icons-content {
    flex: 1;
  }

}
<div class="internal-icons">
  <div class="internal-icons-layout">
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem Ipsum Dolor
        </div>
        <div class="internal-icons-content">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </div>
      </div>
    </div>
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem ipsum dolor
        </div>
        <div class="internal-icons-content">
          Fusce cursus, risus eu rhoncus varius, ipsum massa eleifend libero, non accumsan elit felis et augue.
        </div>
      </div>
    </div>
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem ipsum dolor
        </div>
        <div class="internal-icons-content">
          Fusce cursus, risus eu rhoncus varius, ipsum massa eleifend libero, non accumsan elit felis et augue.
          <br />
          <strong>LOREM IPSUM DOLOR >></strong>
        </div>
      </div>
    </div>
  </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2025-01-17 09:40:30

.internal-icons-layout 中删除 height: 100% 并将其放在 .internal-icons-icon>div 上。

最嵌套的容器必须具有 100% 的高度,并且所有父容器都会自然扩展以适合您的 100% 高度容器,因此您只需将 100% 高度放在 .internal-icons-icon>div.现在,所有列都将扩展以适合弹性容器,该容器将与最高的列一样高。

还在 .internal-icons-icon 上添加了 flex-grow: 1 ,以便当您的列变成行时,它们会增长以填充宽度。

* {
  box-sizing: border-box;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 15px;
  line-height: 1.42857143;
  color: #565757;
}

.internal-icons-icon {
  margin: 20px 0;
  height: 100%;
  flex-grow: 1;
}

.internal-icons-layout {
  display: flex;
  flex-wrap: wrap;
}

.internal-icons-icon>div {
  height: 100%;
}

.internal-icons-icon>div {
  padding: 30px;
  text-align: center;
  border: 1px solid #575859;
  color: #565757;
  text-decoration: none;
  box-shadow: 2px 4px 10px 8px rgb(0 0 0 / 10%);
}

.internal-icons-icon div {
  margin: 20px 0;
}

.internal-icons-content {
  font-size: 16px;
}

.internal-icons-title {
  font-size: 22px;
  font-weight: 700;
  text-transform: uppercase;
}

@media (min-width: 990px) {
  .internal-icons {
    display: flex;
    flex-direction: row;
  }
  .internal-icons-layout {
    display: flex;
    flex-wrap: wrap;
  }
  .internal-icons-icon {
    margin: 0;
    width: 33.333%;
    height: 100%;
    padding: 15px;
  }
  .internal-icons-content {
    flex: 1;
  }
}
<div class="internal-icons">
  <div class="internal-icons-layout">
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem Ipsum Dolor
        </div>
        <div class="internal-icons-content">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </div>
      </div>
    </div>
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem ipsum dolor
        </div>
        <div class="internal-icons-content">
          Fusce cursus, risus eu rhoncus varius, ipsum massa eleifend libero, non accumsan elit felis et augue.
        </div>
      </div>
    </div>
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem ipsum dolor
        </div>
        <div class="internal-icons-content">
          Fusce cursus, risus eu rhoncus varius, ipsum massa eleifend libero, non accumsan elit felis et augue.
          <br />
          <strong>LOREM IPSUM DOLOR >></strong>
        </div>
      </div>
    </div>
  </div>
</div>

Remove height: 100% from .internal-icons-layout and put it on .internal-icons-icon>div.

The most nested container has to have the 100% height, and all parent containers will naturally expand to fit your 100% height container, so you only have to put 100% height on .internal-icons-icon>div. Now all your columns will expand to fit the flex container, which will be as tall as the tallest column.

Also added flex-grow: 1 on .internal-icons-icon so that when your columns become rows, they will grow to fill the width.

* {
  box-sizing: border-box;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 15px;
  line-height: 1.42857143;
  color: #565757;
}

.internal-icons-icon {
  margin: 20px 0;
  height: 100%;
  flex-grow: 1;
}

.internal-icons-layout {
  display: flex;
  flex-wrap: wrap;
}

.internal-icons-icon>div {
  height: 100%;
}

.internal-icons-icon>div {
  padding: 30px;
  text-align: center;
  border: 1px solid #575859;
  color: #565757;
  text-decoration: none;
  box-shadow: 2px 4px 10px 8px rgb(0 0 0 / 10%);
}

.internal-icons-icon div {
  margin: 20px 0;
}

.internal-icons-content {
  font-size: 16px;
}

.internal-icons-title {
  font-size: 22px;
  font-weight: 700;
  text-transform: uppercase;
}

@media (min-width: 990px) {
  .internal-icons {
    display: flex;
    flex-direction: row;
  }
  .internal-icons-layout {
    display: flex;
    flex-wrap: wrap;
  }
  .internal-icons-icon {
    margin: 0;
    width: 33.333%;
    height: 100%;
    padding: 15px;
  }
  .internal-icons-content {
    flex: 1;
  }
}
<div class="internal-icons">
  <div class="internal-icons-layout">
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem Ipsum Dolor
        </div>
        <div class="internal-icons-content">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        </div>
      </div>
    </div>
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem ipsum dolor
        </div>
        <div class="internal-icons-content">
          Fusce cursus, risus eu rhoncus varius, ipsum massa eleifend libero, non accumsan elit felis et augue.
        </div>
      </div>
    </div>
    <div class="internal-icons-icon">
      <div>
        <div class="internal-icons-image">
          <img src="https://img.icons8.com/fluency/85/000000/money.png" />
        </div>
        <div class="internal-icons-title">
          Lorem ipsum dolor
        </div>
        <div class="internal-icons-content">
          Fusce cursus, risus eu rhoncus varius, ipsum massa eleifend libero, non accumsan elit felis et augue.
          <br />
          <strong>LOREM IPSUM DOLOR >></strong>
        </div>
      </div>
    </div>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文