[CSS] Position

๊น€์ฑ„์šดยท2021๋…„ 11์›” 6์ผ
0

CSS

๋ชฉ๋ก ๋ณด๊ธฐ
5/9

๐Ÿ’ก position์€ '์œ„์น˜'๋ž€ ๋œป์œผ๋กœ ์š”์†Œ๋“ค์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ์†์„ฑ์ด๋‹ค.
์ด ์†์„ฑ์„ ์ด์šฉํ•ด ํŽ˜์ด์ง€์˜ ๋ ˆ์ด์•„์›ƒ์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

position์˜ ์ข…๋ฅ˜

1. Static & relative & absolute

๐Ÿ’ก static์€ position์˜ ๊ธฐ๋ณธ๊ฐ’์ด๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํŠน์ •ํ•œ position๊ฐ’์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด static๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ html์— ์“ด ํƒœ๊ทธ์ˆœ์œผ๋กœ ์œ„์น˜๊ฐ€ ์ •ํ•ด์ง€๊ฒŒ ๋œ๋‹ค. ๋˜ํ•œ, static์€ ๋ถ€๋ชจ๊ฐ์ฒด์—์„œ ๋‹ค๋ฅธ position ์†์„ฑ๊ฐ’์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋ฅผ ๋ฌด์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋  ๋•Œ๊ฐ€ ์žˆ๋‹ค.

๐Ÿ’ก relative๋Š” '์ƒ๋Œ€์ ์ธ'์ด๋ผ๋Š” ๋œป์„ ๊ฐ€์ง„ ์š”์†Œ์ด๋‹ค. ๋ฌด์—‡์œผ๋กœ ๋ถ€ํ„ฐ ์ƒ๋Œ€์ ์ด๋ƒ, ํ˜„์žฌ ์ž์‹ ์˜ ์œ„์น˜์— ๋Œ€ํ•ด์„œ ์ƒ๋Œ€์ ์ด๋‹ค.
ex) position: relative; ํ•˜๊ณ  left: 10px; ํ•˜๋ฉด ๋ณธ์ธ static ์ž๋ฆฌ์—์„œ ์™ผ์ชฝ์œผ๋กœ 10px๋งŒํผ ๋–จ์–ด์ง„ ์ž๋ฆฌ์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค.

๐Ÿ’ก absolute๋Š” HTML์˜ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์ ์œผ๋กœ ์žก๋Š”๋ฐ, ๋งŒ์•ฝ ๋ถ€๋ชจ ์š”์†Œ์— position๊ฐ’์ด ์ •ํ•ด์ง€๊ฒŒ ๋˜๋ฉด ๋ถ€๋ชจ์˜ position์„ ๊ธฐ์ค€์œผ๋กœ ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค. ๋งˆ์น˜ relative๊ฐ€ static์˜ ์ž๋ฆฌ๋ฅผ ์œ ๋…ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์ด์น˜๋‹ค.

<style>
        .box1 {
            position: static;
            background-color: green;
            color: white;
            width: 100px;
            height: 100px;
        }

        .๊ธฐ์ค€ {
            position: relative;
        }

        .box2 {
            position: absolute;
            left: 40px;
            top: 30px;
            background-color: green;
            color: white;
            width: 100px;
            height: 100px;
            z-index: 1;
        }

        .box3 {
            position: absolute;
            left: 50px;
            top: 50px;
            background-color: aqua;
            color: white;
            width: 100px;
            height: 100px;
        }
    </style>
<body>
    <div class="box1">box1</div>

    <div class="๊ธฐ์ค€">
        <div class="box2">box2</div>
        <div class="box3">box3</div>
    </div>
</body>

2. fixed

๐Ÿ’ก fixed๋Š” ์Šคํฌ๋กค์„ ์›€์ง์ผ ๋•Œ ํŠน์ •ํ•œ ๋ฐ•์Šค๊ฐ€ ๊ณ ์ •๋˜๋„๋ก ํ•ด์ฃผ๋Š”๊ฑด๋ฐ, ์ ˆ๋Œ€์ ์ธ ์†์„ฑ์ด๋‹ค.
absolute์ฒ˜๋Ÿผ fixed๋„ ๋ถ€๋ชจ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์œ„์น˜๋ฅผ ์ •ํ•˜๋Š”๋ฐ, ๋‹ค๋ฅธ์ ์€ absolute๋Š” ๋ถ€๋ชจ์˜ ์š”์†Œ์— ๋”ฐ๋ผ ์œ„์น˜ํ•˜์ง€๋งŒ fixed๋Š” ๋ฌด์กฐ๊ฑด์ ์œผ๋กœ body๋ฅผ ๋ถ€๋ชจ๋กœ ์—ฌ๊ธด๋‹ค. ์ด ๋ง์€ ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๊ณ ์žˆ๋Š” ๋ทฐํฌํŠธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ™”๋ฉด์— ๋ถ™์€ ๊ฒƒ ์ฒ˜๋Ÿผ ๊ฐ™์€ ์ž๋ฆฌ์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค.


