1. 程式人生 > >HTML與CSS布局技巧總結

HTML與CSS布局技巧總結

overflow 絕對定位 back hidden port 子元素 屏幕 hex key

很多人對CSS的布局有困惑,實際的應用場景中由於布局種類多難以選擇。今天我花些時間總結下自己對CSS布局的理解,分析下了解各種布局的優劣,同時希望能分享給初入前端的朋友們一些在布局上的經驗,如果有那些地方總結的不好,歡迎大家指正。言歸正傳,現在就來揭開各種布局的面紗。

單列布局

<div class="parent">
    <div class="child"></div>
</div>

水平居中

水平居中的布局方式是最常見的一種,常常用於頭部、內容區、頁腳,它主要的作用是控制盒子在整個頁面以水平居中的方式呈現。

技術分享圖片

使用margin:0 auto來實現

.child{width:800px; margin: 0 auto;}

優勢:兼容性好
劣勢:需要指定盒子 寬度


1.使用table來實現

.child{display: table; margin: 0 auto;}

優勢:不需要父容器parent,只需要對自身進行設置
劣勢:IE6、7需要調整結構


2.使用inline-block和text-align來實現

.parent{text-align: center;}
.child{display: inline-block;}

優勢:兼容性好
劣勢:需要同時設置子元素和父元素


3.使用絕對定位absolute來實現

使用絕對定位來實現水平居中布局有兩種情況,一種子容器無寬度,另一種子容器有寬度。無寬度可以用一下代碼,如果是有寬度,則可以設置margin-left負值為容器寬度的一半。

.parent{position: relative;}
.child{position: absolute; left: 50%; transform: translateX(-50%);}

優勢:無需設置容器寬度,在移動端可以使用
劣勢:兼容性差,需要IE9及以上瀏覽器的支持


4.使用flex布局來實現

flex有兩種方法來實現水平居中,父容器設置display:flex, 一種直接在父容器中設置justify-content屬性值center。第二種在子容器中使用margin: 0 auto

.parent{display: flex; justify-content: center;}
.parent{display: flex;}
.child{margin: 0 auto;}

優勢:實現起來簡單,尤其是使用在響應式布局中
劣勢:兼容性差,如果大面積的使用該布局可能會影響效率


垂直居中

技術分享圖片

這邊說的垂直居中是子容器無高的垂直居中,並非單行文本垂直居中line-height

1.使用絕對定位absolute來實現(同水平居中的使用方法,優劣一樣)

.parent{position: relative;}
.child{position: absolute; top: 50%; transform: translateY(-50%);}

2.使用flex來實現

.parent{display: flex; align-items: center;}

3.使用display:table-cell來實現

.parent{display: table-cell;vertical-align: middle;height: 400px;}

總結:將水平居中和垂直居中兩種布局方法相互的結合起來就可以實現水平居中布局。這邊只舉一個用絕對定位來實現水平垂直居中布局的方法,別的方法大家可以嘗試自己練習。(以下介紹各種布局時都是基於上面水平和垂直居中的方法,所有對於它們的優劣就不再分析。)

.parent{position: relative;}
.child{position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);}

多列布局

多列布局也是非常常見的,適用於一側定寬,另一側自適應的布局。

技術分享圖片

浮動布局

前段時間我總結過關於兩列浮動布局方法,這裏我就不再從新總結了,如果有興趣的朋友可以參考前端時間關於浮動布局的方法(總結)這篇博客。

多列等分布局

多列等分布局常常出現在內容中,多數為同功能、同階級內容的並排顯示。
技術分享圖片

HTML代碼

<div class="parent">
    <div class="column">1</div>
    <div class="column">2</div>
    <div class="column">3</div>
    <div class="column">4</div>
</div>

1.使用flex來實現多列布局

.parent{display: flex;}
.column{flex: 1;}
.column+ .column{margin-left: 20px;}

2.使用table來實現多列布局

.parent{display: table; table-layout: fixed; width: 100%;}
.column{display: table-cell; padding-left: 20px;}

3.使用float來實現多列布局

.column{float: left; width: 25%; padding-left: 20px; box-sizing: border-box;}

提示:使用table和float實現多列布局的時候需要註意,如果要設置背景顏色則必須將.column盒子作為父容器在其裏面添加一個子容器,在設置背景顏色,如果直接在.column容器中設置背景顏色會由於padding而無法產生盒子之間的間距。

九宮格布局

技術分享圖片
HTML代碼

<div class="parent">
    <div class="row">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
    <div class="row">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
    <div class="row">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
</div>

1.使用flex來實現九宮格布局

.parent{display: flex; flex-direction: column;width: 300px;}
.row{height: 100px; display: flex;border: 1px solid red;}
.item{width: 100px; background-color: #ccc;border: 1px solid red;}

2.使用table來實現九宮格布局

.parent{display: table; table-layout: fixed; width: 100%;}
.row{display: table-row;}
.item{display: table-cell; width: 33.3%; height: 200px; border: 1px solid red;}

全屏布局

技術分享圖片

HTML代碼

<div class="parent">
    <div class="top"></div>
    <div class="left"></div>
    <div class="right"></div>
    <div class="bottom"></div>
</div>

使用絕對定位實現全屏布局

html,body,.parent{height: 100%; overflow: hidden;}
        .top{position: absolute; top: 0; left: 0; right: 0; height: 0; background-color: black; height: 100px;}
        .left{position: absolute; top: 100px; left: 0;bottom: 50px; width: 200px; background-color: orange;}
        .right{position: absolute; top: 100px; left: 200px; right: 0; bottom: 50px; background-color: grey; overflow: hidden;}
        .bottom{position: absolute; left: 0; right: 0; bottom: 0; height: 50px; background-color: pink;}

響應式布局

meta標簽的使用

<meta name="viewport" content="width=device-width, initial-scale=1"/>

使用媒體查詢

@media screen and (max-width: 480px){
         /*屏幕小於480px的樣式*/
}

HTML與CSS布局技巧總結