Flex

Jihyun-Jeonยท2022๋…„ 4์›” 5์ผ
1

HTML,CSS

๋ชฉ๋ก ๋ณด๊ธฐ
16/34

๐Ÿ”† flex์—ฐ์Šต ๊ฐœ๊ตฌ๋ฆฌ ์‚ฌ์ดํŠธ : http://flexboxfroggy.com/#ko
๐Ÿ”† mdn flex๊ฐœ๋… ์ฝ๊ธฐ : https://developer.mozilla.org/ko/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox
๐Ÿ”† flex์— ๋Œ€ํ•ด
https://www.joshwcomeau.com/css/interactive-guide-to-flexbox/#wrapping

  • ๊ธฐ๋ณธ ์ฝ”๋“œ
<!--html-->
<div id="container">
      <div class="box">1</div>
      <div class="box">2</div>
      <div class="box">3</div>
      <div class="box">4</div>
      <div class="box">5</div>
      <div class="box">6</div>
  </div>

<!--css-->
.box {
  width: 100px;
  height: 100px;
  background-color: rosybrown;
  border: 1px solid black;
}

๐Ÿ”ถflex ์ปจํ…Œ์ด๋„ˆ

: items๋ฅผ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด flexbox container์ด ํ•„์š”ํ•จ.
: "๋ฐ”๋กœ ์œ„" ๋ถ€๋ชจ์š”์†Œ์— display:flex๋ฅผ ํ•ด์•ผ, ์ž์‹์š”์†Œ์ธ items๊ฐ€ ๊ฐ€๋กœ ์ •๋ ฌ ๋จ.

  #container{
  height: 1000px;
  display : flex;
  }

๐Ÿ”ถflex-direction

flex-direction: row(๊ธฐ๋ณธ๊ฐ’) / row-reverse/ column/ column-reverse ;

* flex-direction : "row์ผ ๋•Œ"

 #container{
  flex-direction: row(๊ธฐ๋ณธ๊ฐ’) ;
  justify-content: space-around; (๊ฐ€๋กœ์ถ• ์ •๋ ฌ)
  align-items: center; (์„ธ๋กœ์ถ• ์ •๋ ฌ)
  }

* flex-direction : "column์ผ ๋•Œ" : ๊ฐ€๋กœ์ถ•,์„ธ๋กœ์ถ•์ด ๋’ค๋ฐ”๋€œ

 #container{
  flex-direction: column;
  justify-content: space-around; (์„ธ๋กœ์ถ• ์ •๋ ฌ)
  align-items: center; (๊ฐ€๋กœ์ถ• ์ •๋ ฌ)
  }

๐Ÿ”ถjustify-content

(flex-direction์ด row์ผ ๋•Œ)
์ฃผ์ถ•์„ ๋”ฐ๋ผ ์ฝ˜ํ…์ธ  ํ•ญ๋ชฉ ๊ฐ„ ๋ฐ ๊ทธ ์ฃผ์œ„์— ๊ณต๊ฐ„์„ ๋ถ„๋ฐฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. / ๊ฐ€๋กœ ์ •๋ ฌ์„ ์„ค์ •ํ•œ๋‹ค๋Š” ์˜๋ฏธ.

justify-content:flex-start // ์š”์†Œ๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์˜ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
justify-content:flex-end // ์š”์†Œ๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์˜ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
justify-content:center // ์š”์†Œ๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์˜ ๊ฐ€์šด๋ฐ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
justify-content:space-between // ์š”์†Œ๋“ค ์‚ฌ์ด์— ๋™์ผํ•œ ๊ฐ„๊ฒฉ์„ ๋‘ก๋‹ˆ๋‹ค.
justify-content:space-around // ์š”์†Œ๋“ค ์ฃผ์œ„์— ๋™์ผํ•œ ๊ฐ„๊ฒฉ์„ ๋‘ก๋‹ˆ๋‹ค.

๐Ÿ”ถalign-items

(flex-direction์ด row์ผ ๋•Œ)
๊ต์ฐจ ์ถ•์— ์žˆ๋Š” ํ•ญ๋ชฉ์˜ ์ •๋ ฌ์„ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค. / ์„ธ๋กœ ์ •๋ ฌ์„ ์„ค์ •ํ•œ๋‹ค๋Š” ์˜๋ฏธ.

align-items: flex-start // ์š”์†Œ๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์˜ ๊ผญ๋Œ€๊ธฐ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
align-items: flex-end // ์š”์†Œ๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์˜ ๋ฐ”๋‹ฅ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
align-items: center // ์š”์†Œ๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์˜ ์„ธ๋กœ์„  ์ƒ์˜ ๊ฐ€์šด๋ฐ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
align-items: baseline // ์š”์†Œ๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์˜ ์‹œ์ž‘ ์œ„์น˜์— ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
align-items: stretch // ์š”์†Œ๋“ค์„ ์ปจํ…Œ์ด๋„ˆ์— ๋งž๋„๋ก ๋Š˜๋ฆฝ๋‹ˆ๋‹ค.

