[CSS] Flexible Box๋ž€?

dygreenยท2022๋…„ 4์›” 4์ผ
0

CSS

๋ชฉ๋ก ๋ณด๊ธฐ
10/11
post-thumbnail
post-custom-banner

๐Ÿ—’ ๋ฐ˜์‘ํ˜•์›น ์ œ์ž‘์‹œ ์œ ์šฉํ•œ flex box์— ๋Œ€ํ•ด์„œ ์ž์„ธํžˆ ์„ค๋ช…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

๐Ÿ“Œ 1. Flexible Box ๊ธฐ๋ณธ๊ฐœ๋…

Flex box์—๋Š” ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•œ๋‹ค.
์ฆ‰, ๋ถ€๋ชจ์š”์†Œ์ธ Flexible Box ์•ˆ์—๋Š” ์ž์‹์š”์†Œ Flex Item์ด ๋“ค์–ด์žˆ๋‹ค

  1. ๋ถ€๋ชจ๋ฐ•์Šค์ธ flex container์— display: flex;๋ฅผ ์ ์šฉํ•˜๋Š”๊ฒŒ ์‹œ์ž‘์ด๋‹ค.
  • display: flex; ๐Ÿ‘‰ block ์š”์†Œ์™€ ๊ฐ™์€ ์„ฑํ–ฅ์„ ๊ฐ€์ง (์ˆ˜์ง ์Œ“์ž„)
  • display: inline-flex; ๐Ÿ‘‰ inline-block ์š”์†Œ์™€ ๊ฐ™์€ ์„ฑํ–ฅ์„ ๊ฐ€์ง (์ˆ˜ํ‰ ์Œ“์ž„)
    โ–บ ์—ฌ๊ธฐ์„œ flex item๋“ค์€ ๊ฐ€๋กœ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐฐ์น˜๋˜๊ณ , ์ž์‹ ์ด ๊ฐ€์ง„ ๋‚ด์šฉ๋ฌผ์˜ width๋งŒํผ๋งŒ ์ฐจ์ง€ํ•˜๊ฒŒ ๋œ๋‹ค(inline ์š”์†Œ์™€ ๊ฐ™์Œ) + height๋Š” ๋†’์ด๋งŒํผ ๋Š˜์–ด๋‚จ
1~7๋ฒˆ : ๋ถ€๋ชจ์—๋งŒ ์ ์šฉ ๊ฐ€๋Šฅ(์ž์‹ ๋ฐฐ์น˜)
8๋ฒˆ~ : ์ž์‹์—๋งŒ ์ ์šฉ ๊ฐ€๋Šฅ

๐Ÿ“Œ 2. flex-direction

  1. flex item๋“ค์ด ๋ฐฐ์น˜๋˜๋Š” ์ถ•์˜ ๋ฐฉํ–ฅ์„ ๊ฒฐ์ •ํ•œ๋‹ค. (๋ฉ”์ธ์ถ•์˜ ๋ฐฉํ–ฅ์„ ๊ฐ€๋กœ๋กœ ํ• ์ง€ , ์„ธ๋กœ๋กœ ํ• ์ง€..)

flex-direction ์ข…๋ฅ˜ 4๊ฐ€์ง€

  • row : (์•„์ดํ…œ/๋ฐ•์Šค๋ฅผ)์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ ๋ฐฐ์น˜
  • row-reverse : ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ๋ฐฐ์น˜
  • column : ์œ„์—์„œ ์•„๋ž˜๋กœ ๋ฐฐ์น˜
  • column-reverse : ์•„๋ž˜์—์„œ ์œ„๋กœ ๋ฐฐ์น˜

๐Ÿ“Œ 3. flex-wrap

  1. flex container(๋ถ€๋ชจ)๊ฐ€ ๋” ์ด์ƒ flex items(์ž์‹)์„ ํ•œ ์ค„์— ๋‹ด์„ ์—ฌ์œ  ๊ณต๊ฐ„์ด ์—†์„ ๋•Œ ์•„์ดํ…œ ์ค„๋ฐ”๊ฟˆ์„ ์–ด๋–ป๊ฒŒ ํ• ์ง€ ๊ฒฐ์ •

