๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

CSS ์†์„ฑ 16. flex

โ—๏ธ ํ•ด๋‹น ๊ธ€์€ ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜์—์„œ HTML & CSS, SASS(SCSS) Part์˜ ๋ฐ•์˜์›… ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฐ•์˜์ž๋ฃŒ๋ฅผ ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

flex ๋Š” ์›น ํŽ˜์ด์ง€์—์„œ ์ˆ˜ํ‰ ๊ตฌ์กฐ ๋ ˆ์ด์•„์›ƒ์„ ์‰ฝ๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.


flex ์ด์ „์˜ ๋ฐฉ๋ฒ•๋“ค๊ณผ์˜ ๋น„๊ต#

inline-block#

<div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div></div>
.container {border: 2px solid red;font-size: 0;}.container .item {width: 100px;height: 100px;border: 2px solid;border-radius: 10px;display: inline-block;font-size: 16px;}

โ˜๏ธ inline-block ์€ block ์š”์†Œ๋ฅผ inline ์š”์†Œ(ํ…์ŠคํŠธ)์ฒ˜๋Ÿผ ๋‹ค๋ฃจ๊ฒŒ ๋˜๊ณ , ์ด๋Š” ์š”์†Œ์‚ฌ์ด์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•œ font-size ์ˆ˜์ •์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋“ฑ ์ข‹์ง€ ์•Š์€ ๋ฐฉ๋ฒ•์ด๋‹ค.


float ์†์„ฑ#

<div class="container clearfix"><div class="item">1</div><div class="item">2</div><div class="item">3</div></div>
.clearfix::after {content: "";display: block;clear: both;}.container {border: 2px solid red;}.container .item {width: 100px;height: 100px;border: 2px solid;border-radius: 10px;float: left;}

โ˜๏ธ float ์ด๋ผ๋Š” ์†์„ฑ์ด ์š”์†Œ๋ฅผ ์ˆ˜ํ‰ํ™”ํ•˜๋Š”๋ฐ ์ „๋ฌธ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ์ด ์•„๋‹ˆ๋‹ค.


flex#

<div class="container"><div class="item">1</div><div class="item">2</div><div class="item">3</div></div>
.container {border: 2px solid red;display: flex;}.container .item {width: 100px;height: 100px;border: 2px solid;border-radius: 10px;float: left;}

โ˜๏ธ flex ๊ฐ€ ์ˆ˜ํ‰ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฐ€์žฅ ๊ฐ„ํŽธํ•˜๊ณ  ๋ช…ํ™•ํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.


CSS3 Flexible Box#

flex ๋Š” ์š”์†Œ์˜ ํฌ๊ธฐ๊ฐ€ ๋ถˆ๋ถ„๋ช…ํ•˜๊ฑฐ๋‚˜ ๋™์ ์ธ ๊ฒฝ์šฐ์—๋„, ๊ฐ ์š”์†Œ๋ฅผ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋Š”ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

flex ๋Š” 2๊ฐœ์˜ ๊ฐœ๋…์œผ๋กœ ๋‚˜๋‰˜๋Š”๋ฐ 1. container, 2. items ์ด๋‹ค.

container ๋Š” items ๋ฅผ ๊ฐ์‹ธ๋Š” ๋ถ€๋ชจ ์š”์†Œ์ด๊ณ , ๊ฐ item ์„ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด container ๊ฐ€ ํ•„์ˆ˜์ ์ด๋‹ค.

container ์™€ items ์— ์ ์šฉํ•˜๋Š” ์†์„ฑ์ด ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋Š”๋ฐ, container ์—๋Š” display , flex-flow , justify-content ๋“ฑ์˜ ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , items ์—๋Š” order , flex , align-self ๋“ฑ์˜ ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

2020-02-03-css-์†์„ฑ-16-flex-image-0

container-items


flex container#

์†์„ฑ#

