讲给后台程序员看的前端系列教程(25)——定位


C语言自学完备手册(33篇)

Android多分辨率适配框架

HTML5前端开发实战系列教程

MySQL数据库实操教程(35篇图文版)

推翻自己和过往——自定义View系列教程(10篇)

走出思维困境,踏上精进之路——Android开发进阶精华录

讲给Android程序员看的前端系列教程(40集免费视频教程+源码)


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

定位(position)的简介

在HTML中,利用定位(position)可较为精确地定义元素显示的位置,这个位置可以是相对于其本身出现的位置,也可以是相对于其上级元素的位置,还可以是相对于其他元素的位置,亦可为相对于浏览器视窗左上角的位置。


定位(position)的语法

position:relative | absolute | static | fixed ;
top | right | bottom | left:Mpx ;

小结如下:

  • 定位(position)常用类型:static、absolute、relative、fixed

  • 定位(position)通常与top、right、bottom、left属性联合使用

  • 定位(position)的使用可能导致元素脱离标准流

现在分别介绍四种常用的定位(position)类型。

static

position:static是CSS中默认的定位类型,在该类型下元素以标准流的显示方式,其位置不受top、bottom、left、right的影响。

请看如下示例:

<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="utf-8">
		<title>static定位</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			div {
				width: 510px;
				height: 200px;
				border: 1px solid red;
				background-color: greenyellow;
				position: static;
				left: 50px;
				top: 80px;
			}
		</style>
	</head>

	<body>
		<div></div>
	</body>

</html>

运行后,效果如下图所示:
在这里插入图片描述

在该示例中,在标签选择器div里设置了position:static并且为left和top属性设置了具体的值,但是我们发现:<div></div>的位置没有发生改变。

relative

相对定位position:relative用于设置元素位置,即相对于元素本身原来的位置进行调整。

相对定位特性:

  • 相对于自身原来在标准流中位置而移动
  • 移动后继续占有原来在标准流中的位置
  • 元素设置position:relative后该元素不会脱离标准流

我们先来看一个不设置定位的例子:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>无定位</title>
		<style>
			
			.fatherDiv {
				width: 400px;
				height: 500px;
				background-color: pink;
			}
			
			.blueSonDiv{
				width: 200px;
				height: 200px;
				background-color: blue;
			}
			
			.greenyellowSonDiv{
				width: 200px;
				height: 200px;
				background-color: greenyellow;
			}

		</style>

	</head>
	<body>
		<div class="fatherDiv">
			<div class="blueSonDiv"></div>
			<div class="greenyellowSonDiv"></div>
		</div>
	</body>
</html>

结果如下:
在这里插入图片描述

接下来,我们为blueSonDiv设置相对定位:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>相对定位</title>
		<style>
			
			.fatherDiv {
				width: 400px;
				height: 500px;
				background-color: pink;
			}
			
			.blueSonDiv{
				width: 200px;
				height: 200px;
				background-color: blue;
				/* 相对定位 */
				position: relative;
				top: 30px;
				left: 30px;
			}
			
			.greenyellowSonDiv{
				width: 200px;
				height: 200px;
				background-color: greenyellow;
			}

		</style>

	</head>
	<body>
		<div class="fatherDiv">
			<div class="blueSonDiv"></div>
			<div class="greenyellowSonDiv"></div>
		</div>
	</body>
</html>

运行后,效果如下图所示:
在这里插入图片描述

absolute

绝对定位position:absolute是元素参照带有定位的父级元素实现位置移动。

绝对定位特性:

  • 如果父元素和爷爷元素等上级元素都没有定位,则子元素以浏览器左上角为参照进行定位。
  • 如果父元素或爷爷元素等上级元素有定位,则子元素依据最近的已经定位(绝对、固定或相对定位)的父元素或爷爷元素进行定位。
  • 子元素设置绝对定位后将脱离原来的标准流,在原标准流中亦不再保留其原来的位置

绝对定位示例1

子元素采用绝对定位而父元素无定位。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>绝对定位</title>
		<style>
			
			/* 父元素无定位 */
			.fatherDiv {
				width: 400px;
				height: 500px;
				background-color: pink;
			}
			
			.blueSonDiv{
				width: 200px;
				height: 200px;
				background-color: blue;
				/* 子元素绝对定位 */
				position: absolute;
				top: 30px;
				left: 30px;
			}
			
			.greenyellowSonDiv{
				width: 200px;
				height: 200px;
				background-color: greenyellow;
			}

		</style>

	</head>
	<body>
		<div class="fatherDiv">
			<div class="blueSonDiv"></div>
			<div class="greenyellowSonDiv"></div>
		</div>
	</body>
</html>

运行后,效果如下图所示:
在这里插入图片描述
从结果中我们可以看到:

  • 1、子元素blueSonDiv以屏幕的左上角为参照进行定位
  • 2、子元素blueSonDiv脱离了标准流

绝对定位示例2

子元素采用绝对定位而父元素采用相对定位(子绝父相)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>绝对定位</title>
		<style>
			
			/* 父元素使用相对定位 */
			.fatherDiv {
				width: 400px;
				height: 500px;
				background-color: pink;
				position: relative;
			}
			
			.blueSonDiv{
				width: 200px;
				height: 200px;
				background-color: blue;
				/* 子元素绝对定位 */
				position: absolute;
				top: 30px;
				left: 30px;
			}
			
			.greenyellowSonDiv{
				width: 200px;
				height: 200px;
				background-color: greenyellow;
			}

		</style>

	</head>
	<body>
		<div class="fatherDiv">
			<div class="blueSonDiv"></div>
			<div class="greenyellowSonDiv"></div>
		</div>
	</body>
</html>

运行后,效果如下图所示:
在这里插入图片描述

从结果中我们可以看到:

  • 1、子元素参照父元素位置移动
  • 2、子元素脱离了标准流

fixed

position:fixed表示固定定位,即相对浏览器窗口定位。采用该定位后,不论页面如何滚动,该盒子显示的位置不变并完全脱离标准流。

请看如下示例:

<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="utf-8">
		<title>fixed定位</title>
		<style type="text/css">
			img {
				width: 20%;
				/* 固定定位 */
				position: fixed;
				left: 300px;
				top: 200px;
			}
			
			.blueDiv{
				width: 200px;
				height: 200px;
				background-color: blue;
			}
			
			.greenyellowDiv{
				width: 200px;
				height: 200px;
				background-color: greenyellow;
			}

		</style>
	</head>

	<body>
		<img src="img/girlfriend.jpg"/>
		<div class="blueDiv"></div>
		<div class="greenyellowDiv"></div>
		<div class="blueDiv"></div>
		<div class="greenyellowDiv"></div>
		<div class="blueDiv"></div>
		<div class="greenyellowDiv"></div>
	</body>

</html>

运行后,效果如下图所示:
在这里插入图片描述

在该示例中,在标签选择器img中设置了position:fixed;属性。从效果图中可以看到:图片一直固定在某个位置,当我们拖动滚动条时它的位置仍然保持不变


规避脱标流

有时使用float和position来实现某种效果,但是这很可能导致某些元素脱离标准流(脱标);所以,应尽可能地避免脱标,可遵守以下原则:

  • 网页布局过程中尽量使用标准流布局
  • 标准流不能解决的问题优先考虑使用浮动
  • 浮动不能解决的问题再考虑使用定位
  • 在子元素中使用margin-left:auto 将其置于父元素的最右边
  • 在子元素中使用margin-right:auto将其置于父元素的最左边
  • 在子元素中使用margin 0 auto;使其在父元素内水平居中显示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谷哥的小弟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值