定位
文档流
文档流,是指盒子按照html标签编写的顺序一次从上到下,从左到右排列,先写的先排列,后写的排在后面,每个盒子都占据自己的位置
关于定位
我们可以使用css中的position属性来设置元素的定位类型,position的设置项如下:
- relative:生成相对定位元素,元素所占据的文档流的位置保留,元素本身相对自身原位置进行偏移。
- absolute: 生成绝对定位元素,元素脱离文档流,不占据文档流的位置,可以理解为漂浮在文档流的上方,相对于上一个设置了定位的父级元素来定位,如果找不到,则相对于body元素进行定位。
- fixed 生成固定定位元素,元素脱离文档流,不占据文档流的文职,可以理解为漂浮在文档流的上方,相当于浏览器窗口进行定位。
- static 默认值,没有定位,元素出现在正常的文档流中,相当于取消定位属性或者不设置定位属性。
- inherit:从父元素继承position属性的值。
定位元素的偏移
定位的元素还需要使用left、right、top、bottom来设置参照元素的偏移值
定位元素层级
使用z-index来设置元素的层级,元素层级越高,定位元素就会浮动在正常的文档流之上。
实例:
<!DOCTYPE html>
<html>
<head>
<title>元素定位层级</title>
<style type="text/css">
.con{
width: 400px;
height: 600px;
border:1px solid black;
margin: 50px auto 0;
/*将父类元素设置为相对定位*/
position:relative;
}
.con div{
width: 200px;
height: 100px;
/*margin: 10px;*/
/*将子类元素设置为绝对定位absolute*/
position: absolute;
}
.box01{
background-color: green;
left: 20px;
top: 20px;
/*使用z-index定义层级*/
z-index: 10;
}
.box02{
background-color: gold;
left: 40px;
top: 40px;
/*z-index后面写入数字就可以了,数字越高,那么层级也就越高,就会覆盖其他页面上*/
z-index: 12
}
.box03{
background-color: pink;
left: 60px;
top: 60px;
z-index: 12
}
.box04{
background-color: yellow;
left: 80px;
top: 80px;
}
</style>
</head>
<body>
<div class="con">
<div class="box01"></div>
<div class="box02"></div>
<div class="box03"></div>
<div class="box04"></div>
</div>
</body>
</html>
浏览器中的打开。
定位元素特性
绝对定位和固定定位的元素和行内元素会自动转化为行内块元素
相对元素定位实例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>相对元素定位</title>
<style type="text/css">
.con{
width: 400px;
height:600px;
border: 1px solid black;
margin: 50px auto 0;
}
.box01, .box02{
width: 200px;
height: 100px;
margin:10px;
}
.box01{
background-color: green;
/*相对定位: 按照父类的大小来设置移动距离*/
position: relative;
left: 50px;
top:50px;
}
.box02{
background-color: gold;
}
</style>
</head>
<body>
<div class="con">
<div class="box01"></div>
<div class="box02"></div>
</div>
</body>
</html>
浏览器中打开
绝对定位元素实例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>绝对定位</title>
<style type="text/css">
.con{
width: 400px;
height:600px;
border: 1px solid black;
margin: 50px auto 0;
/*将父类元素设置为相对定位*/
position: relative;
}
.box01, .box02{
width: 200px;
height: 100px;
margin:10px;
}
.box01{
background-color: green;
/*绝对定位: 若父类元素没有设置定位,则按照body标签来定位*/
position: absolute;
left: 50px;
top:50px;
}
.box02{
background-color: gold;
}
</style>
</head>
<body>
<div class="con">
<div class="box01"></div>
<div class="box02"></div>
</div>
</body>
</html>
浏览器中打开
固定元素定位
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>绝对定位</title>
<style type="text/css">
.con{
width: 400px;
height:600px;
border: 1px solid black;
margin: 50px auto 0;
}
.box01, .box02{
width: 200px;
height: 100px;
margin:10px;
}
.box01{
background-color: green;
/*固定定位:生成固定定位元素,元素脱离文档流,不占据文档流的位置
相当于对于浏览器窗口进行定位。*/
position: fixed;
left: 50px;
top:50px;
}
.box02{
background-color: gold;
}
</style>
</head>
<body>
<div class="con">
<div class="box01"></div>
<div class="box02"></div>
</div>
</body>
</html>
浏览器中打开
元素定位实例-消息图标
做一个消息提示的小图标
<!DOCTYPE html>
<html>
<head>
<title>元素定位</title>
<style type="text/css">
.con{
margin: 50px auto 0;
width: 100px;
height: 100px;
background-color: gold;
position: relative;
border-radius: 14px;
}
.box{
width: 28px;
height: 28px;
background-color: red;
color:white;
position: relative;
text-align: center;
line-height: 28px;
left: 86px;
top: -14px;
/*css3选择器,圆角属性, 半径为14像素的圆*/
border-radius: 14px;
font-family: fantasy;
}
</style>
</head>
<body>
<div class="con">
<div class="box">5</div>
</div>
</body>
</html>
运行程序,效果如下:
元素定位小实例二–弹窗列表
<!DOCTYPE html>
<html>
<head>
<title>定位实例</title>
<style type="text/css">
.list{
height: 80px;
width: 960px;
background-color: gold;
top: 0;
left: 0;
/*元素定位, 固定元素定位*/
position: fixed;
left: 50%;
margin-left: -480px;
line-height: 80px;
z-index: 10;
}
.con p{
text-align: center;
}
.popup{
width: 500px;
height: 300px;
border:1px solid #000;
background-color: #fff;
position: fixed;
left: 50%;
top: 50%;
margin-left: -251px;
margin-top: -151px;
z-index: 9999;
}
.popup h2{
background-color: gold;
height: 40px;
margin:10px;
}
.bg{
position: fixed;
width: 100%;
height: 100%;
background-color: #000;
z-index: 9998;
left: 0;
top:0;
/*设置透明度*/
opacity: 0.5;
}
.popup_bg{
display: none;
}
.all:hover .popup_bg{
display: block;
}
</style>
</head>
<body>
<div class="con">
<div class="all">
<!-- 制作弹框 -->
<h3>弹窗</h3>
<div class="popup_bg">
<div class="popup"><h2>弹窗列表</h2></div>
<div class="bg"></div>
</div>
</div>
<!-- 第一:制作固定在顶部的水平居中的菜单栏 -->
<div class="list">菜单</div>
<!-- 第二:制作浮动的菜单列表 -->
<p>菜单列表</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>菜单列表</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>菜单列表</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>菜单列表</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>菜单列表</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>菜单列表</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>菜单列表</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p>菜单列表</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</body>
</html>
效果如下;
background属性
属性解释
background属性时css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个符合属性,它可以分解成如下几个设置项:
-
background-color: 设置背景颜色
-
background-img:设置背景图片, 用url(img_path)来设置图片地址
-
background-repeat:设置背景图片如何重复平铺(repeat-y/x)
-
background-position:设置图片位置(left top)
-
background-attachment: 设置背景图片是固定还是随着页面滚动条滚动
实际应用中,我们可以用background属性将上面所有的设置项放在一起,而且也建议这么做,这样做性能更高,而且兼容性更好,比如:‘background:url(images/a.jpg) blue no-repeat center center;’,这里面的blue是设置background-color,url(images/a.jpg)是设置背景图片,no-repeat是设置background-repeat,说明只重复一次, center center是设置background-position的位置。各个设置项用空格隔开,有的设置项也可以不用写,它会使用默认值。
background属性实例
<!DOCTYPE html>
<html>
<head>
<title>背景图片实例</title>
<style type="text/css">
.box{
width: 100px;
height: 100px;
border:5px solid #000;
margin:50px auto 0;
/*
background-image: url(images/阴阳怪气的先报警再说.jpg);
background-repeat: no-repeat;
background-position: -10px -200px;
*/
/*以上操作一句话完成*/
background: url(images/阴阳怪气的先报警再说.jpg) no-repeat -10px -200px;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
原图片
代码运行之后的图片:
雪碧图制作
做一个
<!DOCTYPE html>
<html>
<head>
<title>雪碧图案列</title>
<style type="text/css">
.box{
width: 300px;
height: 305px;
margin: 50px auto 0;
padding:0;
/*background: cyan;*/
}
.box li{
list-style: none;
height: 60px;
border-bottom: 1px #000 dotted;
line-height: 60px;
/*首行缩进*/
text-indent: 50px;
/*插入图片*/
background: url(images/bg01.png) left 10px no-repeat;
}
.box .bg01{
background: url(images/b g01.png) 0px -71px no-repeat;
}
.box .bg02{
background: url(images/bg01.png) 0px -153px no-repeat;
}
.box .bg03{
background: url(images/bg01.png) 0px -235px no-repeat;
}
.box .bg04{
background: url(images/bg01.png) 0px -315px no-repeat;
}
</style>
</head>
<body>
<ul class="box">
<li>美人鱼</li>
<li class="bg01">琅琊榜</li>
<li class="bg02">基尼太美</li>
<li class="bg03">大副</li>
<li class="bg04">肖申克的救赎</li>
</ul>
</body>
</html>
效果如下: