首頁 > web前端 > css教學 > 主體

css如何實現三欄佈局

王林
發布: 2020-04-21 09:12:35
轉載
2980 人瀏覽過

css如何實現三欄佈局

實作方法:

一、float浮動

<section class=&#39;layout float&#39;>
         <style>
             .layout.float .left-right-center{
                 height: 100px;
             }
             .layout.float .left{
                 float: left;
                 width: 300px;
                 background-color: red;
             }

             .layout.float .right{
                 float: right;
                 width: 300px;
                 background-color: blue;
             }

             .layout.float .center{
                 background-color: yellow;
             }
         </style>
         <article class="left-right-center">
             <div class="left"></div>
             <div class="right"></div>
             <div class="center">我是中间的自适应元素--浮动</div>
         </article>
     </section>
登入後複製

原理:左右兩個div由於浮動脫離了文檔流,center就會上移,造成三欄佈局的效果(前提是高度相同)

優點:兼容性高

缺點:需要清除浮動來防止影響其他元素

如果高度不固定,中間的內容會被撐開,左右兩邊不會一起撐開

(推薦教學:CSS教學

二、絕對定位

<section class="layout absolute">
         <style>
             .layout.absolute .left-center-right div{
                 position: absolute;
                 height: 100px;
             }

             .layout.absolute .left{
                 left: 0;
                 width: 300px;
                 background-color: red;
             }

             .layout.absolute .center{
                 left: 300px;
                 right: 300px;
                 background-color: yellow;
             }

             .layout.absolute .right{
                 right: 0;
                 width: 300px;
                 background-color: blue;
             }
         </style>
         <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                我是中间的自适应元素--绝对定位
            </div>
            <div class="right"></div>
         </article>
     </section>
登入後複製

原理:利用絕對定位以及寬度,將左右兩邊的div固定住,中間div的寬度就會有自適應的效果

優點:快捷

缺點:如果父元素脫離了文檔流,子元素一定會脫離文檔流,運用的場景不多

如果中間元素的高度增加,兩邊元素的高度不會增加,所以只有中間的div會撐開

三、flex佈局

<section class="layout flex">
         <style>
             .layout.flex .left-center-right{
                 display: flex;
                 height: 100px;
             }

             .layout.flex .left{
                 width: 300px;
                 background-color: red;
             }

             .layout.flex .center{
                 flex: 1;
                 background-color: yellow;
             }

             .layout.flex .right{
                 width: 300px;
                 background-color: blue;
             }
         </style>
         <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                我是中间的自适应元素--flex布局
            </div>
            <div class="right"></div>
         </article>
     </section>
登入後複製

原理:將父元素設定為flex佈局,然後中間元素設定flex為1,達到自適應的效果

##優點:在實際開發中常用

缺點:IE8及以下的瀏覽器不支援

如果高度不固定,中間內容的高度撐開後,兩邊也會隨之撐開

#四、table佈局

   <section class="layout table">
        <style>
            .layout.table .left-center-right{
                display: table;
                height: 100px;
                width: 100%;
            }

            .layout.table .left{
                display: table-cell;
                width: 300px;
                background-color: red;
            }

            .layout.table .center{
                display: table-cell;
                background-color: yellow;
            }

            .layout.table .right{
                display: table-cell;
                width: 300px;
                background-color: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                我是中间的自适应元素--table
            </div>
            <div class="right"></div>
        </article>
    </section>
登入後複製

原理:將父元素設定為table佈局,然後每個子元素都是teble-cell,給左右兩個格子設定固定的寬度,中間的格子就可以達到自適應的效果

優點:相容性好,可做flex佈局在ie8以下的代替

缺點:局限性

如果高度不固定,中間被撐開時,左右兩邊也會被撐開,和flex類似

五、網格佈局

<section class="layout grid">
        <style>
            .layout.grid .left-center-right{
                display: grid;
                width: 100%;
                grid-template-rows: 100px;/*每一格都是100px高*/
                grid-template-columns: 300px auto 300px;/*左右两格都是300px,中间是自适应*/
            }
            
            .layout.grid .left{
                background-color: red;
            }

            .layout.grid .center{
                background-color: yellow;
            }

            .layout.grid .right{
                background-color: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                我是中间的自适应元素--grid布局
            </div>
            <div class="right"></div>
        </article>
    </section>
登入後複製
原理:將父元素設定為網格佈局,然後規定每格的高度以及每格的寬度,只用分別給每格單獨設置顏色即可

優點:技術比較新,方便

缺點:兼容性不是很好

如果高度不固定,中間元素添加文本,也不會撐開

相關影片教學推薦:

css影片教學#

以上是css如何實現三欄佈局的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
css
來源:jb51.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!