[CSS] position/z-index/float

XIXIยท2022๋…„ 5์›” 16์ผ
0

๋Œ€๊ตฌAI์Šค์ฟจ

๋ชฉ๋ก ๋ณด๊ธฐ
18/57
post-thumbnail

๐ŸŒฑ position

โœ๏ธstatic

<style>
        .parent-static{
            width: 300px;
            /* height: 300px; -3*/
            background-color: antiquewhite;
        }

        .child-static{
            position: static;
            width: 100px;
            height: 100px;
            background-color: blueviolet;
            /* margin-top: 100px; -1)*/
            /* top: 100px; -2)*/
        }
</style>
  • position: static
    -๊ธฐ๋ณธ๊ฐ’(์„ค์ •ํ•˜์ง€ ์•Š์•„๋„ ๊ธฐ๋ณธ ์ ์šฉ)
    -2์ฐจ์› ์†์„ฑ -> float ์‚ฌ์šฉํ•˜๋ฉด ์œ„์น˜ ์ง€์ • ๊ฐ€๋Šฅ

    3๊ฐ€์ง€ ์ฒดํฌ ์‚ฌํ•ญ
    1) ๋ถ€๋ชจ ์ž์‹ ๊ฐ„ ๋งˆ์ง„ ๋ณ‘ํ•ฉ? ๋งˆ์ง„ ๋ณ‘ํ•ฉ ๋ฐœ์ƒ
    2) top, right, bottom, left ์†์„ฑ ์‚ฌ์šฉ ๊ฐ€๋Šฅ? ์‚ฌ์šฉ๋ถˆ๊ฐ€
    3) ์ž์‹ ๋†’์ด๊ฐ€ ๋ถ€๋ชจ ๋†’์ด์— ์˜ํ–ฅ? ์˜ํ–ฅ์„ ์ค€๋‹ค

<body>
<div class="parent-static">
    <div class="child-static"></div>
</div>
</body>

โœ๏ธfixed

<style>
        .box1{
            width: 300px;
            height: 300px;
            background-color: palegreen;
        }

        .parent-fixed{
            width: 300px;
            /* height: 300px;)*/
            background-color: antiquewhite;
        }

        .child-fixed{
            position: fixed;
            width: 100px;
            height: 100px;
            background-color: blueviolet;
            /* margin-top: 100px;*/
            /* top:100px;*/
        }

        .box2{
            width: 300px;
            height: 300px;
            background-color: pink;
        }
</style>
  • position: fixed
    -top ๋ฒ„ํŠผ, ์Šคํฌ๋กคํ•ด๋„ ๋‚ด๋ ค์˜ค๋Š” ๋ฉ”๋‰ด
    -3์ฐจ์› ์†์„ฑ

    3๊ฐ€์ง€ ์ฒดํฌ ์‚ฌํ•ญ
    1) ๋ถ€๋ชจ ์ž์‹ ๊ฐ„ ๋งˆ์ง„ ๋ณ‘ํ•ฉ? ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
    2) top, right, bottom, left ์†์„ฑ ์‚ฌ์šฉ ๊ฐ€๋Šฅ? ์‚ฌ์šฉ๊ฐ€๋Šฅ-๊ธฐ์ค€์€ ๋ธŒ๋ผ์šฐ์ €
    3) ์ž์‹ ๋†’์ด๊ฐ€ ๋ถ€๋ชจ ๋†’์ด์— ์˜ํ–ฅ? ์˜ํ–ฅ ์—†์Œ

<body>
<div class="box1"></div>
<div class="parent-fixed">
    <div class="child-fixed"></div>
</div>
<div class="box2"></div>
</body>

โœ๏ธrelative

<style>
        .box1{
            width: 300px;
            height: 300px;
            background-color: palegreen;
        }

        .parent-relative{
            width: 300px;
            /* height: 300px; */ /*3*/
            background-color: antiquewhite;
        }

        .child-relative{
            position: relative;
            width: 100px;
            height: 100px;
            background-color: blueviolet;
            /* margin-top: 100px;*/ /*1*/
            /*top:100px;*/ /*2*/
        }

        .box2{
            width: 300px;
            height: 300px;
            background-color: pink;
        }
</style>
  • position: relative
    -ํฐ ๋ ˆ์ด์•„์›ƒ ์ž‘์„ฑ์‹œ์— ์ž์ฃผ ์‚ฌ์šฉ

    3๊ฐ€์ง€ ์ฒดํฌ ์‚ฌํ•ญ
    1) ๋ถ€๋ชจ ์ž์‹ ๊ฐ„ ๋งˆ์ง„ ๋ณ‘ํ•ฉ? ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
    2) top, right, bottom, left ์†์„ฑ ์‚ฌ์šฉ ๊ฐ€๋Šฅ? ์‚ฌ์šฉ๊ฐ€๋Šฅ-๊ธฐ์ค€์€ ๋ธŒ๋ผ์šฐ์ €
    3) ์ž์‹ ๋†’์ด๊ฐ€ ๋ถ€๋ชจ ๋†’์ด์— ์˜ํ–ฅ? ์˜ํ–ฅ ์—†์Œ

<body>
<div class="box1"></div>
<div class="parent-relative">
    <div class="child-relative"></div>
