html - 当父级已经使用 flex 来调整内容时,使用 flexbox 使用等高的 css 类

标签 html css flexbox

我有多行 3 列。在每一列中都有一个主要内容 SavedSearchBox-content(具有不同的排列,因此高度可以不同)和一个页脚 SavedSearchBox-footer

我正在使用 flexbox 以便我可以使用 3 列并且之间有空间,所以只有中间列有水平空间。

这是我的示例行的 HTML 结构,其中包含 3 列和 3 种排列。

  • 使用药丸和按钮
  • 没有药丸,没有按钮
  • 有药丸,没有按钮
<div class="SavedSearches-wrapper">
  <div class="SavedSearchBox">
    <div class="SavedSearchBox-content">
      <h1 class="SavedSearchBox-title">This is a longer title for testing</h1>
      <p class="SavedSearchBox-tagline">This is a tag line</p>
      <a href="javascript:;" class="SavedSearchBox-searchLink">This is a link</a>
      <div class="SavedSearchBox-propertyCount">
        <div class="Pill">4 New</div> <strong>searches</strong>
      </div>
    </div>
    <div class="SavedSearchBox-footer"><a class="Button ">Subscribe to this Alert</a></div>
  </div>
  <div class="SavedSearchBox">
    <div class="SavedSearchBox-content"><h1 class="SavedSearchBox-title">This is a title</h1>
      <p class="SavedSearchBox-tagline">This is a tagline</p>
      <a href="javascript:;" class="SavedSearchBox-searchLink">This is a link</a>
      <div class="SavedSearchBox-propertyCount"></div>
    </div>
    <div class="SavedSearchBox-footer">
      <div class="RadioListToggler SavedSearchBox-Toggler">
        <a href="javascript:;" class="RadioListToggler-Link">Change alert frequency</a>
      </div>
    </div>
  </div>
  <div class="SavedSearchBox">
    <div class="SavedSearchBox-content"><h1 class="SavedSearchBox-title">This is a title</h1>
      <p class="SavedSearchBox-tagline">This is a tagline</p>
      <a href="javascript:;" class="SavedSearchBox-searchLink">This is a link</a>
      <div class="SavedSearchBox-propertyCount">
        <div class="Pill">6 New</div> <strong>searches</strong>
      </div>
    </div>
    <div class="SavedSearchBox-footer">
      <div class="RadioListToggler SavedSearchBox-Toggler">
        <a href="javascript:;" class="RadioListToggler-Link">Change alert frequency</a>
      </div>
    </div>
  </div>
</div>

这是我的 css,包装器使用 flex 和 justify 内容。

.SavedSearches-wrapper {
    padding: 0;
    margin: 0;
    list-style: none;
    display: flex;
    flex-flow: row wrap;
    width: 100%;
    justify-content: space-between;
}

.SavedSearchBox {
    max-width: 340px;
    width: calc((100%/3) - 64px);
    border: 1px solid #d8d8d8;
    display: inline-block;
    vertical-align: top;
    margin: 20px 0px;
    background: #ffffff;
}

.SavedSearchBox .SavedSearchBox-content {
    padding: 22px 22px 0px 22px;
}

我的问题是,有没有一种方法可以让所有 SavedSearchBox-content 具有相同的高度,同时又不会影响 space-between 的 3 列布局?如果可能的话,我不希望我们使用 JavaScript 解决方案。

JSFiddle

Screenshot

最佳答案

不,我认为仅使用 css 无法使元素的高度相等。但是您可以使用简单的 jquery 代码使其工作。

 function kb_equal_height_two() {
  var highest_element_two = 0;

  // Lösche die Höhe
  $('.SavedSearchBox-content').each(function() {
    $(this).removeAttr('style');
  });

  // Prüfe, welches Element am höchsten ist
  $('.SavedSearchBox-content').each(function() {
    if ($(this).height() > highest_element_two) {
      highest_element_two = $(this).height();
    }
  });

  // Weise diese Höhe allen Elementen zu.
  $('.SavedSearchBox-content').each(function() {
    $(this).height(highest_element_two);
  });
};
var resizeTimer_two;
$(window).resize(function() {
  clearTimeout(resizeTimer_two);
  resizeTimer_two = setTimeout(kb_equal_height_two, 1);
});

只需在页面加载时添加一个加载事件以均衡高度。 https://jsfiddle.net/sqk8howx/1/

-> 只有调整窗口大小才有效

关于html - 当父级已经使用 flex 来调整内容时,使用 flexbox 使用等高的 css 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55691765/

相关文章:

javascript - 是否可以仅使用 HTML(5) 和 JavaScript 构建 Torrent 客户端?

java - 在javascript中调用java方法

css - 嵌套的、居中的、CSS 网格列出现在负 X 轴的屏幕外,即使是 "overflow-x: scroll"

html - 如何扩展 React 组件以使用 Flexbox 填充页面

css - 如何创建一个内容居中并在包裹时保持居中的 flexbox 容器?

html - 将 html img 标签转换为 css

html - 具有多列布局的两个 div 的 CSS3 宽度,并排

html - 浏览器中的图像比在 css 中设置的大

html - 具有相对位置和高度的父级自动不会扩展到 float 子级的高度?

html - 如何更改 :active link color Twitter Bootstrap