【web前端(十八)】html_绝对定位

 body设重定义

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>绝对定位1</title>
		<style>
			/*对body设重定义*/
			body{
				height: 1000px;
				}
				/*当我们将body设置好了之后,
				 当浏览器的高较大时,我们就在当前页面进行*/
				
				
			.c1{
				width: 100px;
				height: 100px;
				background-color: #87CEEB;
				position:absolute;
				/*top: 350px;
				left: 350px;*/
				top: 0px;
				left: 0px;
			}
			
			.c2{
				width: 100px;
				height: 100px;
				background-color: #FF0000;
				/*这里是参考当前浏览器窗口定位的(左下角、右上角)*/
				position:absolute;
				right:0px;
				bottom: 0px;
			}
			
			.c3{
				width: 300px;
				height: 300px;
				background-color:#000000;
				
				/*隐藏*/
				/*display: none;*/
				
				/*父窗口加margin*/
				margin:110px;
				position: absolute;
			} 
			
			.c4{
				width: 300px;
				height: 100px;
				background-color:#00FF00;
				position:absolute;
				top: 0px;
				left: 0px;
				
			}
						
			/*在父块没有做修饰或定位的情况下,
				 在绝对定位条件下,
				 子块是不受父块约束的。*/
				/*父集要用相对定位,
				 子集要用绝对定位*/
				/*大块也可用相对定位*/
		</style>
	</head>
	<body>
		<div class="c1">
			
		</div>
		
		<div class="c2">
			
		</div>
		
		<div class="c3">
			
		</div>
		
		<div class="c4">
			
		</div>
	</body>
</html>

 

 

 

水平居中和垂直居中

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>绝对定位2</title>
		<style>
			.c1{
				width: 100px;
				height: 100px;
				background-color: #124444;
				position:absolute;
				/*绝对定位还可用百分比*/
				/*此时,在浏览器窗口中有中心点,
				 块是左对齐的,
				 所以看着不像是对半分*/
				top: 50%;
				left: 50%;
				/*margin在该地方,
				 若移动为正值,
				 则向右移动;反之为向左移动。*/
				/*margin: 100px;*/
				
				/*此时,经过调整之后,
				 块的中心与窗口中心重合*/
				
				
				/*水平居中*/
				margin-left: -50px;
				
				
				/*垂直居中*/
				margin-top: -50px;
			}

		</style>
	</head>
	<body>
		<div class="c1">
			
		</div>

	</body>
</html>

此时,块的中心与浏览器窗口的中心是重合的,即块处在窗口正中心位置 。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_浮舍_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值