์ด๋Ÿฐ์‹์œผ๋กœ ๊ณ ์ •๋˜์–ด ์žˆ์Œ.

3. sticky

๐Ÿ’ก sticky๋Š” ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด scroll๋œ ์š”์†Œ์— ๋‹ฌ๋ผ๋ถ™๋Š”๋‹ค.
๊ทธ๋ž˜์„œ scroll๋˜๊ณ  ์žˆ๋Š” ๋ถ€๋ชจ์š”์†Œ๊ฐ€ scroll์ด ๋๋‚  ๋•Œ ๊ทธ sticky๊ฐ€ ๋๋‚˜๊ณ  ๋‹ค์Œ sticky๊ฐ€ ์‹œ์ž‘๋œ๋‹ค. ์ด ๋œป์€ sticky ์š”์†Œ๊ฐ€ ์ธ๊ณ„์ ์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ offset์ด ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋œ๋‹ค.
๐Ÿ’ fixed์™€์˜ ์ฐจ์ด์ ์€,
fixed๋Š” ๋ทฐํฌํŠธ์— ๊ณ ์ •์ด๋˜๊ณ , scroll์ด ๋ผ๋„ ์›ํ•˜๋Š” ์œ„์น˜์— ๊ณ ์ •์ด ๋˜๋Š” ๊ฒŒ sticky๋‹ค. ์Šคํฌ๋กค์— ๋”ฐ๋ผ ์›€์ง์ด๋‹ค offset์ด ์ธ๊ณ„์ ์— ๋‹ฟ์œผ๋ฉด ๊ทธ ์ˆœ๊ฐ„๋ถ€ํ„ฐ ๊ณ ์ •์ด ๋œ๋‹ค.

์ฒ˜์Œ์—” ์ด๋ ‡๊ฒŒ ๊ณ ์ •๋˜์–ด ์žˆ๋‹ค๊ฐ€,

์ธ๊ณ„์ ์„ ๋งŒ๋‚˜๋ฉด ์ด๋ ‡๊ฒŒ ๋œ๋‹ค.

๐Ÿคฃ position์œผ๋กœ ๋งŒ๋“ค์–ด ๋ณธ ๋‚˜๋งŒ์˜ ์บ๋ฆญํ„ฐ!!

 <style>
        body {
            margin: 150px;
        }

        .ear-left {
            position: absolute;
            top: 10px;
            left: 20px;
            display: inline-block;
            background: #FFC8C4;
            width: 150px;
            height: 200px;
            border-radius: 30px;
            transform: rotate(14deg);
        }

        .ear-left2 {
            position: absolute;
            top: 10px;
            left: 20px;
            display: inline-block;
            background: #FFC8C4;
            width: 150px;
            height: 200px;
            border-radius: 30px;
            transform: rotate(14deg);
            box-shadow: 8px 5px 5px gray;
        }

        .ear-right {
            top: 10px;
            right: 20px;
            top: 2px;
            position: absolute;
            display: inline-block;
            background: #FFC8C4;
            width: 150px;
            height: 200px;
            border-radius: 30px;
            transform: rotate(-14deg);
        }

        .ear-right2 {
            top: 10px;
            right: 20px;
            top: 2px;
            position: absolute;
            display: inline-block;
            background: #FFC8C4;
            width: 150px;
            height: 200px;
            border-radius: 30px;
            box-shadow: -8px 5px 5px gray;
            transform: rotate(-14deg);
        }

        .face {
            position: relative;
            background: #FFC8C4;
            width: 600px;
            height: 550px;
            border-radius: 300px;
        }

        .nose {
            position: absolute;
            top: 260px;
            left: 210px;
            right: 100px;
            background: #FC7CA5;
            width: 180px;
            height: 140px;
            border-radius: 180px;
        }

        .nose-left {
            background: #FFC8C4;
            width: 50px;
            height: 50px;
            border-radius: 25px;
            position: absolute;
            left: 35px;
            top: 50px;
        }

        .nose-right {
            background: #FFC8C4;
            width: 50px;
            height: 50px;
            border-radius: 25px;
            position: absolute;
            left: 100px;
            top: 50px;
        }

        .eye-left {
            position: absolute;
            background: black;
            width: 70px;
            height: 70px;
            border-radius: 35px;
            left: 180px;
            top: 150px;
        }

        .eye-right {
            position: absolute;
            background: black;
            width: 70px;
            height: 70px;
            border-radius: 35px;
            top: 150px;
            left: 360px;
        }

        .small-eye__left {
            position: absolute;
            background: white;
            width: 20px;
            height: 20px;
            border-radius: 10px;
            left: 32px;
            top: 5px;
        }

        .small-eye__right {
            position: absolute;
            background: white;
            width: 20px;
            height: 20px;
            border-radius: 10px;
            left: 32px;
            top: 5px;
        }


        .flower {
            position: absolute;
            background: yellow;
            top: 7px;
            left: 412px;
            width: 70px;
            height: 70px;
            border-radius: 35px;
            z-index: 5;
        }

        .small-flower1 {
            position: absolute;
            background: #F16585;
            width: 60px;
            height: 60px;
            border-radius: 30px;
            top: -2px;
            right: 75px;
        }

        .small-flower2 {
            position: absolute;
            background: #F16585;
            width: 60px;
            height: 60px;
            border-radius: 30px;
            top: -2px;
            right: 170px;
        }

        .small-flower3 {
            position: absolute;
            background: #F16585;
            width: 60px;
            height: 60px;
            border-radius: 30px;
            right: 125px;
            top: -30px;
        }

        .small-flower4 {
            position: absolute;
            background: #F16585;
            width: 60px;
            height: 60px;
            border-radius: 30px;
            top: 50px;
            right: 95px;
        }

        .small-flower5 {
            position: absolute;
            background: #F16585;
            width: 60px;
            height: 60px;
            border-radius: 30px;
            right: 155px;
            top: 53px;
        }
    </style>