</div>
</body>

โœ๏ธabsoulute

<style>
        .box1{
            width: 300px;
            height: 300px;
            background-color: palegreen;
        }

        .parent-absolute{
            width: 300px;
            height: 300px;
            background-color: antiquewhite;

            /* ์ขŒํ‘œ(top) ์‚ฌ์šฉ์‹œ ์ด๋™ ๊ธฐ์ค€์„ ๋ธŒ๋ผ์šฐ์ €-> ์› ์œ„์น˜ ๋ฐ”๊พธ๊ณ  ์‹ถ์„ ๋•Œ ๋ถ€๋ชจ์š”์†Œ์— position:relative๋กœ ๋งŒ๋“ ๋‹ค */
            position: relative;
        }

        .child-absolute{
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: blueviolet;
            margin-top: 100px; 
            /* top:100px; 2 */
        }
</style>
  • position: absoulute
    -ํฐ ๋ ˆ์ด์•„์›ƒ ์ž‘์„ฑ์‹œ์— ์ž์ฃผ ์‚ฌ์šฉ

    3๊ฐ€์ง€ ์ฒดํฌ ์‚ฌํ•ญ
    1) ๋ถ€๋ชจ ์ž์‹ ๊ฐ„ ๋งˆ์ง„ ๋ณ‘ํ•ฉ? ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
    2) top, right, bottom, left ์†์„ฑ ์‚ฌ์šฉ ๊ฐ€๋Šฅ? ์‚ฌ์šฉ๊ฐ€๋Šฅ-๊ธฐ์ค€์€ ๋ธŒ๋ผ์šฐ์ €
    3) ์ž์‹ ๋†’์ด๊ฐ€ ๋ถ€๋ชจ ๋†’์ด์— ์˜ํ–ฅ? ์˜ํ–ฅ ์—†์Œ

<body>
<div class="box1"></div>
<div class="parent-absolute">
    <div class="child-absolute"></div>
</div>
</body>

โœ๏ธz-index

<style>
        .box1{
            width: 100px;
            height: 300px;
            background-color: aquamarine;
            position: absolute;
            z-index: 1;
        }

        .box2{
            width: 200px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            /* z-index: 2; */
        }
</style>
  • z-index: z์ถ•์˜ ๋†’์ด๋ฅผ ์กฐ์ •ํ•œ๋‹ค.
    (๊ธฐ๋ณธ) z-index: 0
    (ํฐ์ˆ˜) ์œ„๋กœ -> (์ž‘์€์ˆ˜) ์•„๋ž˜๋กœ
    3์ฐจ์› position: absolut, fixed, relative
<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>

โœ๏ธfloat

<style>
        body{
            width: 100%;
        }

        .left{
            width: 300px;
            height: 300px;
            background-color: antiquewhite;
            float: left;
            /* overflow: hidden; */
            overflow-x: scroll;
            overflow-y: scroll;
        }

        .left-2{
            width: 100px;
            height: 300px;
            background-color: blue;
            float: left;
        }

        .right{
            width: 300px;
            height: 300px;
            background-color: red;
            float: right;
        }

        .right-2{
            width: 100px;
            height: 300px;
            background-color: green;
            float: right;
        }

        .clearfix{
            clear: both;
            /* float ์‚ฌ์šฉ ํ•ด์ œ๋ฅผ ๋ช…ํ™•ํžˆ ํ‘œ์‹œ -> ๋น ๋ฅธ ์ฝ”๋“œ ํ™•์ธ ๊ฐ€๋Šฅ */
        }

        footer{
            width: 100%;
            height: 100px;
            background-color: black;
            /* clear: left | right | both */
        }
</style>
  • float: left; โžช ์™ผ์ชฝ์— ๋„์›Œ์ง„ ๋ฐ•์Šค ์ƒ์„ฑ
  • float: right; โžช ์˜ค๋ฅธ์ชฝ์— ๋„์›Œ์ง„ ๋ฐ•์Šค ์ƒ์„ฑ
  • overflow-x: scroll; โžช x์ถ•์œผ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๋‚ด์šฉ ์Šคํฌ๋กค ํ™•์ธ
  • overflow-y: scroll; โžช y์ถ•์œผ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๋‚ด์šฉ ์Šคํฌ๋กค ํ™•์ธ
  • clear: both; โžช float์ ์šฉ์„ ์ทจ์†Œํ•œ๋‹ค
<body>
    <div class="left">
        <p>
            Lorem ipsum dolor sit amet consectetur, adipisicing elit.
            Consequatur dolorum quasi quo eius explicabo nam ad praesentium provident,
            non magnam possimus maiores dicta. Iure tenetur magni deleniti quasi. Dolor, dicta!
            Lorem ipsum dolor sit amet consectetur, adipisicing elit
            Lorem ipsum dolor sit amet consectetur, adipisicing elit
            Lorem ipsum dolor sit amet consectetur, adipisicing elit
            Lorem ipsum dolor sit amet consectetur, adipisicing elit
        </p>
    </div>
    <div class="left-2"></div>
    <div class="right"></div>
    <div class="right-2"></div>
    <div class="clearfix"></div>
    <footer></footer>
</body>

float

profile
Life is experience:)

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