๐Ÿ”ถalign-self

  • ๋ถ€๋ชจ์š”์†Œ๊ฐ€ ์•„๋‹Œ ๊ฐœ๋ณ„ item์— ์ ์šฉํ•จ

  • align-self๋Š” align-item๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค.
    ๋‹ค์‹œ ๋งํ•ด ์„ธ๋กœ์ถ• ๋ฐฉํ–ฅ์— ์žˆ๋Š” item์˜ ์œ„์น˜๋ฅผ ๋ฐ”๊พธ๊ฒŒ ๋œ๋‹ค.

  • align-items์™€ align-self์˜ ์ฐจ์ด์ 
    : align-items๋Š” flex container์ธ ๋ถ€๋ชจ์š”์†Œ์— ์ ์šฉ,
    : align-self๋Š” ์ž์‹items ์ค‘ ํ•œ item์— ๊ฐœ๋ณ„์ ์œผ๋กœ ์ ์šฉํ•จ.

๐Ÿ”ถorder (grid์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€)

  • ๋ถ€๋ชจ์š”์†Œ๊ฐ€ ์•„๋‹Œ ๊ฐœ๋ณ„ item์— ์ ์šฉํ•จ
  • item์š”์†Œ ๊ฐ๊ฐ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พผ๋‹ค.
    (HTML์„ ํ†ตํ•ด ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๊ธฐ ํž˜๋“ค ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.)

  • ๊ธฐ๋ณธ ๊ฐ’์€ 0
    :order๋ฅผ 1(์–‘์ˆ˜)๋กœ ์ค„ ๊ฒฝ์šฐ, ํ•ด๋‹น item์ด ์ ค ๋’ค์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค.
    :order๋ฅผ -1(์Œ์ˆ˜)๋กœ ์ค„ ๊ฒฝ์šฐ, ํ•ด๋‹น item์ด ์ ค ์•ž์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค.

๐Ÿ”ถflex-wrap

  • flex-wrap :nowrap(๊ธฐ๋ณธ๊ฐ’)
    : item๋“ค์ด ๋ชจ๋‘ ๊ฐ™์€ "ํ•œ ์ค„์—" ์žˆ๋„๋ก ์œ ์ง€ํ•จ.
    ๊ทธ๋ž˜์„œ ์ง€์ •ํ•œ ๋„ˆ๋น„๊ฐ€ ๊นจ์ง€๋”๋ผ๋„ ์ค„์—ฌ์„œ ๊ฐ€๋กœ์ •๋ ฌ ๋จ.

  • flex-wrap :wrap
    : item๋“ค์˜ ๋„ˆ๋น„๋ฅผ ์œ ์ง€ํ•œ ์ฑ„, ๋„ˆ๋น„๊ฐ€ ๊ฝ‰์ฐจ๋ฉด "๋‹ค๋ฅธ ์ค„๋กœ" ๋ฐ”๋€Œ๊ฒŒ ๋จ.

  • flex-wrap :reverse
    : flex-wrap์„ ๋ฐ˜๋Œ€๋กœ ํ•จ.

  • ์ฝ”๋“œ1
    flex-direction์™€ flex-wrap์„ ์ด์šฉํ•œ ์ •๋ ฌ
