๐Ÿ”นposition

โ•ฑ๐Ÿšง under construction

CSS โŸฉ layout โŸฉ position

่จญๅฎšไบ† position ็š„ๅ…ƒ็ด ็จฑ็‚บๅฎšไฝๅ…ƒ็ด  (positioned element) ใ€‚

  • relative๏ผšๆไพ›ๅญๅ…ƒ็ด ๆ–ฐ็š„ๅƒ่€ƒๅบงๆจ™๏ผŒไฝ†่‡ชๅทฑไฝ็ฝฎไธ่ฎŠใ€‚

  • absolute๏ผšๅƒ่€ƒๆœ€่ฟ‘ๅฎšไฝ็ˆถๅ…ƒ็ด ๏ผŒๅฐ‡่‡ชๅทฑๅฎšไฝๅœจ็ต•ๅฐไฝ็ฝฎไธŠใ€‚

  • fixed๏ผš

  • sticky๏ผš

  • static๏ผš

"position"   "top/left" relative to               removed from flow
-----------------------------------------------------------------------
fixed        `viewport`                                  โœ… 
relative     where it should be                          โŒ 
absolute     `document` / nearest positioned parent      โœ… 
  • static ๆ˜ฏ้ ่จญๅฑฌๆ€ง๏ผŒไธๆœƒ่ขซๅญๅ…ƒ็ด ่ฆ–็‚บๆ˜ฏใ€Œๅฎšไฝ็ˆถๅ…ƒ็ด ใ€ ใ€‚

  • By default, any positioned element appears in front of any non-positioned elements.

If none of the absolutely positioned elementโ€™s ancestors are positioned, then the element will be positioned based on the initial containing block - an area with dimensions equal to the viewport size, anchored at the top of the page.

By setting top/bottom, left/right , you also implicitly define the width / height of the (fixed and absolutely, but not relatively positioned) element.

Unlike fixed and absolute positioning, you cannot use top/bottom, left/right to change the size of a relatively positioned element.

  • can use top or bottom, but not both (bottom will be ignored).

  • can use left or right, but not both (right will be ignored).

z-index

  • z-index only works on positioned elements.

  • applying a z-index to a positioned element establishes a stacking context.

  • Elements with a negative z-index appear behind static elements.

Last updated