๐Ÿ—‚ TIL) CSS์˜ Flexbox

Solmiiยท2020๋…„ 4์›” 20์ผ
0

CSS

๋ชฉ๋ก ๋ณด๊ธฐ
3/7
post-thumbnail

๐Ÿ“ฆ Flexbox๋ž€?

  • Flexbox๊ฐ€ ์—†์„๋• position, float, table ๋“ฑ์œผ๋กœ Item๊ณผ Box์˜ ์œ„์น˜๋ฅผ ์„ค์ •ํ–ˆ์—ˆ๋Š”๋ฐ ๋„ˆ๋ฌด ๋ณต์žกํ–ˆ๊ณ , Box์•ˆ์˜ Item์„ ์ˆ˜์ง ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ฑฐ๋‚˜, Item์˜ ํฌ๊ธฐ์— ์ƒ๊ด€์—†์ด ๋™์ผํ•œ ๊ฐ„๊ฒฉ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๋Š”๊ฒƒ ๋“ฑ์˜ ๊ธฐ๋Šฅ ๊ตฌํ˜„์ด ๊นŒ๋‹ค๋กœ์› ๋‹ค.

    ๐Ÿคท๐Ÿปโ€โ™€๏ธ Flexbox ์ด์ „์— ๋งŽ์ด ์ผ๋˜ Float ์ด๋ž€?
    floatfloat์˜ ์›๋ž˜ ๋ชฉ์ ์€ image์™€ text์˜ ๋ฐฐ์น˜ ์ƒํƒœ๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•จ์œผ๋กœ, float์œผ๋กœ item๊ณผ box ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์€ HACKํ•œ ์‚ฌ์šฉ๋ฒ•์ด๋‹ค!


โ˜๏ธ Flexbox์—๋Š” ์ค‘์‹ฌ์ถ•๊ณผ ๋ฐ˜๋Œ€์ถ•์ด ์กด์žฌ

๐Ÿ’๐Ÿปโ€โ™€๏ธ Item๋“ค์ด ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์œผ๋กœ(โ†’) ์ •๋ ฌ
Horizontal

  • Item์˜ ์ •๋ ฌ ๋ฐฉํ–ฅ์ธ ์ˆ˜ํ‰์ถ•์ด ์ค‘์‹ฌ์ถ•
  • ์ˆ˜์ง์ถ•์ด ๋ฐ˜๋Œ€์ถ•

๐Ÿ’๐Ÿปโ€โ™€๏ธ Item๋“ค์ด ์ˆ˜์ง ๋ฐฉํ–ฅ์œผ๋กœ(โ†“) ์ •๋ ฌ
verticality

  • Item์˜ ์ •๋ ฌ ๋ฐฉํ–ฅ์ธ ์ˆ˜์ง์ถ•์ด ์ค‘์‹ฌ์ถ•
  • ์ˆ˜ํ‰์ถ•์ด ๋ฐ˜๋Œ€์ถ•

โœŒ๏ธ Flexbox์—๋Š” Container(Box),Item์— ์ ์šฉ๋˜๋Š” ์†์„ฑ๊ฐ’์ด ๊ฐ๊ฐ ์กด์žฌ

๐Ÿ” ์ฐธ๊ณ ํ•œ ์˜์ƒ์—์„œ ๋” ์ž์„ธํžˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

๐Ÿ™๐Ÿป Container์— ์ ์šฉ ๊ฐ€๋Šฅํ•œ ์†์„ฑ๊ฐ’ = ๋ถ€๋ชจ

๐Ÿ’๐Ÿปโ€โ™€๏ธ display

display:flex;

Flexbox๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์œผ๋ฉด, ๋จผ์ € display์†์„ฑ๊ฐ’์„ ์ค˜์•ผํ•œ๋‹ค.

๐Ÿ’๐Ÿปโ€โ™€๏ธ flex-direction

1. flex-direction: row;
์ˆ˜ํ‰์ถ• : ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ (๊ธฐ๋ณธ๊ฐ’)

2. flex-direction: row-reverse;
์ˆ˜ํ‰์ถ• : ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ

3. flex-direction: column;
์ˆ˜์ง์ถ• : ์œ„์—์„œ ์•„๋ž˜ ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ

4. flex-direction: column-reverse;
์ˆ˜์ง์ถ• : ์•„๋ž˜์—์„œ ์œ„ ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ

๐Ÿ’๐Ÿปโ€โ™€๏ธ flex-wrap
(ํ•œ ์ค„์— ๊ฐ€๋“์ฐจ๋ฉด ๋‹ค์Œ์ค„๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ํ• ๊ฑด์ง€, ํ•œ์ค„์— ๋‹ค ๋‚˜์˜ค๊ฒŒ ํ• ๊ฑด์ง€ ๊ฒฐ์ •)

1. flex-wrap: nowrap;
item์ด ํ•œ ์ค„์— ๋นผ๊ณกํ•˜๊ฒŒ ๋ถ™์–ด์žˆ์Œ
container์˜ ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ค๋ฉด item์˜ ํฌ๊ธฐ๋„ ์ค„์–ด๋“ค๋ฉด์„œ ํ•œ ์ค„์— ๋ถ™์–ด์žˆ์Œ (๊ธฐ๋ณธ๊ฐ’)

2.flex-wrap: wrap;
item์ด ํ•œ ์ค„์— ๊ฝ‰์ฐจ๋ฉด ๋‹ค์Œ ์ค„๋กœ ๋„˜์–ด๊ฐ
container์˜ ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ค๋ฉด item ํฌ๊ธฐ๋Š” ์œ ์ง€ํ•˜๋ฉด์„œ ์—ฌ๋Ÿฌ์ค„๋กœ ๋‚˜๋ˆ„์–ด์ง„๋‹ค.

3. flex-wrap: wrap-reverse;
๋งˆ์ง€๋ง‰ item์—์„œ ๋ถ€ํ„ฐ ๊ฑฐ๊พธ๋กœ wraping ๋œ๋‹ค.

๐Ÿ‘ Flex-direction + Flex-wrap

flex-flow: column nowrap;

flex-direction๊ณผ flex-wrap์„ ๊ฐ๊ฐ ๋”ฐ๋กœ ์“ฐ์ง€ ์•Š๊ณ  flow๋กœ ํ•œ ์ค„์— ์“ธ ์ˆ˜ ์žˆ๋‹ค!
(ex. border: 1px solid black;๋กœ width,style,color ์†์„ฑ์„ ํ•œ์ค„์— ์ ์„ ์ˆ˜ ์žˆ๋“ฏ์ด)

๐Ÿ’๐Ÿปโ€โ™€๏ธjustify-content
(์ค‘์‹ฌ์ถ•์—์„œ Item์„ ์–ด๋–ป๊ฒŒ ๋ฐฐ์น˜ํ• ๊ฑด์ง€ ๊ฒฐ์ •)

1. justify-content : flex-start;
์ค‘์‹ฌ์ถ•์— ๋”ฐ๋ผ ์™ผ์ชฝโ†’์˜ค๋ฅธ์ชฝ,์œ„โ†’์•„๋ž˜๋กœ ๋ฐฐ์น˜(๊ธฐ๋ณธ๊ฐ’)

2.justify-content: flex-end;
item ๊ฐ๊ฐ์˜ ์ˆœ์„œ๋Š” ์œ ์ง€ํ•˜๋ฉด์„œ, ์ค‘์‹ฌ์ถ•์— ๋”ฐ๋ผ ์˜ค๋ฅธ์ชฝโ†”์™ผ์ชฝ,์•„๋ž˜โ†”์œ„๋กœ(๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ) ๋ฐฐ์น˜
flex-end3. justify-content: center;
๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜์—ฌ ๋ฐฐ์น˜ (์ค‘์‹ฌ์ถ•์— ๋”ฐ๋ผ ์—ด,ํ–‰์ด ๋‹ฌ๋ผ์ง)

4. justify-content: space-around;
๊ฐ๊ฐ์˜ Box๋ฅผ ๋‘˜๋Ÿฌ์‹ธ๋Š” space๋ฅผ ๊ฐ๊ฐ ๋ถ€์—ฌspace-around5. justify-content: space-evenly;
Box ์‚ฌ์ด์— ๋˜‘๊ฐ™์€ space๋ฅผ ๋ถ€์—ฌ!space-evenly6. justify-content: space-between;
Box ์‚ฌ์ด์— ๋˜‘๊ฐ™์€ space๋ฅผ ๋ถ€์—ฌํ•˜๋˜, ์ œ์ผ ๋์— ์žˆ๋Š” Box์™€ container ์‚ฌ์ด์˜ space๋Š” ์—†์•ฐ space-between

