CSS常見(jiàn)布局

常見(jiàn)布局種類(lèi)
1.左右布局


image.png
    HTML代碼                
        <div class="left"></div>
        <div class="right"></div>

    CSS代碼
    .left{
        float: left;
        height: 20px;
        width: 30%;
        background: red;
    }
    .right{
        float: left;
        height: 20px;
        width: 70%;
        background: green;
    }

上面是自適應(yīng)的兩列布局:width擁百分比+float;


image.png
HTML代碼
    <div class="wrapper">
          <div class="left"></div>
          <div class="right"></div>
    </div>

CSS代碼
    .wrapper{
        width: 500px;
    }
    .left{
        float: left;
        height: 20px;
        width: 30%;
        background: red;
    }
    .right{
        float: left;
        height: 20px;
        width: 70%;
        background: green;
    }

上面是固定寬度的兩列布局:width:具體值/父級(jí)元素的寬度確定,width+百分比;+float;
如果沒(méi)有加float的話(huà),不能實(shí)現(xiàn)并排的兩列布局。

2.三列布局


image.png
HTML代碼
    <div class="left">left</div>
    <div class="center">center</div>
    <div class="right">right</div>

CSS代碼
    .left{
        float: left;
        height: 20px;
        width: 30%;
        background: red;
    }
    .center{
        float: left;
        height: 20px;
        width: 10%;
        background: yellow;
    }
    .right{
        float: left;
        height: 20px;
        width: 60%;
        background: green;
    }

傳統(tǒng)的三列布局依托于float實(shí)現(xiàn)


image.png
  HTML代碼
    <div class="wrapper">
      <div class="left">left</div>
      <div class="center">center</div>
      <div class="right">right</div>
    </div>

  CSS代碼
    .wrapper{
        position: relative;
    }
    .left{
        position: absolute;
        top: 0;
        left: 0; 
        height: 20px;
        width: 30%;
        background: red;
    }
    .center{
        margin-left: 30%; 
        height: 20px;
        width: 10%;
        background: yellow;
    }
    .right{
        position: absolute;
        top: 0;
        right: 0; 
        height: 20px;
        width: 60%;
        background: green;
    }

左右使用絕對(duì)定位來(lái)實(shí)現(xiàn),中間用margin實(shí)現(xiàn).


image.png
 HTML代碼
    <div class="left">left</div>
    <div class="right">right</div>
    <div class="center">center</div>

 CSS代碼
  .left{
        float: left;
        height: 20px;
        width: 500px;
        background: red;
    }
    .right{
        float: right;
        height: 20px;
        width: 500px;
        background: green;
    }
    .center{
        margin-left: 500px;
        margin-right: 500px;
        height: 20px;
        background: yellow;
    }

左右模塊各自向左右浮動(dòng),并設(shè)置中間模塊的 margin 值使中間模塊寬度自適應(yīng)。

3.水平居中


image.png

塊級(jí)元素水平居中設(shè)置margin: 0 auto;

image.png

在父元素上設(shè)置 text-align: center 使文字/圖片水平居中。
text-align CSS屬性定義行內(nèi)內(nèi)容(例如文字)如何相對(duì)它的塊父元素對(duì)齊。text-align 并不控制塊元素自己的對(duì)齊,只控制它的行內(nèi)內(nèi)容的對(duì)齊。

4.垂直居中
設(shè)置上下pading相等實(shí)現(xiàn)居中
絕對(duì)定位加上負(fù)邊距實(shí)現(xiàn)居中
vertical-align: middle;實(shí)現(xiàn)居中
display: table-cell;實(shí)現(xiàn)居中

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問(wèn)答題47 /72 常見(jiàn)瀏覽器兼容性問(wèn)題與解決方案? 參考答案 (1)瀏覽器兼容問(wèn)題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,125評(píng)論 1 92
  • 1. 前言 前端圈有個(gè)“?!保涸诿嬖嚂r(shí),問(wèn)個(gè)css的position屬性能刷掉一半人,其中不乏工作四五年的同學(xué)。在...
    YjWorld閱讀 4,910評(píng)論 5 15
  • 單列布局 水平居中 水平居中的頁(yè)面布局中最為常見(jiàn)的一種布局形式,多出現(xiàn)于標(biāo)題,以及內(nèi)容區(qū)域的組織形式,下面介紹四種...
    寄去遠(yuǎn)方的盛夏閱讀 386評(píng)論 0 2
  • 1、外媒:婚姻觀念轉(zhuǎn)變?cè)絹?lái)越多中國(guó)女性單身步入30歲 2、中國(guó):承諾嚴(yán)控和慎用死刑只針對(duì)極少數(shù)嚴(yán)重犯罪 3、市場(chǎng):...
    笑揖輕風(fēng)閱讀 192評(píng)論 0 0
  • 本周總體總結(jié) 本周給自己打6分,睡眠超級(jí)差,基本上每天才睡3個(gè)小時(shí)左右,無(wú)止盡的失眠。狀態(tài)特別不好。狀態(tài)不好,也能...
    羽青閱讀 312評(píng)論 0 0

友情鏈接更多精彩內(nèi)容