09CSS网页布局基础

[目录]

一、基础介绍

认识布局:

  • 以最适合浏览器的方式将图片和文字排放在页面的不同位置
  • 布局模式有多种,不同的制作者会有不同的布局设计

二、经典的行布局

  1. 行布局自适应;行布局自适应限制最大宽;行布局垂直水平居中

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>经典的行布局1</title>
        <style type="text/css">
            body{
                margin: 0;
                padding: 0;
                color: #fff;
                text-align: center;
            }
            .container{
                width: 800px;
                height: 200px;
                background: #007aff;
                position: absolute;
                top: 50%;
                left: 50%;
                margin-top: -100px;
                margin-left: -400px;
            }
        </style>
    </head>
    <body>
    <div class="container">这是页面内容</div>
    </body>
    </html>
    
  2. 行布局固定宽;行布局某部位自适应;行布局导航随屏幕滚动

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>经典的航布局2</title>
        <style type="text/css">
            body{
                margin: 0;
                padding: 0;
                color: #fff;
                text-align: center;
                font-size: 16px;
            }
            .header{
                width: 100%;
                height: 50px;
                background: #77b2ff;
                margin: 0 auto;
                line-height: 50px;
                position: fixed;
            }
            .banner{
                width: 800px;
                height: 300px;
                background: #ba57ff;
                margin: 0 auto;
                padding-top: 50px;
            }
            .container{
                width: 800px;
                height: 1000px;
                background: #ff8500;
                margin: 0 auto;
            }
            .footer{
                width: 800px;
                height: 100px;
                background: #ff9ee2;
                margin: 0 auto;
                line-height: 100px;
            }
        </style>
    </head>
    <body>
        <div class="header">这是页面的头部</div>
        <div class="banner">这是页面的banner部分</div>
        <div class="container">这是页面的内容部分</div>
        <div class="footer">这是页面的底部</div>
    </body>
    </html>
    

三、经典的列布局

  1. 两列布局固定;两列布局自适应

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>经典的列布局1</title>
        <style type="text/css">
            body{
                margin: 0;
                padding: 0;
                color: #fff;
            }
            .container{
                width: 90%;
                height: 1000px;
                margin: 0 auto;
            }
            .left{
                width: 60%;
                height: 1000px;
                background: #33cccc;
                float: left;
            }
            .right{
                width: 40%;
                height: 1000px;
                background: #007aff;
                float: right;
            }
        </style>
    </head>
    <body>
    <div class="container">
        <div class="left">这是页面的左侧</div>
        <div class="right">这是页面的右侧</div>
    </div>
    </body>
    </html>
    
  2. 三列布局固定;三列布局自适应

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>经典的列布局2</title>
        <style type="text/css">
            body{
                margin: 0;
                padding: 0;
                color: #fff;
            }
            .container{
                width: 100%;
                margin: 0 auto;
            }
            .left{
                width: 25%;
                height: 1000px;
                background: #007aff;
                float: left;
            }
            .middle{
                width: 50%;
                height: 1000px;
                background: #33cccc;
                float: left;
            }
            .right{
                width: 25%;
                height: 1000px;
                background: #77b2ff;
                float: right;
            }
        </style>
    </head>
    <body>
    <div class="container">
        <div class="left">这是页面的左侧</div>
        <div class="middle">这是页面的中间</div>
        <div class="right">这是页面的右侧</div>
    </div>
    </body>
    </html>
    

四、混合布局

混合布局固定;混合布局自适应

```
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>混合布局1</title>
    <style type="text/css">
        body{
            margin: 0;
            padding: 0;
            font-size: 16px;
            color: #fff;
            text-align: center;
        }
        .header{
            width: 100%;
            height: 50px;
            background: #a417ff;
            margin: 0 auto;
            line-height: 50px;
        }
        .banner{
            width: 100%;
            height: 200px;
            background: #ff7463;
            margin: 0 auto;
        }
        .container{
            width: 100%;
            margin: 0 auto;
            height: 1000px;
        }
        .container .left{
            width: 40%;
            height: 1000px;
            background: #4cff76;
            float: left;
        }
        .container .right{
            width: 60%;
            height: 1000px;
            background: #0ed8ff;
            float: right;
        }
        .footer{
            width: 100%;
            height: 100px;
            background: #77b2ff;
            margin: 0 auto;
            line-height: 100px;
        }
    </style>
</head>
<body>
    <div class="header">这是页面的头部</div>
    <div class="banner">这是页面的轮播图</div>
    <div class="container">
        <div class="left">这是页面的左侧</div>
        <div class="right">这是页面的右侧</div>
    </div>
    <div class="footer">这是页面的底部</div>
</body>
</html>
```

