CSS页面布局基础2——定位

1、四种定位类型:通过position属性指定分别是static(默认值)、relative、absolute、fixed
static( 静态定位 ):表示按照正常定位方案,元素盒按照在文档流中出现的顺序依次格式化
relative( 相对定 ):将移动元素盒,但是它在文档流中的原始空间会保留下来。
absolute( 绝对定位 ):元素盒彻底从文档流中脱离出来,并相对于其容器块进行定位。因为这些元素从文档流中脱离出来,所以它们不再影响周边元素的布局,并且它们占据的空间不会被保留。
fixed( 固定定位 ):与绝对定位类似,元素从文档流中脱离,但是它们不是相对于容器块定位,而是相对于视口定位。
2、 相对定位 :(1)声明:position:relative
(2)使用top,right,bottom,left中的一个或者多个偏移属性相对于它们在正常文档流中的初始位置进行定位。没有设置偏移属性的,默认被设置为auto。
(4)在文档流中所占据的原始空间被保留。
(3)可能会覆盖其他的元素。
<!DOCTYPE html>
<html>
<head>
	<title>相对定位</title>
	<meta charset="utf-8" />
	<style type="text/css">
	.div1{
		width: 450px;
		height: 450px;
		border:solid;
		border-color: black;
		margin:50px;
	}
	.div2{
		width: 400px;
		height: 400px;
		border:solid;
		border-color: red;
		margin: 10px;
	}
	.div3{
		width: 300px;
		height: 300px;
		border:solid;
		border-color: green;
		margin: 10px;
	}
	.div4{
		position: relative;
		left:50px;
		top:50px;
		width: 100px;
		height: 100px;
		margin:10px;
		background-color: #fa1;
	}
	</style>
</head>
<body>
	<div class="div1">
		<div class="div2">
			<div class="div3">
				<div class="div4"></div>
			</div>
		</div>	
	</div>
</body>
</html>
将上述代码中的
position: relative;
		left:50px;
		top:50px;

去掉的时候,运行结果为:


加上后的效果为:



3、
绝对定位 :(1)声明:position:absolute
(2)使用top,right,bottom,left中的一个或者多个偏移属性相对于其容器块的边缘进行定位。没有设置偏移属性的,默认被设置为auto。
(3)偏移值应用于元素盒的外边缘(包含margin值)

(4)绝对定位的元素从文档流中完全脱离出来。该元素在正常文档流中所占据的空间不保留,并且不影响其他元素。

<!DOCTYPE html>
<html>
<head>
	<title>相对定位</title>
	<meta charset="utf-8" />
	<style type="text/css">
	.div1{
		width: 450px;
		height: 450px;
		border:solid;
		border-color: black;
		margin:50px;
	}
	.div2{
		width: 400px;
		height: 400px;
		border:solid;
		border-color: red;
		margin: 10px;
	}
	.div3{
		width: 300px;
		height: 300px;
		border:solid;
		border-color: green;
		margin: 10px;
		position: absolute;<!--绝对定位div3-->
	}
	.box1{
		width: 100px;
		height: 100px;
		background-color: #bbb;
/*		position: relative;
		top: -79px;
		left: -89px;*/  <!--相对定位代码-->
	}
	.box2{
		width: 100px;
		height: 100px;
		background-color:yellow;<pre name="code" class="html"> <span style="white-space:pre">		</span><!--绝对定位代码-->
/*position: absolute;top: 10px;left: 10px;*/}.box3{width: 100px;height: 100px;background-color:#faa;
 <span style="white-space:pre">		</span><!--固定定位代码-->
position: fixed;top: 10px;left: 10px;}</style></head><body><div class="div1"><div class="div2"><div class="div3"><div class="box1">box1</div><div class="box2">box2</div><div class="box3">box3</div></div></div></div></body></html>

 
绝对定位box2前: 



绝对定位box2后:


绝对定位div3和box2后:


固定定位box3和绝对定位div3后(观察固定定位和绝对定位的区别)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS(层叠样式表)是一种用于描述网页样式的标记语言,通过控制网页元素的外观和布局来美化页面。背景样式在 CSS 中扮演了重要角色,可以通过设置背景颜色、背景图片、背景大小、背景重复等属性来改变元素的背景效果。以下是一些常用的背景样式属性: 1. 背景颜色(background-color):用于设置元素的背景色,可以使用命名颜色或者十六进制颜色值。 示例: ```css div { background-color: red; } ``` 2. 背景图片(background-image):用于设置元素的背景图片,可以使用图片的 URL 来指定。 示例: ```css div { background-image: url("image.jpg"); } ``` 3. 背景大小(background-size):用于设置背景图片的尺寸大小,可以使用关键字(如 `cover`、`contain`)或具体数值(如像素、百分比)来指定。 示例: ```css div { background-size: cover; } ``` 4. 背景重复(background-repeat):用于设置背景图片的重复方式,可以是水平重复、垂直重复或者不重复。 示例: ```css div { background-repeat: repeat-x; } ``` 5. 背景定位(background-position):用于设置背景图片的起始位置,可以使用关键字(如 `top`、`center`、`bottom`)或具体数值(如像素、百分比)来指定。 示例: ```css div { background-position: center; } ``` 以上是一些常用的背景样式属性,通过灵活运用它们,可以实现丰富多样的背景效果。希望对你有所帮助!如需了解更多,请参考相关的 CSS 教程或文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值