简体   繁体   English

flex-direction 列弹性容器中弹性项目的高度相同

[英]Same height for flex items in the flex-direction column flex container

Is there a way to have flex items within a flex container with flex-direction: column have the same height, using CSS only?有没有办法让 flex 容器中的 flex 项目与flex-direction: column具有相同的高度,仅使用 CSS?

<div style="display: flex; flex-direction: column;">
 <div class="flex-item">
   <!-- other html elements with image, links, button etc. -->
 </div>
 <!-- ... -->
 <div class="flex-item"></div>
</div>

JSFiddle: https://jsfiddle.net/hzxa9v54/ JSFiddle: https ://jsfiddle.net/hzxa9v54/

You will need a script to solve that using Flexbox, here done using jQuery.您将需要一个脚本来使用 Flexbox 解决该问题,此处使用 jQuery 完成。

Updated fiddle更新的小提琴

Stack snippet堆栈片段

 (function ($) { // preload object array to gain performance var $items = $('.item') // run at resize $( window ).resize(function() { $.fn.setHeight(0); }); $.fn.setHeight = function(height) { // reset to auto or else we can't check height $($items).css({ 'height': 'auto' }); // get highest value $($items).each(function(i, obj) { height = Math.max(height, $(obj).outerHeight()) }); // set the height $($items).css({ 'height': height + 'px' }); } // run at load $.fn.setHeight(0); }(jQuery));
 .container { display: flex; flex-direction: column; width: 200px; } .item { border: 1px solid #ddd; margin: 10px 0; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="item"> come content 1 </div> <div class="item"> come content 2 come content 3 </div> <div class="item"> come content 4 come content 5 come content 6 </div> <div class="item"> come content 7 </div> </div>


Update based on a comment, where eg also loading images needs to be taken into account.基于评论进行更新,例如还需要考虑加载图像。

Here is an updated version of the script, that adjust after image loads.这是脚本的更新版本,可在图像加载后进行调整。

 (function ($) { // preload object array to gain performance var $items = $('.item') // run at resize $( window ).resize(function() { $.fn.setHeight(0); }); $.fn.setHeight = function(height) { // reset to auto or else we can't check height $($items).css({ 'height': 'auto' }); // get highest value $($items).each(function(i, obj) { height = Math.max(height, $(obj).outerHeight()) }); // set the height $($items).css({ 'height': height + 'px' }); } function imageLoaded() { $.fn.setHeight(0); } var images = $('.item img'); if (images) { images.each(function() { if( this.complete ) { imageLoaded.call( this ); } else { $(this).one('load', imageLoaded); } }); } else { // run at load $.fn.setHeight(0); } }(jQuery));
 .container { display: flex; flex-direction: column; width: 200px; } .item { border: 1px solid #ddd; margin: 10px 0; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="item"> come content 1 </div> <div class="item"> <img src="http://placehold.it/200x100/f00" alt=""> </div> <div class="item"> come content 4 come content 5 come content 6 </div> <div class="item"> come content 7 </div> </div>

UPDATED: using your fiddle, i made slight modifications and used flex: 1 1 100%;更新:使用你的小提琴,我做了一些修改并使用了flex: 1 1 100%; on the flex children and it appears to be working.在 flex 孩子上,它似乎正在工作。

updated fiddle: https://jsfiddle.net/hzxa9v54/4/更新小提琴: https : //jsfiddle.net/hzxa9v54/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM