当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


CSS flex-wrap用法及代码示例


CSS flex-wrap属性用于指定将弹性项目强制插入到单行还是换行到多行。 flex-wrap属性允许启用线堆叠的控制方向。它用于指定单行或multi-line格式以伸缩flex容器内的项目。

用法:

flex-wrap:nowrap|wrap|wrap-reverse|initial;

属性值:


  • wrap:此属性用于将弹性项目分成多行。它可以根据弹性项目的宽度将弹性项目换行成多行。
    用法:
    flex-wrap:wrap;

    例:

    <!DOCTYPE html> 
    <html> 
    <head> 
        <title>flex-wrap property</title> 
        <style> 
            #main { 
                width:400px; 
                height:300px; 
                border:5px solid black; 
                display:flex; 
                flex-wrap:wrap; 
            } 
              
            #main div { 
                width:100px; 
                height:50px; 
            } 
            h1 { 
                color:#009900; 
                font-size:42px; 
                margin-left:50px; 
            } 
            h3 { 
                margin-top:-20px; 
                margin-left:50px; 
            } 
        </style> 
    </head> 
    <body> 
        <h1>GeeksforGeeks</h1> 
        <h3>The flex-wrap:wrap property</h3> 
        <div id="main"> 
            <div style="background-color:#009900;">1</div> 
            <div style="background-color:#00cc99;">2</div> 
            <div style="background-color:#0066ff;">3</div> 
            <div style="background-color:#66ffff;">4</div> 
            <div style="background-color:#660066;">5</div> 
            <div style="background-color:#663300;">6</div> 
        </div> 
    </body> 
    </html>                    

    输出:
    flex-wrap property

  • nowrap:wrap-flex的默认值为nowrap。用于指定该项目没有包装。它使项目用单行换行。
    用法:
    flex-wrap:nowrap;

    例:

    <!DOCTYPE html> 
    <html> 
    <head> 
        <title>flex-wrap property</title> 
        <style> 
            #main { 
                width:400px; 
                height:300px; 
                border:5px solid black; 
                display:flex; 
                flex-wrap:nowrap; 
            } 
              
            #main div { 
                width:100px; 
                height:50px; 
            } 
            h1 { 
                color:#009900; 
                font-size:42px; 
                margin-left:50px; 
            } 
            h3 { 
                margin-top:-20px; 
                margin-left:50px; 
            } 
        </style> 
    </head> 
    <body> 
        <h1>GeeksforGeeks</h1> 
        <h3>The flex-wrap:nowrap property</h3> 
        <div id="main"> 
            <div style="background-color:#009900;">1</div> 
            <div style="background-color:#00cc99;">2</div> 
            <div style="background-color:#0066ff;">3</div> 
            <div style="background-color:#66ffff;">4</div> 
            <div style="background-color:#660066;">5</div> 
            <div style="background-color:#663300;">6</div> 
        </div> 
    </body> 
    </html>                    

    输出:
    flex-wrap property

  • wrap-reverse:当弹性项目换行时,此属性用于反转弹性项目的流动。
    用法:
    flex-wrap:wrap-reverse;

    例:

    <!DOCTYPE html> 
    <html> 
    <head> 
        <title>flex-wrap property</title> 
        <style> 
            #main { 
                width:400px; 
                height:300px; 
                border:5px solid black; 
                display:flex; 
                flex-wrap:wrap-reverse; 
            } 
              
            #main div { 
                width:100px; 
                height:50px; 
            } 
            h1 { 
                color:#009900; 
                font-size:42px; 
                margin-left:50px; 
            } 
            h3 { 
                margin-top:-20px; 
                margin-left:50px; 
            } 
        </style> 
    </head> 
    <body> 
        <h1>GeeksforGeeks</h1> 
        <h3>The flex-wrap:wrap-reverse property</h3> 
        <div id="main"> 
            <div style="background-color:#009900;">1</div> 
            <div style="background-color:#00cc99;">2</div> 
            <div style="background-color:#0066ff;">3</div> 
            <div style="background-color:#66ffff;">4</div> 
            <div style="background-color:#660066;">5</div> 
            <div style="background-color:#663300;">6</div> 
        </div> 
    </body> 
    </html>                    

    输出:
    flex wrap property

  • initial:此属性用于将其设置为默认值。
    用法:
    flex-wrap:initial;

    例:

    <!DOCTYPE html> 
    <html> 
    <head> 
        <title>flex-wrap property</title> 
        <style> 
            #main { 
                width:400px; 
                height:300px; 
                border:5px solid black; 
                display:flex; 
                flex-wrap:initial; 
            } 
              
            #main div { 
                width:100px; 
                height:50px; 
            } 
            h1 { 
                color:#009900; 
                font-size:42px; 
                margin-left:50px; 
            } 
            h3 { 
                margin-top:-20px; 
                margin-left:50px; 
            } 
        </style> 
    </head> 
    <body> 
        <h1>GeeksforGeeks</h1> 
        <h3>The flex-wrap:initial property</h3> 
        <div id="main"> 
            <div style="background-color:#009900;">1</div> 
            <div style="background-color:#00cc99;">2</div> 
            <div style="background-color:#0066ff;">3</div> 
            <div style="background-color:#66ffff;">4</div> 
            <div style="background-color:#660066;">5</div> 
            <div style="background-color:#663300;">6</div> 
        </div> 
    </body> 
    </html>                    

    输出:
    flex wrap property

支持的浏览器:CSS flex-wrap属性支持的浏览器如下:

  • Google Chrome 29.0、21.0 -webkit-
  • Internet Explorer 11.0
  • Firefox 28.0、18.0 -moz-
  • Opera 17.0
  • Safari 9.0、6.0 -webkit-


相关用法


注:本文由纯净天空筛选整理自ManasChhabra2大神的英文原创作品 CSS | flex-wrap property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。