๐Ÿ˜ฎ Flex

๋ฐ•์ƒ์€ยท2021๋…„ 11์›” 1์ผ
0

๐Ÿช„ CSS ๐Ÿช„

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

๋ณธ์ธ์„ ์œ„ํ•ด ์ž‘์„ฑํ•˜๋Š” ๊ฒŒ์‹œ๊ธ€์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฒ•๋งŒ ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ™‚ ์šฉ์–ด ์ •๋ฆฌ

  • ๋ฉ”์ธ์ถ•: ๊ธฐ์ค€์ด ๋˜๋Š” ์ถ•
  • ๊ต์ฐจ์ถ•: ๊ธฐ์ค€์ด ๋˜๋Š” ์ถ•์˜ ๋ฐ˜๋Œ€ ์ถ•
  • flex-box: display: flex๋ฅผ ์ ์šฉํ•œ ํƒœ๊ทธ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ˜ฎ Flex-Box์— ์ ์šฉํ•˜๋Š” ์†์„ฑ๋“ค

1. inline-flex

flex๋ฅผ inlineํ˜•ํƒœ๋กœ ์ ์šฉ์‹œํ‚จ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด <div>๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” display: block์ด๋ฏ€๋กœ ํ•œ ์ค„ ์ „์ฒด๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค.
display: flex์€ flex-box์ƒํƒœ๋กœ display: block์ฒ˜๋Ÿผ ํ•œ ์ค„ ์ „์ฒด๋ฅผ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ display: inline-flex๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” inline-block์ฒ˜๋Ÿผ ๋ณธ์ธ ํฌ๊ธฐ๋งŒํผ๋งŒ width๋ฅผ ์ฐจ์ง€ํ•˜๊ฒŒ ๋œ๋‹ค.

2. flex-direction

ํ”Œ๋ ‰์Šค๋ฐ•์Šค์˜ ๋ฉ”์ธ์ถ•์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ( ์ •๋ ฌ ๋ฐฉํ–ฅ์„ ์ง€์ • )

  • ์†์„ฑ
    1. row: ์ขŒ์ธก -> ์šฐ์ธก ( ๊ธฐ๋ณธ๊ฐ’ )
    2. row-reverse: ์šฐ์ธก -> ์ขŒ์ธก
    3. column: ์œ„ -> ์•„๋ž˜
    4. column-reverse: ์•„๋ž˜ -> ์œ„
.flex-box {
  display: flex;
  flex-direction: row;
  flex-direction: column;
  flex-direction: row-reverse;
  flex-direction: column-reverse;
}

3. flex-wrap

flex-box์˜ ์ค„๋„˜๊น€ ์ฒ˜๋ฆฌ๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ( ๋ฐ•์Šค์˜ ํฌ๊ธฐ๋ณด๋‹ค ์•„์ดํ…œ๋“ค์˜ ํฌ๊ธฐ๊ฐ€ ๋” ํด ๊ฒฝ์šฐ )

  • ์ „์ฒด ์กฐ๊ฑด: flex-box์˜ ํฌ๊ธฐ๋ณด๋‹ค ์•„์ดํ…œ๋“ค์˜ ํฌ๊ธฐ๊ฐ€ ๋” ํด ๊ฒฝ์šฐ
    1. nowrap: ์•„์ดํ…œ์˜ ๋†’์ด๋ฅผ ๋†’์ž„ ( ๊ธฐ๋ณธ๊ฐ’ )
    2. wrap: ์•„์ดํ…œ์„ ๋‹ค์Œ ๋ผ์ธ์œผ๋กœ ๋„˜๊น€
    3. wrap-reverse: ์•„์ดํ…œ์„ ์—ญ์ˆœ์œผ๋กœ ๋‹ค์Œ ๋ผ์ธ์œผ๋กœ ๋„˜๊น€
.flex-box {
  display: flex;
  flex-wrap: nowrap;
  flex-wrap: wrap;
  flex-wrap: wrap-reverse;
}

4. flex-flow

flex-direction๊ณผ flex-flow์˜ ์ถ•์•ฝ ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค.

.flex-box {
  display: flex;
  flex-flow: row nowrap;
}

5. justify-content / align-content

๋ฉ”์ธ์ถ•/๊ต์ฐจ์ถ• ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

  • align-content์˜ ๊ฒฝ์šฐ flex-flow: row wrap;๊ฐ™์€ ์ƒํƒœ์ผ ๋•Œ
    ์•„์ดํ…œ์˜ ๋ฐฐ์น˜๋Š” ๊ฐ€๋กœ๋กœ ๋ฐฐ์น˜๋˜์ง€๋งŒ ๊ธธ์ด๊ฐ€ ๊ธธ์–ด์„œ ์„ธ๋กœ๋กœ ๋‚˜๋ˆ ์งˆ ๋•Œ ์ •๋ ฌ ๊ธฐ์ค€์„ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

