CSS Position(定位)

    position 属性指定了元素的定位类型。

    position 属性的五个值:

   ·static

   ·relative

   ·fixed

   ·absolute

   ·sticky

 

static 定位

   HTML元素的默认值,既没有定位,元素出现在正常的流中。

   静态定位的元素不会受到top、bottom、left、right影响。

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		div.static {
			position:static;
			border:3px solid #73AD21;
		}
	</style>
</head>  
<body > 
	<h2>position: static;</h2>
	<p>使用 position:static; 定位的元素,无特殊定位,遵循正常的文档流对象。</p>
	<div class="static">该元素使用了 position: static;</div>
</body>  
</html>

fix 定位

   元素的位置相对于浏览器窗口是固定位置;

   即使窗口是滚动的它也不会移动:

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		p.pos_fixed
		{
			position:fixed;
			top:30px;
			right:5px;
		}
	</style>
</head>  
<body > 
	<p class="pos_fixed">Some more text</p>
<p><b>注意:</b> IE7 和 IE8 支持只有一个 !DOCTYPE 指定固定值.</p>
<p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p>
</body>  
</html>

    Fixed 定位在 IE7 和 IE8 下需要描述 !DOCTYPE 才能支持。

    Fixed定位使元素的位置与文档流无关,因此不占据空间。

    Fixed定位的元素和其他元素重叠。

 

relative 定位

  相对定位元素的定位是相对其正常位置

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		h2.pos_left
		{
			position:relative;
			left:-20px;
		}
		h2.pos_right
		{
			position:relative:
			left:20px;
		}
	</style>
</head>  
<body > 
	<h2>这是位于正常位置的标题</h2>
	<h2 class="pos_left">这个标题相对于其正常位置向左移动。</h2>
	<h2 class="pos_right">这个标题相对于其正常位置向右移动。</h2>
	<p>相对定位会按照元素的原始位置对该元素进行移动。</p>
	<p>样式 "left:-20px" 从元素的原始左侧位置减去 20 像素。</p>
	<p>样式 "left:20px" 向元素的原始左侧位置增加 20 像素。</p>
</body>  
</html>

   可以移动的相对定位元素的内容和相互重叠的元素,它原本所占的空间不会改变。

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		h2.pos_top
		{
			position:relative;
			top:-50px;
		}
	</style>
</head>  
<body > 
	<h2>这是一个没有定位的标题</h2>
	<h2 class="pos_top">这个标题是根据其正常位置向上移动</h2>
	<p><b>注意:</b> 即使相对定位元素的内容是移动,预留空间的元素仍保存在正常流动。</p>
</body>  
</html>

   相对定位元素经常被用来作为绝对定位元素的容器模块。

 

absolute 模块

   绝对定位的元素的位置相对于最近的已定位元素,如果元素没有已定位的父元素,那么它的位置相对于<html>

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		h2
		{
			position:absolute;
			left:100px;
			top:150px;
		}
	</style>
</head>  
<body > 
	<h2>这是一个绝对定位了的标题</h2>
	<p>用绝对定位,一个元素可以放在页面上的任何位置。标题下面放置距离左边的页面100 px和距离页面的顶部150 px的元素。.</p>
</body>  
</html>

   absolute 定位使元素的位置与文档流无关,因此不占据空间。

   absolute 定位的元素和其他元素重叠。

 

sticky 定位

    position: sticky; 基于用户的滚动位置来定位。

   粘性定位的元素是依赖于用户的滚动,在 position:relative 与 position:fixed 定位之间切换。

   它的行为就像 position:relative; 而当页面滚动超出目标区域时,它的表现就像 position:fixed;,它会固定在目标位置。

   元素定位表现为在跨越特定阈值前为相对定位,之后为固定定位。

    这个特定阈值指的是 top, right, bottom 或 left 之一,换言之,指定 top, right, bottom 或 left 四个阈值其中之一,才可使粘性定位生效。否则其行为与相对定位相同。

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		div.sticky {
		  position: -webkit-sticky;
		  position: sticky;
		  top: 0;
		  padding: 5px;
		  background-color: #cae8ca;
		  border: 2px solid #4CAF50;
		}
	</style>
</head>  
<body > 
	<p>尝试滚动页面。</p>
	<p>注意: IE/Edge 15 及更早 IE 版本不支持 sticky 属性。</p>

	<div class="sticky">我是粘性定位!</div>

	<div style="padding-bottom:2000px">
	  <p>滚动我</p>
	  <p>来回滚动我</p>
	  <p>滚动我</p>
	  <p>来回滚动我</p>
	  <p>滚动我</p>
	  <p>来回滚动我</p>
	</div>
</body>  
</html>

