css 讓元素固定不動

Darren
Dec 7, 2020

--

利用position讓元素固定在某個地方,當網頁拉動時,元素還是固定不動

  • static (預設值): 元素會被放在預設的地方。
  • absolute: 元素會被放在瀏覽器內的某個位置 (依 top、bottom、left、和 right 的值而定)。當使用者將網頁往下拉時,元素也會跟著改變位置。
  • relative: 元素被放的地方將會與預設的地方有所不同。不同的程度是依照 top、bottom、left、和 right 的值而定。
  • fixed: 元素會被放在瀏覽器內的某個位置 (依 top、bottom、left、和 right 的值而定)。當使用者將網頁往下拉時,元素的位置不會改變。

不囉嗦,看範例!

html:

<div class =’fixed’> <button>test</button> <div>

css:

.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 200px;
background-color: white;
}

效果:test按鈕固定在右下角

--

--

Darren
Darren

Written by Darren

喜歡研究技術的廢材工程師

No responses yet