flex-direction: row์ผ ๊ฒฝ์šฐ ์‹œ์ž‘์ ์€ ์ขŒ์ธก
flex-direction: column์ผ ๊ฒฝ์šฐ ์‹œ์ž‘์ ์€ ์œ„

์•„๋ž˜ ์„ค๋ช…์€ flex-direction: row๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

  • ์†์„ฑ
  1. stretch: ๋ชจ๋‘ ๋™์ผํ•œ ๋น„์œจ์˜ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๋Š˜์–ด๋‚จ ( ๊ธฐ๋ณธ๊ฐ’ )
  2. flex-start: ์•„์ดํ…œ๋“ค์„ ํฌ๊ธฐ์— ๋งž๊ฒŒ ์Œ“์•„์„œ ์ขŒ์ธก์— ๋ถ™์ž„
  3. center: ์ „์ฒด width์—์„œ ์•„์ดํ…œ์„ ๋‚˜์—ดํ•˜๊ณ  ์ „์ฒด์˜ ์ค‘๊ฐ„๊ณผ ๋ฐฐ์น˜ํ•œ ์•„์ดํ…œ๋“ค์˜ ์ค‘๊ฐ„์˜ ์œ„์น˜๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ฆ
  4. flex-end: ์•„์ดํ…œ๋“ค์„ ํฌ๊ธฐ์— ๋งž๊ฒŒ ์Œ“์•„์„œ ์šฐ์ธก์— ๋ถ™์ž„
  5. space-between: ์•„์ดํ…œ๋“ค์„ ์ขŒ์ธก, ์šฐ์ธก ๋์— ๋ถ™์—ฌ์„œ ๋™์ผํ•œ ๊ฐ„๊ฒฉ์œผ๋กœ ๋–จ์–ดํŠธ๋ฆผ
  6. space-around: ์•„์ดํ…œ๋“ค์„ ๋ชจ๋‘ ๊ฐ๊ฐ ๋™์ผํ•œ ๊ฐ„๊ฒฉ์„ ๊ฐ€์ง
  7. space-evenly: ์•„์ดํ…œ๋“ค์„ ์ค‘๋ณต๋œ ๊ฐ„๊ฒฉ์„ ์ œ์™ธํ•˜๊ณ  ๋™์ผํ•œ ๊ฐ„๊ฒฉ์œผ๋กœ ๋–จ์–ดํŠธ๋ฆผ
.flex-box {
  display: flex;
  justify-content: flex-start;
  justify-content: flex-end;
  justify-content: center;
  justify-content: space-between;
  justify-content: space-around;
  justify-content: space-evenly;
}

6. align-items

๊ต์ฐจ์ถ• ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

  • ์†์„ฑ
  1. stretch: ์•„์ดํ…œ์ด ์ „์ฒด ๋†’์ด์— ๋งž๊ฒŒ ๋Š˜์–ด๋‚จ ( ๊ธฐ๋ณธ๊ฐ’ )
  2. flex-start: ๊ธฐ๋ณธ ๋†’์ด๋งŒํผ ๊ฐ€์ง€๊ณ  ์‹œ์ž‘ ์ง€์ ์— ๋ถ™์Œ
  3. flex-end: ๊ธฐ๋ณธ ๋†’์ด๋งŒํผ ๊ฐ€์ง€๊ณ  ๋ ์ง€์ ์— ๋ถ™์Œ
  4. center: ๊ธฐ๋ณธ ๋†’์ด๋งŒํผ ๊ฐ€์ง€๊ณ  ์ค‘๊ฐ„์— ๋ถ™์Œ
  5. baseline: ํ…์ŠคํŠธ์˜ ์•„๋ž˜ ๋ผ์ธ์„ ๋งž์ถฐ์„œ ๋ฐฐ์น˜ํ•จ
.flex-box {
  display: flex;
  align-items: stretch;
  align-items: flex-start;
  align-items: flex-end;
  align-items: center;
  align-items: baseline;
}

๐Ÿ˜ฏ Flex-Item์— ์ ์šฉํ•˜๋Š” ์†์„ฑ๋“ค

1. flex-basis

์•„์ดํ…œ์˜ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

width์™€ ๋‹ค๋ฅธ์ ์€ ์œ ์—ฐํ•จ์— ์žˆ์Šต๋‹ˆ๋‹ค.
flex-basis: 100px;์€ 100px์„ ์ดˆ๊ณผํ•  ๊ฒฝ์šฐ ์œ ์—ฐํ•˜๊ฒŒ ๋ณธ์ธ ํฌ๊ธฐ๋งŒํผ ๋Š˜์–ด๋‚˜์ง€๋งŒ width: 100px;๋Š” ์ •์งํ•˜๊ฒŒ 100px๋งŒํผ๋งŒ ์ฐจ์ง€ํ•ฉ๋‹ˆ๋‹ค.

