CSS Making Layouts (Flexbox)

Oh Joonยท2020๋…„ 12์›” 16์ผ
0

Roadmap CSS

๋ชฉ๋ก ๋ณด๊ธฐ
9/10
post-thumbnail

๐ŸŽˆ Flexbox?

flexbox๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๋ ˆ์ด์•„์›ƒ๊ณผ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ์†์„ฑ(float, display, position ๋“ฑ) ๋ณด๋‹ค ํ–‰ ๋˜๋Š” ์—ด๋กœ ์ž์œ ์ž์žฌ๋กœ ์š”์†Œ๋“ค์„ ๋ฐฐ์น˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ์—˜๋ฆฌ๋จผํŠธ๋“ค์˜ ํฌ๊ธฐ๋‚˜ ์œ„์น˜๋ฅผ ์‰ฝ๊ฒŒ ์žก์•„์ฃผ๋Š” ๋„๊ตฌ์ด๋ฉฐ CSS์˜ ๊ฝƒ์ด๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค๐ŸŒท


๐Ÿ”ด Flex ๊ธฐํƒ€ ์†์„ฑ๋“ค

flexbox๋Š” container ๋ฐ•์Šค์— ์ ์šฉํ•˜๋Š” ์†์„ฑ๋“ค์ด ์žˆ๊ณ , container์•ˆ์— item ๊ฐ๊ฐ์— ์ ์šฉํ•˜๋Š” ์†์„ฑ๋“ค์ด ์กด์žฌํ•œ๋‹ค.

container

  • display
  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

item

  • order
  • flex-grow
  • flex-shrink
  • flex
  • align-self

๊ตฌ์ฒด์ ์ธ ์†์„ฑ๋“ค์„ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ

flex properties์—๋Š” ์ง€์ •ํ•œ ์†์„ฑ๋Œ€๋กœ ์•„์ดํ…œ๋“ค์ด ์–ด๋–ป๊ฒŒ ์ ์šฉ๋˜๋Š”์ง€ ๊ฐ€์‹œ์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
CSS-TRICKS flexbox
Flexbox Froggy ๊ฒŒ์ž„


๐ŸŸก Flex Basic

flex๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ปจํ…Œ์ด๋„ˆ ํƒœ๊ทธ์— display:flex ์†์„ฑ์„ ๋ถ€์—ฌํ•ด์•ผํ•œ๋‹ค. flex์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

  • display:flex
  • flex-direction
<!Doctype>
<html>
<head>
    <style>
        .container{
            background-color: powderblue;
            height:200px;
            display:flex;
            flex-direction:row;
        }
        .item{
            background-color: tomato;
            color:white;
            border:1px solid white;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
    </div>
</body>
</html>

flex๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฐ”๊นฅ์ชฝ์— ์žˆ๋Š” ๋ถ€๋ชจํƒœ๊ทธ์— display:flex๋ฅผ ๋ถ€์—ฌํ•ด์•ผ ํ•œ๋‹ค.
flex-direction:row;๋Š” ํ–‰์˜ ๋ฐฉํ–ฅ์œผ๋กœ divํƒœ๊ทธ๋“ค์„ ์ •๋ ฌ์‹œํ‚จ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์—ด ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” flex-direction:column ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

  • row, column + (-reverse)๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ์ •๋ ฌ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๊ฒŒ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๊ธฐ๋ณธ์ ์œผ๋กœ flex-direction์„ ํ•˜์ง€ ์•Š์œผ๋ฉด row๋ฅผ ์ ์šฉํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.


๐ŸŸ  grow & shrink (+basis)

์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ์žˆ๋Š” ์•„์ดํ…œ์€ ์ปจํ…Œ์ด๋„ˆ์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ์„œ ์ž‘์•„์ง€๊ธฐ๋„ ํ•˜๊ณ  ์ปค์ง€๊ธฐ๋„ ํ•œ๋‹ค. ์ด ๋•Œ ์ž‘์•„์ง€๊ณ  ์ปค์ง€๋Š” ๋น„์œจ์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด grow & shrink ์ด๋‹ค. ์ด ์†์„ฑ๊ณผ ํ•จ๊ป˜ basis๋„ ํ•จ๊ป˜ ์•Œ์•„๋ณด์ž.

<!doctype>
<html>
<head>
    <style>
        .container{
            background-color: powderblue;
            height:200px;
            display:flex;
            flex-direction:row;
        }
        .item{
            background-color: tomato;
            color:white;
            border:1px solid white;         
        }
        .item:nth-child(1){
            flex-basis: 150px;
        }
        .item:nth-child(2){
            flex-basis: 150px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
    </div>
</body>
</html>

์•„์ดํ…œ ํด๋ž˜์Šค๋’ค์— nth-child(n)์„ ์ง€์ •ํ•จ์œผ๋กœ ์•„์ดํ…œ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” n๋ฒˆ์งธ ์•„์ดํ…œ์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

flex-basis๋Š” flex์˜ ๋ฐฉํ–ฅ(row, column)์— ํ•ด๋‹น๋˜๋Š” ์—˜๋ฆฌ๋จผํŠธ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์œ„ ๊ทธ๋ฆผ์„ ๋ณผ ๋•Œ 1,2 ์•„์ดํ…œ์˜ ํฌ๊ธฐ๊ฐ€ row ๋ฐฉํ–ฅ์œผ๋กœ 150px์ธ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


๐ŸŸข grow

์œ„ basis๋ฅผ ์„ค๋ช…ํ•œ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ์˜ค๋ฅธ์ชฝ์— ๋ฐฐ๊ฒฝ์ƒ‰์ด powderblue๋กœ ๋œ ์—ฌ๋ฐฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. grow๋ฅผ ์‚ฌ์šฉํ•ด ์—ฌ๋ฐฑ์„ ์ฑ„์šฐ๋ฉด์„œ grow์˜ ์˜๋ฏธ๋ฅผ ์‚ดํŽด๋ณด์ž.

<!doctype>
<html>
<head>
    <style>
        .container{
            background-color: powderblue;
            height:200px;
            display:flex;
            flex-direction:row;
        }
        .item{
            background-color: tomato;
            color:white;
            border:1px solid white;  
            flex-grow: 1;       
        }
        .item:nth-child(1){
            
        }
        .item:nth-child(2){
            
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
    </div>
</body>
</html>

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์•„์ดํ…œ ํด๋ž˜์Šค ์ „์ฒด์— flew-grow:1์„ ์ง€์ •ํ•˜์˜€๋‹ค.๊ทธ๋ฆผ์„ ๋ณด๋ฉด powderblue ์—ฌ๋ฐฑ์ด ์ „ํ˜€ ๋ณด์ด์ง€ ์•Š๊ณ  5๊ฐœ์˜ ์•„์ดํ…œ ๋ฐ•์Šค๋“ค์ด ๋™์ผํ•œ ํฌ๊ธฐ์˜ ๋น„์œจ์„ ๊ฐ€์ง€๊ณ  ์—ฌ๋ฐฑ ์ „์ฒด๋ฅผ ๋ฉ”๊พผ๋‹ค. ์ „์ฒด ์—ฌ๋ฐฑ์„ n๋ถ„์˜ 1๋กœ ๊ฐ๊ฐ์˜ ์•„์ดํ…œ์ด ์ฑ„์šฐ๊ณ  ์žˆ๋‹ค.

        .item:nth-child(1){
            flex-grow: 2; 
        }

์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž. ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ ๋ฐ•์Šค์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅธ ๋ฐ•์Šค๋“ค์˜ ํฌ๊ธฐ๋ณด๋‹ค 2๋ฐฐ๊ฐ€ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ „์ฒด ํฌ๊ธฐ๋ฅผ 6์ด๋ผ๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ 2,3,4,5 ์•„์ดํ…œ์ด ์ „์ฒด์˜ 6๋ถ„์˜1์”ฉ ๊ฐ€์ ธ๊ฐ€ 6๋ถ„์˜4๋ฅผ ์ฐจ์ง€ํ•˜๊ณ  ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ ์ƒ์ž๊ฐ€ 6๋ถ„์˜ 2๋ฅผ ๊ฐ€์ง„๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

๐Ÿ”ต shrink

grow์†์„ฑ๊ณผ ๋ฐ˜๋Œ€๋กœ shrink๋Š” ์•„์ดํ…œ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ผ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. grow์™€ ๋™์ผํ•˜๊ฒŒ ๊ฐ’์€ ๋น„์œจ์ด๋‹ค.

<!doctype>
<html>
<head>
    <style>
        .container{
            background-color: powderblue;
            height:200px;
            display:flex;
            flex-direction:row;
        }
        .item{
            background-color: tomato;
            color:white;
            border:1px solid white;  
                
        }
        .item:nth-child(1){
            flex-basis: 400px; 
        }
        .item:nth-child(2){
            flex-basis: 600px; 
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
    </div>
</body>
</html>

1,2 ์•„์ดํ…œ์— ๊ฐ๊ฐ 400px, 600pxํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜์˜€๋‹ค. ๋ฐ‘ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ์›น์‚ฌ์ดํŠธ ์ฐฝ์„ ์ค„์ด๋ฉด ์—ฌ๋ฐฑ์ด ์—†์–ด์ง€๋Š” ์ˆœ๊ฐ„๋ถ€ํ„ฐ!!!! flex-basis๊ฐ€ ์ง€์ •๋˜์–ด ์žˆ๋Š” 1,2 ์•„์ดํ…œ๋“ค๋งŒ ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“œ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

shrink ๊ฐ’์„ ์ง€์ •ํ•˜๋ฉด ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ค„์–ด๋“ค์ง€ ์•Š๊ณ  ์ž์‹ ์˜ ํฌ๊ธฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

        .item:nth-child(2){
            flex-basis: 600px; 
            flex-shrink: 0;
        }

์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž. ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด flex-shrink:0์ด ์ง€์ •๋˜์–ด ์žˆ๋Š” ์•„์ดํ…œ 2์˜ ํฌ๊ธฐ๋Š” ์ค„์–ด๋“ค์ง€ ์•Š๊ณ  ์ „๊ณผ ๋‹ค๋ฅด๊ฒŒ ์•„์ดํ…œ 1์˜ ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“œ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

  • shrink๋ฅผ 1๋กœ ์ง€์ •ํ–ˆ์„ ๋•Œ๋Š” ์ „๊ณผ ๊ฐ™์ด ๋™์ผํ•˜๊ฒŒ ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ ๋‹ค.
        .item:nth-child(1){
            flex-basis: 600px; 
            flex-shrink: 1;
        }
        .item:nth-child(2){
            flex-basis: 600px; 
            flex-shrink: 2;
        }

์•„์ดํ…œ 1,2 ๋™์ผํ•˜๊ฒŒ 600px๋กœ ์ง€์ •ํ•˜๊ณ  flex-shrink์˜ ๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ๋ถ€์—ฌํ•ด๋ณด์•˜๋‹ค.
์ด ๋น„์œจ 1์—์„œ 1์€ 3๋ถ„์˜ 1๋งŒํผ๋งŒ, 2๋Š” 3๋ถ„์˜ 2๋งŒํผ๋งŒ ์ค„์–ด๋“ค์—ˆ๋‹ค. 1๋ณด๋‹ค 2๊ฐ€ ํ›จ์”ฌ ๋” ๋น ๋ฅด๊ฒŒ ํฌ๊ธฐ๊ฐ€ ์ž‘์•„์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ํด์ˆ˜๋ก ํฌ๊ธฐ๊ฐ€ ๋” ๋งŽ์ด ์ค„์–ด๋“ ๋‹ค.

๐Ÿ”Žshrink๊ฐ’์ด ์‚ฌ์šฉ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์–ด๋–ค ํŠน์ •ํ•œ ์•„์ดํ…œ์— basis ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.


์ฐธ์กฐ

MDN flex์˜ ๊ธฐ๋ณธ ๊ฐœ๋…
๋“œ๋ฆผ์ฝ”๋”ฉ CSS Flexbox ์™„์ „ ์ •๋ฆฌ. ํฌํŠธํด๋ฆฌ์˜ค ๋งŒ๋“œ๋Š” ๋‚ ๊นŒ์ง€! | ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ž…๋ฌธํŽธ: HTML, CSS, Javascript
์ƒํ™œ์ฝ”๋”ฉ flex

profile
Front-end developer

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