flex布局

2021-08-05  本文已影响0人  sun_hl

flex弹性布局常用
https://www.ruanyifeng.com/blog/2015/07/flex-grammar.html的属性

容器的属性

项目属性:

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。

1、CSS > Flex 布局中的放大和收缩计算

2、flex布局平分宽度
css3 父元素 display:flex; 子元素给定flex占比 1的时候子元素平分

.color_container {
    width: 100%;
    height: 22px;
    display: flex;
    flex-direction: row
}
.color_container>div{
    height: 22px;
    flex: 1;
}

flex实现三栏等分布局(https://www.cnblogs.com/kunmomo/p/9891404.html)
方法一:flex
外层容器也就是ul设置display:flex,对项目也就是li设置flex:auto,代表 flex: 1 1 auto

解析:我们注意到width的设置,外层ul是500,li也是500,但是实际看到的确实li平分了ul的宽度,这是因为设置了flex:auto,代表有剩余空间的话项目等分剩余空间放大,空间不足项目等比例缩小

<style>
        ul {
            width: 500px; 
            height: 200px; 
            background: red; 
            display: flex; 
            margin: auto; 
            margin-top: 100px; 
            padding: 0 10px;
            align-items: center;
        }
        li {
            background: green; 
            height: 100px; 
            width: 500px; 
            display: inline-block; 
            margin: 2px;            
           line-height: 100px;                  
           text-align: center;       
          flex: auto        
       }
</style>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</body>

方法二:flex

同上,只不过将flex设置为 1 1 33.33%

上一篇 下一篇

猜你喜欢

热点阅读