/ walking bear:D / CSS Layout - The position Property

CSS Layout - The position Property

2017-06-02 posted in [筆記]

一直搞不清楚 CSS 排版相關細節,參考這篇:CSS Layout - The position Property HTML element 的屬性 position 可以有四種 values : static (default), relative, absolute, fixed。static 是缺省默認值,fixed 參照的是 ViewPort 座標。比較讓人混淆的是 relativeabsolute,注意下面的用例,relative 不會影響其他已經 rendered 的 elements,但 absolute 會讓其他 elements 調整位置填補空位。

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

hover this div element to change position: relative;

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.

hover this div element to change 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.

<style>
div.toAbsolute:hover {
   position: absolute;
   left: 30px;
   top: 30px;
   border: 3px solid #DA3712;
}
div.toRelative:hover {
   position: relative;
   left: 10px;
   top: 10px;
   border: 3px solid #12DA37;
}
div.toRelative, div.toAbsolute{
    border: 3px solid #73AD21;
}
</style>
<div class="toRelative">
hover this div element to change position: relative;
</div>
<div class="toAbsolute">
hover this div element to change position: absolute;
</div>

. reply_count: 0 get_replies : 0 .

walking bear:DRSS feed

关于

wkliang

Clarke's Three Laws:

  • When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
  • The only way of discovering the limits of the possible is to venture a little way past them into the impossible.
  • Any sufficiently advanced technology is indistinguishable from magic.
  • 版权申明

    知识共享许可协议

    Fork me on GitHub

    Powered by

    Disqus, GitHub, Google Custom Search, Gravatar, HighlightJS, jekyll