์†์„ฑ์˜๋ฏธ
displayflex container ๋ฅผ ์ •์˜
flex-flowflex-direction ์™€ flex-wrap ์˜ ๋‹จ์ถ• ์†์„ฑ
flex-directionflex items ์˜ ์ฃผ ์ถ•(main-axis)์„ ์„ค์ •
flex-wrapflex items ์˜ ์—ฌ๋Ÿฌ ์ค„ ๋ฌถ์Œ(์ค„ ๋ฐ”๊ฟˆ) ์„ค์ •
justify-content์ฃผ ์ถ•(main-axis)์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •
align-content๊ต์ฐจ ์ถ•(cross-axis)์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •(2์ค„ ์ด์ƒ)
align-items๊ต์ฐจ ์ถ•(cross-axis)์—์„œ items ์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •(1์ค„)

display#

display ์†์„ฑ์œผ๋กœ flex container ๋ฅผ ์ •์˜ํ•œ๋‹ค.

๋ณดํ†ต display: block; , display: inline , display: inline-block , display: none; ์œผ๋กœ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ, ( Property-06-display ์ฐธ์กฐ) display: flex; ๋˜๋Š” display: inline-flex; ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
flexBlock ํŠน์„ฑ์˜ flex container ๋ฅผ ์ •์˜
inline-flexInline ํŠน์„ฑ์˜ flex container ๋ฅผ ์ •์˜

flex ์™€ inline-flex ์˜ ์ฐจ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

2020-02-03-css-์†์„ฑ-16-flex-image-1

flex-display

display: flex; ๋กœ ์ง€์ •๋œ flex container ๋Š” block ์š”์†Œ์™€ ๊ฐ™์ด ์ˆ˜์ง์œผ๋กœ ์Œ“์ด๋ฉฐ,

display: inline-flex; ๋กœ ์ง€์ •๋œ flex container ๋Š” inline ์š”์†Œ์™€ ๊ฐ™์ด ์ˆ˜ํ‰์œผ๋กœ ์Œ“์ธ๋‹ค.

โ˜๏ธ flex ์™€ inline-flex ์˜ ๋‚ด๋ถ€์— items ๊ฐ€ ์Œ“์ด๋Š” ๋ฐฉ์‹์€ ๋‘˜์ด ์™„์ „ํžˆ ๊ฐ™๋‹ค.


flex-flow#

flex items ์˜ ์ฃผ ์ถ•(main-axis)์™€ items ์˜ ์—ฌ๋Ÿฌ ์ค„ ๋ฌถ์Œ(์ค„ ๋ฐ”๊ฟˆ) ์„ค์ • - ๋‹จ์ถ•์†์„ฑ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
flex-directionitems ์˜ ์ฃผ ์ถ•(main-axis)์„ ์„ค์ •row
flex-wrapitems์˜ ์—ฌ๋Ÿฌ ์ค„ ๋ฌถ์Œ(์ค„ ๋ฐ”๊ฟˆ) ์„ค์ •nowrap

์‚ฌ์šฉ๋ฒ•#

flex-flow: ์ฃผ์ถ• ์—ฌ๋Ÿฌ์ค„๋ฌถ์Œ;
.flex-container {flex-flow: row-reverse wrap;}

flex-direction#

Items ์˜ ์ฃผ ์ถ•(main-axis) ์„ค์ •


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
rowItems ๋ฅผ ์ˆ˜ํ‰์ถ•(์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ)์œผ๋กœ ํ‘œ์‹œrow
row-reverseItems ๋ฅผ row ์˜ ๋ฐ˜๋Œ€ ์ถ•์œผ๋กœ ํ‘œ์‹œ
columnItems ๋ฅผ ์ˆ˜์ง์ถ•(์œ„์—์„œ ์•„๋ž˜๋กœ)์œผ๋กœ ํ‘œ์‹œ
column-reverseItems ๋ฅผ column ์˜ ๋ฐ˜๋Œ€ ์ถ•์œผ๋กœ ํ‘œ์‹œ

2020-02-03-css-์†์„ฑ-16-flex-image-2

flex-direction


์ฃผ ์ถ•(main-axis)๊ณผ ๊ต์ฐจ ์ถ•(cross-axis)#

row ๋Š” items ๋ฅผ ์ˆ˜ํ‰์ถ•์œผ๋กœ ํ‘œ์‹œํ•˜๋ฏ€๋กœ ์ฃผ ์ถ•์ด ์ˆ˜ํ‰์ด๋ฉฐ ๊ต์ฐจ ์ถ•์€ ์ˆ˜์ง์ด ๋œ๋‹ค .

