第五次web前端学习笔记

学习目标:

掌握css的定位和层级显示

学习内容:

1、 固定定位 2、 绝对定位 3、 相对定位 4、 元素的显示方式 5、 元素的层级

学习时间:

2021年1月6日

学习产出:

1、 技术笔记 1遍 2、CSDN 技术博客 1 篇 3、 制作58同城登录界面

固定定位

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#d1{
				width: 700px;
				height: 700px;
				border: 1px solid red;
			}
			/* 固定定位 */
			#d2{				
				width: 80px;
				height: 40px;
				line-height: 30px;
				text-align: center;
				background-color: #ccc;
				/* 开始固定定位  
				   fixed 生成绝对定位的元素,相对于浏览器窗口进行定位 */
				position: fixed;
				right: 5px;
				bottom: 30px;
				
			}
			a{
				text-decoration: none;
			}
		</style>
	</head>
	<body>
		<h1>我是h1</h1>
		<p>我是p1</p>
		<p>我是p2</p>
		<div id="d1">
			<div id="d2">
				<a href="#">回到顶部</a>
			</div>
		</div>
		
	</body>
</html>

绝对定位

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			/* 绝对定位:绝对定位元素位置相对于已经定位的最近的父元素
			(如果参照最近的父元素,那么父元素必须开启定位)如果祖先元素没有开启定位,
			 那么参照的位置可能是画布可能是html 
			 注意:开启绝对定位的元素和文档流没有关系,所以它们可能会覆盖其他元素
			
			*/
			div{
				width: 400px;
				height: 300px;
				border: 1px solid red;	
				/* 生成相对定位的元素,相对于其正常位置进行定位。 */
				position: relative;
			}
			p{
				/* 开启绝对定位 */
				/* 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。 */
				position: absolute;
				bottom: 30px;
				width: 400px;
				background-color: #CCCCCC;
				text-align: center;
				line-height: 30px;
			}
		</style>
		
	</head>
	<body>
		<div id="">
			<img src="img/book1.jpg" >
			<p>书山有路勤为径</p>
		</div>
	</body>
</html>

相对定位

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			/* 
			相对定位:相对上一次的位置进行(x y)的偏移
			注意:无论如何偏移相对定位不会释放自己所占的位置,也就是说相对定位不会脱离文档流。 
			除了浮动定位相对定位,固定定位,绝对定位开始使用postion
			 
			 */
			#d1,#d2{
				width: 200px;
				height: 200px;
				border: 1px solid red;
			}
			#d1{
				/* 给d1开启相对定位 */
				position: relative;
				/* 上下左右都可以设置偏移量 */
				top: 150px;
				right: 5px;
				
			}
		</style>
	</head>
	<body>
		<div id="d1">
			我是d1
		</div>
		<p>定位时观察我的位置</p>
		<div id="d2">
			
		</div>
	</body>
</html>

元素的显示方式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			/* 
				块元素:从上到下排列
				行内元素:从左到右,不能设置宽高(span,a)
				行内块:从左到右,可以设置宽高(input,img)
				
				display来设置元素的显示方式,可取值有:
				none:不显示该元素,释放其占用位置
				block:将元素设置为块元素
				inline:将元素设置为行内元素
				inline-block:将元素设置为行内块元素
			 */
			#d1,#d2{
				width: 100px;
				height: 100px;
				border: 1px solid red;
				/* 将块元素装换位行内块元素 */
				/* display: inline-block; */
				display: inline;
			}
			#d3{
				width: 500px;
				height: 500px;
				border: 1px solid yellow;
			}
			img{
				width: 200px;
				height: 300px;
				/* 行内块元素转换为块元素 */
				display: block;
			}
			/* 元素的隐藏 */
			#d4{
				width: 200px;
				height: 200px;
				background-color: #006666;
				display: none; 
			}
			
		</style>
	</head>
	<body>
		<div id="d1">d1</div>
		<div id="d2">
			d2
		</div>
		<div id="d3">
			<img src="img/book3.jpg" >
			<img src="img/book2.jpg" >
		</div>
		<div id="d4">
			
		</div>
		<p>观察我的位置</p>
	</body>
</html>

元素的层级

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			/* 
			 
				z-index:可以设置元素的层级,值为整数,z-index的值越大那么元素层级越高,如果元素的层级一样,则优先显示靠下的元素。
				祖先元素的层级再高,也不能覆盖子元素。
			 
			 */
			/* .box1{
				width: 400px;
				height: 200px;
				background: #bfa;
				position: absolute;
				
			}
			.box2{
				width: 300px;
				height: 200px;
				background-color: green;
				position: absolute;
				
				
			} */
			.box3{
				width: 200px;
				height: 200px;
				background-color: yellow;
				position: absolute;
				z-index: 5;
				
			}
			.box4{
				width: 100px;
				height: 100px;
				background-color: orange;
				position: absolute;
				z-index: 1;
			}
		</style>
	</head>
	<body>
		<div class="box1">1</div>
		<div class="box2">2</div>
		<div class="box3">3
			<div class="box4">
				4
			</div>
		</div>
	</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值