</head>

<body>
    <div class="face">
        <div class="ear-left"></div>
        <div class="ear-left2"></div>
        <div class="ear-right"></div>
        <div class="ear-right2"></div>
        <div class="nose">
            <div class="nose-left"></div>
            <div class="nose-right"></div>
        </div>
        <div class="eye-left">
            <div class="small-eye__left"></div>
        </div>
        <div class="eye-right">
            <div class="small-eye__right"></div>
        </div>
        <div class="flower"></div>
        <div class="small-flower1"></div>
        <div class="small-flower2"></div>
        <div class="small-flower3"></div>
        <div class="small-flower4"></div>
        <div class="small-flower5"></div>

    </div>
</body>

๐Ÿ’ก์ˆ˜์ง,์ˆ˜ํ‰ ์ •๋ ฌ์‹œ ๊ฟ€ํŒ!!

  • ์ˆ˜์ง์ •๋ ฌ์‹œ, top:50%, transform:trnslateY(-50%) -> flex์—†์ด ์ˆ˜์ง์ •๋ ฌ์‹œ
  • ์ˆ˜ํ‰์ •๋ ฌ์‹œ, margin: 0 auto; -> auto๊ฐ€ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ๋‚จ์•„์žˆ๋Š” margin๊ฐ’์„ ์ชผ๊ฐœ์„œ ์ž๋™์œผ๋กœ ๊ฐ€์šด๋ฐ ์ •๋ ฌ์„ ํ•ด์คŒ.
    !! but, width๊ฐ’์ด ์—†์„๋• ์ž‘๋™ํ•˜์ง€ ์•Š์Œ
  • position์„ ์ ์šฉํ•ด์„œ 50๋ฐ€์–ด์ฃผ๊ณ  -50๋งŒํผ ๋Œ์–ด๋‹น๊ธฐ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Œ.

๐Ÿ’_ position์„ ๋ฐฐ์šฐ๊ณ  relative์™€ absolute๋ฅผ ์ด์šฉํ•ด์„œ ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ๋ฅผ ํ•ด๋ดค๋‹ค!!!
position์„ ์ด์šฉํ•˜๋‹ˆ๊นŒ ์œ„์น˜๋ฅผ ๋งˆ์Œ๋Œ€๋กœ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ƒ๊ฐ๋ณด๋‹ค ๋ง‰์—ฐํ•˜์ง€ ์•Š์•˜๋‹ค.
๊ทธ์ „์—๋Š” ์œ„์น˜ ์กฐ์ •์„ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๋ฌด์กฐ๊ฑด์ ์œผ๋กœ relative๋งŒ ์‚ฌ์šฉํ•ด์„œ ์œ„์น˜๊ฐ€ ์ œ๋ฉ‹๋Œ€๋กœ ์›€์ง์ด๊ณ 
offset์ด ๋จน์ง€ ์•Š์•„์„œ ๋‹ต๋‹ตํ•˜๊ณ  ๋ณต์žกํ–ˆ๋Š”๋ฐ position์„ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์˜ ํŽธ๋ฆฌํ•จ์„ ์ด๋ฒˆ์— ๋Š๋ผ๊ฒŒ ๋˜์–ด์„œ ํ›„๋ จํ–ˆ๋‹ค!!!

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