CSS3flex弹性盒子

flex弹性盒子

一、弹性盒子是 CSS3 的一种新的布局模式。
1、CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式。
2、引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子元素进行排列、对齐和分配空白空间。
3、弹性盒子与绝对定位的比较
优点: flex:flexible box 弹性盒子。可以轻松控制元素的排列,对齐和顺序。
弹性盒子没有脱离标准的文档流,绝对定位脱离了标准文档流。

<style>
/*弹性盒子子元素居中*/
        .father{
            width: 400px;
            height: 400px;
            margin: 0 auto;
            margin-top: 50px;
            border: 1px solid black;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .son{
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son">
        </div>
    </div>

二、flex声明
1、 声明定义:
使用display:flex或display:inline-flex 声明一个容器为弹性盒子。这个容器中的子元素们,会遵循弹性布局。
【注】一般是使用display:flex. inline-flex极少用。
flex:如果没有为父元素设置宽,默认为100%;
inline-flex:如果没有设置宽,,默认为所有子元素的宽的和。
例如

<style>
       .father {
           height: 400px;
           /* width: 400px; */
           margin: 0 auto;
           margin-top: 50px;
           border: 1px solid black;
           display: flex;
       }
       
       .son {
           width: 100px;
           height: 100px;
           background-color: blue;
       }
       
       .father2 {
           height: 400px;
           margin: 0 auto;
           margin-top: 50px;
           border: 1px solid black;
           display: inline-flex;
       }
       
       .son2 {
           width: 100px;
           height: 100px;
           background-color: blue;
       }
   </style>
</head>
<body>
   <div class="father">
       <div class="son">
       </div>
       <div class="son">
       </div>
       <div class="son">
       </div>
   </div>
   <div class="father2">
       <div class="son2">
       </div>
       <div class="son2">
       </div>
       <div class="son2">
       </div>
   </div>
</body>
</html>

在这里插入图片描述
三、flex-direction
用于设置盒子中的子元素的排列方向。
row 从左到右水平排列子元素(默认值)
column 从上往下垂直排列子元素
row-reverse 从右到左排列子元素
column-reverse 从下往上垂直排列子元素

 <style>
        .father {
            width: 500px;
            height: 400px;
            margin: 0 auto;
            margin-top: 50px;
            border: 1px solid black;
            display: flex;
            /*flex-direction: row;
            flex-direction:  row-reverse;
            flex-direction: column;*/
            flex-direction:  column-reverse ;
        }    
        .son {
            width: 100px;
            height: 100px;
            background-color: blue;
            background-clip: content-box;
            padding: 0 10px;
            box-sizing: border-box;
            font-size: 30px;
            line-height: 100px;
            text-align: center;
        }
    </style>
</head>
<body>

    <div class="father">
        <div class="son">1
        </div>
        <div class="son">2
        </div>
        <div class="son">3
        </div>
    </div>
</body>
</html>

flex-direction: row从左到右水平排列子元素
在这里插入图片描述
flex-direction: row-reverse 从右到左排列子元素
在这里插入图片描述
flex-direction: column从上往下垂直排列子元素
在这里插入图片描述
flex-direction: column-reverse 从下往上垂直排列子元素
在这里插入图片描述
四、flex-wrap
规定flex容器是单行还是多行,是否换行
nowrap:不换行(默认值)
wrap:换行
wrap-reverse 反向换行。
flex-flow
是flex-direction与flex-wrap的组合写法。
flex-flow:flex-direction flex-wrap;
例如

<style>
       .father {
           width: 400px;
           height: 400px;
           margin: 0 auto;
           margin-top: 50px;
           border: 1px solid black;
           display: flex;
           flex-flow: column wrap-reverse;
       }        
       .son {
           width: 100px;
           height: 100px;
           background-color: blue;
           background-clip: content-box;
           padding: 0 10px;
           box-sizing: border-box;
           font-size: 30px;
           line-height: 100px;
           text-align: center;
       }
   </style>
</head>
<body>
   <div class="father">
       <div class="son">1
       </div>
       <div class="son">2
       </div>
       <div class="son">3
       </div>
       <div class="son">4
       </div>
       <div class="son">5
       </div>
   </div>
</body>
</html>

子元素从上到下排列反向换行
在这里插入图片描述
五、弹性盒子与弹性元素
弹性盒子:指的是使用display:flex或display:inline-flex声明的容器。
弹性元素:指的是弹性盒子中的子元素。
主轴与侧轴(交叉轴)
主轴方向:为子元素排列的方向。可以通过flex-direction改变。
侧轴方向:与主轴方向垂直,为子元素的换行方向。可以通过flex-wrap改变。
六、justify-content
用于控制弹性元素在主轴上的排列方式。
flex-start 元素紧靠主轴的起点(默认值)
在这里插入图片描述

flex-end 元素紧靠主轴的终点。
在这里插入图片描述

center 元素在主轴上居中。
在这里插入图片描述
space-between 第一元素紧靠起点,最后一个元素紧靠终点,余下的元素平均分配剩余空间。
在这里插入图片描述
space-around 每个元素两侧的间隔相等。元素之间的间隔比元素与容器边框的间隔要大一倍。
在这里插入图片描述
space-evenly 元素间距平均分配
在这里插入图片描述

<style>
        .father {
            width: 500px;
            height: 400px;
            margin: 0 auto;
            margin-top: 50px;
            border: 1px solid black;
            display: flex;
            flex-flow: row wrap;
            justify-content:  flex-start ;
            justify-content: flex-end ;
            justify-content:  center ;
            justify-content: space-between;
            justify-content: space-around ;
            justify-content: space-evenly;
        }        
        .son {
            width: 100px;
            height: 100px;
            background-color: blue;
            background-clip: content-box;        
            box-sizing: border-box;
            font-size: 30px;
            line-height: 100px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son">1
        </div>
        <div class="son">2
        </div>
        <div class="son">3
        </div>
        <div class="son">4
        </div>
    </div>
</body>
</html>

七、align-items
控制元素在侧轴上的排列方式。
多行单行都适用的属性。
flex-start 从侧轴开始的地方对齐
flex-end 从侧轴结束的地方对齐
center 中间对齐
stretch 拉伸 没有设置侧轴方向上的宽/高时,等于父盒子的宽或高。
例如

    <style>
        .father {
            width: 300px;
            height: 300px;
            margin: 0 auto;
            margin-top: 50px;
            border: 1px solid seagreen;
            display: flex;
            flex-flow: row wrap;
            align-content: stretch;
        }        
        .son {
            width: 100px;           
            background-color: sienna;
            background-clip: content-box;
            box-sizing: border-box;
            font-size: 30px;
            line-height: 100px;
            text-align: center;
        }
    </style>
</head>
<body>  
    <div class="father">
        <div class="son">1
        </div>
        <div class="son">2
        </div>
        <div class="son">3
        </div>
    </div>
</body>
</html>

在这里插入图片描述
align-content
控制元素在侧轴上的排列方式。只适用于多行显示的弹性容器。
stretch 拉伸
flex-start 元素紧靠行的起点
flex-end 元素紧靠行的终点
center 元素在行中居中
space-between 第一个元素紧靠行的起点,最后一个元素紧靠行的终点,余下元素平均分配剩余空间
space-around 元素在侧轴方向上的间隔相等。
space-evenly 元素间距离平均分配
八、弹性元素:
不要再去设置float。
设置完绝对定位后,该元素就不再是弹性元素了,不参与弹性布局。
弹性元素均为块级元素。
align-self:
用于控制单个元素在侧轴上的排列方式。
stretch 拉伸
flex-start
flex-end
center
九、 flex-grow
用于将弹性盒子的可用空间,按照比例分配给弹性元素。

<style>
      .father {
          width: 400px;
          height: 300px;
          margin: 0 auto;
          margin-top: 50px;
          border: 1px solid seagreen;
          display: flex;
          flex-flow: row wrap;
      }      
      .son {
          width: 100px;
          height: 100px;
          background-color: sienna;
          background-clip: content-box;
          box-sizing: border-box;
          font-size: 30px;
          line-height: 100px;
          text-align: center;
          flex-grow: 1;
      }       
      .son:nth-child(2) {
          background-color: cadetblue;
          flex-grow: 2;
      }
  </style>
</head>
<body>
  <div class="father">
      <div class="son">1
      </div>
      <div class="son">2
      </div>
      <div class="son">3
      </div>
  </div>
</body>
</html>

在这里插入图片描述
九、flex-shrink
在弹性盒子装不下子元素时,对子元素缩小的比例设置。
1.计算缺少的值。
2.当前元素应该缩小比例:802/(1001+802+1001)=44.4%
3.应该缩小的值:缺少的值缩小的比例=8044.4%=35.6px
4.最终尺寸:缺少的值-应该缩小的值 80-35.6=44.4px;

  <style>
        .father{
            width: 200px;
            height: 300px;
            margin: 0 auto ;
            margin-top: 50px;
            border: 1px solid seagreen;
            display: flex;
            flex-flow: row nowrap;
        }
        .son{
            width: 100px;
            height: 100px;
            background-color: sienna;
            background-clip: content-box;
            box-sizing: border-box;
            font-size: 30px;
            line-height: 100px;
            text-align: center;
            flex-grow: 1;
        }
        .son:nth-child(2){
            width: 80px;
            background-color: cadetblue;
            flex-shrink: 2;
        }
    </style>
</head>
<body>

在这里插入图片描述
十、flex-basis
大部分情况下与宽度相等。优先级比宽要高。定义了元素在主轴上的空间。

       <style>
        .father {
            width: 300px;
            height: 300px;
            margin: 0 auto;
            margin-top: 50px;
            border: 1px solid seagreen;
            display: flex;
            flex-flow: row nowrap;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: sienna;
            background-clip: content-box;
            box-sizing: border-box;
            font-size: 30px;
            line-height: 100px;
            text-align: center;
        }
        
        .son:nth-child(2) {
            flex-basis: 80px;
            width: 100px;
            background-color: cadetblue;
        }
    </style>
</head>

<body>
  
    <div class="father">
        <div class="son">1
        </div>
        <div class="son">2
        </div>
        <div class="son">3
        </div>
    </div>
</body>
</html>

在这里插入图片描述
复合写法:
flex:flex-grow flew-shrink flex-basis
order:
用于控制弹性元素的位置。默认为0,数值越小越靠前。

 <style>
      .father {
          width: 300px;
          height: 300px;
          margin: 0 auto;
          margin-top: 50px;
          border: 1px solid seagreen;
          display: flex;
          flex-flow: row nowrap;
      }     
      .son {
          width: 100px;
          height: 100px;
          background-color: sienna;
          background-clip: content-box;
          box-sizing: border-box;
          font-size: 30px;
          line-height: 100px;
          text-align: center;
      }
      
      .son:nth-child(2) {
          width: 100px;
          background-color: cadetblue;
          flex: 2 1 100px;
          order: -2;
      }
  </style>
</head>
<body>
  <div class="father">
      <div class="son">1
      </div>
      <div class="son">2
      </div>
      <div class="son">3
      </div>
  </div>
</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值