简体   繁体   English

CSS Flexbox包装第一行

[英]CSS Flexbox Wrap First of Line

I'm struggling with the wrapping option of the css flexbox in combination with margins between the items. 我正在努力将CSS flexbox的包装选项与项目之间的边距结合在一起。

What I'm trying to archieve is: Have a flexcontainer with wrapping enabled and a number of items with variable with and minimum width. 我要归档的内容是:有一个启用包装的flexcontainer容器,以及一些具有最小宽度和可变宽度的项目。 Between these items I want to have a small gap (margin) but I don't want a margin between the item and the container on the left or right side. 在这些项目之间,我希望有一个小的间隙(边距),但是我不想在项目与左侧或右侧的容器之间有一个边距。

Right now I'm using the first-child and last-child pseudo class but this approach doesn't work unfortunatly when the container wraps the content. 现在,我使用的是第一个孩子和最后一个孩子的伪类,但是当容器包装内容时,这种方法并不奏效。 See JsFiddle for a minimalistic demo. 有关简单演示,请参见JsFiddle

HTML HTML

<div class="container">
    <div class="item">A</div>
    <div class="item">B</div>
    <div class="item">C</div>
    <div class="item">D</div>
    <div class="item">E</div>
</div>

CSS CSS

.container {
    display: flex;
    flex-wrap: wrap;
    border: 1px solid red;
}
.item {
    min-width: 100px;
    flex: 1;
    border: 1px solid black;
    margin-left: 20px;
    margin-right: 20px;
}
.item:first-child {
    margin-left: 0px;
}
.item:last-child {
    margin-right: 0px;
}

Any suggestions? 有什么建议么?

flexbox has some specific properties, so in your case this is what needs to be fixed: flexbox具有一些特定的属性,因此在您的情况下,这是需要修复的:

  • instead of min-width use flex-basis 代替min-width使用flex-basis
  • use justify-content: space-between 使用justify-content: space-between
  • to apply margin on items there's a special hack (check out the example -to fix border coherency apply a wrapper); 要在项目上应用边距,有一个特殊的技巧(请查看示例-要解决边界一致性,请使用包装器);

 .container-bg { width:100%; border: 1px solid green; overflow:hidden; } .container { display: flex; flex-wrap: wrap; justify-content: space-between; /* border: 1px solid red;*/ margin: 0 -20px; } .item { /*min-width: 100px;*/ flex: 1 0 100px;/*if you want to set 100px as min-width you'll have to set flex-shrink to 0*/ border: 1px solid black; margin: 0 20px; } /*.item:first-child { margin-left: 0px; } .item:last-child { margin-right: 0px; }*/ 
 <p>resize the window for wrapping</p> <div class="container-bg"><div class="container"> <div class="item">A</div> <div class="item">B</div> <div class="item">C</div> <div class="item">D</div> <div class="item">E</div> </div></div> 

EDIT : 编辑:

set flex-shrink to 0 is don't want to go below a certain width.. flex-shrink设置为0是不希望低于某个宽度。

If you add another wrapper around your items, you can use this dirty hack: https://jsfiddle.net/xw5uo2j1/13/ 如果您在商品周围添加另一个包装器,则可以使用此肮脏的技巧: https : //jsfiddle.net/xw5uo2j1/13/

You set your main container to be 100% of the width, then your "sub" container to be 100% plus the amount of space between you're going to add. 将主容器设置为宽度的100%,然后将“子”容器设置为宽度的100%加上要添加的空间。 Then add the same amount of margin-right to the items. 然后向项目添加相同数量的margin-right

 .maincontainer { width:100%; border: 1px solid red; } .container { display: flex; flex-flow: row wrap; justify-content:space-between; align-content:space-between; width:calc(100% + 5px); } .item { min-width: 100px; flex: 1; border: 1px solid black; margin-right:5px; } 
 <p>resize the window for wrapping</p> <div class="maincontainer"> <div class="container"> <div class="item">A</div> <div class="item">B</div> <div class="item">C</div> <div class="item">D</div> <div class="item">E</div> </div> </div> 

UPDATED, with more than 5 items 更新,超过5个项目

Use calc and jusify-content: space-between; 使用calcjusify-content: space-between;

 * { box-sizing: border-box; margin: 0; } .container { border: thin solid red; display: -webkit-flex; display: flex; -webkit-flex-wrap: wrap; flex-wrap: wrap; margin: 0 -10px; } .item { border: thin solid black; -webkit-flex: 1 1 calc(20% - 20px); flex: 1 1 calc(20% - 20px); margin: 0 10px; padding: 20px; position: relative; } 
 <div class="container"> <div class="item">A</div> <div class="item">B</div> <div class="item">C</div> <div class="item">D</div> <div class="item">E</div> </div> 

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

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