.container {
  height: 300px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

๐Ÿ”ถflex-flow

  • flex-direction๊ณผ flex-wrap์ด ์ž์ฃผ ๊ฐ™์ด ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์—, flex-flow๊ฐ€ ์ด๋ฅผ ํ•œ๋ฒˆ์— ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ์Œ.

  • ์ด ์†์„ฑ์€ ๊ณต๋ฐฑ๋ฌธ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‘ ์†์„ฑ์˜ ๊ฐ’๋“ค์„ ์ธ์ž๋กœ ๋ฐ›์Œ.

  • ์ฝ”๋“œ2
    flex-direction์™€ flex-wrap์„ ์ด์šฉํ•œ ์ •๋ ฌ โ†’ flex-flow๋กœ ํ•œ๋ฒˆ์— ์‚ฌ์šฉ๊ฐ€๋Šฅ.
    (์œ„์— ์ฝ”๋“œ1๊ณผ ์ฝ”๋“œ2๋Š” ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ด)

.container {
  height: 300px;
  display: flex;
  flex-flow: column wrap;
}

๐Ÿ”ถalign-content

  • line์„ ๋ณ€๊ฒฝํ•จ
    : item๋“ค์ด ์—ฌ๋Ÿฌ ํ–‰์— ๊ฑธ์ณ ๋‚˜์˜ฌ ๋•Œ, ํ–‰๊ฐ„ ๊ณต๋ฐฑ์„ ์–ผ๋งˆ๋‚˜ ๋‘˜ ๊ฑด์ง€์— ๊ด€ํ•œ ๊ฒƒ.

  • justify-content๋Š” ํ•œ ํ–‰์— ๋Œ€ํ•œ ์ •๋ ฌ, align-content๋Š” ํ–‰๊ฐ„์— ๋Œ€ํ•œ ์ •๋ ฌ์ž„.

  • ์‚ฌ์šฉ์ฝ”๋“œ
    : ๋‘ ํ–‰๊ฐ„์˜ ์‚ฌ์ด๊ฐ€ ๋์—์„œ ๋์œผ๋กœ ๋ฉ€์–ด์ง€๊ฒŒ ๋จ.

.container {
  height: 600px;
  display: flex;
  flex-flow: row wrap;
  align-content: space-between;
}

๐Ÿ”ถflex-shrink , flex-grow

flex-shrink

  • ๋ชจ๋“  item์˜ ํฌ๊ธฐ๊ฐ€ ํ”Œ๋ ‰์Šค ์ปจํ…Œ์ด๋„ˆ๋ณด๋‹ค ํฌ๋ฉด, item๋“ค์€ ์ถ•์†Œ๋˜๋Š”๋ฐ, ์ด ๋•Œ item์˜ ์ˆ˜์ถ•๋ฅ ์„ ์„ค์ •ํ•จ.
  • ๊ฐ item์— ์„ค์ •
  • ๊ธฐ๋ณธ๊ฐ’ 1
.box:nth-child(4) {
  background-color: cadetblue;
  flex-shrink: 2;
}

flex-grow

  • item์ด ๋Š˜์–ด๋‚  ๋–„, ๋˜‘๊ฐ™์€ ๊ฐ„๊ฒฉ์œผ๋กœ ๋Š๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, box์˜†์— ๋‚จ์•„์žˆ๋Š” ๊ณต๊ฐ„์˜ n๋ฐฐ๋ฅผ ๊ฐ€์ ธ๊ฐ€๊ฒŒ ๋จ.
  • ๊ฐ item์— ์„ค์ •
  • ๊ธฐ๋ณธ๊ฐ’ 0
.box:nth-child(4) {
  background-color: cadetblue;
  flex-grow: 2;
}

๐Ÿ”ถflex-basis

  • ์•„์ดํ…œ์˜ ์ฒ˜์Œ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ.(item์˜ width,height๋ฅผ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ์Œ.)
    : flex-direction ์ด row ์ผ ๋•Œ, width ์ง€์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šท
    : flex-direction ์ด column ์ผ ๋•, height ์ง€์ •ํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šท
.box {
  flex-basis: 100px;
  height: 200px;
  background-color: rosybrown;
}

/* 
1.ํฌ๊ธฐ๋ฅผ ์ง์ ‘ ์ง€์ •
flex-basis: 10em;
flex-basis: 3px;
flex-basis: auto;

2.์›๋ณธ ํฌ๊ธฐ ํ‚ค์›Œ๋“œ
flex-basis: fill;
flex-basis: max-content;
flex-basis: min-content;
flex-basis: fit-content;

3.ํ”Œ๋ ‰์Šค ์•„์ดํ…œ ๋‚ด์šฉ ํฌ๊ธฐ์— ๋”ฐ๋ผ ์กฐ์ ˆ
flex-basis: content;
*

๐Ÿ”ถ ์ถ•์•ฝํ˜• ์†์„ฑ flex

  • ๋ณดํ†ต์€ flex-grow, flex-shrink, flex-basis ๊ฐ’์„ ๊ฐ๊ฐ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ด ์„ธ ์†์„ฑ์„ ํ•œ๋ฒˆ์— ์ง€์ •ํ•˜๋Š” flex ์ถ•์•ฝํ˜•์„ ๋งŽ์ด ์‚ฌ์šฉํ•จ.
  • flex ์ถ•์•ฝํ˜•์˜ ๊ฐ’์€ flex-grow, flex-shrink, flex-basis ์ˆœ์„œ๋กœ ์ง€์ •๋จ.
/* css */
 #wrapper {
        display: flex;
        width: 400px;
      }

      .one {
        background-color: blueviolet;
        flex: 1;
      }

      .two {
        background-color: rgb(217, 226, 43);
        flex: 2;
      }

      .three {
        background-color: rgb(34, 173, 34);
        flex: 1;
      }
      
      
 /* html */
  <div id="wrapper">
      <div class="one">1</div>
      <div class="two">2</div>
      <div class="three">3</div>
  </div>

โ†’ 1:2:1๋กœ ๋„ˆ๋น„๊ฐ€ ๋‚˜๋ˆ ์ง
โ†’ 1(100px) , 2(200px) , 3(100px)

๐Ÿ”ถ flex ์ง€์ •๋œ ์š”์†Œ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ค์ •

โ€ป grid์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž„
1) column-gap: 10px; - ์—ด๊ณผ ์—ด ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ
2) row-gap: 10px; - ํ–‰๊ณผ ํ–‰ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ
3) gap: 10px; - ์—ด๊ณผ์—ด, ํ–‰๊ณผํ–‰ ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ ํ•œ๋ฒˆ์— ์„ค์ •

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