---
title: "position 的 static、relative、absolute、fixed、inherit"
author: "yonghengmoming"
date: "2017-08-25T22:48:09Z"
tags: []
categories: ["Web前端"]
slug: "position-static-relative-absolute-fixed-inherit"
draft: false
lastmod: "2017-08-25T22:48:09Z"
id: 1609326
url: "https://weklyd.iteye.com/blog/1609326"
---

转载自：（http://www.cftea.com/c/2009/01/84CEP6T7Q4BYZ1OZ.asp）

position 有五个值：static、relative、absolute、fixed、inherit。

static

是默认值。就是按正常的布局流从上到下从左到右布局，平常我们做网页时，没有指定 position，也就表示使用 static。

relative

没有脱离布局流 ，此时可以使用 top、right、bottom、left 属性。

- top 和 bottom 共存时，使用 top 值，忽略 bottom 值；
- left 和 right 共存时，使用 left 值，忽略 right 值；

relative 是相对位置，指相对于元素的 position 为 static 时的位置 ：

- top 相对于 static 下移多少像素（若 top 是负值，则上移）。
- right 相对于 static 左移多少像素（若 right 是负值，则右移）。
- bottom 相对于 static 上移多少像素（若 bottom 是负值，则下移）。
- left 相对于 static 右移多少像素（若 left 是负值，则左移）。

使用 relative 之后：

- 原来的空间不会 被其他元素挤占。
- 元素在最终位置时也不会挤占其他元素的空间，它浮动到其它元素的上方。

absolute

脱离布局流 ，此时可以使用 top、right、bottom、left，但这些属性不再是相对于 static 时的位置，而是相对于 containing block 的，相对于其边框内边缘的，而不是其 padding 内边缘 。

使用 absolute 之后：

- 原来的空间会 被其他元素挤占。
- 元素在最终位置时也不会挤占其他元素的空间，它浮动到其它元素的上方。

fixed

它的模式与 absolute 相同，不过无论怎么拖动滚动条，它始终固定在屏幕的指定位置。在 IE6 中不支持这个属性；在 IE7 中支持这个属性但需要指明 DOCTYPE；Firefox 等浏览器支持这个属性。

top、right、bottom、left 属性指相对于视口 的 。

inherit

继续父元素的 position 值。