CSS-position

版权声明:若想转载,请在显眼的地方附上文章来源。 https://blog.csdn.net/Abudula__/article/details/81591614

我觉得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:

首先static是差不多是我们用不到的,默认的就是static。

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>
<head>
<style>
div.fixed {
    position: fixed;
    bottom: 100;
    right: 300;
    width: 300px;
    border: 3px solid #73AD21;
}
</style>
</head>
<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>

效果如下:

 

 

当我们把样式中的bottom.right怎么调,这个框都会在页面的4个角落,而不可能在别的地方,可以把代码复制过去,试一下。

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>
<head>
<style>
div.relative {
    position: relative;
    width: 400px;
    height: 200px;
    border: 3px solid #73AD21;
} 

div.absolute {
    position: absolute;
    top: 80px;
    right: 0;
    width: 200px;
    height: 100px;
    border: 3px solid #73AD21;
}
</style>
</head>
<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>

 效果如下:

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

如果没有父亲positioned元素,它会相对于页面主体。

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>
<head>
<style>
div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 10px;
  padding: 5px;
  background-color: #cae8ca;
  border: 2px solid #4CAF50;
}
</style>
</head>
<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>

<div style="padding-bottom:2000px">
  <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>

 

阅读更多

没有更多推荐了,返回首页