Style Sheet/CSS

๐ŸŽจ Flex์—๋„ Grid์™€ ๊ฐ™์ด gap(์—ฌ๋ฐฑ)์„ ์“ฐ์ž

์ธํŒŒ_ 2021. 10. 17. 00:48

flexbox-gap

CSS Flex gap

์˜ˆ์ „์—๋Š” flex box๋ฅผ ์‚ฌ์šฉํ• ๋•Œ, ๊ฐ item๋“ค์˜ ์‚ฌ์ด ๊ณต๊ฐ„์„ ์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” margin์„ ์‚ฌ์šฉํ•ด์•ผ ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ margin ๊ณผ gap์˜ ์ฐจ์ด์ ์€ margin์€ ๋ฌด์กฐ๊ฑด ์—ฌ๋ฐฑ์„ ์ค˜์„œ ๋ถˆํ•„์š”ํ•œ ๊ณต๊ฐ„์„ ๋งŒ๋“ค์ง€๋งŒ, gap์˜ ๊ฒฝ์šฐ ์—˜๋ฆฌ๋จผํŠธ ์š”์†Œ๋“ค์˜ ์‚ฌ์ด์— ์ธ์ ‘ํ•œ ์š”์†Œ๊ฐ€ ์žˆ์„๋•Œ๋งŒ ๊ณต๊ฐ„(space)์„ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ํ›จ์”ฌ ๋ชจ๋˜ํ•˜๋‹ค.

ํ•˜์ง€๋งŒ ์ด์ œ flexbox๋„ grid์™€ ๊ฐ™์ด gap๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋ฌธ๋ฒ•์€ ๊ทธ๋ฆฌ๋“œ์™€ ์™„์ „ํžˆ ํŒ๋ฐ•์ด๋‹ˆ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

gap: [์ƒํ•˜์ขŒ์šฐ ์ „์ฒด์˜ ๊ฐ’];
gap: [์ƒํ•˜ ์‚ฌ์ด์˜๊ฐ’] [์ขŒ์šฐ ์‚ฌ์ด์˜๊ฐ’];

 

๋™์ผํ•œ ์—ฌ๋ฐฑ ์ฃผ๊ธฐ

<div class="test-parent">
  <div class="test-child">1</div>
  <div class="test-child">2</div>
  <div class="test-child">3</div>
  <div class="test-child">4</div>
  <div class="test-child">5</div>
  <div class="test-child">6</div>
  <div class="test-child">7</div>
  <div class="test-child">8</div>
  <div class="test-child">9</div>
</div>
.test-parent {
    display: flex;
    flex-wrap: wrap;
    gap: 10px; /* ์ „์ฒด์— 10๊ณต๊ฐ„์„ ์ค€๋‹ค */
}

.test-child {
    width: 180px;
    height: 180px;
    background: gray;
    color: white;
    font-size: 3em;
}

flexbox-gap

 

๊ฐ๊ธฐ ๋‹ค๋ฅธ ์—ฌ๋ฐฑ ์ฃผ๊ธฐ

gap์€ ์ „์ฒด ๊ฐ’์„ ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ๊ณ  ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ตฌ๋ถ„์ž๋กœํ•˜์—ฌ ์ƒํ•˜ ๊ทธ๋ฆฌ๊ณ  ์ขŒ์šฐ ๋‹ค๋ฅด๊ฒŒ ๊ฐ’์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. 

.test-parent {
  display: flex;
  flex-wrap: wrap;
  gap: 50px 10px;
}

flexbox-gap


# ์ฐธ๊ณ ์ž๋ฃŒ

https://webisfree.com/2020-12-05/[css]-%EC%8A%A4%ED%83%80%EC%9D%BC%EC%86%8D%EC%84%B1-gap%EC%9D%84-flex%EC%97%90%EC%84%9C-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0