css浮动总结

原创 2019-04-29 14:43:23 346
摘要:<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>圣杯布局</title>&nb

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>圣杯布局</title>
    <style>
        /* 头部 */
        .header{
            /* 通常宽度默认为100% */
            width: 100%;
            background-color: lightgreen;
        }
        .header .content{
            /* 头部内容区,应该居中显示,所有要有宽度 */
            height: 60px;
            width: 1000px;
            background-color: lightcoral;
            /* 上下外边距为0,左右自动居中 */
            margin: 0 auto;

        }
        .header .content .nav{
            /* 清空导航UL元素的默认样式 */
            margin: 0;
            padding-left: 0;
        }
        .header .content .nav .item{
           list-style-type: none;
        }
        .header .content .nav .item a{
            /* 一定要将浮动设置到链接标签<a>上面,否则无法实现导航区的点击与高亮 */
            float: left;
            /* 设置最小宽度与最小高宽,以适应导航文本的变化 */
            min-width: 40px;
            min-height: 60px;
            /* 设置行高与头部区块等高,使导航文本可以垂直居中显示 */
            line-height: 60px;
            color: blueviolet;
            /* 将导航文本设置为系统根字体大小的1.2倍 */
            font-size:1.2rem;
            /* 设置民航文本的左右内边距,使导航文本不要挨的太紧 */
            padding: 0 40px;
            /* 去掉链接标签默认的下划线 */
            text-decoration: none;
            /* 让导航文本在每一个小区块中居中显示 */
            text-align: center;
        }
        .header .content .nav .item a:hover {
            /* 当鼠标移入到导航链接上时改变背景色与文本前景色,实现当前导航高亮功能 */
            background-color: #444;
            color: white;  
        }
        /* 使用圣杯布局实现主体部分 */

        /* 第一步: 主体容器设置的宽度与中间区块相同,并水平居中 */
        .container {
            width: 600px;
            min-height: 600px;
            margin: 5px auto;
            background-color: #ccc;
        }
        /* 第二步: 左,右二侧固定宽度,中间区块继承父级container宽度*/
        .main {
            width: inherit;
            min-height: 600px;
            background-color: lightcyan;
        }
        /* 设置左,右区块的宽度和高度(因为无内容,所以设置了最小高度),并设置参考色块 */
        .left {
            width: 200px;
            min-height: 600px;
            background-color: lightcoral;
        }

        .right {
            width: 200px;
            min-height: 600px;
            background-color: lightseagreen
        }
        /* 第三步:将中间,左,右区块全部左浮动 */ 
        /* 因中间区块宽度100%,所以左右会被挤压到下面 */
        .main, .left, .right {
            float: left;
        }
        /* 第四步: 将left和right拉回到中间区块的二边 */
        /* 通过设置区块的负外边距的方式,实现向反方向移动区块 */
        .left {
            margin-left: -100%;   /* -100%等价于-1000px,将左区块拉回到中间的起点处*/
        }
        .right {
            margin-left: -200px; /* -200px就正好将右区块上移到中间区块右侧显示 */
        }

        /* 现在还有最后一个问题,中间内容区块main没有显示出来 */
        /* 第五步: 设置容器container内边距给左右区块预留位置 */
        .container {
            padding-left:200px;
            padding-right: 200px;
        }
        /* 第六步:左右区块使用相对定位,回到正确的位置上 */
        .left {
            position: relative;
            left: -200px;
        }
        .right {
            position: relative;
            left: 200px;
        }
        /* 底部与头部的基本样式类似 */
        .footer {
            width: 100%;
            background-color: lightseagreen;
        }

        .footer .content {
            width: 1000px;
            height: 60px;
            background-color: lightblue;
            margin: 0 auto;
        }
        .footer .content p {
            text-align: center;
            line-height: 60px;
        }

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

        /* 鼠标移入时显示下划线并加深字体前景色 */
        .footer .content  a:hover {
            text-decoration: underline;
            color: white;
        }

    </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="container">
       
            <!-- 中间内容main区块中 -->
            <div class="main">主体内容区</div>
    
            <!-- 创建左侧边栏区块 -->
            <div class="left">左侧</div>
    
            <!-- 创建右侧边栏区块 -->
            <div class="right">右侧</div>
    
        </div>
    
        <!-- 底部 -->
        <div class="footer">
            <div class="content">
                <p>
                    <a href="">© PHP中文网版权所有</a>  | 
                    <a href="">0551-88889999</a>  | 
                    <a href="">皖ICP2016098801-1</a>
                </p>
                
            </div>
        </div>
    
</body>
</html>

批改老师:查无此人批改时间:2019-04-30 09:18:58
老师总结:完成的不错。浮动一般在手机网页用的比较多。继续加油

发布手记

热门词条