flex-wrap ์ข…๋ฅ˜ 3๊ฐ€์ง€

  • nowrap : ๋ฐ•์Šค๋ฅผ ํ•œ ์ค„์— ๋ฐฐ์น˜(๊ธฐ๋ณธ๊ฐ’). ์ค„๋ฐ”๊ฟˆ์„ ํ•˜์ง€ ์•Š์œผ๋ฉฐ, ๋„˜์น˜๋ฉด ๊ทธ๋ƒฅ ๋น ์ ธ๋‚˜๊ฐ.
  • wrap : ๋ฐ•์Šค๋ฅผ ์—ฌ๋Ÿฌ์ค„๋กœ ๋ฐฐ์น˜. ์ค„๋ฐ”๊ฟˆ์„ ํ•จ
  • wrap-reverse : ๋ฐ•์Šค๋ฅผ ์—ฌ๋Ÿฌ์ค„๋กœ ๋ฐฐ์น˜ํ•˜๋˜, ์—ญ๋ฐฉํ–ฅ(์—ญ์ˆœ)์œผ๋กœ ๋ฐฐ์น˜.

๐Ÿ“Œ 4. flex-flow

: flex-direction๊ณผ flex-wrap์„ ํ•œ๊บผ๋ฒˆ์— ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์ถ• ์†์„ฑ

/* ์˜ˆ์‹œ */
.opt1{flex-flow: row wrap;} /*์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐฐ์น˜, ์ค„๋ฐ”๊ฟˆ*/
.opt2{flex-flow: row nowrap;} /*์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐฐ์น˜, ์ค„๋ฐ”๊ฟˆ ์•ˆํ•˜๊ณ  ๋„˜์นจ*/

๐Ÿ“Œ 5. justify-content

: ๋ฉ”์ธ์ถ• ๋ฐฉํ–ฅ ์ •๋ ฌ
justify-content ์ข…๋ฅ˜ 6๊ฐœ

.opt1{justify-content: flex-start;} /*์•„์ดํ…œ๋“ค์„ ์‹œ์ž‘์ ์œผ๋กœ ์ •๋ ฌ*/
.opt2{justify-content: flex-end;} /*์•„์ดํ…œ๋“ค์„ ๋์ ์œผ๋กœ ์ •๋ ฌ*/
.opt3{justify-content: center;} /*์•„์ดํ…œ๋“ค์„ ๊ฐ€์šด๋ฐ๋กœ ์ •๋ ฌ*/
.opt4{justify-content: space-between;} /*๋งˆ์ง€๋ง‰ ๋ฐ•์Šค๋Š” ์–‘์ชฝ ๋์œผ๋กœ ๋ถ™์ด๊ณ , ์•„์ดํ…œ๋“ค์˜ "์‚ฌ์ด(between)"์— ๊ท ์ผํ•œ ๊ฐ„๊ฒฉ ๋งŒ๋“ ๋‹ค*/
.opt5{justify-content: space-around;} /*์•„์ดํ…œ๋“ค์˜ "๋‘˜๋ ˆ(around)=์–‘์ชฝ ๋์— ์žˆ๋Š” ๋ฐ•์Šค์˜ ์–‘์˜†"์— ๊ท ์ผํ•œ ๊ฐ„๊ฒฉ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.*/
.opt6{justify-content: space-evenly;} /*๋ชจ๋‘ ๋˜‘๊ฐ™์€ ๊ฐ„๊ฒฉ*/

์ฝ”๋“œ ์ˆœ์„œ๋Œ€๋กœ ๊ฒฐ๊ณผ๋ฌผ


๐Ÿ“Œ 6. align-items

: ์ˆ˜์ง์ถ• ๋ฐฉํ–ฅ ์ •๋ ฌ
align-items ์ข…๋ฅ˜ 5๊ฐœ

.opt1{align-items: stretch;} /*(๊ธฐ๋ณธ๊ฐ’)์•„์ดํ…œ๋“ค์ด ์ˆ˜์ง์ถ• ๋ฐฉํ–ฅ์œผ๋กœ ๋๊นŒ์ง€ ์ญˆ์šฑ ๋Š˜์–ด๋‚œ๋‹ค*/
.opt2{align-items: flex-start;} /*์•„์ดํ…œ๋“ค์„ ์‹œ์ž‘์ ์œผ๋กœ ์ •๋ ฌ*/
.opt3{align-items: flex-end;} /*์•„์ดํ…œ๋“ค์„ ๋์œผ๋กœ ์ •๋ ฌ*/
.opt4{align-items: center;} /*์•„์ดํ…œ๋“ค์„ ๊ฐ€์šด๋ฐ๋กœ ์ •๋ ฌ*/
.opt5{align-items: baseline;} /*์•„์ดํ…œ๋“ค์„ ๋ฌธ์ž ๊ธฐ์ค€์„ ์— ์ •๋ ฌ*/