重叠元素

   元素的定位与文档流无关,所以它们可以覆盖页面上的其它元素

   z-index属性指定了一个元素的堆叠顺序(哪个元素应该放在前面,或后面)

   一个元素可以有正数或负数的堆叠顺序:

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		img
		{
			position:absolute;
			left:0px;
			top:0px;
			z-index:-1;
		}
	</style>
</head>  
<body > 
	<h1>This is a heading</h1>
	<img src="w3css.gif" width="100" height="140" />
	<p>因为图像元素设置了 z-index 属性值为 -1, 所以它会显示在文字之后。</p>
</body>
</body>  
</html>

   具有更高堆叠顺序的元素总是在较低的堆叠顺序元素的前面

实例

   裁剪元素的外形

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		img
		{
			position:absolute;
			clip:rect(0px,60px,200px,0px);
		}
	</style>
</head>  
<body > 
	<h1>This is a heading</h1>
	<img src="w3css.gif" width="100" height="140" />
</body>
</body>  
</html>

   如何使用滚动条来显示元素内溢出的内容

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		div.scroll
		{
			background-color:#00FFFF;
			width:100px;
			height:100px;
			overflow:scroll;
		}

		div.hidden 
		{
			background-color:#00FF00;
			width:100px;
			height:100px;
			overflow:hidden;
		}
	</style>
</head>  
<body > 
	<p>overflow 属性规定当内容溢出元素框时发生的事情。.</p>

	<p>overflow:scroll</p>
	<div class="scroll">You can use the overflow property when you want to have better control of the layout. The default value is visible.</div>

	<p>overflow:hidden</p>
	<div class="hidden">You can use the overflow property when you want to have better control of the layout. The default value is visible.</div>
</body>
</body>  
</html>

   如何设置浏览器自动溢出处理

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		div.scroll
		{
			background-color:#00FFFF;
			width:100px;
			height:100px;
			overflow:auto;
		}
	</style>
</head>  
<body > 
	<p>overflow 属性规定当内容溢出元素框时发生的事情。</p>

	<p>overflow:scroll</p>
	<div class="scroll">You can use the overflow property when you want to have better control of the layout. The default value is visible.</div>

	
</body>
</body>  
</html>

   更改图标

<!DOCTYPE html>
<html>  
<head>  
    <meta charset="utf-8">  
    <title>自学教程(如约智惠.com)</title>  
	<style>
		div.scroll
		{
			background-color:#00FFFF;
			width:100px;
			height:100px;
			overflow:auto;
		}
	</style>
</head>  
<body > 
	<p>请把鼠标移动到单词上,可以看到鼠标指针发生变化:</p>
	<span style="cursor:auto">auto</span><br>
	<span style="cursor:crosshair">crosshair</span><br>
	<span style="cursor:default">default</span><br>
	<span style="cursor:e-resize">e-resize</span><br>
	<span style="cursor:help">help</span><br>
	<span style="cursor:move">move</span><br>
	<span style="cursor:n-resize">n-resize</span><br>
	<span style="cursor:ne-resize">ne-resize</span><br>
	<span style="cursor:nw-resize">nw-resize</span><br>
	<span style="cursor:pointer">pointer</span><br>
	<span style="cursor:progress">progress</span><br>
	<span style="cursor:s-resize">s-resize</span><br>
	<span style="cursor:se-resize">se-resize</span><br>
	<span style="cursor:sw-resize">sw-resize</span><br>
	<span style="cursor:text">text</span><br>
	<span style="cursor:w-resize">w-resize</span><br>
	<span style="cursor:wait">wait</span><br>

	
</body>
</body>  
</html>

所有的CSS定位属性

"CSS" 列中的数字表示哪个CSS(CSS1 或者CSS2)版本定义了该属性。

属性说明CSS
bottom定义了定位元素下外边距边界与其包含块下边界之间的偏移。auto
length
%

inherit
2
clip剪辑一个绝对定位的元素shape
auto
inherit
2
cursor显示光标移动到指定的类型url
auto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help
2
left定义了定位元素左外边距边界与其包含块左边界之间的偏移。auto
length
%

inherit
2
overflow设置当元素的内容溢出其区域时发生的事情。auto
hidden
scroll
visible
inherit
2
overflow-y指定如何处理顶部/底部边缘的内容溢出元素的内容区域auto
hidden
scroll
visible
no-display
no-content
2
overflow-x指定如何处理右边/左边边缘的内容溢出元素的内容区域auto
hidden
scroll
visible
no-display
no-content
2
position指定元素的定位类型absolute
fixed
relative
static
inherit
2
right定义了定位元素右外边距边界与其包含块右边界之间的偏移。auto
length
%

inherit
2
top定义了一个定位元素的上外边距边界与其包含块上边界之间的偏移。auto
length
%

inherit
2
z-index设置元素的堆叠顺序number
auto
inherit

 

参考:

https://www.yuque.com/docs/share/85cee995-c646-4871-976e-ecc56b1279d0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值