๋ฐ˜๋Œ€๋กœ column ์€ items ๋ฅผ ์ˆ˜์ง์ถ•์œผ๋กœ ํ‘œ์‹œํ•˜๋ฏ€๋กœ ์ฃผ ์ถ•์€ ์ˆ˜์ง์ด๋ฉฐ ๊ต์ฐจ ์ถ•์€์ˆ˜ํ‰์ด ๋œ๋‹ค.

โ˜๏ธ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ์ฃผ ์ถ•๊ณผ ๊ต์ฐจ ์ถ•์ด ๋‹ฌ๋ผ์ง„๋‹ค.

2020-02-03-css-์†์„ฑ-16-flex-image-3

flex-direction-main-axis


์‹œ์ž‘์ (flex-start)๊ณผ ๋์ (flex-end)#

์ฃผ ์ถ•(main-axis)์ด๋‚˜ ๊ต์ฐจ ์ถ•(cross-axis)์˜ ์‹œ์ž‘ํ•˜๋Š” ์ง€์ ๊ณผ ๋๋‚˜๋Š” ์ง€์ ์„ ์ง€์นญ.

์ด ์—ญ์‹œ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ์‹œ์ž‘์ (flex-start)๊ณผ ๋์ (flex-end)์ด ๋‹ฌ๋ผ์ง„๋‹ค.

2020-02-03-css-์†์„ฑ-16-flex-image-4

flex-direction-main-start

2020-02-03-css-์†์„ฑ-16-flex-image-5

flex-direction-cross-start

โ˜๏ธ ๊ต์ฐจ์ถ•์˜ ์‹œ์ž‘์ (flex-start)๊ณผ ๋์ (flex-end)์€ ๋‹ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค.


flex-wrap#

items ์˜ ์—ฌ๋Ÿฌ ์ค„ ๋ฌถ์Œ(์ค„ ๋ฐ”๊ฟˆ)์„ ์„ค์ •ํ•œ๋‹ค.


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
nowrap๋ชจ๋“  items ๋ฅผ ์—ฌ๋Ÿฌ ์ค„๋กœ ๋ฌถ์ง€ ์•Š์Œ(ํ•œ ์ค„์— ํ‘œ์‹œ)nowrap
wrapitems ๋ฅผ ์—ฌ๋Ÿฌ ์ค„๋กœ ๋ฌถ์Œ
wrap-reverseitems ๋ฅผ wrap ์˜ ์—ญ ๋ฐฉํ–ฅ์œผ๋กœ ์—ฌ๋Ÿฌ ์ค„๋กœ ๋ฌถ์Œ

โ˜๏ธ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ nowrap ์ด ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— items ๋Š” ํ•œ ์ค„์—์„œ๋งŒ ํ‘œ์‹œ๋˜๊ณ  ์ค„๋ฐ”๊ฟˆ ๋˜์ง€ ์•Š์œผ๋ฉฐ ์ง€์ •๋œ ํฌ๊ธฐ(์ฃผ ์ถ•์— ๋”ฐ๋ผ width ๋‚˜ height )๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ํ•œ ์ค„์•ˆ์—์„œ๋งŒ ๊ฐ€๋ณ€ํ•œ๋‹ค.

2020-02-03-css-์†์„ฑ-16-flex-image-6

flex-wrap


์˜ˆ์ œ#

<div class="container"><div class="item">A</div><div class="item">B</div><div class="item">C</div><div class="item">D</div><div class="item">E</div></div>
.container {border: 4px solid;display: flex;flex-wrap: wrap;}.container .item {width: 150px;height: 100px;background: tomato;border: 4px dashed red;}

justify-content#

์ฃผ ์ถ•(main-axis)์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•œ๋‹ค.


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
flex-startitems ๋ฅผ ์‹œ์ž‘์ (flex-start)์œผ๋กœ ์ •๋ ฌflex-start
flex-enditems ๋ฅผ ๋์ (flex-end)์œผ๋กœ ์ •๋ ฌ
centeritems ๋ฅผ ๊ฐ€์šด๋ฐ ์ •๋ ฌ
space-between์‹œ์ž‘ item ์€ ์‹œ์ž‘์ ์—, ๋งˆ์ง€๋ง‰ item ์€ ๋์ ์— ์ •๋ ฌ๋˜๊ณ , ๋‚˜๋จธ์ง€ items ๋Š” ์‚ฌ์ด์— ๊ณ ๋ฅด๊ฒŒ ์ •๋ ฌ๋จ
space-arounditems ๋ฅผ ๊ท ๋“ฑํ•œ ์—ฌ๋ฐฑ์„ ํฌํ•จํ•˜์—ฌ ์ •๋ ฌ

2020-02-03-css-์†์„ฑ-16-flex-image-7

flex-justify-content


์˜ˆ์ œ#

<div class="container"><div class="item">A</div><div class="item">B</div><div class="item">C</div><div class="item">D</div></div>
.container {border: 4px solid;display: flex;justify-content: center;}.container .item {width: 100px;height: 100px;background: tomato;border: 4px dashed red;border-radius: 10px;}

align-content#

๊ต์ฐจ ์ถ•(cross-axis)์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •

flex-wrap ์†์„ฑ์„ ํ†ตํ•ด items ๊ฐ€ ์—ฌ๋Ÿฌ ์ค„(2์ค„ ์ด์ƒ)์ด๊ณ  ์—ฌ๋ฐฑ์ด ์žˆ์„ ๊ฒฝ์šฐ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

โ˜๏ธ items ๊ฐ€ ํ•œ ์ค„์ผ ๊ฒฝ์šฐ align-items ๋ฅผ ์‚ฌ์šฉ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
stretchcontainer ์˜ ๊ต์ฐจ ์ถ•์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด items ๋ฅผ ๋Š˜๋ฆผstretch
flex-startitems ๋ฅผ ์‹œ์ž‘์ (flex-start)์œผ๋กœ ์ •๋ ฌ
flex-enditems ๋ฅผ ๋์ (flex-end)์œผ๋กœ ์ •๋ ฌ
centeritems ๋ฅผ ๊ฐ€์šด๋ฐ ์ •๋ ฌ
space-between์‹œ์ž‘ item ์€ ์‹œ์ž‘์ ์—, ๋งˆ์ง€๋ง‰ item ์€ ๋์ ์— ์ •๋ ฌ๋˜๊ณ  ๋‚˜๋จธ์ง€ items ๋Š” ์‚ฌ์ด์— ๊ณ ๋ฅด๊ฒŒ ์ •๋ ฌ๋จ
space-arounditems ๋ฅผ ๊ท ๋“ฑํ•œ ์—ฌ๋ฐฑ์„ ํฌํ•จํ•˜์—ฌ ์ •๋ ฌ

โ˜๏ธ stretch ๋Š” ๊ต์ฐจ ์ถ•์— ํ•ด๋‹นํ•˜๋Š” ๋„ˆ๋น„(์†์„ฑ width ํ˜น์€ height )๊ฐ€ ๊ฐ’์ด auto (๊ธฐ๋ณธ๊ฐ’)์ผ ๊ฒฝ์šฐ ๊ต์ฐจ ์ถ•์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด ์ž๋™์œผ๋กœ ๋Š˜์–ด๋‚œ๋‹ค.

2020-02-03-css-์†์„ฑ-16-flex-image-8

flex-align-content


์˜ˆ์ œ#

<div class="container"><div class="item">A</div><div class="item">B</div><div class="item">C</div><div class="item">D</div><div class="item">E</div><div class="item">F</div></div>
.container {height: 400px;border: 4px solid;display: flex;flex-wrap: wrap;align-content: center;}.container .item {width: 120px;height: 100px;background: tomato;border: 4px dashed red;border-radius: 10px;}

align-items#

๊ต์ฐจ ์ถ•(cross-axis)์—์„œ items ์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•œ๋‹ค.

items ๊ฐ€ ํ•œ ์ค„์ผ ๊ฒฝ์šฐ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

โ˜๏ธ items ๊ฐ€ flex-wrap ์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ์ค„(2์ค„ ์ด์ƒ)์ผ ๊ฒฝ์šฐ์—๋Š” align-content ์†์„ฑ์ด ์šฐ์„ ํ•œ๋‹ค.๋”ฐ๋ผ์„œ align-items ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด align-content ์†์„ฑ์„ ๊ธฐ๋ณธ๊ฐ’( stretch )์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
stretchcontainer ์˜ ๊ต์ฐจ ์ถ•์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด items ๋ฅผ ๋Š˜๋ฆผstretch
flex-startitems ๋ฅผ ์‹œ์ž‘์ (flex-start)์œผ๋กœ ์ •๋ ฌ
flex-enditems ๋ฅผ ๋์ (flex-end)์œผ๋กœ ์ •๋ ฌ
centeritems ๋ฅผ ๊ฐ€์šด๋ฐ ์ •๋ ฌ
baselineitems ๋ฅผ ๋ฌธ์ž ๊ธฐ์ค€์„ ์— ์ •๋ ฌ

2020-02-03-css-์†์„ฑ-16-flex-image-9

flex-align-items


์˜ˆ์ œ#

<div class="container"><div class="item">A</div><div class="item">B</div><div class="item">C</div><div class="item">D</div><div class="item">E</div><div class="item">F</div></div>
.container {height: 400px;border: 4px solid;display: flex;flex-wrap: wrap;align-items: baseline;}.container .item {width: 120px;height: 100px;background: tomato;border: 4px dashed red;border-radius: 10px;font-size: 30px;}.container .item:nth-child(3n) {font-size: 50px;}

flex items#

์†์„ฑ#

์†์„ฑ์˜๋ฏธ
orderflex item ์˜ ์ˆœ์„œ๋ฅผ ์„ค์ •
flexflex-grow , flex-shrink , flex-basis ์˜ ๋‹จ์ถ• ์†์„ฑ
flex-growflex item ์˜ ์ฆ๊ฐ€ ๋„ˆ๋น„ ๋น„์œจ์„ ์„ค์ •
flex-shrinkflex item ์˜ ๊ฐ์†Œ ๋„ˆ๋น„ ๋น„์œจ์„ ์„ค์ •
flex-basisflex item ์˜ (๊ณต๊ฐ„ ๋ฐฐ๋ถ„ ์ „) ๊ธฐ๋ณธ ๋„ˆ๋น„ ์„ค์ •
align-self๊ต์ฐจ ์ถ•(cross-axis)์—์„œ item ์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •

order#

item ์˜ ์ˆœ์„œ๋ฅผ ์„ค์ •

item ์— ์ˆซ์ž๋ฅผ ์ง€์ •ํ•˜๊ณ  ์ˆซ์ž๊ฐ€ ํด์ˆ˜๋ก ์ˆœ์„œ๊ฐ€ ๋ฐ€๋ฆฐ๋‹ค. (์Œ์ˆ˜๊ฐ€ ํ—ˆ์šฉ)

โ˜๏ธ HTML ๊ตฌ์กฐ๋ณด๋‹ค ์šฐ์„ ํ•ด์„œ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์šฉorder ์˜ ์ˆซ์ž๊ฐ€ ๊ฐ™์œผ๋ฉด HTML ๊ตฌ์กฐ์— ๋”ฐ๋ผ ์ •๋ ฌ๋œ๋‹ค


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
์ˆซ์žitem ์˜ ์ˆœ์„œ๋ฅผ ์„ค์ •

2020-02-03-css-์†์„ฑ-16-flex-image-10

flex-order


flex#

item ์˜ ๋„ˆ๋น„(์ฆ๊ฐ€, ๊ฐ์†Œ, ๊ธฐ๋ณธ)๋ฅผ ์„ค์ • - ๋‹จ์ถ• ์†์„ฑ


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
flex-growitem ์˜ ์ฆ๊ฐ€ ๋„ˆ๋น„ ๋น„์œจ์„ ์„ค์ •0
flex-shrinkitem ์˜ ๊ฐ์†Œ ๋„ˆ๋น„ ๋น„์œจ์„ ์„ค์ •1
flex-basisitem (๊ณต๊ฐ„ ๋ฐฐ๋ถ„ ์ „) ๊ธฐ๋ณธ ๋„ˆ๋น„ ์„ค์ •auto

์‚ฌ์šฉ๋ฒ•#

