๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Front-end/CSS

CSS flex-box ์˜ ๋ถ€๋ชจ ์†์„ฑ justify-content & align-content

by ciocio 2021. 8. 7.

๐Ÿ“Œ  Flex-Box?

 

 

๋ง ๊ทธ๋Œ€๋กœ ์–ด์–ด์—„์ฒญ ํž™ํ•œ ์•„์ด 

๋ถ€๋ชจ์š”์†Œ์— ๊ผญ flex-box๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ์ž์‹์š”์†Œ๋“ค์ด flex์†์„ฑ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค 

 

<!--๋ถ€๋ชจ์š”์†Œ์ธ div.container๋ฅผ Flex Container๋ผ๊ณ  ๋ถ€๋ฅด๊ณ -->
<!--๋ถ€๋ชจ์š”์†Œ์— ์ ์šฉํ•ด์•ผํ•˜๋Š” ์†์„ฑ๊ณผ-->
<div class="container">
  <!--์ž์‹์š”์†Œ์ธ div.item๋“ค์„ Flex Item์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค-->
  <!--์ž์‹์š”์†Œ์— ์ ์šฉํ•ด์•ผํ•˜๋Š” ์†์„ฑ์ด ๋‹ค๋ฅด๋‹ค !!-->
  <div class="item">helloflex</div>
  <div class="item">abc</div>
  <div class="item">helloflex</div>
</div>

 

 

๐Ÿ“Œ  Flex-Box : Flex Container ๋‚˜๋Š” ๋ถ€๋ชจ ์†์„ฑ

 

 

๐Ÿ  display: flex

 

.container {
  display: flex;
}

 

item๋“ค์ด ๊ฐ€์ง„ ๋‚ด์šฉ๋ฌผ์˜ width๋งŒํผ ๋„ˆ๋น„๋ฅผ ์ฐจ์ง€ํ•œ ์ฑ„, inline์š”์†Œ๋“ค์ฒ˜๋Ÿผ ๋‹ค๋‹ฅ๋‹ค๋‹ฅ๋ถ™๋Š”๋‹ค.

์—ฌ๊ธฐ์„œ container์— height์„ ์„ค์ •ํ•ด์ฃผ๋ฉด item๋“ค๋„ ๋ถ€๋ชจ ๋†’์ด ๋”ฐ๋ผ์„œ ์ขŒ๋ผ๋ฝ ์˜ˆ์˜๊ฒŒ ๋†“์—ฌ์ง„๋‹ค.

 

 

 

๐Ÿ  display: inline-flex

 

.container {
  display: inline-flex;
}

 

flex๊ฐ€ div์ฒ˜๋Ÿผ ํ™”๋ฉด ํ•œ ์ค„์„ ์žก๊ณ  ์ดค๋ผ๋ฝ์ด๋ผ๋ฉด

inline-flex๋Š” item๋“คํ•œํ…Œ flex๊ฐ’ ๋จน์ด๊ณ  ๊ทธ ์ค„์— ๊ณต๊ฐ„ ๋‚จ์œผ๋ฉด ๋‹ค๋ฅธ ์š”์†Œ๋“ค์„ ๊ทธ ์ค„๋กœ ์˜ฌ๋ผ์˜ค๊ฒŒ ํ•˜๋Š” ^^
inline-block๊ฐ™์€ ์•„์ด๋‹ค.

 

 

 

๐Ÿ  flex-direction: row / row-reverse / column / column-reverse

 

item๋“ค์ด ๋ฐฐ์น˜๋˜๋Š” ์ถ•์˜ ๋ฐฉํ–ฅ์„ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ.

 

.container {
  flex-direction: row;
}

 

์ขŒ  -->  ์šฐ  : ๊ฐ€๋กœ ๋ฐฐ์น˜

display: flexํ–ˆ์„ ๋•Œ ๊ธฐ๋ณธ๊ฐ’

 

.container {
  flex-direction: row-reverse;
}

 

์šฐ  -->  ์ขŒ  : ๊ฐ€๋กœ ๋ฐฐ์น˜

(โ€ป item๋“ค ์ˆœ์„œ๋„ ๋ฐ”๋€œ ์ฃผ์˜)

 

.container {
  flex-direction: column;
}

 

์ƒ  -->  ํ•˜  : ์„ธ๋กœ ๋ฐฐ์น˜

 

.container {
  flex-direction: column-reverse;
}

 

ํ•˜  -->  ์ƒ  : ์„ธ๋กœ ๋ฐฐ์น˜

(โ€ป item๋“ค ์ˆœ์„œ๋„ ๋ฐ”๋€œ ์ฃผ์˜)

 

 

 

๐Ÿ  flex-wrap: nowrap / wrap / wrap-reverse

 

container๊ฐ€ item๋“ค์„ ํ™”๋ฉด์ƒ์—์„œ ํ•œ ์ค„์— ๋ณด์—ฌ์ฃผ์ง€ ๋ชปํ•  ๋•Œ (ex. ์‚ฌ์šฉ์ž๊ฐ€ ํ™”๋ฉด์„ ์ค„์ธ๋‹ค๊ฑฐ๋‚˜ ๋ญ)

