html中相对定位与绝对定位的笔记

本文详细解析了HTML中body与html的区别及CSS中相对定位(relative)与绝对定位(absolute)的使用方法。通过实例展示了margin与top/left/right/bottom属性在布局中的不同作用。

html是整个文档空间,body是html中的文档空间, body与html相差9cm左右

body是靠里面的文档空间撑大的

postion中的相对定位:relative

postion中的绝对定位:absolute

相对定位:

当没有参照物的时候,相对定位是相对与body定位的(从上,下,左,右开始寻找文档文件是否被占用了(除了绝对定位以外,若用绝对定位了),若有文档空间被占用,则开始从这定位)

绝对定位:

当没有参照物额时候,绝对定位是相对与window定位的(从父类的定位position开始寻找参照物,一级一级向上找,若没有,则 直到最后的window)


使用margin的时候,是占用文档空间的

使用top,left,right,button的时候,是不占用文档空间的,看情况使用

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <style>
	*{margin:0;padding:0;}
	.content{		
		width:500px;
		height:600px;
		margin:100px auto;		
		box-shadow:1px 1px 6px #ff3300;
	}
	.content1{		
		width:100px;
		height:100px;
		margin-top:50px;
		border:1px solid blue;
	}
	.content11{
		position:relative;
		width:110px;
		height:110px;
		top:50px;
		border:1px solid green;
	}
	.content2{
		
		width:100px;
		height:100px;
		top:50px;
		border:1px solid red;
	}
  </style>
 </head>
 <body>
	<div class="content">
		<div class="content1"></div>
		<div class="content11"></div>
		<div class="content2"></div>
	</div>
 </body>
</html>
上面的代码运行如下图:


从上图可以看出当使用相对定位的时候,.content2的父级是.content,则位置紧挨着.content1和.content11的总高度

当.content11改为如下图文档的时候,如下图:

.content11{
		
	width:110px;
	height:110px;
	margin-top:50px;
   border:1px solid green;
}

上图当.content11里面的文档改为margin-top的时候,则占用了文档空间,则如上图所示定位

则定位只是用来定其位置,而margin-top/left/botton/right是占用控件文档


我们一般是在相对里面套用绝对来进行项目的开发

      divAddress{
                    position: relative;
                    margin-top: 48px;//这就是刚才提的当使用margin的时候才能进行下面的相对
                    width: 370px;
                    height: 66px;
                    border-bottom: 1px #c1c1c1 solid;
                }
                #img1{
                    position: absolute;
                    float: left;margin-top:2px;
                    width: 24px; height: 24px;
                }
                .address{
                    position: absolute;
                    margin-left: 34px;
                    font-size: 20px;
                    color: #c1c1c1;
                }

               #peoplePost1{
                    position: relative;
                    margin-top: 12px;
                    width: 370px;
                    height: 40px;
                    border-bottom: 1px #c1c1c1 solid;
                }
                #people1{
                    position: absolute;
                    float: left;margin-top:2px;
                    width: 24px; height: 24px;
                }
                .people2{
                    position: absolute;
                    margin-left: 34px;
                    font-size: 20px;
                    color: #c1c1c1;
                }
                #post1{
                    position: absolute;
                    float: left;margin-top:2px;margin-left:192px;
                    width: 24px; height: 24px;
                }
                .post2{
                    position: absolute;
                    margin-left: 222px;
                    font-size: 20px;
                    color: #c1c1c1;
                }

               <div id="divAddress">
                    <img id="img1" alt="address" src="<%=request.getContextPath()%>/images/address.png">
                    <div class="address">地址 : <span style="font-weight: 600;color: black;">输入上海市卢湾区卢湾榜路758弄10号1011室</span></div>
                </div>

               <div id="peoplePost1">
                    <img id="people1" alt="people" src="<%=request.getContextPath()%>/images/people.png">
                    <div class="people2">联系人 : <span style="font-weight: 600;color: black;">张三</span></div>
                    <img id="post1" alt="post" src="<%=request.getContextPath()%>/images/post.png">
                    <div class="post2">职位 : <span style="font-weight: 600;color: black;">业务经理</span></div>
                </div>


上面也就是相对和绝对结合的html代码中的一段

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值