牛骨文教育服务平台(让学习变的简单)

CSS Positioning(定位)



定位有时很棘手!

决定显示在前面的元素!

元素可以重叠!


Positioning(定位)

CSS定位属性允许你为一个元素定位。它也可以将一个元素放在另一个元素后面,并指定一个元素的内容太大时,应该发生什么。

元素可以使用的顶部,底部,左侧和右侧属性定位。然而,这些属性无法工作,除非是先设定position属性。他们也有不同的工作方式,这取决于定位方法.

有四种不同的定位方法。


Static 定位

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

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


Fixed 定位

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

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

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>牛骨文(niuguwen.cn)</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>牛骨文(niuguwen.cn)</title> 
<style>
h2.pos_left
{
	position:relative;
	left:-20px;
}

h2.pos_right
{
	position:relative;
	left:20px;
}
</style>
</head>

<body>
<h2>This is a heading with no position</h2>
<h2 class="pos_left">This heading is moved left according to its normal position</h2>
<h2 class="pos_right">This heading is moved right according to its normal position</h2>
<p>Relative positioning moves an element RELATIVE to its original position.</p>
<p>The style "left:-20px" subtracts 20 pixels from the element's original left position.</p>
<p>The style "left:20px" adds 20 pixels to the element's original left position.</p>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

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

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>牛骨文(niuguwen.cn)</title> 
<style>
h2.pos_top
{
	position:relative;
	top:-50px;
}
</style>
</head>

<body>
<h2>This is a heading with no position</h2>
<h2 class="pos_top">This heading is moved upwards according to its normal position</h2>
<p><b>注意:</b> 即使相对定位元素的内容是移动,预留空间的元素仍保存在正常流动。</p>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

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

Absolute 定位

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

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>牛骨文(niuguwen.cn)</title> 
<style>
h2
{
	position:absolute;
	left:100px;
	top:150px;
}
</style>
</head>

<body>
<h2>This is a heading with an absolute position</h2>
<p>用绝对定位,一个元素可以放在页面上的任何位置。标题下面放置距离左边的页面100 px和距离页面的顶部150 px的元素。.</p>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

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

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


重叠的元素

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

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

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

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>牛骨文(niuguwen.cn)</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>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

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

注意: 如果两个定位元素重叠,没有指定z - index,最后定位在HTML代码中的元素将被显示在最前面。

更多实例

裁剪元素的外形

此示例演示如何设置元素的外形。该元素被剪裁成这种形状,并显示出来。

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>牛骨文(niuguwen.cn)</title> 
<style>
img 
{
	position:absolute;
	clip:rect(0px,60px,200px,0px);
}
</style>
</head>

<body>
<img src="w3css.gif" width="100" height="140" />
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

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

这个例子演示了overflow属性创建一个滚动条,当一个元素的内容在指定的区域过大时如何设置以适应。

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>牛骨文(niuguwen.cn)</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>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

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

这个例子演示了如何设置浏览器来自动处理溢出。

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>牛骨文(niuguwen.cn)</title> 
<style>
div 
{
	background-color:#00FFFF;
	width:150px;
	height:150px;
	overflow:auto;
}
</style>
</head>

<body>
<p>overflow 属性规定当内容溢出元素框时发生的事情。</p>

<div>
当你想更好的控制布局时你可以使用 overflow 属性。尝试修改 overflow 属性为: visible, hidden, scroll, 或 inherit 并查看效果。 默认值为 visible。
</div>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

更改光标

这个例子演示了如何改变光标。

实例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>牛骨文(niuguwen.cn)</title> 
</head>
<body>
<p>将鼠标移动到这些字上改变鼠标样式cursor.</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>
</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
2