item ์ค„๋ฐ”๊ฟˆ์„ ์–ด๋–ป๊ฒŒ ํ• ๊ฑด์ง€ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ.

 

.container {
  flex-wrap: nowrap;
}

 

item์ด ํ™”๋ฉด์ƒ์—์„œ ๋„˜์ณ๋ฒ„๋ฆฌ๋ฉด ์ฟจํ•˜๊ฒŒ ์•ˆ๋ณด์—ฌ์คŒ ํŒจ์“ฐ ~

flex-wrap์˜ ๊ธฐ๋ณธ๊ฐ’.

 

.container {
  flex-wrap: wrap;
}

 

float / inline-block์œผ๋กœ ๋ฐฐ์น˜ํ•œ ์š”์†Œ๋“ค๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

๋„˜์นœ item์„ ์นœ์ ˆํ•˜๊ฒŒ ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค์„œ ๋ณด์—ฌ์ค€๋‹ค.

 

.container {
  flex-wrap: wrap-reverse;
}

 

๋„˜์นœ item์„ ์นœ์ ˆํ•˜๊ฒŒ ์œ„๋กœ ์˜ฌ๋ ค์„œ ๋ณด์—ฌ์ค€๋‹ค. ใ…Ž

 

 

 

๐Ÿ  flex-flow: flex-direction flex-wrap

 

flex-direction ์†์„ฑ๊ณผ flex-wrap ์†์„ฑ์„ ์ด๋ ‡๊ฒŒ ํ•œ๊บผ๋ฒˆ์— flex-flow ๋กœ ๋ฌถ์–ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

 

.container {
  flex-flow: row(direction) wrap(wrap);
}

 

 

 

๐Ÿ  justify-content:               flex-start / flex-end / center / space-between / -around / -evenly

๐Ÿ  align-content:  stretch / flex-start / flex-end / center / space-between / -around / -evenly

 

justify-content  -->  ์ฃผ ์ถ• (main-axis) ์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•.

align-content  -->  ๊ต์ฐจ์ถ• (cross-axis) ์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•.

-->  ๋‘˜ ๋‹ค flex line์„ ์ •๋ ฌํ•œ๋‹ค.

 

.container {
  display: flex;
  justify-content: flex-start;
}

.container {
  display: flex;
  flex-direction: column;
  align-content: flex-start;
}

 

์•„์ดํ…œ ์ค„ ๊ทธ๋Œ€๋กœ ~ ์ขŒ์ธก ์ •๋ ฌ

 

.container {
  display: flex;
  justify-content: flex-end;
}

.container {
  display: flex;
  flex-direction: column;
  align-content: flex-end;
}

 

์•„์ดํ…œ ์ค„ ๊ทธ๋Œ€๋กœ ~ ์šฐ์ธก ์ •๋ ฌ

(โ€ป item์ˆœ์„œ ์•ˆ๋ฐ”๋€œ!)

 

.container {
  display: flex;
  justify-content: center;
}

.container {
  display: flex;
  flex-direction: column;
  align-content: center;
}

 

์•„์ดํ…œ ์ค„ ๊ทธ๋Œ€๋กœ ~ ๊ฐ€์šด๋ฐ ์ •๋ ฌ

 

 

โ— ์ฃผ์˜์  โ—

 

์›น ๋ธŒ๋ผ์šฐ์ €๋Š” width๋Š” ๋ณดํ†ต (์ž์—ฐ)๊ณ ์ •๋˜์–ด์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ height๋Š” height๊ฐ’์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด, content ์ž์ฒด์˜ ๋†’์ด๋ฅผ ๋”ฐ๋ฅธ๋‹ค.

 

01

( content์— height๊ฐ’์„ ์ฃผ์ง€ ์•Š์•˜๋‹ค๊ณ  ๊ฐ€์ • )

flex-direction์ด row์ผ๋•Œ justify-content๋Š” ๋‹ค์–‘ํ•œ ๋ณ€ํ™”๋ฅผ ์ค„ ์ˆ˜ ์žˆ์ง€๋งŒ, align-content๋Š” ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.

flex-direction์ด column์ผ๋•Œ๋„ height๊ฐ’์˜ ๋ถ€์žฌ๋กœ, justify-content์™€ align-content ๋ชจ๋‘ ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.

๋‚ด๊ฐ€ ์ƒ์ƒํ•˜๋Š”๋Œ€๋กœ ์ ˆ๋Œ€ ์›€์ง์ด์ง€ ์•Š์•˜๋‹ค.

 

02

( container์— flex-wrap๊ฐ’์„ ์ฃผ์ง€ ์•Š์•˜๋‹ค๊ณ  ๊ฐ€์ • )

flex-direction์ด row์ผ๋•Œ justify-content๋Š” ์ž˜ ์ ์šฉ๋˜์ง€๋งŒ, align-content๋Š” ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.

wrap๊ฐ’์ด ์—†์œผ๋ฉด item์˜ ์œ„์น˜๊ฐ€ ๊ฐ€๋กœ์ถ•์— ๊ณ ์ •๋˜์–ด ์„ธ๋กœ์ถ•์— ๋ณ€ํ™”๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋Š” ์—ฌ์ง€๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค.  -->  flex-line์ด ํ•˜๋‚˜์ด๊ธฐ์— !!

