css布局 定位(绝对相对固定)

定位详解
position 属性 规定元素的定位类型。
属性值
absolute
生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。
元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。
fixed
生成绝对定位的元素,相对于浏览器窗口进行定位。
元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。
relative
生成相对定位的元素,相对于其正常位置进行定位。
因此,“left:20” 会向元素的 LEFT 位置添加 20 像素。
static 默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。z-index 改变坐标值,现在是z轴。
inherit 规定应该从父元素继承 position 属性的值。

下面是哪个简单的例子展示了相对布局、绝对布局和固定位置

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>定位详解</title>
	<style>
	*{margin: 0;padding: 0;}
	div{
		width: 1000px;
		height: 400px;
		background: lightgray;
	}
	#p1{
		width: 100px;
		height: 200px;
		background-color: red;
	}
	.a #p2{
		width: 200px;
		height: 100px;
		background-color: blue;
		position: relative;/*相对定位*/
		left: 20px;
		top: 20px;
	}
	.a #p3{
		width: 1000px;
		height: 50px;
		background-color: lightblue;
	}
	.b #p3{
		width: 1000px;
		height: 2000px;
		background-color: lightblue;
	}
	.b #p2{
		width: 200px;
		height: 100px;
		background-color: blue;
		position: absolute;/*绝对定位*/
		left: 20px;
		bottom: 20px;
	}
	.c #p2{
		width: 200px;
		height: 100px;
		background-color: blue;
		position: fixed;/*绝对定位*/
		left: 20px;
		bottom: 20px;
	}
	.c #p3{
		width: 1000px;
		height: 2000px;
		background-color: lightblue;
</style>
</head>
<body>
	<div class="a">
		<p>相对定位</p>
		<p id="p1">p1</p>
		<p id="p2">p2</p>
		<p id="p3">p3</p>
		
	</div>
	<br>
	<div class="b">
		<p>绝对定位</p>
		<p id="p1">p1</p>
		<p id="p2">p2</p>
		<p id="p3">p3</p>
		
	</div>
	<div class="c">
		<p>固定位置</p>
		<p id="p1">p1</p>
		<p id="p2">p2</p>
		<p id="p3">p3</p>
	</div>
</body>
</html>

绝对定位的应用DEMO

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>绝对定位应用</title>
	<style>
*{margin: 0;padding:0;}
ul{
	background: lightgray;
	width: 600px;
}
li{
	background: lightblue;
	color: #fff;
	margin-bottom: 10px;
	height:  80px;
	line-height: 60px;
	position: relative;
}
input[type="checkbox"]{
	width: 20px;
	height: 20px;
	position: absolute;
	top: 20px;
	right: 40px;
}
</style>
</head>
<body>
	<h2>请勾选已经读过的书</h2>
<ul>
    <li>hello code <input type="checkbox"></li>
    <li>21 days <input type="checkbox"></li>
    <li>js path <input type="checkbox"></li>
</ul>	
</body>
</html>

固定定位应用demo

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>固定定位应用</title>
	<style>
	body{
		height: 2000px;
	}
*{margin: 0;padding:0;}

button{
	position: fixed;
	right: 20px;
	top: 50%;
	margin-top: -30px;
}
</style>
</head>
<body>
<button style="width: 80px; height: 60px;">我要反馈</button>
</body>
</html>

课后习题
用css做下拉菜单
做一个固定位置的弹出登录界面

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值