๐Ÿ’๐Ÿปโ€โ™€๏ธ align-items
(๋ฐ˜๋Œ€์ถ•์—์„œ Item์„ ์–ด๋–ป๊ฒŒ ๋ฐฐ์น˜ํ• ๊ฑด์ง€ ๊ฒฐ์ •)

1. align-items: stretch;
๋ฐ˜๋Œ€์ถ•์— ๋งž๊ฒŒ ์ญ‰ ๋Š˜์–ด๋‚จ(์ŠคํŠธ๋ ˆ์นญ ํ•˜๋“ฏ์ด ์ญ‰~)(๊ธฐ๋ณธ๊ฐ’)

2. align-items: flex-start;
ex. ์ค‘์‹ฌ์ถ•์ด ์ˆ˜ํ‰์ผ๋•Œ, ์ˆ˜์ง์ ์œผ๋กœ ๊ฐ€์žฅ ์œ„์— ์ •๋ ฌ

3. align-items: center;
ex. ์ค‘์‹ฌ์ถ•์ด ์ˆ˜ํ‰์ผ๋•Œ, ์ˆ˜์ง์ ์œผ๋กœ ๊ฐ€์šด๋ฐ ์ •๋ ฌ

4. align-items: baseline;
text๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ท ๋“ฑํ•˜๊ฒŒ ์ •๋ ฌbaseline
โš ๏ธ align-content์™€ ๋‹ค๋ฅธ ์  :
Item ์ „์ฒด์˜ ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•œ ์ค„์ด๋“ ,์—ฌ๋Ÿฌ ์ค„์ด๋“  ์ƒ๊ด€์—†์ด ์ ์šฉ ๊ฐ€๋Šฅ

๐Ÿ’๐Ÿปโ€โ™€๏ธ align-content
(๋ฐ˜๋Œ€์ถ•์—์„œ Item์˜ ์ค„๋“ค ๊ฐ„์˜ ๊ฐ„๊ฒฉ ์ง€์ •)

  • flex-wrap: wrap; ์ผ ๊ฒฝ์šฐ์—๋งŒ ์ ์šฉ๋จ
    โ‡’ ์ค„๋“ค ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์ง€์ •ํ•˜๋Š” ์†์„ฑ๊ฐ’์ด๋ผ, ํ•œ ์ค„๋งŒ ์žˆ์„๋•Œ๋Š” ์•„๋ฌด๋Ÿฐ ํšจ๋ ฅ์ด ์—†์Œ!
  • justify-content์˜ ์†์„ฑ๋“ค์„ ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ฐ˜๋Œ€์ถ• ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋จ

๐Ÿ™๐Ÿป Item์— ์ ์šฉ ๊ฐ€๋Šฅํ•œ ์†์„ฑ๊ฐ’ = ์ž์‹

๐Ÿ’๐Ÿปโ€โ™€๏ธ order
(Item์˜ ์ˆœ์„œ๋ฅผ ์ง€์ •)

.item1 {
   order: 2;
}
.item2 {
   order: 1;
}
  1. ๊ธฐ๋ณธ๊ฐ’์€ 0,
    item1์ด ์ฒซ๋ฒˆ์งธ์— ์žˆ์ง€๋งŒ order ๊ฐ’์œผ๋กœ ๋‘๋ฒˆ์งธ๋กœ ๋ณด๋‚ผ์ˆ˜ ์žˆ์Œ
  2. ํ˜„์—…์—์„œ ์ž˜ ์“ฐ์ด์ง„ ์•Š๋Š”๋‹ค.

๐Ÿ’๐Ÿปโ€โ™€๏ธ flex-grow
(Container์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋Š˜์–ด๋‚ ๋•Œ, Container์˜ ์—ฌ๋ฐฑ์„ Item์ด ์–ผ๋งŒํผ ๋‚˜๋ˆ ๊ฐ€์งˆ์ง€ ์ง€์ •)

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

๐Ÿ™‹๐Ÿปโ€โ™€๏ธ flex-grow๋Š” item์˜ width๊ฐ’์˜ ๋น„์œจ์„ ์ง€์ •ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ...
item์ด ์ฐจ์ง€ํ•˜๊ณ  ์žˆ์ง€ ์•Š์€, ๋‚˜๋จธ์ง€ container์˜ ์—ฌ๋ฐฑ์˜ ๋น„์œจ์„ ์ง€์ •ํ•ด์„œ item์ด ๋‚˜๋ˆ ๊ฐ–๋Š”๊ฒƒ!
grow1์—ฌ๋ฐฑ์˜ ๋น„์œจ์„ 1:2:1๋กœ ๋‚˜๋ˆˆ ํ›„...
grow2
item์— ๋‚˜๋ˆ ๊ฐ€์ง„ ์—ฌ๋ฐฑ์„ ๊ฐ–๋‹ค๋ถ™์ธ๋‹ค!

๐Ÿ’๐Ÿปโ€โ™€๏ธ flex-shrink
(Container์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ž‘์•„์งˆ๋•Œ, Item์€ ์–ผ๋งŒํผ ์ค„์–ด๋“ค์ง€ ์ง€์ •)

.item1 {
   background: #ef9a9a;
   flex-shrink: 2;
}
.item2 {
   background: #ce93d8;
   flex-shrink: 1;
}
.item3 {
   background: #90caf9;
   flex-shrink: 1;
}

shrinkflex-shrink ๊ฐ’์ด ๋†’์„์ˆ˜๋ก ๋” ๋งŽ์ด ์ค„์–ด๋“ ๋‹ค!

๐Ÿ’๐Ÿปโ€โ™€๏ธ flex-basis
(Container์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋ณ€ํ• ๋•Œ, Item์€ ์–ผ๋งŒํผ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ• ์ง€ ์„ธ๋ถ€์ ์œผ๋กœ ์ง€์ •)

.item {
   flex-basis: 0;
}
.item:nth-child(1) {
   flex-grow: 1;
}
.item:nth-child(2) {
   flex-grow: 2;
}
.item:nth-child(3) {
   flex-grow: 1;
}
  1. flex-basis: 60%;
    โ‡’ Container๊ฐ€ ์ปค์งˆ๋•Œ๋„, ์ž‘์•„์งˆ๋•Œ๋„ ์ง€์ •ํ•œ %๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ item์˜ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ™”

  2. flex-basis: auto;
    โ‡’ flex-grow,shrink์˜ ์„ค์ •์— ๋”ฐ๋ฅธ๋‹ค. (์—ฌ๋ฐฑ์„ 1:2:1๋กœ ๋‚˜๋ˆ”)(๊ธฐ๋ณธ๊ฐ’)

  3. flex-basis: 0;
    item์ด ์ ์œ ํ•˜๊ณ  ์žˆ๋Š” ๊ณต๊ฐ„์„ 0์œผ๋กœ ์ธ์‹ โ‡’ container ์ „์ฒด๊ฐ€ ์—ฌ๋ฐฑ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋ฐฑ์„ 1:2:1๋กœ ๋‚˜๋ˆ„์–ด ๊ฐ€์ง€๋ฉด ์‹ค์ œ item์˜ ๋น„์œจ๋„ 1:2:1์ด ๋จ

๐Ÿ‘ Flex-grow + shrink + basis

flex: 2 2 auto; โ‡’ ๊ฐ๊ฐ ์ˆœ์„œ๋Œ€๋กœ grow, shrink, basis๋ฅผ ํ•œ ์ค„๋กœ ํ‘œ๊ธฐ
flex: 1; โ‡’ grow:1, shrink:1, basis:0 ์„ ์˜๋ฏธํ•จ.

๐Ÿ’๐Ÿปโ€โ™€๏ธ align-self
(Item๋ณ„๋กœ Item ์ •๋ ฌ ๋ฐฉ์‹์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค)

.item1 {
   background: #ef9a9a;
   align-self: center; =โ‡’ Container์— ์ง€์ •๋œ ์†์„ฑ๊ฐ’์„ ๋ฒ—์–ด๋‚˜์„œ, .item1 ํ•˜๋‚˜๋งŒ ๊ฐ€์šด๋ฐ ์ •๋ ฌ ๊ฐ€๋Šฅ
}
.item2 {
   background: #ce93d8; โ‡’ Container์— ์ง€์ •๋œ ์†์„ฑ๊ฐ’์„ ๋”ฐ๋ฆ„
}

