1. One Child ๐Ÿ‘ฉโ€๐Ÿ’ป

1. Problem

<section class="container">
  <div class="child child-1"></div>
</section>
.container {
  border: 4px solid #a00;
  box-sizing: border-box;
  width: 300px;
  position: relative;
}

.child {
  position: absolute;
  width: 200px;
  height: 150px;
  background-color: #37e;
  top: 0;
  right: 20px;
}

Problem case

์ด๋Ÿฐ์‹์œผ๋กœ ์ž์‹์ด ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์ž์œ ๋กœ์›Œ์ง€๋‹ค๋ณด๋‹ˆ ๋ถ€๋ชจ๋Š” content ๊ฐ€ ์—†๋Š” ๊ฒƒ์ด๋‚˜ ๋งˆ์ฐฌ๊ฐ€์ง€์ธ ์ƒํ™ฉ์ด ๋˜์–ด ๋†’์ด๋ฅผ ๊ฐ–์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

2. Solution 1 - Calculate Height

๊ฐ€์žฅ ์‰ฝ์ง€๋งŒ ๋ณ„๋กœ ์“ฐ๊ณ  ์‹ถ์ง€ ์•Š์€ ๋ฐฉ๋ฒ•์ด๋‹ค. ์ž์‹์„ ๊ฐ์‹ธ๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋†’์ด๋ฅผ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด์„œ ๋„ฃ๋Š” ๊ฒƒ์ด๋‹ค. ๋ฌธ์ œ๋Š” ์ž์‹์ด ๋ณ€๊ฒฝ๋˜๋ฉด ๋ถ€๋ชจ ์—ญ์‹œ ํฌ๊ธฐ๋ฅผ ์ˆ˜์ •ํ•ด์ค˜์•ผํ•˜๋ฉฐ, ๋ฐ˜์‘ํ˜•์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ๋ฌด๋ฆฌ๊ฐ€ ์ƒ๊ธด๋‹ค. ์ด ํฌ์ŠคํŒ…์„ ์“ฐ๊ฒŒ ๋œ ๊ณ„๊ธฐ๊ฐ€ ์ธ๊ฐ•์—์„œ ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐ€๋ฅด์น˜๋Š” ๊ฒƒ์„ ๋ณธ ์ ์ด ์žˆ๋‹ค. ์ธ๊ฐ•์ด ์ด๋Ÿฌ๋ฉด ์•ˆ ๋˜๋Š”๊ฑฐ ์•„๋ƒ? ๐Ÿคจ

.container {
  border: 4px solid #a00;
  box-sizing: border-box;
  width: 300px;
  position: relative;
  height: 158px;
}

.child {
  position: absolute;
  width: 200px;
  height: 150px;
  background-color: #37e;
  top: 0;
  right: 20px;
}

3. Solution 2 - Use float

์š”์ฆ˜์—์•ผ flex์™€ grid๊ฐ€ ๋‚˜์™€์„œ ๊ฐ€๋กœ ๋ฐฐ์น˜๋ฅผ ์œ„ํ•ด ๊ตณ์ด float์„ ์•ˆ ์“ฐ์ง€๋งŒ ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ข‹์€ ํ•ด๊ฒฐ์ฑ…์ด ๋  ์ˆ˜ ์žˆ๋‹ค.

<section class="container clearfix">
  <div class="child child-1"></div>
</section>
.container {
  border: 4px solid #a00;
  box-sizing: border-box;
  width: 300px;
}

.clearfix {
  display: flow-root;
}

.child {
  width: 200px;
  height: 150px;
  background-color: #37e;
  float: right;
  margin-right: 20px;
}

๋˜๋Š”

.container {
  border: 4px solid #a00;
  box-sizing: border-box;
  width: 300px;
}

.clearfix::after {
  content: "";
  display: block;
  clear: both;
}

.child {
  width: 200px;
  height: 150px;
  background-color: #37e;
  float: right;
  margin-right: 20px;
}

4. Solution 4 - Use flex/grid

๋ฐ”๋กœ ์œ„ float ์„ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ absolute ๋Œ€์‹  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

.container {
  border: 4px solid #a00;
  box-sizing: border-box;
  width: 300px;
  display: flex;
  justify-content: flex-end;
}

.child {
  width: 200px;
  height: 150px;
  background-color: #37e;
  margin-right: 20px;
}

๋˜๋Š”

.container {
  border: 4px solid #a00;
  box-sizing: border-box;
  width: 300px;
  display: grid;
  justify-content: end;
}

.child {
  width: 200px;
  height: 150px;
  background-color: #37e;
  margin-right: 20px;
}

2. Children ๐Ÿ‘ฉโ€๐Ÿ’ป

<section class="container">
  <div class="child child-1"></div>
  <div class="child child-2"></div>
</section>

๊ทธ๋ ‡๋‹ค๋ฉด ์ž์‹์ด ๋‘˜ ์ด์ƒ์ด๊ณ , ์ด๋“ค์˜ ๋ฐฐ์น˜๊ฐ€ ์ ˆ๋Œ€๊ฐ’์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค๋ฉด(= absolute ๋ฅผ ํ†ตํ•œ ๊ฒน์นจ) ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

1. Solution 1 - Calculate Height

์ด ๊ฒฝ์šฐ ๋‹น์—ฐํ•˜๊ฒŒ๋„ ๋†’์ด๋ฅผ ๊ณ„์‚ฐํ•ด ๋„ฃ์–ด์ฃผ๋ฉด ์ •์ƒ์ ์œผ๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์œ„์—์„œ๋„ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ๋ฐ˜์‘ํ˜•์— ๋Œ€์‘ํ•  ์ˆ˜ ์—†์–ด ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋‹ค.

.container {
  border: 4px solid #a00;
  width: 300px;
  position: relative;
  box-sizing: border-box;
  height: 158px;
}

.child {
  position: absolute;
  width: 200px;
  height: 150px;
  background-color: #37e;
  top: 0;
  right: 20px;
  
  &.child-2 {
    background-color: #3e7;
    opacity: 0;

    &:hover {
      opacity: 1;
    }
  }
}

2. Solution 2 - Use float

์ด ๊ฒฝ์šฐ float๊ณผ relative/absolute๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

  • ๋ถ€๋ชจ์—๊ฒŒ ๋†’์ด๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด float์ด ํ•„์š”.
  • ์ž์‹์ด ์„œ๋กœ ๊ฒน์น˜๊ธฐ ์œ„ํ•ด relative/absoltue๊ฐ€ ํ•„์š”.

ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

.container {
  border: 4px solid #a00;
  box-sizing: border-box;
  width: 300px;
  position: relative;
}

.clearfix {
  display: flow-root;
}

.child {
  width: 200px;
  height: 150px;
  background-color: #37e;
  
  &.child-1 {
    float: right;
    margin-right: 20px;
  }

  &.child-2 {
    background-color: #3e7;
    opacity: 0;
    position: absolute;
    top: 0;
    right: 20px;

    &:hover {
      opacity: 1;
    }
  }
}

3. Solution 3 - Use flex/grid

์ด ๊ฒฝ์šฐ ์—ญ์‹œ ๋ฐ”๋กœ ์œ„ float ์„ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ flex/grid์™€ relative/absolute๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

.container {
  border: 4px solid #a00;
  box-sizing: border-box;
  width: 300px;
  position: relative;
  display: flex;
  justify-content: flex-end;
}

.child {
  width: 200px;
  height: 150px;
  background-color: #37e;

  &.child-1 {
    margin-right: 20px;
  }
  
  &.child-2 {
    background-color: #3e7;
    opacity: 0;
    position: absolute;
    top: 0;
    right: 20px;

    &:hover {
      opacity: 1;
    }
  }
}

4. Solution 4 - Use block

ํ•˜์ง€๋งŒ ์œ„ ์ผ€์ด์Šค๋“ค์€ ๋ชจ๋‘ CSS ์ž‘๋™ ์›๋ฆฌ์— ๊ธฐ๋ฐ˜ํ•ด ์–ด๋–ป๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ์˜ˆ์ œ์ผ ๋ฟ ์‹ค์ œ๋กœ ์ €๋Ÿฐ ์ผ€์ด์Šค๋Š” ๋ณด๊ธฐ ํž˜๋“ค๋‹ค. ์ฃผ๋กœ ๋งŽ์ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ 2๊ฐœ๊ฐ€ ๊ฒน์น˜์ง€๋งŒ ๋ถ€๋ชจ๊ฐ€ ๋‹จ์ง€ Wrapper ๋กœ์„œ์˜ ์—ญํ• ๋งŒ ํ•  ๊ฒฝ์šฐ ์œ„์™€ ๊ฐ™์ด ๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐํ•  ํ•„์š” ์—†์ด ๋ถ€๋ชจ๋Š” relative๋ฅผ ๊ฐ–๊ณ , ์ž์‹ ํ•˜๋‚˜๋Š” position ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ€์ ธ ๋†’์ด๋ฅผ ์ œ๊ณตํ•˜๊ณ , ๋‹ค๋ฅธ ์ž์‹์€ absolute๋ฅผ ๊ฐ€์ ธ ๊ฒน์น˜๋„๋ก ํ•˜๋ฉด ๋œ๋‹ค.

.container {
  border: 4px solid #a00;
  box-sizing: border-box;
  width: 300px;
  position: relative;
}

.child {
  width: 100%;
  height: 150px;
  background-color: #37e;

  &.child-2 {
    background-color: #3e7;
    opacity: 0;
    position: absolute;
    top: 0;
    left: 0;

    &:hover {
      opacity: 1;
    }
  }
}

์ด๊ฒƒ์€ ์ฃผ๋กœ โ€˜๊ทธ๋ฆผ๊ณผ gif ๋ฅผ ๊ฒน์น˜๊ฑฐ๋‚˜โ€™, โ€˜๊ทธ๋ฆผ๊ณผ ๋™์˜์ƒ์„ ๊ฒน์น  ๋•Œโ€™ ์‚ฌ์šฉ๋œ๋‹ค. ์•„๋ž˜๋Š” ์‚ฌ์šฉ ์˜ˆ์‹œ๋‹ค.

disney
marvel
pixar
star wars
national geographic