简体   繁体   English

两个 div 并排使用 flex

[英]Two divs side by side with flex

I have 4 cells in laptops mode And when I want to have two cells side by side in phone mode I have to remove the flex in the outer div But I can no longer have an auto height div .我在笔记本电脑模式下有 4 个单元格当我想在电话模式下并排放置两个单元格时,我必须移除外部 div 中的 flex 但我不能再拥有自动高度 div 。 How to fix this如何解决这个问题

<div class="box">
    <div class="row">
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
    </div>
</div>

CSS CSS

.box {
    height: auto;
    padding: 50px;
    background-color: red;
}
.row {
    display: flex;
}
.cell {
    float: right;
    width: 50%;
    height: 50px;
}

You must use 2 more css attribute, flex and flex-wrap.您必须使用另外 2 个 css 属性,flex 和 flex-wrap。

To ensure the cell wrap to the next line when the screen is too small, you must add the property flex-wrap : wrap to the row class.为了确保当屏幕太小时单元格换行到下一行,您必须将属性flex-wrap : wrap到 row 类。

Then you need to define a flex property for the cell.然后你需要为单元格定义一个flex属性。

The flex property is defined like that flex属性是这样定义的

flex : flex-grow flex-shrink flex-basis;

Flex-grow and flex-shrink are grow and shrink coefficient. flex-grow 和 flex-shrink 是增长和收缩系数。 Here, all the cell must have the same width, so we do not care about this value (I've set these to 1).在这里,所有单元格必须具有相同的宽度,所以我们不关心这个值(我已经将这些设置为 1)。 Flex-basis indicate the default width of your cell flex-basis 表示单元格的默认宽度

Here's an example :这是一个例子:

 .box { height: auto; padding: 50px; background-color: red; } .row { display: flex; flex-wrap:wrap; } .cell { flex: 1 1 200px; height: 50px; border:1px solid black; background-color: blue; }
 <div class="box"> <div class="row"> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> </div> </div>

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

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