博客列表 >圣杯布局的整理和总结-2019年1月15日

圣杯布局的整理和总结-2019年1月15日

的博客
的博客原创
2019年01月17日 17:32:20802浏览

圣杯布局是一种相对布局,主体的中间栏要在浏览器中优先展示渲染

具体实现方法:

设置HTML基本DOM框架,mian为主体的中间栏

<!-- 主体 -->

<div class="container">

    <div class="main">主体内容优先显示,所以将主体div写在开头</div>

    <div class="left">左侧</div>

    <div class="right">右侧</div>

</div>

设置基本的CSS样式

.container{

    width:600px;

    height:800px;

    margin: 5px auto;

}

.container .main,.left,.right{

    height: inherit;

}

.main{

    width:inherit;

    background-color: lightblue;

}

.left{

    width: 200px;

    background-color: lightcyan;

}

.right{

    width: 200px;

    background-color: lightgreen;

}

例如上面的样式,左右两个区块宽度都设置为200px,主体总宽度为1000px,扣去左右两个区块的宽度400px,中间区块设置为600px


将主体的三个区块修改为浮动:

.container .main,.left,.right{

    float: left;

}

修改后的三个区块都在同一浮动层内,至此只要修改左边区块和右边区块到指定的位置就可以了

移动左边区块,先添加相对定位属性,再修改他的左外边距,令左区块移动到浮动层(.container .main,.left,.right)的开头

.left{

    position: relative;

    margin-left: -100%;

    left: -200px;

}

负的margin-left会让元素沿文档流向左移动,如果负的数值比较大就会一直移动到上一行。

接着调整右边区块,也设置为相对定位并修改负的margin-left,使其向左移动

.right{

    position: relative;

    margin-left: -200px;

    left: 200px;

}

这就完成了圣杯布局,主体区块的中间内容都会优先向用户展示,且无论浏览器的宽度有多窄,依然会显示中间区块的内容


实例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0%;
            margin: 0%;
        }

        .clear {
            clear: both;
        }

        .header {
            width: 100%;
            background-color: lightblue
        }

        .header .content {
            width: 1000px;
            height: 60px;
            margin: 0 auto;
            background: lightcoral;
        }

        .header .content .nav {
            list-style: none;
        }

        .header .content .nav a {
            text-decoration: none;
            color: lightgray;
            font-size: 1.2rem;
            min-width: 80px;
            min-height: 60px;
            line-height: 60px;
            text-align: center;
            float: left;
            padding: 0 15px;
        }

        .header .content .nav a:hover {
            background-color: #444;
            color: white;
        }

        .container{
            width:600px;
            height:800px;
            margin: 5px auto;
            
        }

        .container .main,.left,.right{
            height: inherit;
            float: left;
        }

        .main{
            width: inherit;
            background-color: lightblue;
            
        }

        .left{
            width: 200px;
            background-color: lightcyan;
            position: relative;
            margin-left: -100%;
            left: -200px;

        }

        .right{
            width: 200px;
            background-color: lightgreen;
            position: relative;
            margin-left: -200px;
            left: 200px;
        }


        .footer {
            width: 100%;
            height: 60px;
            background-color: antiquewhite;
        }

        .footer .content p {
            text-align: center;
            line-height: 60px;
            background-color: azure;
        }

        .footer .content a {
            text-decoration: none;
        }

        .footer .content a:hover {
            text-decoration: underline;
            color: #444;
        }
    </style>
</head>

<body>
    <!-- 头部导航 -->
    <div class="header">
        <div class="content">
            <ul class="nav">
                <li class="item"><a href="">首页</a></li>
                <li class="item"><a href="">公司新闻</a></li>
                <li class="item"><a href="">最新产品</a></li>
                <li class="item"><a href="">关于我们</a></li>
            </ul>
        </div>
    </div>
    <div class="clear"></div>
    <!-- 主体 -->
    <div class="container">
        <div class="main">主体内容优先显示,所以将主体div写在开头</div>
        <div class="left">左侧</div>
        <div class="right">右侧</div>
    </div>
    <!-- 底部 -->
    <div class="footer">
        <div class="content">
            <p>
                <a href="">Copyright 2014-2018 http://www.php.cn/ All Rights Reserved</a> | 
                <a href="">皖B2-20150071-9</a>
            </p>
        </div>
    </div>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例



总结思考:

    圣杯布局使用了绝对定位来调整区块位置,适当使用负边距可以让元素向左移动

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议