๐Ÿ”ฅFinal Project Day 4 - ๊ทธ๋ฆฌ๋“œ ์—์„œ์˜ min-content, max-content

โ›ฑmin-content, max-content

์ƒ๊ฐํ–ˆ๋˜ content size ๋ฅผ ์ง์ ‘ ๋””์ž์ธ ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.

content ๋ž€? div ์•ˆ์— ์žˆ๋Š” ํ…์ŠคํŠธ๋ฅผ ์ผ์ปซ๋Š”๋‹ค.

<body>
  <div class="grid">
    <div class="item">This is a very long text</div>
    <div class="item">This is a very long text</div>
    <div class="item">This is a text</div>
    <div class="item">This is a very longer longer long text</div>
    <div class="item">This is a very longer longer long text</div>
    <div class="item">This is a very longer longer long text</div>
    <div class="item">This is a very longer longer long text</div>
    <div class="item">This is a very longer longer long text</div>
    <div class="item">This is a very longer longer long text</div>
    <div class="item">This is a very longer longer long text</div>
  </div>
</body>

div ์•ˆ์— ์žˆ๋Š” content ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณด์ด๊ฒŒ ํ• ๊นŒ ์— ๋Œ€ํ•œ ์˜ต์…˜์„ ์ฃผ๊ธฐ ์œ„ํ•จ ์ด๋‹ค.

min-content ์™€ max-content ๋Š” fr ๊ณผ ๊ฐ™์ด size ์ด๋‹ค.

min-content ๋Š” content (์—ฌ๊ธฐ์„œ๋Š” text) ๊ฐ€ ์ž‘์•„์งˆ ์ˆ˜ ์žˆ๋Š” ๋งŒํผ ์ž‘์•„์ง€๊ฒŒ ๋งŒ๋“œ๋Š” ์†์„ฑ์ด๋‹ค.

max-content ๋Š” ๋ฐ•์Šค๋ฅผ content (text ์˜ ๊ธธ์ด) ๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ์‚ฌ์ด์ฆˆ๋งŒํผ ํฌ๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.

โ›ฑCSS ์ฝ”๋“œ

.grid {
  color: white;
  display: grid;
  gap: 5px;

  grid-template-columns: 100px 100px;

  grid-auto-rows: 100px;
  margin-bottom: 30px;
}

.item:nth-child(odd) {
  background: #2ecc71;
}

.item:nth-child(even) {
  background: #3498db;
}

min1

๋ณ„ ํŠน์ด์‚ฌํ•ญ์€ ์—†์–ด ๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด์ œ ์—ฌ๊ธฐ์„œ,

grid-template-columns ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ด ๋ณด์•˜๋‹ค.

โ›ฑmin-content, max-content ์ ์šฉํ•˜๊ธฐ

grid-template-columns: max-content min-content;

min2

์ฒซ ๋ฒˆ์งธ ์ปฌ๋Ÿผ์€ content (text ์˜ ๊ธธ์ด) ๊ฐ€ ํ—ˆ์šฉํ•˜๋Š” ์ตœ๋Œ€์น˜, ํ•„์š”๋กœ ํ•˜๋Š” ์‚ฌ์ด์ฆˆ๋งŒํผ ํฌ๊ฒŒ ๋งŒ๋“ค์–ด์กŒ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๋‘ ๋ฒˆ์งธ ์ปฌ๋Ÿผ์€ content ๊ฐ€ ์ž‘์•„์งˆ ์ˆ˜ ์žˆ๋Š” ๋งŒํผ ์ž‘์•„์ง€๊ฒŒ ๋งŒ๋“ค์–ด์กŒ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

โ›ฑminmax ์™€ ํ•จ๊ป˜ ์จ๋ณธ max-content

grid-template-columns: repeat(5, minmax(max-content, 1fr));

์ปฌ๋Ÿผ์„ 5๊ฐœ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  minmax ๋กœ ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์ด ์ค„์–ด ๋“ค ๋•Œ์˜ ์ตœ์†Œ ๊ธธ์ด๋ฅผ max-content ๋กœ ์ฃผ๊ณ ,

ํ™”๋ฉด์ด ๋Š˜์–ด๋‚  ๋•Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ 1fr ๋กœ ์ฃผ์—ˆ๋‹ค.

์ด์ œ ํ™”๋ฉด์ด ์ค„์–ด ๋“ค์–ด๋„ content ์ฆ‰, ํ…์ŠคํŠธ ํฌ๊ธฐ๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ํฌ๊ธฐ ์ดํ•˜๋กœ๋Š” ์ ˆ๋Œ€ ๋–จ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.

โ›ฑminmax ์™€ ํ•จ๊ป˜ ์จ๋ณธ min-content

grind-template-columns ๋ฅผ ๋‹ค์‹œ ์ˆ˜์ •ํ•˜์˜€๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์ด ์ค„์–ด ๋“ค ๋•Œ์˜ ์ตœ์†Œ ๊ธธ์ด๋ฅผ min-content ๋กœ ์ฃผ๊ธฐ ์œ„ํ•จ์ด์˜€๋‹ค.

grid-template-columns: repeat(5, minmax(min-content, 1fr));

์ด์ œ ํ™”๋ฉด์ด ์ค„์–ด ๋“ค๋ฉด content, ํ…์ŠคํŠธ๊ฐ€ ์ž‘์•„์งˆ ์ˆ˜ ์žˆ๋Š” ๋งŒํผ ์ตœ๋Œ€ํ•œ ์ชผ๊ทธ๋ผ ๋“ค์–ด ๋ฒ„๋ฆฌ๊ฒŒ ๋œ๋‹ค.

โ›ฑauto-fill, auto-fit ๊ณผ์˜ ๊ฒฐํ•ฉ

์ด๋ฒˆ์—๋Š” ์›€์งค ์—†์ด ์ฝ”๋“œ๋งŒ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋ ์ง€ ์ƒ์ƒ์— ๋งก๊ฒจ ๋ณด์ž.

grid-template-columns: repeat(auto-fill, minmax(20px, max-content));
grid-template-columns: repeat(auto-fit, minmax(20px, max-content));

Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook