简体   繁体   English

CSS Flexbox布局,带有两个动态列和三个项目

[英]CSS Flexbox Layout with two dynamic columns and three items

What I want to do is have two colums: One main column on the left and one column on the right with 2 items in it. 我要做的是有两个列:左边一列,右边一列,其中有2项。 The problem is, that the heights of all three items and therefore the entire container vary wildly, which prohibits the layout from wrapping. 问题在于,所有三个项目的高度以及整个容器的高度都发生了巨大的变化,这阻碍了布局的包装。

I cannot put the right items into one div, because I need to move one of them to the top on mobile (via order). 我无法将正确的项目放到一个div中,因为我需要将其中一项移动到移动设备的顶部(通过订单)。

Basically, I want to achieve the result below, without having to set the container to a fixed height. 基本上,我想实现以下结果,而不必将容器设置为固定高度。

 .flex { display: flex; flex-wrap: wrap; flex-direction: column; height: 800px; } .child--main { height: 800px; width: calc(200% / 3); color: #fff; text-align: center; text-transform: uppercase; background-color: #6b6bef; line-height: 800px; flex-basis: 100%; } .child--video { height: 300px; width: calc(100% / 3); background-color: #f1b36a; color: white; text-align: center; line-height: 300px; text-transform: uppercase; } .child--sidebar { height: 400px; width: calc(100% / 3); text-align: center; line-height: 400px; color: #fff; background-color: #81ca3a; text-transform: uppercase; } 
 <div class="flex"> <div class="child child--main">main</div> <div class="child child--video">video</div> <div class="child child--sidebar">sidebar</div> </div> 

This is an alternative to the flexbox. 这是flexbox的替代方法。

Hope this help you. 希望这对您有所帮助。

 $(document).ready(function() { setInterval(function(){ if (parseInt($('.child--main').css('height'), 10) == 1000) { $('.child--main').animate({'height': '100px'}, 1000); } else { $('.child--main').animate({'height': '1000px'}, 1000); } }, 2000); }); 
 .flex { /*display: flex; flex-wrap: wrap; flex-direction: column; height: 800px;*/ } .child--main { float:left; height: 800px; width: calc(200% / 3); color: #fff; text-align: center; text-transform: uppercase; background-color: #6b6bef; line-height: 800px; flex-basis: 100%; } .child--video { float:right; height: 300px; width: calc(100% / 3); background-color: #f1b36a; color: white; text-align: center; line-height: 300px; text-transform: uppercase; } .child--sidebar { float:right; clear:right; /* this for a small height body */ height: 400px; width: calc(100% / 3); text-align: center; line-height: 400px; color: #fff; background-color: #81ca3a; text-transform: uppercase; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="flex"> <div class="child child--main">main</div> <div class="child child--video">video</div> <div class="child child--sidebar">sidebar</div> </div> 

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

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