溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CSS的flex在布局中的用法

發(fā)布時間:2020-06-01 20:59:27 來源:億速云 閱讀:268 作者:鴿子 欄目:web開發(fā)

元素居中

【1】伸縮容器上使用主軸對齊justify-content和側(cè)軸對齊align-items

<style>.parent{
    display: flex;
    justify-content: center;
    align-items: center;}</style>
<div class="parent"  style="background-color: lightgrey; height: 100px; width: 200px;">
    <div class="in" style="background-color: lightblue;">DEMO</div>      </div>

【2】在伸縮項目上使用margin:auto

<style>.parent{
    display: flex;}.in{
    margin: auto;}</style>
<div class="parent"  style="background-color: lightgrey;height: 100px;width: 200px;">
    <div class="in" style="background-color: lightblue;">DEMO</div>      </div>

兩端對齊

<style>.parent{
   display: flex;    
   justify-content:space-between
   }
</style>
<div class="parent"  style="background-color: lightgrey;height: 100px;width: 200px;">
    <div class="in" style="background-color: lightblue;">DEMO</div>
    <div class="in" style="background-color: lightgreen;">DEMO</div>
    <div class="in" style="background-color: lightcyan;">DEMO</div>    
    <div class="in" style="background-color: lightseagreen;">DEMO</div>     
 </div>

底端對齊

<style>.parent{
    display: flex;
    align-items: flex-end;
    }
</style>
<div class="parent"  style="background-color: lightgrey;height: 100px;width: 200px;">
    <div class="in" style="background-color: lightblue; height:20px;">DEMO</div>
    <div class="in" style="background-color: lightgreen; height:30px;">DEMO</div>
    <div class="in" style="background-color: lightcyan; height:40px;">DEMO</div>
    <div class="in" style="background-color: lightseagreen; height:50px;">DEMO</div>      
</div>

輸入框按鈕

<style>.inputBox{
    display: flex;
    width: 250px;}.inputBox-ipt{
    flex: 1;}
</style>
<div class="inputBox">
  <input class="inputBox-ipt">
  <button class="inputBox-btn">按鈕</button>
</div>

等分布局

<style>body,p{margin: 0;}.parent{
    display: flex;}.child{
    flex:1;
    height: 100px;}.child + .child{
    margin-left: 20px;}
</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="child" style="background-color: lightblue;">1</div>
    <div class="child" style="background-color: lightgreen;">2</div>
    <div class="child" style="background-color: lightsalmon;">3</div>
    <div class="child" style="background-color: pink;">4</div>                
</div>

多列自適應(yīng)布局

<style>p{margin: 0;}.parent{display: flex;}.left,.center{margin-right: 20px;}.right{flex: 1;}</style>
<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
        <p>left</p>
    </div>            
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>            
    <div class="right"  style="background-color: lightgreen;">
        <p>right</p>
        <p>right</p>
    </div>                    
</div>

懸掛布局

<style>        
     .box{
    display: flex;
    background-color: lightgrey;
    width: 300px;}.left{
    margin-right: 20px;
    background-color: lightblue;
    height: 30px;}.main{
    flex:1;}
 </style>
<div class="box">
    <div class="left">左側(cè)懸掛</div>
    <div class="main">主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容主要內(nèi)容</div>    
</div>

全屏布局

<style>body,p{margin: 0;}body,html,.parent{height: 100%;}.parent{
    display: flex;
    flex-direction: column;}.top,.bottom{
    height: 50px;}.middle{
    display: flex;
    flex: 1;}.left{
    width: 100px;
    margin-right: 20px;}.right{
    flex: 1;
    overflow: auto;}.right-in{
    height: 1000px;}</style>
<div class="parent" id="parent" style="background-color: lightgrey;">
    <div class="top" style="background-color: lightblue;">
        <p>top</p>
    </div> 
    <div class="middle" style="background-color: pink;">
        <div class="left" style="background-color: orange;">
            <p>left</p>
        </div>     
        <div class="right" style="background-color: lightsalmon;">
            <div class="right-in">
                <p>right</p>
            </div>            
        </div>                    
    </div>              
    <div class="bottom" style="background-color: lightgreen;">
        <p>bottom</p>
    </div>        
</div>

以上就是CSS彈性盒模型flex在布局中的使用方法的詳細(xì)內(nèi)容,更多請關(guān)注億速云其它相關(guān)文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI