๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/HTML CSS

[HTML/CSS] grid-column, grid-row ์œผ๋กœ ๋ ˆ์ด์•„์›ƒ ๋งŒ๋“ค๊ธฐ

by Connecting-the-dots 2022. 3. 2.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

  • ์ด์ „ ์‹ค์Šต ๋•Œ display : grid ๋ฅผ ํ™œ์šฉํ•ด์„œ ๊ตฌ์—ญ์„ ๋‚˜๋ˆ„์–ด๋ณด์•˜๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” ์‹ค์ œ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด์„œ ๋ฐ˜์‘ํ˜•์œผ๋กœ ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.
  • PC ํ™”๋ฉด์—์„œ๋Š” ์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€๊ฐ€ ์ œ์ผ ํฌ๊ฒŒ 2*2 ์‚ฌ์ด์ฆˆ๋ฅผ ์ฐจ์ง€ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” 1*1 ์‚ฌ์ด์ฆˆ๋กœ, ์ด 6์žฅ์˜ ์ด๋ฏธ์ง€๊ฐ€ ๊ฐ ์ž๋ฆฌ์— ๋ฐฐ์น˜๋œ๋‹ค.
  • ๋ชจ๋ฐ”์ผ ํ™”๋ฉด์—์„œ๋Š” ๋ชจ๋“  ์ด๋ฏธ์ง€๊ฐ€ ๋™์ผํ•˜๊ฒŒ 1*1 ์‚ฌ์ด์ฆˆ๋กœ ์„ธ๋กœ ์ •๋ ฌ๋˜์–ด ๋ฐฐ์น˜๋œ๋‹ค.

๐Ÿ’œ PC ํ™”๋ฉด

  • ๊ธฐ๋ณธ์ ์œผ๋กœ html ํŒŒ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.
<div class="grid-container">
    <div class="first"><img src="https://placeimg.com/500/500/any"></div>
    <div><img src="https://placeimg.com/500/500/arch"></div>
    <div><img src="https://placeimg.com/500/500/nature"></div>
    <div><img src="https://placeimg.com/500/500/people"></div>
    <div><img src="https://placeimg.com/500/500/tech"></div>
    <div><img src="https://placeimg.com/600/600/people"></div>
</div>

 

.grid-container {
  display: grid;
  max-width: 1200px;
  margin: auto;
  grid-template-columns: 1fr 1fr 1fr;
}

.grid-container img {
  width: 100%;
  height: 100%; 
  /* width ๋งŒ 100% ๋กœ ์ง€์ •ํ–ˆ๋”๋‹ˆ ๋ฏธ์„ธํ•˜๊ฒŒ(8px ์ •๋„?) ์‚ฌ์ง„ ํ•˜๋‹จ์— ํ‹ˆ์ด ์ƒ๊ฒจ height ๋„ 100% ๋กœ ๋งž์ถฐ์ฃผ์—ˆ๋‹ค.*/
}

.first {
  grid-column: 1 / 3;
  grid-row: 1 / 3;
}

๐Ÿ’œ ๋ชจ๋ฐ”์ผ ํ™”๋ฉด

  • ๊ธฐ์กด์— ์—ด์ด 3๊ฐœ(์ฒซ๋ฒˆ์งธ ์‚ฌ์ง„์€ ์—ด์„ 2๊ฐœ ์ฐจ์ง€ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์นœ๋‹ค.)์˜€๋‹ค๋ฉด ๋ชจ๋ฐ”์ผ ํ™”๋ฉด์€ ์—ด์ด 1๊ฐœ์—ฌ์•ผ ํ•˜๋ฏ€๋กœ grid-template-column ๋„ ์ˆ˜์ •ํ•œ๋‹ค.
  • PC ํ™”๋ฉด์ผ ๋•Œ ์ฒซ๋ฒˆ์งธ ์‚ฌ์ง„์˜ ์‚ฌ์ด์ฆˆ๋ฅผ 2*2 ๋กœ ๋งž์ถฐ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋ฐ”์ผ ํ™”๋ฉด์ผ ๋•Œ๋Š” 1*1 ์ด ๋  ์ˆ˜ ์žˆ๋„๋ก CSS ์ˆ˜์ •ํ•œ๋‹ค.
@media screen and (max-width: 768px) {
  .grid-container {
    grid-template-columns: 1fr;
  }

  .first {
    grid-column: 1 / 2;
    grid-row: 1 / 2;
  }
}

 

728x90
๋ฐ˜์‘ํ˜•