position (static, relative, absolute, fixed, sticky)

uoayopยท2021๋…„ 5์›” 4์ผ
0

CSS

๋ชฉ๋ก ๋ณด๊ธฐ
4/6
post-thumbnail

position? ๐Ÿ“Œ

position ์†์„ฑ์€ ์š”์†Œ๋“ค์„ ํŠน์ • ์œ„์น˜์— ๊ณ ์ •๋˜์–ด ์žˆ๋„๋ก ๋งŒ๋“ค ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ธ๋‹ค.


position:static ๐Ÿ“ฆ

  • ๋ชจ๋“  ํƒœ๊ทธ์˜ default ๊ฐ’์ด๋‹ค.
  • ์œ„์น˜๋ฅผ ์ž„์˜๋กœ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ์•Œ์•„์„œ ๋ฐฐ์น˜๋œ๋‹ค.

<div style="width: 100px; height: 100px; background-color: yellow;">div1</div>
<div style="width: 100px; height: 100px; background-color: greenyellow;"> div2</div>

position: relative ๐Ÿ“

  • ์ƒ๋Œ€์ ์œผ๋กœ ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • static์ผ ๋•Œ์˜ ์›๋ž˜ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€ ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • top, right, left, bottom ์œผ๋กœ ์›๋ž˜ ์œ„์น˜์—์„œ ์–ผ๋งˆ๋‚˜ ๋ฉ€์–ด์งˆ ์ง€ ์ •ํ•œ๋‹ค. (margin ๊ฐ™์€ ๋Š๋‚Œ)

<div style="
        width: 100px; 
        height: 100px; 
        background-color: yellow;">
        div1
</div>
    
<div style="
        width: 100px; 
        height: 100px; 
        background-color: greenyellow; 
        position: relative;
        top: 100px;
        left: 100px;"> 
        div2
</div>

position: absolute ๐Ÿ“ + ๐Ÿ“

  • position์ด relative ์ธ ์ƒ์œ„ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€ ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒ๋Œ€ ์œ„์น˜์˜ ์ƒ๋Œ€ ์œ„์น˜!

<div style="
        width: 300px; 
        height: 300px; 
        background-color: yellow;
        position: relative;">
        
        <div style="background-color: greenyellow">๊ธฐ๋ณธ ์œ„์น˜</div>
        <div
            style="
            position: absolute;
            background-color: antiquewhite;
            top: 100px;
            left: 100px;
            width: 100px;
            height: 100px;">
            absolute ์š”์†Œ
        </div>
    </div>

position: fixed ๐Ÿ“

  • ํ™”๋ฉด์„ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€์ ์ธ ์œ„์น˜๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฌด์กฐ๊ฑด ํ™”๋ฉด ์ƒ๋‹จ์— ๋ถ™์–ด์žˆ๋Š” ๋„ค๋น„๊ฒŒ์ด์…˜, ํ•˜๋‹จ์— ๋ถ™์–ด์žˆ๋Š” ๋ฒ„ํŠผ ๋“ฑ์„ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

<body style="height: 500px;">
    <div style="
        width: 80%;
        height: 100px; 
        background-color: antiquewhite;
        position: fixed;">
        ๋„ค๋น„๊ฒŒ์ด์…˜๋ฐ”
    </div>
</body>

position: sticky ๐Ÿƒ

  • fixed๊ฐ€ ํ™”๋ฉด์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ˜๋“œ์‹œ ๊ทธ ์ž๋ฆฌ์— ์œ„์น˜ํ•œ๋‹ค๋ฉด,
    sticky๋Š” ์–ด๋Š์ •๋„ ํ™”๋ฉด์„ ๋”ฐ๋ผ ์ด๋™ํ•˜์ง€๋งŒ, ์Šคํฌ๋กค ์œ„์น˜๊ฐ€ ์ž„๊ณ„์ ์— ์ด๋ฅด๋ฉด fixed์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.
  • ํŠน์ • ์›นํŽ˜์ด์ง€์˜ ์šฐ์ธก์— ์œ„์น˜ํ•˜๋Š” ๊ด‘๊ณ ๋ฐฐ๋„ˆ๋ฅผ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๊ฒ ๋‹ค. (๋‚˜๋ฅผ ๋”ฐ๋ผ์˜จ๋‹ค)

<body style="height: 2000px;">
    <div style="
        width: 80%;
        height: 100px; 
        background-color: antiquewhite;
        position: fixed;">
        fixed
    </div>

    <div style="
        width: 300px;
        height: 100px; 
        background-color: lightskyblue;
        position: sticky;
        top: 0px;
        margin-top:200px;
        float:right;">
        sticky
    </div>
</body>

[์ถœ์ฒ˜]

[์ฝ”๋ฎค๋‹ˆํ‹ฐ] ํ•œ ์ž…, ์›น๊ฐœ๋ฐœ #9. position ์†์„ฑ

profile
slow and steady wins the race ๐Ÿข

0๊ฐœ์˜ ๋Œ“๊ธ€