flex: ์ฆ๊ฐ€๋„ˆ๋น„ ๊ฐ์†Œ๋„ˆ๋น„ ๊ธฐ๋ณธ๋„ˆ๋น„;
.item {flex: 1 1 20px; /* ์ฆ๊ฐ€๋„ˆ๋น„ ๊ฐ์†Œ๋„ˆ๋น„ ๊ธฐ๋ณธ๋„ˆ๋น„ */flex: 1 1; /* ์ฆ๊ฐ€๋„ˆ๋น„ ๊ฐ์†Œ๋„ˆ๋น„ */flex: 1 20px; /* ์ฆ๊ฐ€๋„ˆ๋น„ ๊ธฐ๋ณธ๋„ˆ๋น„ (๋‹จ์œ„๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด flex-basis๊ฐ€ ์ ์šฉ */flex: 1; /* ์ฆ๊ฐ€๋„ˆ๋น„ */}

โ˜๏ธ flex ๋‹จ์ถ• ์†์„ฑ์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ, flex-basis ์˜ ๊ธฐ๋ณธ๊ฐ’์€ 0 ์ด๋‹ค. (๊ฐœ๋ณ„ ์†์„ฑ์„์‚ฌ์šฉํ•˜๋ฉด auto )


flex-grow#

item ์˜ ์ฆ๊ฐ€ ๋„ˆ๋น„ ๋น„์œจ์„ ์„ค์ •.

์ˆซ์ž๊ฐ€ ํฌ๋ฉด ๋” ๋งŽ์€ ๋„ˆ๋น„๋ฅผ ๊ฐ€์ง

item ์ด ๊ฐ€๋ณ€ ๋„ˆ๋น„๊ฐ€ ์•„๋‹ˆ๊ฑฐ๋‚˜, ๊ฐ’์ด 0 ์ผ ๊ฒฝ์šฐ ํšจ๊ณผ๊ฐ€ ์—†๋‹ค.


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
์ˆซ์žitem ์˜ ์ฆ๊ฐ€ ๋„ˆ๋น„ ๋น„์œจ์„ ์„ค์ •

2020-02-03-css-์†์„ฑ-16-flex-image-11

flex-grow

๋ชจ๋“  items ์˜ ์ด ์ฆ๊ฐ€ ๋„ˆ๋น„( flex-grow )์—์„œ ๊ฐ item ์˜ ์ฆ๊ฐ€ ๋„ˆ๋น„์˜ ๋น„์œจ๋งŒํผ๋„ˆ๋น„๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š”๋ฐ,

์ฒซ ๋ฒˆ์งธ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ item ์ด 3๊ฐœ์ด๊ณ  ์ฆ๊ฐ€ ๋„ˆ๋น„๊ฐ€ ๊ฐ๊ฐ 1 , 2 , 1 ์ด๋ฉด

์ฒซ ๋ฒˆ์งธ item ์€ ์ด ๋„ˆ๋น„์˜ 25% (1/4),

๋‘ ๋ฒˆ์งธ item ์€ ์ด ๋„ˆ๋น„์˜ 50% (2/4),

์„ธ ๋ฒˆ์งธ item ์€ ์ด ๋„ˆ๋น„์˜ 25% (1/4)์„ ๊ฐ€์ง€๊ณ ,

๋‘ ๋ฒˆ์งธ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ item ์ด 3๊ฐœ์ด๊ณ  ์ฆ๊ฐ€ ๋„ˆ๋น„๊ฐ€ ๊ฐ๊ฐ 1 , 3 , 2 ์ด๋ฉด

์ฒซ ๋ฒˆ์งธ item ์€ ์ด ๋„ˆ๋น„์˜ 16.6% (1/6),

๋‘ ๋ฒˆ์งธ item ์€ ์ด ๋„ˆ๋น„์˜ 50% (1/2),

์„ธ ๋ฒˆ์งธ item ์€ ์ด ๋„ˆ๋น„์˜ 33.3% (1/3)๋ฅผ ๊ฐ€์ง„๋‹ค.


์˜ˆ์ œ#

<div class="container"><div class="item item1">1</div><div class="item item2">2</div><div class="item item3">3</div></div>
.container {border: 4px solid;display: flex;}.container .item {width: 100px; /* ๋ชจ๋“  ์•„์ดํ…œ์˜ ๊ธฐ๋ณธ ๋„ˆ๋น„๊ฐ€ ์ง€์ •๋˜์–ด ์žˆ๋Š” ์ƒํƒœ */height: 100px;background: tomato;border: 4px dashed red;border-radius: 10px;}.item1 {flex-grow: 1;}.item2 {flex-grow: 2;}