๐Ÿ“Œ 7. align-content

: ๊ต์ฐจ์ถ•์˜ ์ •๋ ฌ๋ฐฉ๋ฒ•์œผ๋กœ "flex-wrap: wrap;"์†์„ฑ์„ ํ†ตํ•ด items๊ฐ€ ์—ฌ๋Ÿฌ์ค„(2์ค„ ์ด์ƒ)์ด๊ณ  ์—ฌ๋ฐฑ์ด ์žˆ์„ ๊ฒฝ์šฐ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
align-content ์ข…๋ฅ˜ 7๊ฐœ

.opt1{align-content: stretch;} /*(๊ธฐ๋ณธ๊ฐ’)์•„์ดํ…œ๋“ค์ด ์ˆ˜์ง์ถ• ๋ฐฉํ–ฅ์œผ๋กœ ๋๊นŒ์ง€ ์ญˆ์šฑ ๋Š˜์–ด๋‚œ๋‹ค*/
.opt2{align-content: flex-start;} /*์•„์ดํ…œ๋“ค์„ ์‹œ์ž‘์ ์œผ๋กœ ์ •๋ ฌ*/
.opt3{align-content: flex-end;} /*์•„์ดํ…œ๋“ค์„ ๋์œผ๋กœ ์ •๋ ฌ*/
.opt4{align-content: center;} /* ์•„์ดํ…œ๋“ค์„ ๊ฐ€์šด๋ฐ๋กœ ์ •๋ ฌ*/
.opt5{align-content: space-between;} /*์‹œ์ž‘item์€ ์‹œ์ž‘์ ์—, ๋งˆ์ง€๋ง‰ item์€ ๋์ ์— ์ •๋ ฌ๋˜๊ณ  ๋‚˜๋จธ์ง€ items๋Š” ์‚ฌ์ด์— ๊ณ ๋ฅด๊ฒŒ ์ •๋ ฌ*/
.opt6{align-content: space-around;} /*item์„ ๊ท ๋“ฑํ•œ ์—ฌ๋ฐฑ์„ ํฌํ•จํ•˜์—ฌ ์ •๋ ฌ*/
.opt7{align-content: space-evenly;} /*์•„์ดํ…œ์„ ํ…์ŠคํŠธ ๋ฒ ์ด์Šค๋ผ์ธ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ*/

์œ„์— 7๋ฒˆ๊นŒ์ง€๋Š” ๋ถ€๋ชจ์—๊ฒŒ๋งŒ ์ ์šฉ ๊ฐ€๋Šฅํ•œ ์†์„ฑ์ด์—ˆ๋‹ค๋ฉด, 8๋ฒˆ๋ถ€ํ„ฐ๋Š” ์ž์‹์—๊ฒŒ ์ ์šฉ ๊ฐ€๋Šฅํ•œ ์†์„ฑ์ด๋‹ค!

๐Ÿ“Œ 8. align-self

: align-items๊ฐ€ ์ „์ฒด item์˜ ์ˆ˜์ง์ถ• ๋ฐฉํ–ฅ ์ •๋ ฌ์ด๋ผ๋ฉด, align-self๋Š” ํ•ด๋‹น ์•„์ดํ…œ์˜ ์ˆ˜์ง์ถ• ๋ฐฉํ–ฅ ์ •๋ ฌ์ด๋‹ค.

align-self๋Š” align-item๋ณด๋‹ค ์šฐ์„ ๊ถŒ์ด ์žˆ์Œ (์ „์ฒด ์„ค์ •๋ณด๋‹ค ๊ฐœ์ธ์„ค์ •์ด ์šฐ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ)

align-self ์ข…๋ฅ˜ 6๊ฐœ

.box1{align-self: auto;} /*๊ธฐ๋ณธ๊ฐ’*/
.box2{align-self: strech;} /*container์˜ ๊ต์ฐจ์ถ•์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด item์„ ๋Š˜๋ฆผ*/
.box3{align-self: flex-start;} /*item์„ ๊ฐ ์ค„์˜ ์‹œ์ž‘์ ์œผ๋กœ ์ •๋ ฌ*/
.box4{align-self: flex-end;} /*item์„ ๊ฐ ์ค„์˜ ๋์ ์œผ๋กœ ์ •๋ ฌ*/
.box5{align-self: center;} /*item์„ ๊ฐ€์šด๋ฐ ์ •๋ ฌ*/
.box6{align-self: baseline;} /*item์„ ๋ฌธ์ž ๊ธฐ์ค€์„ ์— ์ •๋ ฌ*/

๐Ÿ“Œ 9. flex-grow | flex-shrink | flex-basis

: flex item์ด container๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ํด ๊ฒฝ์šฐ ์—ฌ๋ฐฑ์ด ์ƒ๊ธฐ๊ฑฐ๋‚˜ ๋„˜์น˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค
๐Ÿ‘‰ 9๋ฒˆ ์†์„ฑ๋“ค์„ ์‚ฌ์šฉํ•˜๋ฉด item์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜์ด๊ฑฐ๋‚˜ ์ค„์ผ ์ˆ˜ ์žˆ๊ณ  ๊ฐ€๋ณ€์ ์ธ ๋ฐ•์Šค๋ฅผ ์ž‘๋™ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค

" ๋ชจ๋“  ์ž์‹์— ๋‹ค ๋„ฃ์–ด์ค˜์•ผ ํ•จ! "

  • flex-basis: ์œ ์—ฐํ•œ ๋ฐ•์Šค์˜ ๊ธฐ๋ณธ ์˜์—ญ(๋ฐ˜์‘ํ˜•)
    ์˜ˆ๋ฅผ ๋“ค์–ด, ์†์„ฑ๊ฐ’์„ 0์œผ๋กœ ํ•˜๋ฉด flex item์— flex-grow, flex-shrink ์†์„ฑ ๊ฐ’์—์„œ ์„ค์ •ํ•œ ๋น„์œจ์ด ๊ทธ๋Œ€๋กœ ์ ์šฉ๋œ๋‹ค.
    ์†์„ฑ๊ฐ’์„ auto๋กœ ํ•˜๋ฉด ํ”Œ๋ ‰์Šค ์•„์ดํ…œ์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํฌ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ flex-grow, flex-shrink ์†์„ฑ๊ฐ’์—์„œ ์„ค์ •ํ•œ ๋น„์œจ์ด ์ ์šฉ๋œ๋‹ค.

  • flex-grow: ์œ ์—ฐํ•˜๊ฒŒ ๋Š˜๋ฆฌ๊ธฐ
    (์•„์ดํ…œ์ด flex-basis์˜ ๊ฐ’๋ณด๋‹ค ์ปค์งˆ ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ)
    ๊ธฐ๋ณธ๊ฐ’์€ 0์ด๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์ ์šฉํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” ์•„์ดํ…œ์ด ๋Š˜์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.

  • flex-shrink: ์œ ์—ฐํ•˜๊ฒŒ ์ค„์ด๊ธฐ
    (์•„์ดํ…œ์ด flex-basis์˜ ๊ฐ’๋ณด๋‹ค ์ž‘์•„์งˆ ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ)
    ๊ธฐ๋ณธ๊ฐ’์ด 1์ด๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์„ธํŒ…ํ•˜์ง€ ์•Š์•˜์–ด๋„ ์•„์ดํ…œ์ด flex-basis๋ณด๋‹ค ์ž‘์•„์งˆ ์ˆ˜ ์žˆ๋‹ค.

์ถ•์•ฝํ˜•
flex: flex-grow flex-shrink flex-basis
(์ฆ๊ฐ€๋„ˆ๋น„ | ๊ฐ์†Œ๋„ˆ๋น„ | ๊ธฐ๋ณธ๋„ˆ๋น„)
flex: 0 1 auto ๐Ÿ‘‰ ๊ธฐ๋ณธ๊ฐ’ (๋ฐฐ์œจ)


๐Ÿ“š Reference

: ๊ตญ๋น„์ง€์› ์ˆ˜์—…

profile
โœจ๊ฐ๋ช…๊นŠ์€ ์•ž๋‹จ์„ ํ–ฅํ•œ ๊พธ์ค€ํ•œ ์—ฌ์ •โœจ
post-custom-banner

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