每行*创建具有相等布局的flexbox行

时间:2019-07-13 08:02:54

标签: html css flexbox

我正在尝试实现一个flexbox,在该行中所有标题都将排成一行。要求:

  1. 图像的高度不一定相同
  2. 描述不一定总是相同的高度
  3. 标题可以是1行,也可以是3行(取决于长度)

这是一个简单的小提琴:

https://jsfiddle.net/youradds/r56j4uLe/6/

如您所见,这就是您得到的:

enter image description here

这是我所追求的:

enter image description here

我的 SCSS 是:

#item-wrapper {

    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;

    .item-block {

    background: yellow;

        flex-grow: 0;
        width: 350px;
        margin: 2rem 1rem;

        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-content: center;
        align-items: center;

        .what-logo {

            img {
                max-height: 100%;
            }
        }

        .text-info {
            flex-grow: 1;

            .desc {
                padding: 1rem;
            }

            h2 {
                flex-grow: 0;
            }

        }


        .action-button {
            margin: 0 auto;
        }



    }
}

.pure-img {
  max-width: 100%;
  height: auto;
  display: block;
}

..并测试HTML:

<div id="item-wrapper">
    <div class="item-block">
        <div class="what-logo">
            <img src="https://bodywisegym.co.uk/2018/images/events/3/3-1562951826-3.png" class="pure-img">
        </div>
        <div class="text-info">
            <h2>Zydrunas Savickas Seminar</h2>
            <div class="desc">Bodywise Gym and Studios are proud to announce we are bringing the greatest strongman Zydrunas Savickas (Big Z) to Horsham for a seminar.</div>
        </div>
        <a href="https://yogida.co.uk/collections/events/products/zydrunas-savickas-big-z-seminar" class="action-button">Find out more &raquo;</a>
    </div>

    <div class="item-block">
        <div class="what-logo">
            <img src="https://bodywisegym.co.uk/2018/images/events/7/7-1562936970-7.jpg" class="pure-img">
        </div>
        <div class="text-info">
            <h2>Class for mums</h2>
            <div class="desc">Join Quick HIIT</div>
        </div>
        <a href="" class="action-button">Find out more &raquo;</a>
    </div>

    <div class="item-block">
        <div class="what-logo">
            <img src="https://bodywisegym.co.uk/2018/images/events/6/6-1562936464-6.png" class="pure-img">
        </div>
        <div class="text-info">
            <h2>Gratitude Day</h2>
            <div class="desc">To spread the positivity you can bring a training partner along to workout with you or to attend one of our classes.</div>
        </div>
        <a href="" class="action-button">Find out more &raquo;</a>
    </div>

    <div class="item-block">
        <div class="what-logo">
            <img src="https://bodywisegym.co.uk/2018/images/events/4/4-1562951950-4.jpg" class="pure-img">
        </div>
        <div class="text-info">
            <h2>The experience of non-duality</h2>
            <div class="desc">Yoga & meditation workshop with Indian Spiritual Master Acharya Shree Shankar </div>
        </div>
        <a href="https://yogida.co.uk/collections/workshops" class="action-button">Find out more &raquo;</a>
    </div>
</div>  

重要说明::这是一种响应式设计,因此我无法在图像div上设置高度(例如,最小高度为600px),因为在较宽的屏幕上,这种排序方式如下:< / p>

enter image description here

...但是在较小的屏幕上,它会按比例缩小到每行1个元素-这意味着我们在图像和图像较小的条目的标题之间会有大量填充:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可能会寻求视觉上的折衷。

flex子级与其他flex父级的flex子级不对齐。

您可以尝试将tex-infowhat-logo居中,并在.desc上添加平均最小高度

下面的演示,以整页播放,以测试行为和视觉效果。

#item-wrapper {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}

#item-wrapper .item-block {
  background: yellow;
  max-width: 350px;
  margin: 2rem 1rem;
  padding:2px;/* see me */
  display: flex;
  flex-direction: column;
  align-items: center;
}

#item-wrapper .item-block .what-logo img {
  max-height: 100%;
}

#item-wrapper .item-block .text-info,
#item-wrapper .item-block .what-logo {/* update */
  margin-top: auto;
}

#item-wrapper .item-block .text-info .desc {
  padding: 1rem;
  min-height: 4em;/* 3 lines , average */
}

#item-wrapper .item-block .text-info h2 {text-align:center}

#item-wrapper .item-block .action-button {
  margin: 0 auto;
}

.pure-img {
  max-width: 100%;
  height: auto;
  display: block;
}
<div id="item-wrapper">
  <div class="item-block">
    <div class="what-logo">
      <img src="https://bodywisegym.co.uk/2018/images/events/3/3-1562951826-3.png" class="pure-img">
    </div>
    <div class="text-info">
      <h2>Zydrunas Savickas Seminar</h2>
      <div class="desc">Bodywise Gym and Studios are proud to announce we are bringing the greatest strongman Zydrunas Savickas (Big Z) to Horsham for a seminar.</div>
    </div>
    <a href="https://yogida.co.uk/collections/events/products/zydrunas-savickas-big-z-seminar" class="action-button">Find out more &raquo;</a>
  </div>

  <div class="item-block">
    <div class="what-logo">
      <img src="https://bodywisegym.co.uk/2018/images/events/7/7-1562936970-7.jpg" class="pure-img">
    </div>
    <div class="text-info">
      <h2>Class for mums</h2>
      <div class="desc">Join Quick HIIT</div>
    </div>
    <a href="" class="action-button">Find out more &raquo;</a>
  </div>

  <div class="item-block">
    <div class="what-logo">
      <img src="https://bodywisegym.co.uk/2018/images/events/6/6-1562936464-6.png" class="pure-img">
    </div>
    <div class="text-info">
      <h2>Gratitude Day</h2>
      <div class="desc">To spread the positivity you can bring a training partner along to workout with you or to attend one of our classes.</div>
    </div>
    <a href="" class="action-button">Find out more &raquo;</a>
  </div>

  <div class="item-block">
    <div class="what-logo">
      <img src="https://bodywisegym.co.uk/2018/images/events/4/4-1562951950-4.jpg" class="pure-img">
    </div>
    <div class="text-info">
      <h2>The experience of non-duality</h2>
      <div class="desc">Yoga & meditation workshop with Indian Spiritual Master Acharya Shree Shankar </div>
    </div>
    <a href="https://yogida.co.uk/collections/workshops" class="action-button">Find out more &raquo;</a>
  </div>
</div>

forked fiddle