๋ง‰๊ฐ„! %(percentage)์™€ vh(view height)์˜ ๊ฐœ๋… ์ •๋ฆฌ

๐Ÿ“ % (percentage)

.test {
   background: red;
   height: 100%; 
    => .test๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ถ€๋ชจ ์š”์†Œ์˜ ๋†’์ด์˜ 100%๋กœ ์ฑ„์šด๋‹ค
} 
=> ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•ด๋„ .test์˜ background ๋Š” ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.
์™œ? .test๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ถ€๋ชจ ์š”์†Œ(ex.body)์˜ height๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์•„์„œ
body {
   height: 100%;
}
.test {
   background: red;
   height: 100%;
} 
=> body์˜ height๋„ ์ง€์ •ํ•ด๋ณด๋ฉด? ๊ทธ๋ž˜๋„ .text์˜ background ๋Š” ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.
์™œ? body๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ถ€๋ชจ ์š”์†Œ(ex.html)์˜ height๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์•„์„œ
body, html {
   height: 100%;
}
.test {
   background: red;
   height: 100%;
} 
=> html๊นŒ์ง€ ์ง€์ •ํ•ด์ฃผ๋ฉด, ๋น„๋กœ์†Œ .test์˜ background๊ฐ€ ํ™”๋ฉด ์ „์ฒด์— ๋ณด์ธ๋‹ค!

๐Ÿ‘€ vh (view height), vw (view width)

.test {
   background: red;
   height: 100vh;
}

=> % ์ฒ˜๋Ÿผ .test๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ถ€๋ชจ ์š”์†Œ์˜ ๋†’์ด๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•„๋„, .text์˜ background๊ฐ€ ํ™”๋ฉด ์ „์ฒด์— ๋ณด์ธ๋‹ค!
=> 100vh = ๋ถ€๋ชจ ์š”์†Œ์— ์ƒ๊ด€์—†์ด ๋ณด์ด๋Š”(view height)์˜ 100%๋ฅผ ๋‹ค ์“ฐ๊ฒ ๋‹ค!


๐Ÿ•น Bonus! Flexbox Froggy ๊ฒŒ์ž„ ํ•ด๋ณด๊ธฐ!

๐Ÿ˜ต ์–ด๋ ค์› ๋˜ ๋‹จ๊ณ„.....
24๋‹จ๊ณ„
game1game21. flex-flow: column-reverse wrap-reverse;
โ‡’ ์ˆ˜์ง์ถ•์œผ๋กœ ์ •๋ ฌ+์ˆœ์„œ ๋ฐ˜์ „, ์ค„ ๋ฐ”๊ฟˆ+์ˆœ์„œ ๋ฐ˜์ „

1.justify-content: center;
โ‡’ ์ค‘์‹ฌ์ถ•(์ˆ˜์ง์ถ•) ๊ธฐ์ค€์œผ๋กœ ๊ฐ€์šด๋ฐ ์ •๋ ฌ

3.align-content: space-between;
โ‡’ ๋ฐ˜๋Œ€์ถ•(์ˆ˜ํ‰์ถ•) ๊ธฐ์ค€์œผ๋กœ ๊ฐ๊ฐ ์–‘ ๋์— ์ •๋ ฌ

clear๋‹ค ๊นผ๋‹น!!!!๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ˜


์ฝ”๋ฆฐ์ด๊ฐœ๋ฐœ ์™•์ดˆ๋ณด ์ฝ”๋ฆฐ์ด์ž…๋‹ˆ๋‹ค!
์ด ๋‚ด์šฉ์€ ํ˜ผ์ž ๋™์˜์ƒ ๊ฐ•์˜&๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ๋ฐฐ์šด ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ์ œ๊ฐ€ ์ดํ•ดํ•˜๊ณ  ๋„˜์–ด๊ฐ„ ๊ฐœ๋…์ด ํ‹€๋ ธ๊ฑฐ๋‚˜ ๋” ๋ณด์ถฉํ•  ๊ฐœ๋…์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ์ •๋ง ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!!

profile
ํ•˜๋ฃจ๋Š” ์น˜์—ดํ•˜๊ฒŒ ์ธ์ƒ์€ ์—ฌ์œ ๋กญ๊ฒŒ

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