โ˜๏ธ ์•„์ดํ…œ์˜ ๊ธฐ๋ณธ ๋„ˆ๋น„๊ฐ€ ์ง€์ •๋˜์–ด ์žˆ๋Š” ์ƒํƒœ์ผ ๊ฒฝ์šฐ ์œ„ ์˜ˆ์ œ์ฒ˜๋Ÿผ item1 ๊ณผ item2 ์˜ flex-grow ๊ฐ’์ด 1 , 2 ์ด๋ผ๊ณ ํ•ด์„œ ์ •ํ™•ํ•˜๊ฒŒ 2๋ฐฐ ์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

.container {border: 4px solid;display: flex;}.container .item {/*   width: 100px; */height: 100px;background: tomato;border: 4px dashed red;border-radius: 10px;}.item1 {flex-grow: 1;}.item2 {flex-grow: 2;}.item3 {width: 100px;}

โ˜๏ธ ์œ„ ์˜ˆ์ œ๋ฅผ ์ด๋ ‡๊ฒŒ ๋ฐ”๊พธ๋ฉด ์ •ํ™•ํžˆ 2๋ฐฐ ์ฐจ์ด๊ฐ€ ๋‚˜๊ฒŒ ๋œ๋‹ค.ํ•˜์ง€๋งŒ ๊ฐ€๋กœ ๋„ˆ๋น„๋ฅผ ์ง€์ •ํ•˜๋Š” width ์™€ ์š”์†Œ์˜ ๊ธฐ๋ณธ ๋„ˆ๋น„๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” flex-basis ์†์„ฑ์˜ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜์žˆ๊ธฐ ๋•Œ๋ฌธ์— flex-grow ๋Š” ์™„๋ฒฝํ•˜๊ฒŒ 2๋ฐฐ, 3๋ฐฐ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ๋…์€ ์•„๋‹ˆ๊ณ  ์ฆ๊ฐ€ ๋„ˆ๋น„ ๋น„์œจ์„ ์„ค์ •ํ•˜๋Š” ๊ฐœ๋…์ด๋‹ค.

.container {border: 4px solid;display: flex;}.container .item {height: 100px;background: tomato;border: 4px dashed red;border-radius: 10px;}.item1 {flex-grow: 1;}.item2 {width: 100px;}

๐Ÿ’ก ์ด๋ ‡๊ฒŒ ํ•œ item ์€ ๊ณ ์ • ๋„ˆ๋น„ width ๋ฅผ ์ง€์ •ํ•˜๊ณ  ํ•œ item ์€ flex-grow ์†์„ฑ์„์ง€์ •ํ•˜๋ฉด, container ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋ณ€ํ•  ๋•Œ ์ง€์ •ํ•œ item ๋งŒ ๊ฐ€๋ณ€ ์‚ฌ์ด์ฆˆ๋ฅผ ์ง€๋‹ˆ๋„๋ก๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.


flex-shrink#

item ์˜ ๊ฐ์†Œ ๋„ˆ๋น„ ๋น„์œจ์„ ์„ค์ •.

์ˆซ์ž๊ฐ€ ํฌ๋ฉด ๋” ๋งŽ์€ ๋„ˆ๋น„๊ฐ€ ๊ฐ์†Œ

item ์ด ๊ฐ€๋ณ€ ๋„ˆ๋น„๊ฐ€ ์•„๋‹ˆ๊ฑฐ๋‚˜, ๊ฐ’์ด 0 ์ผ ๊ฒฝ์šฐ ํšจ๊ณผ๊ฐ€ ์—†๋‹ค.


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
์ˆซ์žitem ์˜ ๊ฐ์†Œ ๋„ˆ๋น„ ๋น„์œจ์„ ์„ค์ •1

2020-02-03-css-์†์„ฑ-16-flex-image-12

flex-shrink

โ˜๏ธ ์˜ค๋ฅธ์ชฝ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๊ธฐ๋ณธ ๋„ˆ๋น„( flex-basis )์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋น„์œจ ๊ณ„์‚ฐ์ด ๋‹ฌ๋ผ์ง„๋‹ค.


flex-basis#

