【问题标题】:Equal height children of flex items弹性项目的等高子项
【发布时间】:2016-08-11 19:10:09
【问题描述】:

我在创建 flexbox 响应式网格时遇到问题,希望有人能指出正确的方向。

我希望所有.block div 的高度相同,并且.bottom div 绝对位于底部。这实际上在当前解决方案中有效,但是 h2 标题太长并达到 2 行时,我希望该行的所有 h2 标题具有相同的高度

这在某种程度上可能吗?

我做了一个 Codepen 来说明这个问题: http://codepen.io/kenvdbroek/pen/eZKdEQ

h1,
h2,
h3 {
  margin: 0;
}
body {
  margin: 0;
  padding: 0;
}
ul.clean-list {
  margin: 0;
  padding: 0;
}
ul.clean-list li {
  list-style: none;
  margin-bottom: 5px;
}
li:last-child {
  margin-bottom: 0;
}
.container {
  padding-top: 50px;
}
.block {
  margin-bottom: 30px;
  border: 1px solid red;
}
.block > .bottom {
  border: 1px solid blue;
}
@media only screen and (min-width: 480px) {
  .row.row-flex-wrapper::before,
  .row.row-flex-wrapper::after {
    content: none !important;
  }
  .row.row-flex-wrapper::after {
    clear: none;
  }
  .row.row-flex-wrapper {
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    flex-wrap: wrap;
    -webkit-flex-wrap: wrap;
    -ms-flex-wrap: wrap;
  }
  .row.row-flex-wrapper .column {
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    float: none;
    border: 1px solid orange;
  }
  .row.row-flex-wrapper .column > .block {
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    flex-direction: column;
    -ms-flex-direction: column;
    -webkit-flex-direction: column;
  }
  .row.row-flex-wrapper .column > .block > .block-list {
    flex: 1 0 auto;
    -ms-flex: 1 0 auto;
    -webkit-flex: 1 0 auto;
  }
  .row.row-flex-wrapper .column > .block > h2 {}
}
@media only screen and (min-width: 480px) and (max-width: 767px) {
  .container .row .column {
    width: 50%;
  }
}
<div class="container">
  <div class="row row-flex-wrapper">
    <div class="column col-sm-6 col-md-4">
      <div class="block">
        <h2>Title 1</h2>
        <div class="block-list">
          <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
            with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        </div>
        <div class="bottom">
          <ul class="clean-list">
            <li><a href="/button">Some link</a>
            </li>
            <li><a href="/button">Some link 2</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="column col-sm-6 col-md-4">
      <div class="block">
        <h2>Another very long title which is actually toooo long...</h2>
        <div class="block-list">
          <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer...</p>
        </div>
        <div class="bottom">
          <ul class="clean-list">
            <li><a href="/another-button">Another button</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="column col-sm-6 col-md-4">
      <div class="block">
        <h2>Title</h2>
        <div class="block-list">
          <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer, Lorem Ipsum has been the industry's standard dummy text...</p>
        </div>
        <div class="bottom">
          <ul class="clean-list">
            <li><a href="/button">Very nice link</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="column col-sm-6 col-md-4">
      <div class="block">
        <h2>This block is cool!</h2>
        <div class="block-list">
          <p>Some text here. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer, Lorem Ipsum has been the industry's standard dummy
            text...</p>
        </div>
        <div class="bottom">
          <ul class="clean-list">
            <li><a href="/button">Another link</a>
            </li>
            <li><a href="/button">Check this item</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="column col-sm-6 col-md-4">
      <div class="block">
        <h2>Title Block</h2>
        <div class="block-list">
          <p>Go check out this item... Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer, Lorem Ipsum has been the industry's standard
            dummy text...</p>
        </div>
        <div class="bottom">
          <ul class="clean-list">
            <li><a href="/button">Button</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="column col-sm-6 col-md-4">
      <div class="block">
        <h2>Buttonssssss</h2>
        <div class="block-list">
          <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
            with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        </div>
        <div class="bottom">
          <ul class="clean-list">
            <li><a href="#button">Button</a>
            </li>
          </ul>
        </div>
      </div>
    </div>

    <div class="column col-sm-6 col-md-4">
      <div class="block">
        <h2>Title</h2>
        <div class="block-list">
          <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
        </div>
        <div class="bottom">
          <ul class="clean-list">
            <li><a href="/button">Link to item</a>
            </li>
            <li><a href="/button">Link to item</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="column col-sm-6 col-md-4">
      <div class="block">
        <h2>Title</h2>
        <div class="block-list">
          <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
            with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        </div>
        <div class="bottom">
          <ul class="clean-list">
            <li><a href="/button">Link to item</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="column col-sm-6 col-md-4">
      <div class="block">
        <h2>Another very very very very very very very long title</h2>
        <div class="block-list">
          <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
            with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        </div>
        <div class="bottom">
          <ul class="clean-list">
            <li><a href="/button">Button to item</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

标签: html css flexbox


【解决方案1】:

通常,这对于 flexbox 或 CSS 是不可能的。

弹性容器的初始设置是align-items: stretch。这会导致弹性项目扩展cross axis 的整个长度。这就是所谓的“flex 等高列”

请记住以下几点:

  • 等高列仅适用于 flex 容器的 子项。换句话说,弹性项目必须具有相同的父项。否则,等高功能不适用。

你的问题:

我希望该行的所有 h2 标题高度相同。这在某种程度上可能吗?

不使用 CSS。因为h2 存在于不同的容器中,所以它们不是兄弟姐妹(它们更像是堂兄弟),因此不适用相等的高度。

  • flexbox 中的等高列仅适用于一条 flex 行。其他行上的项目,通过包装创建,建立自己的等高线。这意味着equal height columns do not work in a multi-line flex container

  • align-self 属性可用于单个弹性项目以覆盖 align-items,这可能会破坏等高功能。

  • 通过指定弹性项目的高度(例如height: 300px),该项目的align-itemsalign-self 都会被覆盖,并且相等的高度设置将被忽略。

  • 这篇文章的重点是一个带有flex-direction: row 的容器。如果容器是flex-direction: column,则等高变为等宽。这是详细的评论:Make flex items take content width, not width of parent container

更多细节:

重复的帖子:

【讨论】:

    【解决方案2】:

    我根据您的示例制定了一个 jQuery 解决方案。

    将类“eh”(等高)添加到要对齐某些(子)子元素的每个父元素,以及包含子元素选择器的 data-eh 元素。

    <div class="row eh" data-eh='["h2",".block-list",".bottom"]'>
    

    然后使用这个函数:

    $('.eh').each(function(){  
      var $this = $(this);
      var equalHeightSelectors = $this.data('eh');  
      $.each(equalHeightSelectors, function( index, value ) { 
        var min_height = 0;
        var $children = $this.find(value);
        $children.each(function(){
          var $el = $(this);
          if($el.height() > min_height){  
            min_height = $el.height();
          }
        });
        $children.height(min_height);
      });
    });
    

    https://codepen.io/pwkip/pen/oNvxNYZ

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 2016-08-27
      • 2017-05-07
      • 2016-07-04
      • 2018-08-20
      • 1970-01-01
      • 2021-07-19
      相关资源
      最近更新 更多