五、圣杯布局

  1. 圣杯布局是由国外的KevinCornell提出的一个布局模型概念;在国内由淘宝UED的工程师传播开来

  2. 布局要求:

    • 三列布局,中间宽度自适应,两边定宽
    • 中间栏要在浏览器中优先展示渲染
    • 允许任意列的高度最高
    • 用最简单的CSS,最少的HACK语句
  3. 流程方式:Middle部分首先要放在container的最前部分,然后是left、right

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>圣杯布局</title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            body{
                min-width: 700px;
            }
            .header, .footer{
                /*float: left;*/
                /*width: 100%;*/
                background: #999999;
                height: 50px;
                line-height: 50px;
                text-align: center;
            }
            .container{
                padding: 0 220px 0 200px;
                overflow: hidden;
            }
            .left,.middle,.right{
                position: relative;
                float: left;
                min-height: 130px;
                line-height: 130px;
                text-align: center;
            }
            .left{
                margin-left: -100%;
                left: -200px;
                width: 200px;
                background: #77b2ff;
            }
            .right{
                margin-left: -220px;
                right: -220px;
                width: 220px;
                background: #4cff76;
                /*float: right;*/
            }
            .middle{
                width: 100%;
                background: #33cccc;
                word-break: break-all;
            }
            .footer{
                clear: both;
            }
        </style>
    </head>
    <body>
        <div class="header">
            <h4>header</h4>
        </div>
        <div class="container">
            <div class="middle">
                <h4>middle</h4>
                <p>这是页面的中间内容</p>
            </div>
            <div class="left">
                <h4>left</h4>
                <p>这是页面的左边内容</p>
            </div>
            <div class="right">
                <h4>right</h4>
                <p>这是页面的右边内容</p>
            </div>
        </div>
        <div class="footer">
            <h4>footer</h4>
        </div>
    </body>
    </html>
    

六、双飞翼布局

  1. 经淘宝UED的工程师针对圣杯布局改良后得出双飞翼布局,去掉相对布局,只需要浮动和负边距

  2. 优点:

    • 主要的内容先加载的优化
    • 兼容目前所有的主流浏览器,包括IE6在内
    • 实现不同的布局方式,可以通过调整相关CSS属性即可实现
  3. 流程方式:main要放最前面,其次是sub、extra

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>双飞翼布局</title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            body{
                min-height: 700px;
            }
            .header,.footer{
                border: 1px solid #333;
                background: #00925F;
                text-align: center;
                height: 50px;
                line-height: 50;
            }
            .sub,.main,.extra{
                float: left;
                min-height: 130px;
                line-height: 130px;
                text-align: center;
            }
            .sub{
                margin-left: -100%;
                width: 200px;
                background: #ff7463;
            }
            .extra{
                margin-left: -220px;
                width: 220px;
                background: #0ed8ff;
            }
            .main{
                width: 100%;
            }
            .main-inner{
                margin-left: 200px;
                margin-right: 220px;
                min-height: 130px;
                background: #4cff76;
                word-break: break-all;
            }
            .footer{
                clear: both;
            }
        </style>
    </head>
    <body>
        <div class="header"></div>
        <div class="main">
            <div class="main-inner">
                <h4>main</h4>
            </div>
        </div>
        <div class="sub">
            <h4>sub</h4>
        </div>
        <div class="extra">
            <h4>extra</h4>
        </div>
        <div class="footer">
            <h4>footer</h4>
        </div>
    </body>
    </html>
    
posted @ 2020-05-19 11:53  a_哆肉  阅读(145)  评论(0编辑  收藏  举报