【问题标题】:Flex items with same height in same row同一行中具有相同高度的 Flex 项目
【发布时间】:2019-04-13 14:51:35
【问题描述】:

我有一个 div (.news-container) 显示 flex。在那个 div 里面,我有 5 个项目,第一个是 100% 宽度,另外 4 个是 50% 宽度。问题是这 4 个项目的高度不同,即使它们是带有align-content: stretch 的弹性项目。当我们设置父显示弹性时,它们应该具有相同的高度还是我在这里做错了什么?

body {
  font-family: "Open Sans", Arial, sans-serif;
}

a {
  text-decoration: none;
}

h1,
h2,
h3,
h4,
h5 {
  margin: 0 0 20px 0;
}

.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

.content {
  margin: 20px auto;
}

.float-content {
  float: left;
}

.left-content {
  width: 60%;
}

.right-content {
  width: 40%;
}

.news-container {
  display: flex;
  flex-wrap: wrap;
  padding-right: 5px;
  overflow: hidden;
  align-items: stretch;
  -webkit-align-content: stretch;
  -ms-flex-line-pack: stretch;
  align-content: stretch;
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

.news-list-container {
  padding-left: 5px;
}

.news-container .item {
  position: relative;
  margin-bottom: 10px;
  width: calc(50% - 5px);
  padding: 0;
}

.news-container .item:nth-child(even) {
  margin-right: 10px;
}

.news-container .item:first-child {
  width: 100%;
  margin-right: 0;
}

.news-container .item .news-link {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  background: #DC191B;
  color: #fff;
  padding: 10px;
  text-transform: uppercase;
}

.news-container .item img {
  width: 100%;
  display: block;
  margin-bottom: 0;
}

.news-container .item a.caption {
  display: block;
  padding: 10px 20px 13px 20px;
  background-color: #000;
  color: #fff;
  font-family: 'Roboto Condensed', sans-serif;
  font-size: 28px;
  font-weight: bold;
}

@media screen and (min-width: 1200px) {
  .content {
    max-width: 970px;
  }
}
<div class="wrapper">
  <div class="content clearfix">
    <div class="left-content float-content">
      <div class="news-container">
        <div class="item">
          <a class="news-link" href="#">
                        New
                    </a>
          <img src="https://www.w3schools.com/html/pic_trulli.jpg" />
          <a class="caption" href="#" target="_blank">
                        Lorem ipsum
                    </a>
        </div>
        <div class="item">
          <img src="https://www.w3schools.com/html/pic_trulli.jpg" />
          <a class="caption" href="#" target="_blank">
                        Lorem ipsum dolor sit amet
                    </a>
        </div>
        <div class="item">
          <img src="https://www.w3schools.com/html/pic_trulli.jpg" />
          <a class="caption" href="#" target="_blank">
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi a turpis sagittis, viverra nibh a, lobortis libero.
                    </a>
        </div>
        <div class="item">
          <img src="https://www.w3schools.com/html/pic_trulli.jpg" />
          <a class="caption" href="#" target="_blank">
                        Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...
                    </a>
        </div>
        <div class="item">
          <img src="https://www.w3schools.com/html/pic_trulli.jpg" />
          <a class="caption" href="#" target="_blank">
                        Quisque sed tincidunt neque. Sed ut lacinia ex.
                    </a>
        </div>
      </div>
    </div>
    <div class="right-content float-content">
      <div class="news-list-container">
        <h2>
          Latest update
        </h2>
        <ul>
          <li>
            News Number 1
          </li>
          <li>
            News Number 2
          </li>
          <li>
            News Number 3
          </li>
          <li>
            News Number 4
          </li>
        </ul>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

  • css不可能做到同样的高度。您应该尝试使用 jQuery 来获取项目的最大高度(不包括第一项)并将所有设置为这个高度。
  • 一个兄弟姐妹应该如何知道另一个兄弟姐妹的身高?这超出了 CSS 的范围。你需要一个脚本。

标签: html css flexbox


【解决方案1】:

实际上,您的物品高度相同。只是每个项目中的内容没有扩展到覆盖项目的高度。这是一个例子:

如开发工具大纲所示,左侧项目(内容较短)与右侧项目高度相同。

您可以在项目上使用display: flex,以便将align-items: stretch 应用于内容。

将此添加到您的代码中:

.news-container .item:nth-child(2),
.news-container .item:nth-child(4),
.news-container .item:nth-child(5) {
  display: flex;
  flex-wrap: wrap;
}

现在内容填充了每个项目。

jsFiddle demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 2016-09-29
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    相关资源
    最近更新 更多