CSS布局方案的示例分析-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)CSS布局方案的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

目前成都創(chuàng)新互聯(lián)公司已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計、新榮網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

居中布局

水平居中

1)使用inline-block+text-align

原理:先將子框由塊級元素改變?yōu)樾袃?nèi)塊元素,再通過設(shè)置行內(nèi)塊元素居中以達(dá)到水平居中。

用法:對子框設(shè)置display:inline-block,對父框設(shè)置text-align:center。

<div class="parent">
    <div class="child">DEMO</div>
</div>
.child{
    display: inline-block;
}
.parent{
    text-align: center;
}

缺點:child里的文字也會水平居中,可以在.child添加text-align:left;還原

CSS布局方案的示例分析

2)使用absolute+transform

<div class="parent">
    <div class="child">DEMO</div>
</div>
.child{
    position: relative;
}
.parent{
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

3)使用flex+justify-content

原理:通過CSS3中的布局利器flex中的justify-content屬性來達(dá)到水平居中。

用法:先將父框設(shè)置為display:flex,再設(shè)置justify-content:center。

<div class="parent">
    <div class="child">DEMO</div>
</div>
.parent{
    display: flex;
    justify-content: center;
}

缺點:低版本瀏覽器(ie6 ie7 ie8)不支持

4)使用flex+margin

原理:通過CSS3中的布局利器flex將子框轉(zhuǎn)換為flex item,再設(shè)置子框居中以達(dá)到居中。

用法:先將父框設(shè)置為display:flex,再設(shè)置子框margin:0 auto。

<div class="parent">
    <div class="child">DEMO</div>
</div>
.parent{
    display: flex;
}
.child{
    margin: 0 auto;
}

垂直居中

1)使用absolute+transform

用法:先將父框設(shè)置為position:relative,再設(shè)置子框position:absolute,top:50%,transform:translateY(-50%)。

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

2)使用flex+align-items

原理:通過設(shè)置CSS3中的布局利器flex中的屬性align-times,使子框垂直居中。

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

水平垂直居中

1)使用absolute+transform

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

2)使用flex+justify-content+align-items

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

感謝各位的閱讀!關(guān)于“CSS布局方案的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)站標(biāo)題:CSS布局方案的示例分析-創(chuàng)新互聯(lián)
新聞來源:http://muchs.cn/article44/ddchhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、標(biāo)簽優(yōu)化、移動網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、品牌網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)