兩欄佈局、三欄佈局、水平垂直居中(良心整理,歡迎交流)

兩欄佈局:

一、float+margincss

.block_left{
            float: left;
            width: 200px;  //左邊定寬
            background-color: blue;
}
.block_right{           //右邊自適應
            background-color: red;
            margin-left:200px;
}        

二、定位html

.content{
            position: relative;
            width: 100%;
        }     
.block_left{
            position: absolute;
            width: 200px;
            background-color: blue;
}
.block_right{
            position: absolute;
            left: 200px;
            right: 0;
            background-color: red;   
}

三、彈性佈局瀏覽器

*{
            margin: 0;
            padding: 0;
        /*這是設置默認的內外邊距。由於瀏覽器標籤自帶的屬性是不統一的,設置爲0。爲了兼容全部的瀏覽器!*/
}
.flex-container{
            width: 100%;
            height: 300px;
            display: flex;
}
.block_left{
            background-color: blue;
            width: 20%;      //這個能夠對其進行控制,左右佔比
            flex:1 0 auto;   //設置其可擴展,不可壓縮,且大小自適應
}
.block_right{
            background-color: red;
            width: 80%;
            flex:1 0 auto;
}

三欄佈局:

一、float:left和float:right佈局

優勢:簡單    缺點:中間部分最後加載,用戶體驗感很差flex

<style>
.left{ background-color: red; float: left; width: 100px; } .right{ background-color: red; width: 100px; float: right;
} .content{ background-color: green; margin-right: 100px; margin-left: 95px; }
</style> <body> <!-- 必需要把類爲content的div元素放在浮動元素以後,由於浮動元素不能夠高於任何在源文檔(html代碼)以前的塊元素或浮動元素,因此若是按照左中右的順序擺放的話會出現如下狀況-->

<div id="container"></div> <div class="left">1</div> <div class="right">3</div> <div class="content">2</div> </body>

二、BFC:即:在第一種方法的基礎上,給content部分加上overflow:hidden來使其造成BFC,利用BFC不會與浮動元素重疊的規則spa

三、聖盃佈局code

<style>
        *{
            padding: 0;
            margin: 0;
        }
        #container{
            /* 讓左右兩欄佔據container左右兩邊的填充 */
            padding-left:100px;
            padding-right: 100px; 
            background-color: blue;
        }
        .left{
            background-color: red;
            float: left;
            position:relative;
            width: 100px;
            height: 100px;
            /* 這個100%是content部分至關於container的,就是指左列要越過中間列的寬度,跑到它前面去 */
            /* 浮動元素的margin值是至關於上一個相鄰的浮動元素來計算的 */
            margin-left:-100%;   
            left:-100px;
        }
        .right{
            background-color: red;
            width: 100px;
            height: 100px;
            float: left;
            position:relative;
            /* 此處的margin-left是相對於其相鄰元素left的 */
            margin-left: -100px;
            right: -100px;
        }
        .content{
            float: left;
            background-color:yellow;
            width:100%;
            height: 100px;
        }
    </style>
</head>
<body>
    <!-- content部分先渲染 -->
<div id="container">
    <div class="content">2aaasadsfdff</div>
    <div class="left">1</div>
    <div class="right">3</div>
</div>  

四、雙飛翼佈局htm

步驟幾乎與聖盃佈局一致,只是在中間部分多加了一層,經過對其設置margin值來消除遮擋blog

五、彈性佈局文檔

#container{
      display:flex;
}
.left{
      background-color: red;
      width: 100px;
      height: 100px;
      flex:0 1 auto;
      order:-1;  //渲染是首先渲染content部分,可是佈局是要將left模塊放到左邊,order默認爲0,因此設置其爲-1,就能夠位置領先於其它元素
}
.right{ background-color: red; width: 100px; height: 100px; flex:0 1 auto; } .content{ background-color:yellow; flex:1 0 auto; height: 100px; }
</style> <body> <!-- content部分先渲染 --> <div id="container"> <div class="content">2aaasadsfdff</div> <div class="left">1</div> <div class="right">3</div> </div>

六、絕對定位:很簡單,經過對三塊區域設置絕對定位,而後經過left/right值來進行定位,就能夠實現三欄佈局

.left{
      background-color: red;
      width: 100px;
      height: 100px;
      position:absolute;
      left:0;
} .right{ background-color: red; width: 100px; height: 100px; position: absolute; right: 0; } .content{ background-color:yellow; position: absolute; height: 100px; left:100px; right: 100px; }

水平垂直居中:

1、文字水平垂直居中

一、單行文字:

text-align:center/*水平居中*/

line-height20px/*行距設爲與div高度一致*/
將行距設爲和div一致,即便得每段文字都具備和div同樣的高度,即:讓文字利用整個div空間,有點像margin:auto
二、多行文字
text-align:center;
display:table-cell;vertical-align:middle;

 還能夠用彈性佈局 : display:flex;justify-content:center;align-item:center

2、塊狀水平垂直居中:

方法1:對塊元素絕對定位,而後將其位置經過margin-left和margin-top負值各拉回自身的一半

方法2:利用margin:auto

方法3:經過彈性盒(必定要保證父級元素有高度)

相關文章
相關標籤/搜索