在 CSS 中,總共有四種 position
position總共有四種:(口訣:rafs)
| position: | 是否會 remain in the natural flow of the page | 是否會被子元素當作定位點(anchor point) |
|---|---|---|
| static | 會 | 不會 |
| relative | 會 | 會 |
| absolute | 不會,而是會以離它最近且有被下position(除了static之外)的父元素為定位點 | 會 |
| fixed | 不會,而是以瀏覽器viewport為定位點 | 會 |
教學網址
position: static;
用法說明:
- position的「預設值」
- 按照元素的自然方式排列
css語法:
1 | div#myDIV { |

範例html語法:
1 | <div id="parent1">Parent1: position: static. |
範例css語法:
1 | #parent1 { |
說明:
#parent1有下position: static;
- It will remain in the natural flow of the page.
#parent1不會被子元素#child1當作是position: absolute;的定位點
Results:

position: relative;
用法說明:
- 元素會相較於「它自己原本的位置」去做移動
css語法:
1 | div#myDIV { |

範例html語法:
1 | <div id="parent2">Parent2: position: relative. |
範例css語法:
1 | #parent2 { |
說明:
#parent2 有下 position: relative;
- It will remain in the natural flow of the page.
#parent2會被子元素#child2當作是position: absolute;的定位點
Results:

position: absolute;
用法說明:
- 元素會以離它最近且有被下position(除了static之外)的父元素為定位點去移動
css語法:
1 | div#myDIV { |

範例html語法:
1 | <div id="parent3">Parent3: position: absolute, top: 750px, right: 15px. |
範例css語法:
1 | #parent3 { |
說明:
#parent3 有下 position: absolute;
- It will NOT remain in the natural flow of the page. 而是會以離它最近且有被下position(除了static之外)的父元素為定位點
#parent3會被子元素#child3當作是position: absolute;的定位點
Results:

position: fixed;
用法說明:
- 元素會以瀏覽器視窗為定位點去移動
css語法:
1 | div#myDIV { |

範例html語法:
1 | <div id="parent4">Parent4: position: fixed, bottom: 0, left: 0, right: 0. |
範例css語法:
1 | #parent4 { |
說明:
#parent4 有下 position: fixed;
- It will NOT remain in the natural flow of the page. 而是以瀏覽器viewport為定位點
#parent4會被子元素#child4當作是position: absolute;的定位點
Results:
