css 佈局(聖盃、雙飛翼)

1、 聖盃佈局、

左右固寬,中間自適應

  • 三列布局,中間寬度自適應,兩邊定寬;
  • 中間部分要在瀏覽器中優先展現渲染;

具體步驟:
1.設置基本樣式
2.聖盃佈局是一種相對佈局,首先設置父元素container的位置:
3.將主體部分的三個子元素都設置左浮動
4.設置main寬度爲width:100%,讓其單獨佔滿一行
5.設置left和right 負的外邊距
6.接下來只要把left和right分別移動到這兩個留白就能夠了。能夠使用相對定位移動 left和right部分。html

代碼以下:瀏覽器

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>聖盃佈局</title>

    <style>
        body{
            margin:0;
        }
        .box{
            margin:0 auto;
            width:900px;
            height:300px;
            background-color:orange;
            padding:0 200px;
        }
        .left{
            width:200px;
            height:300px;
            background-color:red;
            float:left;
            margin-left:-100%;
            position:relative;
            left:-200px;
            
        }
        .center{
            width:100%;
            height:300px;
            background-color:pink;
            float:left;
        }
        .right{
            width:200px;
            height:300px;
            background-color:blue;
            float:left;
            margin-left:-200px;
            position:relative;
            right:-200px;
        }
    </style>

</head>
<body>
<div class="box">
        <div class="center"></div>
        
        <div class="left"></div>
    
        <div class="right"></div>    
</div>
</body>
</html>

效果圖:
圖片描述佈局

2、雙飛翼佈局

左右固寬,中間自適應

  • 左右兩個div浮動,中間的div放在上面
  • 經過margin-left調整到一行
  • 中間div添加一個子節點,設置margin調整位置

代碼以下:spa

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>雙飛翼佈局</title>

    <style>
        body{
            margin:0;
        }
        .box{
            margin:0 auto;
            width:900px;
            height:300px;
            background-color:orange;
        }
        .warp{
            margin:0 200px;
        }
        .left{
            width:200px;
            height:300px;
            background-color:red;
            float:left;
            margin-left:-100%;
        }
        .center{
            width:100%;
            height:300px;
            background-color:pink;
            float:left;
        }
        .right{
            width:200px;
            height:300px;
            background-color:blue;
            float:left;
            margin-left:-200px;
        }
    </style>

</head>
<body>
<div class="box">

    <div class="center">
        <div class="warp"></div>
    </div>
    
    <div class="left"></div>
        
    <div class="right"></div>
        
</div>
</body>

</html>

效果圖:code

圖片描述

持續更新 點歌關注吧!

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息