# CSS-position

## position: static;

HTML elements are positioned static by default.

Static positioned elements are not affected by the top, bottom, left, and right properties.

An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page:

## position: relative;

An element with position: relative; is positioned relative to its normal position.

Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.

relative的意思就是相对的意思，当我们没用position这个属性的时候，我们想修饰的元素本来就自己的位置，我们通过position-relative来修饰，然后通过top,left,right,bottom来修饰相对关于原来的位置以懂得位置。

## position: fixed;

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.

A fixed element does not leave a gap in the page where it would normally have been located.

fixed本来的意思就是固定的意思，用fixed修饰的话，这个元素就会固定在我们用top,right,leftxiu'sh修饰的位置，而且一个特别注意的一点是我们不能固定在正中间呀这样，A fixed element does not leave a gap in the page where it would normally have been located.是指不会跟页面的空白处有距离，给个例子就是一下：

<!DOCTYPE html>
<html>
<style>
div.fixed {
position: fixed;
bottom: 100;
right: 300;
width: 300px;
}
</style>
<body>

<h2>position: fixed;</h2>

<p>An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled:</p>

<div class="fixed">
This div element has position: fixed;
</div>

</body>
</html>

## position: absolute;

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).

However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

Note: A "positioned" element is one whose position is anything except static.

absolute就比较难了，首先它是一个相对感念，会相对于它的父亲元素，absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).如果没有父亲元素，会相对于文件正文(document body).

<!DOCTYPE html>
<html>
<style>
div.relative {
position: relative;
width: 400px;
height: 200px;
}

div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
}
</style>
<body>

<h2>position: absolute;</h2>

<p>An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed):</p>

<div class="relative">This div element has position: relative;
<div class="absolute">This div element has position: absolute;</div>
</div>

</body>
</html>

效果如下:

里面的边框的位置是相对于它的父元素的。

## position: sticky;

An element with position: sticky; is positioned based on the user's scroll position.

A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).

sticky是介于relative和fixed的概念，这里贴了一段代码，请复制过去，钻研一下。

<!DOCTYPE html>
<html>
<style>
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 10px;
background-color: #cae8ca;
border: 2px solid #4CAF50;
}
</style>
<body>

<p>Try to <b>scroll</b> inside this frame to understand how sticky positioning works.</p>
<p>Note: IE/Edge 15 and earlier versions do not support sticky position.</p>

<div class="sticky">I am sticky!</div>

<p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.</p>
<p>Scroll back up to remove the stickyness.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>

</body>
</html>


• 评论

• 上一篇
• 下一篇