์ฆ‰ wrap๊ฐ’์„ ์ ์šฉํ•˜๊ณ  item์ด ์ ์–ด๋„ 2์ค„ ์ด์ƒ์€ ๋˜์–ด์•ผ align-content๋ฅผ ์ ์šฉํ–ˆ์„ ๋•Œ ํšจ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

flex-direction์ด column์ผ๋•Œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค.

column์€ ํŠนํžˆ wrap๊ฐ’์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด ๋ฏธ๋™๋„ ์•ˆํ•œ๋‹ค. justify-content, align-content ๋ชจ๋‘ ์ ์šฉ๋˜์ง€ ์•Š์Œ ใ…œ!

์• ์ดˆ์— ์ˆ˜์ง์œผ๋กœ ๋ฐ€๋ ค์žˆ๋Š” item๋“ค์ด๊ธฐ์— height๊ฐ’๊ณผ wrap๊ฐ’์„ ๊ผญ ์ค˜์•ผ ์›ํ•˜๋Š”๋Œ€๋กœ ์ž‘๋™ํ•œ๋‹ค.

 

/* row & column */
/* ์ขŒ์šฐ ๊ท ์ผ ์ •๋ ฌ */

.container {
  height: 500px;
  display: flex;
  justify-content: space-between; 
}

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


/* row & column */
/* ์ƒํ•˜ ๊ท ์ผ ์ •๋ ฌ */

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

.container {
  height: 500px;
  display: flex;
  flex-flow: column wrap;
  justify-content: space-between;
}

 

flex-direction: row / column       

-->  item๋“ค ์‚ฌ์ด(between)์— ๊ท ๋“ฑํ•œ ๊ฐ„๊ฒฉ์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค

-->  ํ™”๋ฉด์˜ width๊ฐ€ ์ค‘์‹ฌ์œผ๋กœ ํ•˜๊ธฐ๋•Œ๋ฌธ์— ์–‘ ์‚ฌ์ด๋“œ์˜ item์ด ํ™”๋ฉด ๋์— ๋ถ™์–ด๋ฒ„๋ฆฐ๋‹ค ใ…Žใ…Ž

 

/* row & column */
/* ์ขŒ์šฐ ๊ท ์ผ ์ •๋ ฌ */

.container {
  height: 500px;
  display: flex;
  justify-content: space-around; 
}

.container {
  height: 500px;
  display: flex;
  flex-flow: column wrap;
  align-content: space-around;
}


/* row & column */
/* ์ƒํ•˜ ๊ท ์ผ ์ •๋ ฌ */

.container {
  height: 500px;
  flex-flow: row wrap;
  align-content: space-around; 
}

.container {
  height: 500px;
  display: flex;
  flex-flow: column wrap;
  justify-content: space-around;
}

 

flex-direction: row / column       

-->  item๋“ค ๋‘˜๋ ˆ(around)์— ๊ท ๋“ฑํ•œ ๊ฐ„๊ฒฉ์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค

-->  item๋‘˜๋ ˆ์— ๋˜‘๊ฐ™์€ ๊ฐ„๊ฒฉ์„ ์ค˜์„œ ๋„์šฐ๊ธฐ๋•Œ๋ฌธ์— ์–‘ ์‚ฌ์ด๋“œ์˜ item์€ 1/2๋งŒํผ ๋œ ๋„์›Œ์ง„๋‹ค ใ…Žใ…Ž

 

/* row & column */
/* ์ขŒ์šฐ ๊ท ์ผ ์ •๋ ฌ */

.container {
  height: 500px;
  display: flex;
  justify-content: space-evenly; 
}

.container {
  height: 500px;
  display: flex;
  flex-flow: column wrap;
  align-content: space-evenly;
}


/* row & column */
/* ์ƒํ•˜ ๊ท ์ผ ์ •๋ ฌ */

.container {
  height: 500px;
  flex-flow: row wrap;
  align-content: space-evenly; 
}

.container {
  height: 500px;
  display: flex;
  flex-flow: column wrap;
  justify-content: space-evenly;
}

 

flex-direction: row / column       

-->  ๊ณต๋ฐฑ๊ณผ item์„ ๋„๋ฏธ๋…ธ์ฒ˜๋Ÿผ ์ผ๋ ฌ๋กœ ์„ธ์šด ๋ชจ์Šต์„ ๋งŒ๋“ค์–ด์ค€๋‹ค

--> item์ด ํ™”๋ฉด ๋์— ๋ถ™์ง€๋„ ์•Š๊ณ , ์• ๋งคํ•˜๊ฒŒ 1/2๋งŒ ๋„์›Œ์ ธ์žˆ์ง€๋„ ์•Š๋‹ค

--> ๊ฐœ์ธ์ ์œผ๋กœ ๋˜๊ฒŒ ์ข‹์•„ํ•˜๋Š”๋ฐ.. IE์™€ Edge์—์„œ๋Š” ์ง€์›์ด ์•ˆ๋œ๋‹ค

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€