auto, px, rem, cm ๋“ฑ ๋‹จ์œ„ ๊ฐ’ ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ

  • ๊ธฐ๋ณธ๊ฐ’ auto
.flex-box {
  display: flex;
}
.flex-item {
  flex-basis: auto;
  flex-basis: 100px;
}

2. flex-grow

์•„์ดํ…œ์„ ๋ฐฐ์น˜ํ•˜๊ณ  ๋‚œ ๋’ค ๋‚จ์€ ๊ณต๊ฐ„์„ ์•„์ดํ…œ์ด ๊ฐ€์งˆ ๋น„์œจ์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

์ผ๋ฐ˜์ ์ธ ์ž์—ฐ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€๊ณ  ๋“ค์–ด๊ฐ„ ์ž์—ฐ์ˆ˜์˜ ๋น„์œจ์„ ๋งŒํผ ๋‚จ์€ ๊ณต๊ฐ„์˜ ํฌ๊ธฐ๋ฅผ ํ• ๋‹น๋ฐ›๋Š”๋‹ค.

  • ๋‚จ์€ ๊ณต๊ฐ„์„ ๋น„์œจ๋กœ ๋‚˜๋ˆ ์„œ ํ• ๋‹น๋ฐ›๋Š” ๋ฐฉ์‹์ž„์„ ์ฃผ์˜ํ•ด์•ผ ํ•จ
  • ๊ธฐ๋ณธ๊ฐ’ 0
.flex-box {
  display: flex;
}
.flex-item:nth-child(1) {
  flex-grow: 1;
}
.flex-item:nth-child(2) {
  flex-grow: 2;
}

3. flex-shrink

์•„์ดํ…œ์ด ์ค„์–ด๋“œ๋Š” ๋น„์œจ์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
๊ธฐ๋ณธ๊ฐ’์ด 1์ด๋ฉฐ flex-grow๊ณผ ๋ฐ˜๋Œ€ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
์•„์ดํ…œ์ด ๋ณธ์ธ ์›๋ž˜ ํฌ๊ธฐ๋ณด๋‹ค ์ž‘์•„์ง€๊ฒŒ ๋  ๊ฒฝ์šฐ์— ๊ฐ™์€ flex-box์˜ ์•„์ดํ…œ๋“ค์˜ flex-shrink๊ฐ’์˜ ๋น„์œจ์„ ๊ธฐ์ค€์œผ๋กœ ์ž‘์•„์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์•„์ดํ…œ์ด ๋ณธ์ธ์˜ ์›๋ž˜ ํฌ๊ธฐ๋ณด๋‹ค ์ž‘์•„์ง€๊ฒŒ ๋์„ ๋•Œ ์ ์šฉ๋˜๋Š” ๋ฐฉ์‹์ด๋ผ flex-wrap: nowrap์ด์–ด์•ผ ์œ ์—ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

.flex-box {
  display: flex;
  flex-basis: 300px;
}
.flex-box > li:nth-child(1) {
  background: red;
  flex-grow: 1;
  flex-basis: 300px;
  flex-shrink: 0;	/* ๊ธฐ๋ณธ๊ฐ’ 1 */
}
.flex-box > li:nth-child(2) {
  background: green;
  flex-grow: 1;
}

4. flex

flex-grow, flex-shrink, flex-basis์˜ ์ถ•์•ฝ ๋ฌธ๋ฒ•

flex-basis ์ƒ๋žตํ•  ๊ฒฝ์šฐ์—๋Š” 0%๊ฐ€ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ ์šฉ๋œ๋‹ค.

.flex-box {
  display: flex;
  flex-basis: 300px;
}
.flex-box > li:nth-child(1) {
  background: red;
  flex: 1 0 300px;	/* ์•„๋ž˜ 3๊ฐœ์˜ ์ถ•์•ฝ ๋ฌธ๋ฒ• */
  flex-grow: 1;		/* ๊ธฐ๋ณธ๊ฐ’ 0 */
  flex-shrink: 0;	/* ๊ธฐ๋ณธ๊ฐ’ 1 */
  flex-basis: 300px;	/* ๊ธฐ๋ณธ๊ฐ’ auto */
}
.flex-box > li:nth-child(2) {
  background: green;
  flex-grow: 1;
}

5. align-self / justify-self

align-items, justify-content์˜ ๊ฐœ์ธ ์†์„ฑ์„ ์ง€์ •ํ•จ

๐Ÿ‘‡ ์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ

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