๐Ÿ‘ฉ‍๐Ÿ’ป/CSS3

[responsive-web] CSS grid-layout repeat() (auto-fill, auto-fit)

ํ•œ๋‚˜ 2021. 1. 7. 23:18

 

๋ฐ˜์‘ํ˜• ๋ ˆ์ด์•„์›ƒ์„ ์ œ์ž‘ํ•  ๋•Œ, grid-template-columns์˜ repeat() ํ•จ์ˆ˜์˜ ๋‘ ์†์„ฑ๊ฐ’ auto-fill๊ณผ auto-fit์˜ ์ฐจ์ด์ .

repeat() ํ•จ์ˆ˜

๋งŒ์ผ, ์นผ๋Ÿผ 12๊ฐœ๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค๊ณ  ํ•˜๋ฉด, ์šฐ๋ฆฌ๋Š” ๋ช…์‹œ์ ์œผ๋กœ repeat() ํ•จ์ˆ˜์•ˆ์— ๊ทธ ๊ฐ’์„ ์ ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

.grid {
	display: grid;
    
    /* define the number of grid columns */
    grid-template-columns: repeat(12, 1fr);
    column-gap: 12px;
    row-gap: 8px;
}

 

์ด ๊ฐ€๋กœ ๋‹จ ์•ˆ์—์„œ ์นผ๋Ÿผ 12๊ฐœ๊ฐ€ ๋ฏธ๋ฆฌ ์ง€์ •ํ•ด์ค€ row-gap, column-gap ๊ฐ’์„ ์œ ์ง€ํ•˜๋ฉฐ ์ •๋ ฌ๋˜์–ด ์žˆ๋‹ค.

๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ๊ฐ row์— ์ปฌ๋Ÿผ 12๊ฐœ๋ฅผ ์ •๋ ฌํ•˜๋ผ๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ, ํ™”๋ฉด ํฌ๊ธฐ๋ฅผ ๋Š˜๋ ธ์„ ๋•Œ๋“  ์ค„์˜€์„ ๋•Œ๋“  ๋ฌด๊ด€ํ•˜๊ฒŒ ๋™์ผํ•˜๊ฒŒ ์ •๋ ฌ๋˜์–ด ์žˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ปจํ…์ธ ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์ด ๋ชฐ๋ ค ์žˆ์„ ๊ฒฝ์šฐ, ๊ฐ ์ปฌ๋Ÿผ์ด ๋ชจ์–‘์„ ์žƒ๊ณ  squeezed ๋˜์–ด ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋ฏ€๋กœ, minmax() ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์จ์„œ ๊ฐ ์ปฌ๋Ÿผ์— ๊ณ ์ •๋œ ๋„ˆ๋น„๊ฐ’์„ ๋ถ€์—ฌํ•˜์ž.

 

.grid {
    display: grid;
    grid-template-columns: repeat(12, minmax(150px, 1fr));
	column-gap: 12px;
    row-gap: 8px;
}

์ด์ œ ์ปจํ…์ธ ๋Š” ๊ณ ์ •๋œ minmax ๊ฐ’์„ ๊ฐ–๊ฒŒ ๋˜์—ˆ๊ณ , 12๊ฐœ์˜ ์นผ๋Ÿผ์„ ๋ชจ๋‘ ํ‘œํ˜„ํ•ด์ฃผ๋‹ค๋ณด๋‹ˆ ๊ฐ€๋กœ๋กœ ์Šคํฌ๋กค ๋ฐ”๊ฐ€ ์ƒ๊ฒผ๋‹ค. ๋ณดํ†ต์€ ์ž์œ ๋กญ๊ฒŒ ๋ณ€๋™ํ•˜๋Š” ํ™”๋ฉด ๋„ˆ๋น„์— ๋งž๊ฒŒ overflow๋œ ์ปฌ๋Ÿผ๋“ค์€ ์ƒˆ๋กญ๊ฒŒ ์•„๋žซ๋‹จ์œผ๋กœ wrap๋˜๋Š” ํŽธ์ด ํ‰๋ฒ”ํ•˜๋‹ค. ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ 12๊ฐœ์˜ ์ปฌ๋Ÿผ์„ (ํ•œ ์ค„์—) repeatํ•˜๋ผ๋Š” ๋ช…๋ น์„ ์ฒ ํšŒํ•œ ํ›„, ์ด ์ž๋ฆฌ์— auto-fit์ด๋‚˜ auto-fill ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); ๋˜๋Š” grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));

 

auto-fill๊ณผ auto-fit์€ ์ข…์ข… ๋™์ผํ•œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณด์—ฌ์ฃผ๊ธฐ๋„ ํ•˜์ง€๋งŒ, ์–ด๋””๊นŒ์ง€๋‚˜ ํŠน์ • viewport width ์•„๋ž˜์—์„œ ๊ทธ๋ ‡๊ฒŒ ๋ณด์ด๋Š” ๊ฒƒ์ผ ๋ฟ์ด๋‹ค. ๋‘˜ ์ฐจ์ด๋Š” ๋ฏธ๋ฌ˜ํ•˜์ง€๋งŒ ๋ช…๋ฐฑํžˆ ๋‹ค๋ฅธ ์ง€์ ์ด ์žˆ๋‹ค.

 

.grid {
    display: grid;
    column-gap: 12px;
    row-gap: 8px;
}
.fill {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.fit {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}

์•„๊นŒ์ฒ˜๋Ÿผ ๋‘ ๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ์ด ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ,
ํ™”๋ฉด์˜ ๊ธธ์ด๋ฅผ ๋Š˜๋ ค๋ณด๋ฉด ์„ ๋ช…ํ•˜๊ฒŒ ์ฐจ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, auto-fit์€ ์ƒˆ๋กœ์šด ์ปฌ๋Ÿผ์ด ๋“ค์–ด์˜ฌ ์—ฌ์ง€๋ฅผ ๋‚จ๊ฒจ๋‘์ง€ ์•Š๊ณ  ํ˜„์žฌ availableํ•œ grid-item์— ๋Œ€ํ•ด์„œ๋งŒ ์‹ ๊ฒฝ ์“ฐ๋ฉฐ ๋ชจ๋‘ fit๋˜๊ฒŒ ์ •๋ ฌํ•˜์ง€๋งŒ, auto-fill์˜ ๊ฒฝ์šฐ ๋˜๋„๋ก ๋งŽ์€ ์ปฌ๋Ÿผ๋“ค์„ ์ •๋ ฌํ–ˆ์„ ๋•Œ์˜ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ƒ๊ฐํ•˜๋ฉฐ, ๋นˆ ๊ฐ’์„ ์ƒ์ •ํ•œ๋‹ค.

 

์ด๋Ÿฐ ๋ฏธ๋ฌ˜ํ•œ ์ฐจ์ด๋Š” ํ™”๋ฉด์ด ์ถฉ๋ถ„ํžˆ ๋„“์—ˆ์„ ๋•Œ ๋“œ๋Ÿฌ๋‚˜์ง€๋งŒ, ์ค„์–ด๋“ค ๋•Œ๋Š” ํ‰๋ฒ”ํ•˜๊ฒŒ ๊ฐ ์ปฌ๋Ÿผ์ด ์•„๋ž˜๋กœ wrap๋˜๋ฉฐ, ๋–จ์–ด์งˆ ๊ฒƒ์ด๋ฏ€๋กœ ์ฐจ์ด์ ์ด ์ž˜ ๋ณด์ด์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค.

 

์ฐธ๊ณ 

 

css-tricks.com/auto-sizing-columns-css-grid-auto-fill-vs-auto-fit/