Float vs Flex vs Grid - ChoDragon9/posts GitHub Wiki

๊ธ€์˜ ๋ชฉ์ 

๋ ˆ์ด์•„์›ƒ์„ ์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. Float, Flex, Grid ์ˆœ์„œ๋กœ ๋ ˆ์ด์•„์›ƒ์„ ๋ฐฐ์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋ฐœ์ „๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๊ธฐ์ˆ ๋“ค์˜ ์ฐจ์ด์™€ ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋งŒ๋“ค์–ด์ง„ Grid๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค ์žฅ์ ์ด ์žˆ๋Š” ์ง€ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๋“œ ์‹œ์Šคํ…œ ๊ตฌํ˜„

6 Grid๋ฅผ 1:3:2๋กœ ๋‚˜๋ˆ„๊ณ  ๊ฐ๊ฐ ๊ฐ™์€ ์—ฌ๋ฐฑ์„ ๊ฐ€์งˆ ๋•Œ ์ฝ”๋“œ๋ฅผ ๋น„๊ตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2019-04-19 แ„‹แ…ฉแ„’แ…ฎ 6 45 16

Float

Float๋กœ ๊ฐœ๋ฐœํ•  ๋•Œ๋Š” ๊ฐ€๋กœ ์‚ฌ์ด์ฆˆ๋ฅผ ์šฐ๋ฆฌ๊ฐ€ ๊ณ„์‚ฐํ•ด์„œ ์ž‘์„ฑ์„ ํ•ด์•ผ ๋ฌ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  overflow: hidden๊ณผ ๊ฐ™์ธ ํŠน๋ณ„ํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์ฝ”๋”ฉ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ „์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์—ฌ๋ฐฑ๊ฐ’ ์ˆ˜์ • ์‹œ ๋งŽ์€ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค.

.box {overflow: hidden}
.item {float: left}
.item:nth-of-type(1) {
  width: calc((100% - 20px) * 1 / 6)
}
.item:nth-of-type(2) {
  width: calc((100% - 20px) * 3 / 6);
  margin: 0 10px
}
.item:nth-of-type(3) {
  width: calc((100% - 20px) * 2 / 6)
}
<div class="box">
  <div class="item">1/6</div>
  <div class="item">3/6</div>
  <div class="item">2/6</div>
</div>

Flex

Flex๊ฐ€ ๋„์ž…์ด ๋˜๋ฉด์„œ ์ข€๋” ์ถ”์ƒ์ ์œผ๋กœ ๊ธฐ์ž…์ด ๊ฐ€๋Šฅํ–ˆ์Šต๋‹ˆ๋‹ค. 1:3:2 ๋น„์œจ์„ ์ž์‹ ์—˜๋ฆฌ๋จผํŠธ์— ๊ธฐ์ž…ํ•˜๋ฉด ๋ ˆ์ด์•„์›ƒ์„ ์ง€์ • ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

.box {display: flex;}
.item:nth-of-type(1) {flex: 1}
.item:nth-of-type(2) {
  flex: 3;
  margin: 0 10px
}
.item:nth-of-type(3) {flex: 2}
<div class="box">
  <div class="item">flex(1)</div>
  <div class="item">flex(3)</div>
  <div class="item">flex(2)</div>
</div>

Grid

Grid๋Š” Flex์ฒ˜๋Ÿผ ์ถ”์ƒ์ ์œผ๋กœ ๋น„์œจ์ด ๊ธฐ์ž… ๊ฐ€๋Šฅํ•˜๊ณ , ๊ทธ๊ฒƒ์€ ๋ถ€๋ชจ์—์„œ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์ž์‹๋“ค์˜ ๋ ˆ์ด์•„์›ƒ์„ ๋ถ€๋ชจ์—์„œ ์กฐ์ž‘์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๋ ˆ์ด์•„์›ƒ ์ฝ”๋”ฉ์€ ํ•œ ๋ถ€๋ถ„์—์„œ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

.box {
  display: grid;
  grid-template-columns: 1fr 3fr 2fr;
  grid-gap: 10px
}
<div class="box">
  <div>1fr</div>
  <div>3fr</div>
  <div>2fr</div>
</div>

์Šฌ๋ผ์ด๋“œ

๊ธฐ๋Šฅ๊ฒฝ๊ธฐ๋Œ€ํšŒ ๊ณผ์ œ ์ค‘ ๋ผ๋””์˜ค ๋ฐ•์Šค๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ฐฐ๊ฒฝ์ด ๋ฐ”๋€Œ๋Š” ์š”๊ตฌ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. CSS๋งŒ์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผ๋˜์–ด ์–ต์ง€์Šค๋Ÿฌ์šด(?) ๊ธฐ๋Šฅ์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜์ง€๋งŒ Grid๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ „ํ˜€ ๋ถ€์ž์—ฐ์Šค๋Ÿฝ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Float

