08CSS网页布局基础、网页布局案例-课堂作业
网页布局基础
基础介绍
- 以最适合浏览的方式将图片和文字排放在页面的不同位置
- 布局模式有多种,不同的制作者会有不同的布局设计
经典的行布局
一.经典的行布局-1
- 基础的行布局
- 行布局自适应
- 行布局自适应限制最大宽
- 行布局垂直水平居中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></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></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>
经典的两列布局
一.两列布局固定;两列布局自适应
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></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>
二.三列布局固定;三列布局自适应
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></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></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>
圣杯布局
-
圣杯布局是由国外的KevinCornell提出的一个布局模型概念;在国内由淘宝UED的工程师传播开来
-
布局要求:
- 三列布局,中间宽度自适应,两边定宽
- 中间栏要在浏览器中优先展示渲染
- 允许任意列的高度最高
- 用最简单的CSS,最少的HACK语句
-
流程方式: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>
双飞翼布局
-
经淘宝UED的工程师针对圣杯布局改良后得出双飞翼布局,去掉相对布局,只需要浮动和负边距
-
优点:
- 主要的内容先加载的优化
- 兼容目前所有的主流浏览器,包括IE6在内
- 实现不同的布局方式,可以通过调整相关CSS属性即可实现
-
流程方式: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: 50px; } .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">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>
网页布局案例
html代码
<!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>css网页布局案例</title> <link rel="stylesheet" href="index.css"> </head> <body> <!-- 头部 --> <div class="header"> <div class="logo"> <img src="image/logo.png" alt="logo"> </div> <div class="nav"> <ul> <li>首页</li> <li>图片</li> <li>视频</li> <li>手记</li> </ul> </div> </div> <!-- 主体 --> <div class="main"> <div class="top"> <img src="image/1.jpeg"> </div> <!-- 遮罩层 --> <div class="topLayer"></div> <div class="topLayer-top"> <div class="word">MY BEAUTIFUL LIFE</div> <button>LOOK MORE ></button> </div> <!-- 中间部分 --> <div class="middle"> <div class="m-top"> <div class="commen weibo"> <img src="image/weibo.png"> <div>MICROBLOG</div> </div> <div class="commen weixin"> <img src="image/weixin.png"> <div>WECHAT</div> </div> <div class="commen qq"> <img src="image/qq.png"> <div>QQ</div> </div> </div> <div class="clear"></div> <div class="m-middle"> “I want to give good things to record down,<br>after the recall will be very beautiful.” </div> <div class="m-bottom"> <div class="m-com"> <img src="image/03-01.jpg"> <div class="des1">cool Image</div> <div class="des2">Record The Picture</div> </div> <div class="m-com"> <img src="image/03-02.jpg"> <div class="des1">cool Image</div> <div class="des2">Record The Picture</div> </div> <div class="m-com"> <img src="image/03-03.jpg"> <div class="des1">cool Image</div> <div class="des2">Record The Picture</div> </div> </div> </div> <div class="clear"></div> <div class="bottom"> <div class="content"> <div class="title"> FROM THE PHOTO ALBUM </div> <div class="pic-content"> <dl> <dt><img src="image/04-01.jpg"></dt> <dd class="word">Life is like a book,just read more and more refined,more write more carefully.Life is like a book,just read more and more refined,more write more carefully.Life is like a book,just read more and more refined,more write more carefully.</dd> </dl> <dl> <dt><img src="image/04-02.jpg"></dt> <dd class="word">Life is like a book,just read more and more refined,more write more carefully.Life is like a book,just read more and more refined,more write more carefully.Life is like a book,just read more and more refined,more write more carefully.</dd> </dl> </div> <div class="clear"></div> </div> </div> </div> <!-- 底部 --> <div class="footer"> </div> </body> </html>
css代码
*{ padding:0; margin:0; } .header{ width:100%; height:100px; } .header img{ width:300px; height:85px; padding-left:100px; padding-top:8px; } .header .logo{ float:left; } .header .nav{ float:right; } .header .nav ul{ padding-right: 100px; } .header .nav ul li{ float: left; list-style: none; width: 80px; height: 100px; font-size: 15px; font-weight: bolder; color: #7d7d7d; line-height: 100px; } .main .top{ width:100%; height:600px; } .main .top img{ width:100%; height:600px; position: relative; } .main .topLayer{ position: absolute; top:100px; left:0; background-color: #000; width:100%; height:600px; opacity: 0.5; } .main .topLayer-top{ width:500px; height:300px; position:absolute; top:400px; margin-top:-150px; z-index:2; right:50%; margin-right:-250px; } .main .topLayer-top .word{ padding-top:100px; color:#fff; font-size:45px; font-weight: bolder; text-align:center; font-family: "微软雅黑"; } .main .topLayer-top button{ width:200px; height:60px; margin-top:50px; color:#fff; background-color: #f5704f; font-family: "微软雅黑"; text-align: center; font-weight: bolder; font-size: 14px; border-radius: 8px; clear:both; margin-left:150px; } .main .middle{ width:1000px; margin:0 auto; } .main .middle .m-top .commen{ float:left; width:33.3%; padding-top:50px; text-align: center; } .main .middle .m-top .commen img{ width:100px; height:100px; } .main .middle .m-top .commen .comm{ font-size:20px; color: #7d7c7f; padding-top:20px; font-weight:bold; } .main .middle .m-middle{ font-size:22px; color:#e19796; font-weight: bold; padding-top:50px; font-style:italic; text-align: center; padding-bottom: 50px; } .clear{ clear:both; } .main .middle .m-bottom .m-com{ width:33.3%; float:left; text-align: center; } .main .middle .m-bottom .m-com img{ width:310px; height:260px; } .main .middle .m-bottom .m-com .des1{ font-size: 20px; font-weight: bold; color:#7d7d7f; padding-top:20px; } .main .middle .m-bottom .m-com .des2{ font-size: 20px; font-weight: bold; padding-top:10px; color: #bdbdbc; } .main .bottom{ background-color: #f9f9f9; } .main .bottom .content{ width:1000px; margin:0 auto; } .main .bottom .content .title{ text-align: center; font-size: 20px; font-weight: bold; color:#7d7c7f; font-family:"微软雅黑"; padding-top:50px; } .main .bottom .content .pic-content dl{ float:left; width:470px; margin:10px 12px; } .main .bottom .content .pic-content dl img{ width:470px; height:460px; } .main .bottom .content .pic-content dl .word{ padding-top:20px; font-size:20px; font-weight:bold; color:#7d7c7f; padding-bottom:50px; }

浙公网安备 33010602011771号