item ์˜ (๊ณต๊ฐ„ ๋ฐฐ๋ถ„ ์ „) ๊ธฐ๋ณธ ๋„ˆ๋น„๋ฅผ ์„ค์ •.

๊ฐ’์ด auto ์ผ ๊ฒฝ์šฐ width , height ๋“ฑ์˜ ์†์„ฑ์œผ๋กœ item ์˜ ๋„ˆ๋น„๋ฅผ ์„ค์ •ํ•  ์ˆ˜์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋‹จ์œ„ ๊ฐ’์ด ์ฃผ์–ด์ง„ ๊ฒฝ์šฐ ์„ค์ •ํ•  ์ˆ˜ ์—†๋‹ค.


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
auto๊ฐ€๋ณ€ item ๊ณผ ๊ฐ™์€ ๋„ˆ๋น„auto
๋‹จ์œ„px , em , cm ๋“ฑ ๋‹จ์œ„๋กœ ์ง€์ •

โ˜๏ธ flex ๋‹จ์ถ• ์†์„ฑ์—์„œ flex-basis ๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๊ฐ’์ด 0 ์ด ๋œ๋‹ค.

2020-02-03-css-์†์„ฑ-16-flex-image-13

flex-basis

โ˜๏ธ item ์ „์ฒด๊ฐ€ ๋น„์œจ๋Œ€๋กœ( flex-grow ) ์ฆ๊ฐ€ํ•˜๋ ค๋ฉด flex-basis ๊ฐ€ 0 ์ธ ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.


์˜ˆ์ œ#

<div class="container"><div class="item item1">Good job!</div><div class="item item2">A</div><div class="item item3">Hello world!</div></div>
.container {border: 4px solid;display: flex;}.container .item {height: 100px;background: tomato;border: 4px dashed red;border-radius: 10px;flex-grow: 1;flex-basis: 0;}

โ˜๏ธ flex-basis: auto; ์ด๋ฉด ์ฝ˜ํ…์ธ ๋ฅผ ์ œ์™ธํ•œ ์˜์—ญ์˜ ๋น„์œจ์— ๋”ฐ๋ผ item ์˜ ๋„ˆ๋น„๊ฐ€ ์„ค์ •๋œ๋‹ค. flex-basis: 0; ์ด๋ฉด item ์ „์ฒด๊ฐ€ ๋น„์œจ์— ๋”ฐ๋ผ ์„ค์ •๋œ๋‹ค.


align-self#

๊ต์ฐจ ์ถ•(cross-axis)์—์„œ ๊ฐœ๋ณ„ item ์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •

align-items ๋Š” container ๋‚ด ๋ชจ๋“  item ์˜ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ์„ค์ •ํ•˜๋Š”๋ฐ, ํ•„์š”์— ์˜ํ•ด ์ผ๋ถ€ item ๋งŒ ์ •๋ ฌ ๋ฐฉ๋ฒ•์„ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•  ๊ฒฝ์šฐ align-self ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ์†์„ฑ์€ align-items ์†์„ฑ๋ณด๋‹ค ์šฐ์„ ํ•œ๋‹ค.


์†์„ฑ ๊ฐ’#

๊ฐ’์˜๋ฏธ๊ธฐ๋ณธ๊ฐ’
autoContainer ์˜ align-items ์†์„ฑ์„ ์ƒ์†๋ฐ›์Œauto
stretchContainer ์˜ ๊ต์ฐจ ์ถ•์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด item์„ ๋Š˜๋ฆผ
flex-startitem ์„ ๊ฐ ์ค„์˜ ์‹œ์ž‘์ (flex-start)์œผ๋กœ ์ •๋ ฌ
flex-enditem ์„ ๊ฐ ์ค„์˜ ๋์ (flex-end)์œผ๋กœ ์ •๋ ฌ
centeritem ์„ ๊ฐ€์šด๋ฐ ์ •๋ ฌ
baselineitem ์„ ๋ฌธ์ž ๊ธฐ์ค€์„ ์— ์ •๋ ฌ

2020-02-03-css-์†์„ฑ-16-flex-image-14

flex-align-self


Reference#

ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜ - HTML & CSS, SASS(SCSS) Part by ParkYoungWoong

CSS Flex(Flexible Box) ์™„๋ฒฝ ๊ฐ€์ด๋“œ