Float๋ฅผ ์‚ฌ์šฉ ํ•  ๋•Œ๋Š” position์˜ absolute, reletive์™€ top/bottom/left/right์™€ ๊ฐ™์€ ์†์„ฑ์„ ๊ต‰์žฅํžˆ ์„ธ๋ฐ€ํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์–ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ ๊ต‰์žฅํžˆ ์‹œ๊ฐ„๊ณผ ์ฝ”๋”ฉ์–‘์ด ๋งŽ์•„ ์ง‘๋‹ˆ๋‹ค.

div {border: 1px solid #000}
.wrapper {
  position: relative;
  height: 300px;
  overflow: hidden
}
.content {
  position: absolute;
  z-index: 2;
  left: 10%;
  bottom: 30px;
  width: 80%;
  height: 100px;
}
.radio {
  position: absolute;
  z-index: 2;
  right: calc(10% + 20px)
}
.bg {
  position: absolute;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

.radio:nth-of-type(1) {bottom: 95px}
.radio:nth-of-type(2) {bottom: 75px}
.radio:nth-of-type(3) {bottom: 55px}
.radio:nth-of-type(1):checked ~ .bg {
  background: #f00
}
.radio:nth-of-type(2):checked ~ .bg {
  background: #0f0
}
.radio:nth-of-type(3):checked ~ .bg {
  background: #00f
}
<div class="wrapper">
  <div class="content">&nbsp;</div>
  <input type="radio" class="radio" name="btn" checked>
  <input type="radio" class="radio" name="btn">
  <input type="radio" class="radio" name="btn">
  <div class="bg">&nbsp;</div>
</div>

Flex

Flex๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ด์„œ position์˜ absolute, reletive ์‚ฌ์šฉ์ด ์ค„์ง„ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋‹จ์ง€ ๊ฐ ์š”์†Œ๋“ค๊ฐ„์˜ ๊ฐ„๊ฒฉ์„ ์‰ฝ๊ฒŒ ๋งž์ถœ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

div {border: 1px solid #000}
.wrapper {
  position: relative;
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  padding-bottom: 30px
}
.content {
  position: absolute;
  z-index: 2;
  width: 80%;
  height: 100px;
}
.radio {
  position: absolute;
  right: calc(10% + 20px);
  z-index: 2
}
.bg {
  position: absolute;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

.radio:nth-of-type(1) {bottom: 95px}
.radio:nth-of-type(2) {bottom: 75px}
.radio:nth-of-type(3) {bottom: 55px}
.radio:nth-of-type(1):checked ~ .bg {
  background: #f00
}
.radio:nth-of-type(2):checked ~ .bg {
  background: #0f0
}
.radio:nth-of-type(3):checked ~ .bg {
  background: #00f
}
<div class="wrapper">
  <div class="content"></div>
  <input type="radio" class="radio" name="btn" checked>
  <input type="radio" class="radio" name="btn">
  <input type="radio" class="radio" name="btn">
  <div class="bg"></div>
</div>

Grid

Grid๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ž์‹ ์š”์†Œ๋ฅผ ์›ํ•˜๋Š” ์œ„์น˜์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. position์˜ absolute, reletive๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , top/bottom/left/right๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ์›ํ•˜๋Š” ์œ„์น˜์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

div {border: 1px solid #000}
.wrapper {
  display: grid;
  height: 300px;
  grid-template:
    '. . . .' 170px
    '. . . .' 20px
    '. . radio1 .' 20px
    '. . radio2 .' 20px
    '. . radio3 .' 20px
    '. . . .' 20px
    '. . . .' 30px
    / 10% auto 32px 10%
}
.content {
  z-index: 2;
  grid-area: 2 / 2 / 7 / 4
}
.radio {z-index: 2}
.bg {grid-area: 1 / 1 / 8 / 5; z-index: 1}

.radio:nth-of-type(1) {grid-area: radio1}
.radio:nth-of-type(2) {grid-area: radio2}
.radio:nth-of-type(3) {grid-area: radio3}
.radio:nth-of-type(1):checked ~ .bg {
  background: #f00
}
.radio:nth-of-type(2):checked ~ .bg {
  background: #0f0
}
.radio:nth-of-type(3):checked ~ .bg {
  background: #00f
}
<div class="wrapper">
  <div class="content"></div>
  <input type="radio" class="radio" name="btn" checked>
  <input type="radio" class="radio" name="btn">
  <input type="radio" class="radio" name="btn">
  <div class="bg"></div>
</div>
โš ๏ธ **GitHub.com Fallback** โš ๏ธ