CSS 定位

定位类型

/相对定位/
/position: relative;/
/绝对定位,让出位置b2,相对于上一个设置了定位的父级(body)定位/
position: absolute;
/以浏览器窗口为参照物定位,像浮动小广告/
position: fixed;
例子
position: absolute;
left: 50px;
bottom: 400px;

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.box{
			width: 400px;
			height:400px;
			border:1px solid #000;
			margin: 50px auto 0;
			/*加入position,则根据此来定位*/
			position: relative;
		}

		.b1 ,.b2,{
			width: 300px;
			height: 100px;
			
		}
		.b1{
			background-color: gold;
			/*相对定位*/
			/*position: relative;*/
			/*绝对定位,让出位置b2,相对于上一个设置了定位的父级(body)定位*/
			position: absolute;
			left: 50px;
			bottom: 400px;
		}
		.b2{
			background-color: green;
			/*以浏览器窗口为参照物定位,像浮动小广告*/
			position: fixed;
			left: 50px;
			bottom: 100px;

		}

	</style>
</head>
<body>
	<div class="box">
	
		<div class="b1"></div>
		<div class="b2"></div>
		<div class="b3"></div>
		<div class="b4"></div>
	</div>
	
</body>
</html>

实例

目的:固定在顶部的水平居中的菜单
注:绝对定位和固定定位的块元素和行内元素会自动转换为行内元素

/*水平居中技巧*/
		left: 50%;
		margin-left: 此块的一半 如-250px;
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.menu{
			width: 500px;
			height: 50px;
			background-color: gold;
			position: fixed;

			/*水平居中技巧*/
			left: 50%;
			margin-left: -250px;
		}
		
	</style>
</head>
<body>
<div class="menu">菜单</div>

<br>
<p>shuaihai</p>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<p>shuaihai</p>
<p>shuaihai</p>
br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<p>shuaihai</p>
<p>shuaihai</p><p>shuaihai</p><p>shuaihai</p>

	
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>应用消息显示</title>
	<style type="text/css">
		.con{
			width: 100px;
			height: 100px;
			background-color: gold;
			border-radius: 14px;
			position: relative;
			margin: 50px auto 0;
		}
		.box1{
			width: 28px;
			height: 28px;
			border-radius: 14px;
			background-color: red;
			color: white;
			line-height: 28px;
			text-align: center;
			position: absolute;
			left: 86px;
			top: -14px;
		}
	</style>
</head>
<body>
<div class="con">
	<div class="box1">5</div>
</div>
	
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值