position 是用來決定元素在網頁中如何定位的一種屬性,使用 top、bottom、left、right 的值來控制最終的位置。常見的例子如固定在頁首的導覽列(navbar)、頁尾的 footer,或者是一顆按了就可以回到頁面頂端的按鈕,它們的位置都是用 position 處理後的結果。(還有討厭的廣告)
Fig. 1. 固定在頂端的導覽列,MDN
Fig. 2. w3s 偷偷放在右下角的綠色小方塊XD,W3Schools
對元素設 position,再選用適合的值。
通則:有設 position 的會蓋掉沒設的、後設的會蓋掉之前已設的
position: static
position: relative
position: absolute
position: fixed
position: sticky
Fig. 3. 示意用 gif
top: 50px
left: 50px
top: 20px;
喔耶寫完好開心!
第一次用動圖也好開心XD絕對不會是因為被 flex 跟 grid 虐翻才跑來寫被我遺忘的 position 的XD
明天的主題是基本的 display。
基本的 display 結束後,會先談基本 JS。
待基本系列都結束後,就是進階囉~~
邊寫邊彈性滾動,不然會斷尾XD"
以下附 code
<h1>position</h1>
<!-- 預設值 -->
<div class="box">
<h2><code>position: static</code></h2>
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
<div class="d">D</div>
</div>
<div class="box test-relative">
<h2><code>position: relative</code></h2>
<div class="a">A</div>
<div class="b"><code>left: 50px;</code></div>
<div class="c"><code>right: -50px;</code></div>
<div class="d"><code>bottom: 50px;</code></div>
</div>
<div class="box test-absolute">
<h2><code>position: absolute</code></h2>
<p>外層沒定位,會把視窗當爸來定位自己</p>
<div class="a">A</div>
<div class="b">absolute:<code>left: 50px;</code></div>
<div class="c">C</div>
<div class="d">absolute:<code>top: 50px;</code></div>
</div>
<div class="box test-absolute-relative">
<h2>relative+absolute</h2>
<p>外:<code>position: relative</code></p>
<p>內:<code>position: absolute</code></p>
<div class="a">A</div>
<div class="b"><code>left: 50px;</code></div>
<div class="c">C</div>
<div class="d">D</div>
</div>
<div class="box test-fixed">
<h2><code>position: fixed</code></h2>
<div class="a">A</div>
<div class="b">
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Amet in soluta necessitatibus, blanditiis
rem unde provident! Consequuntur deserunt facere beata</p>
<code>position: fixed; top: 20px;</code>
</div>
<div class="c">C</div>
<div class="d">D</div>
</div>
<div class="box test-sticky">
<h2><code>position: sticky</code></h2>
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
<div class="d">D</div>
</div>
<div class="box">
<h2>我是空的</h2>
<div class="a">A</div>
<div class="b">B</div>
<div class="c">C</div>
<div class="d">D</div>
</div>
/* 通用樣式 */
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
margin: 1rem 2rem;
}
h1 {
font-size: 3.5rem;
}
h2 {
font-size: 2.5rem;
}
h3 {
font-size: 1.5rem;
}
p,
span,
button {
font-size: 1.25rem;
margin-bottom: 1rem;
}
.box {
width: 600px;
border: 1px solid black;
margin: 20px auto;
font-size: 2rem;
}
.a {
background-color: #faa;
}
.b {
background-color: #afa;
}
.c {
background-color: #aaf;
}
.d {
background-color: #fa3;
}
.e {
background-color: #3af;
}
.f {
background-color: #a3f;
}
/* 以下開始 */
div {
padding: 10px;
margin: 10px;
}
p {
margin-top: 10px;
}
.box {
width: 500px;
margin-bottom: 3rem;
padding: 1.5rem;
}
/* test-relative */
.test-relative div:nth-of-type(2) {
position: relative;
left: 50px;
}
.test-relative div:nth-of-type(3) {
position: relative;
right: -50px;
}
.test-relative div:nth-of-type(4) {
position: relative;
bottom: 50px;
z-index: -1;
}
/* test-absolute */
.test-absolute div:nth-of-type(2) {
position: absolute;
left: 50px;
}
.test-absolute div:nth-of-type(4) {
position: absolute;
top: 50px;
}
/* test-relative + test-absolute */
.test-absolute-relative {
position: relative;
}
.test-absolute-relative div:nth-of-type(2) {
position: absolute;
left: 50px;
opacity: 0.75;
}
/* test-fixed */
.test-fixed div:nth-of-type(2) {
position: fixed;
width: 300px;
top: 50px;
right: 0px;
opacity: 0.75;
}
/* test-sticky */
.test-sticky {
width: 500px;
position: sticky;
margin-left: 0px;
margin-bottom: 0px;
bottom: 0px;
}
參考資料