12.前端小白学个CSS网页布局

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

为什么要学习网页布局呢?
这是制作一个好的网页的基础

行布局,多列布局,圣杯布局,双飞翼布局

在学习之前肯定要掌握:
HTML和CSS基础
会使用DIV+CSS进行排版
熟悉float属性,position属性

经典的行布局
-基础的行布局
-行布局自适应
-行布局自适应限制最大宽
-行布局垂直水平居中

body{
    
    
	margin:0;
	padding: 0;
	color:#fff;
	text-align:center;
	}
.container{
    
    
	width:300px;
	max-width:1000px;
	height:1000px;
	background:#4c77f2;
	margin:0 auto;
	}

要实现水平垂直居中

.container{
    
    
	width:800px;
	height:200px;
	background:#4c77f2;
	position:absolute;
	top:50%;
	left:50%;
	margin-top:-100px;
	margin-left:-400px;
	}

在这里插入图片描述
行布局实现导航固定随屏幕滚动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        body{
     
     
            margin: 0;
            padding: 0;
            color: #fff;
        }
        .container{
     
     
            width: 800px;
            height: 1000px;
            background: #4c77f2;
            margin: 0 auto;
            padding-top: 40px;
            text-align: center;
        }
        .header{
     
     
            width:100%;
            position: fixed;
            height: 40px;
            background: #414141;
            text-align: center;
            font-size: 16px;
            line-height: 40px;

        }
        .footer{
     
     
            width: 800px;
            height: 100px;
            background: #333;
            margin: 0 auto;
            text-align: center;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <div class="header">这是页面头部</div>
    <div class="container">
        这是页面内容
    </div>
    <div class="footer">这是页面底部</div>
</body>
</html>

经典的列布局
-两列布局固定
-两列布局自适应
-三列布局固定
-三列布局自适应

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>三列布局</title>
    <style>
        body{
     
     
            margin: 0;
            padding: 0;
            color: #fff;
        }
        .left{
     
     
            width:25%;
            height: 1000px;
            float:left;
            background: #67b581;
        }
        .right{
     
     
            width: 25%;
            height: 1000px;
            float: right;
            background: #67b581;
        }
        .middle{
     
     
            width: 50%;
            height: 1000px;
            background: #175bd8;
            word-wrap: break-word;
            float:left;
        }
    </style>
</head>
<body>

    <div class="left">这是页面左侧</div>
    <div class="middle">这是页面中间</div>
    <div class="right">这是页面右侧</div>

</body>
</html>

混合布局自适应:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两列布局</title>
    <style>
        body{
     
     
            margin: 0;
            padding: 0;
            color:#fff;
        }

        .header{
     
     
            width:100%;
            height: 50px;
            background: #8b8d91;
            margin: 0 auto;
            text-align: center;
            font-size: 16px;
            line-height: 50px;
        }
        .banner{
     
     
            width: 100%;
            height: 300px;
            background: #f29196;
            margin: 0 auto;
            text-align: center;
        }
        
        .container{
     
     
            width: 100%;
            height: 1000px;
            background: #4c77f2;
            margin: 0 auto;
            text-align: center;
        }
        .left{
     
     
            width:30%;
            height: 1000px;
            float:left;
            background: #4c77f2;
        }
        .right{
     
     
            width: 70%;
            height: 1000px;
            float:right;
            background: #67b581;
        }
        .footer{
     
     
            width: 100%;
            height: 100px;
            background: #8b8d91;
            margin: 0 auto;
            text-align: center;
            font-size: 16px;
            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>

总的来说,自适应布局就是用百分比来表示,固定的则用具体数值比如500px

圣杯布局:
-圣杯布局是由国外的Kevin Cornell提出的一个布局模型概念
-在国内由淘宝UED的工程师传播开来
在这里插入图片描述
中间栏要在浏览器中优先展示渲染
允许任意列的高度最高
用最简单的CSS、最少的HACK语句

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>圣杯布局</title>
    <style type="text/css">
        *{
     
     margin: 0;padding: 0;}
        body{
     
     min-width: 700px;}
        .header,
        .footer{
     
     
            border: 1px solid #333;
            background: #ddd;
            text-align: center;
            height: 40px;
            line-height: 40px;
        }
        .left,
        .middle,
        .right{
     
     
            position: relative;
            float: left;
            min-height: 130px;
        }
        .container{
     
     
            padding:0 220px 0 200px;
            overflow: hidden;
        }
        .left{
     
     
            margin-left: -100%;
            left: -200px;
            width: 200px;
            background: #f00;
        }
        .right{
     
     
            margin-left: -220px;
            right: -220px;
            width: 220px;
            background: #30a457;
        }
        .middle{
     
     
            width: 100%;
            background: #1a5acd;
            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>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>双飞翼布局</title>
    <style type="text/css">
        *{
     
     margin: 0;padding: 0;}
        body{
     
     min-width: 700px;}
        .header,
        .footer{
     
     
            border: 1px solid #333;
            background: #ddd;
            text-align: center;
            height: 40px;
            line-height: 40px;
        }
        .sub,
        .main,
        .extra{
     
     
            float: left;
            min-height: 130px;
        }
        .sub{
     
     
            margin-left: -100%;
            width: 200px;
            background: #f00;
        }
        .extra{
     
     
            margin-left: -220px;
            width: 220px;
            background: #1a5acd;
        }
        .main{
     
     
            width: 100%;
        }
        .main-inner{
     
     
            margin-left: 200px;
            margin-right: 220px;
            min-height: 130px;
            background: #30a457;
            word-break: break-all;
        }
        .footer{
     
     
            clear: both;
        }
    </style>
</head>
<body>
<div class="header">
    <h4>header</h4>
</div>
<div class="main">
    <div class="main-inner">
        <h4>main</h4>
        <p>
            这是页面的主体内容
            这是页面的主体内容
            这是页面的主体内容
            这是页面的主体内容
            这是页面的主体内容
            这是页面的主体内容
            这是页面的主体内容
            这是页面的主体内容
        </p>
    </div>
</div>
<div class="sub">
    <h4>sub</h4>
    <p>
        这是页面的左边
        这是页面的左边
        这是页面的左边
        这是页面的左边
        这是页面的左边
        这是页面的左边
    </p>
</div>

<div class="extra">
    <h4>extra</h4>
    <p>
        这是页面的右边
        这是页面的右边
        这是页面的右边
        这是页面的右边
    </p>
</div>
<div class="footer">
    <h4>footer</h4>
</div>
</body>
</html>

总结:
行布局:
margin:0 auto;
上下为0,左右居中
页面自适应改为width:100%;

在这里插入图片描述
在这里插入图片描述
当然实际中还是要灵活运用,看要求是固定还是自适应。

猜你喜欢

转载自blog.csdn.net/qq_44682019/article/details/108878854
今日推荐