等高多列/多元素布局,只有CSS [重复]

问题描述 投票:-1回答:1

我想看看是否有人知道一个技巧来实现这个问题,只使用CSS:

假设我有两个,三个或四个列布局(数字是动态的),这些列中的每一个将在内部具有5个不同的部分,并且这些部分中的每一个将具有可变数量的内容。

是否可以使用CSS(而不是JS均衡器解决方案)将每个部分与兄弟列的部分垂直对齐,即使它们的内容不是相同的高度?

我知道使用表格会很容易,但如果有超过四个项目,我必须将布局完全切换到行。

我试图使用Flexbox但没有达到预期的结果,我不确定我可以使用具有可变数量列的网格。

我试图使用Flexbox来实现它,如Codepen https://codepen.io/ramonlapenta/full/gowLqw/所示

由于.item-compare div已经拉伸到与其兄弟姐妹相同的高度,因为能够使每个部分在可用空间中均匀地(伸展)生长的原因,但这不起作用,因为增长/收缩属性指的是可用的空间,而不是项目的内容。

html css flexbox grid-layout css-grid
1个回答
1
投票

这可以使用CSS Grid,但您必须拥有一个容器,因此HTML结构必须完全不同。

就像是:

enter image description here

哪个确实有一些逻辑......

这是自动运作的,因为只有3组项目所以repeat(3, 1fr)是一个神奇的数字。

对于更动态的方法,有自动调整选项。

此外,您可以定义每个班级将坐在哪个grid-row上。例如:

.item-compare-title {
 grid-row:2;
}

等等

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-column-gap: 1em;
  width: 95%;
  margin: auto;
}

.container>div {
  border: 1px solid grey;
}

.item-compare-img img {
  max-width: 100%;
  height: auto;
}

.item-compare-title {
  display: flex;
  flex-direction: column;
  text-align: center;
}

.item-compare-model {
  margin-top: auto;
}
<div class="container">
  <div class="item-compare-img">
    <img src="http://www.cagpurification.com/images/sce/product_images/Chiller%20Picture%201.jpg" alt="Chiller">
  </div>
  <div class="item-compare-img">
    <img src="http://www.cagpurification.com/images/sce/product_images/Chiller%20Picture%201.jpg" alt="Chiller">
  </div>
  <div class="item-compare-img">
    <img src="http://www.cagpurification.com/images/sce/product_images/Chiller%20Picture%201.jpg" alt="Chiller">
  </div>
  <div class="item-compare-title">
    <h4>Ecodan Heatpump that pumps heat out to the world</h4>
    <div class="item-compare-model">
      <p>PUHZ-W682387H / 5KW</p>
    </div>
  </div>
  <div class="item-compare-title">
    <h4>Ecodan Heatpump</h4>
    <div class="item-compare-model">
      <p>PUHZ-W682387H / 5KW</p>
    </div>
  </div>
  <div class="item-compare-title">
    <h4>Ecodan Heatpump</h4>
    <div class="item-compare-model">
      <p>PUHZ-W682387H / 5KW</p>
    </div>
  </div>
  <div class="item-compare-feat">
    <ul class="list-compare-feat">
      <li class="compare-feat-item">USP / Feature goes here</li>
      <li class="compare-feat-item">USP / Feature goes here</li>
    </ul>
  </div>
  <div class="item-compare-feat">
    <ul class="list-compare-feat">
      <li class="compare-feat-item">USP / Feature goes here</li>
      <li class="compare-feat-item">USP / Feature goes here with a longer title for testing</li>
      <li class="compare-feat-item">USP / Feature goes here another long title</li>
      <li class="compare-feat-item">USP / Feature goes here</li>
    </ul>
  </div>
  <div class="item-compare-feat">
    <ul class="list-compare-feat">
      <li class="compare-feat-item">USP / Feature goes here</li>
      <li class="compare-feat-item">USP / Feature goes here</li>
      <li class="compare-feat-item">USP / Feature goes here</li>
    </ul>
  </div>
  <div class="item-compare-attr">
    <ul class="list-compare-attr">
      <li class="compare-attr-item odd">
        <span class="compare-attr-title">Net dimensions</span>
        <span class="compare-attr-value">154 x 558 x 557 mm</span>
      </li>
      <li class="compare-attr-item even">
        <span class="compare-attr-title">Cooling capacity</span>
        <span class="compare-attr-value">3.4 kW</span>
      </li>
      <li class="compare-attr-item odd">
        <span class="compare-attr-title">ErP Rating</span>
        <span class="compare-attr-value">A+++ / A+++</span>
      </li>
      <li class="compare-attr-item even">
        <span class="compare-attr-title">Noise Level</span>
        <span class="compare-attr-value">23 dB</span>
      </li>
      <li class="compare-attr-item odd">
        <span class="compare-attr-title">Something</span>
        <span class="compare-attr-value">655</span>
      </li>
    </ul>
  </div>
  <div class="item-compare-attr">
    <ul class="list-compare-attr">
      <li class="compare-attr-item odd">
        <span class="compare-attr-title">Net dimensions</span>
        <span class="compare-attr-value">154 x 558 x 557 mm</span>
      </li>
      <li class="compare-attr-item even">
        <span class="compare-attr-title">Cooling capacity</span>
        <span class="compare-attr-value">3.4 kW</span>
      </li>
      <li class="compare-attr-item even">
        <span class="compare-attr-title">Noise Level</span>
        <span class="compare-attr-value">23 dB</span>
      </li>
      <li class="compare-attr-item odd">
        <span class="compare-attr-title">Something</span>
        <span class="compare-attr-value">655</span>
      </li>
      <li class="compare-attr-item even">
        <span class="compare-attr-title">Noise Level</span>
        <span class="compare-attr-value">23 dB</span>
      </li>
      <li class="compare-attr-item odd">
        <span class="compare-attr-title">Something</span>
        <span class="compare-attr-value">655</span>
      </li>
    </ul>
  </div>
  <div class="item-compare-attr">
    <ul class="list-compare-attr">
      <li class="compare-attr-item odd">
        <span class="compare-attr-title">Net dimensions</span>
        <span class="compare-attr-value">154 x 558 x 557 mm</span>
      </li>
      <li class="compare-attr-item even">
        <span class="compare-attr-title">Cooling capacity</span>
        <span class="compare-attr-value">3.4 kW</span>
      </li>
      <li class="compare-attr-item odd">
        <span class="compare-attr-title">ErP Rating</span>
        <span class="compare-attr-value">A+++ / A+++</span>
      </li>
      <li class="compare-attr-item even">
        <span class="compare-attr-title">Noise Level</span>
        <span class="compare-attr-value">23 dB</span>
      </li>
      <li class="compare-attr-item odd">
        <span class="compare-attr-title">Something</span>
        <span class="compare-attr-value">655</span>
      </li>
    </ul>
  </div>
</div>

Codepen.io demo

© www.soinside.com 2019 - 2024. All rights reserved.