Style Sheet/CSS

๐ŸŽจ Aspect Ratio - ์ด๋ฏธ์ง€ ํฌ๊ธฐ ๋น„์œจ ๊ฐ„๋‹จ ์ œ์–ด

์ธํŒŒ_ 2021. 10. 19. 10:55

css-๋น„์œจ

Aspect Ratio ์ข…ํšก๋น„ ์†์„ฑ

๊ณผ๊ฑฐ์—๋Š” css์—์„œ ๋น„์œจ๋กœ ์˜์—ญ์„ ์กฐ์ ˆํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” css ํ”„๋กœํผํ‹ฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋ž˜์„œ ์žˆ๋Š” ์†์„ฑ์œผ๋กœ ํŽธ๋ฒ•์œผ๋กœ ํ•˜์—ฌ ์Šคํฌ๋ฆฐ ๋น„์œจ์„ ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

๊ทธ๋ ‡์ง€๋งŒ ์ด์ œ css์—์„œ ์ตœ์‹  ๊ธฐ๋Šฅ์œผ๋กœ ๋“œ๋””์–ด aspect-ratio ๋ผ๋Š” ์ข…ํšก๋น„ ์ „์šฉ ์†์„ฑ์„ ์ง€์›ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

aspect ratio ์†์„ฑ์€ ์ด๋ฏธ์ง€๋‚˜ ๋™์˜์ƒ์„ ๋น„์œจ๋Œ€๋กœ ์ค„์ด๊ฑฐ๋‚˜ ๋Š˜๋ฆฌ๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ๋งค์šฐ ์ง๊ด€์ ์ด๋ผ ๋‹ค๋ฃจ๊ธฐ๋„ ํŽธํ•˜๋‹ค.

๋‹ค๋งŒ ์šฐ๋ฆฌ์˜ ์ฃผ์  IE ๋ธŒ๋ผ์šฐ์ €์—๋Š” ์ง€์›์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ณผ๊ฑฐ์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋˜ ๊ฐ„์ ‘์ ์œผ๋กœ ๋น„์œจ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋”๋ถˆ์–ด, aspect ratio ์†์„ฑ ์‚ฌ์šฉ๋ฒ•์„ ์†Œ๊ฐœํ•  ์˜ˆ์ •์ด๋‹ค.

css-aspect-ratio
๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ


๊ณผ๊ฑฐ ์ข…ํšก๋น„ ์กฐ์ ˆ๋ฒ•

 

1. ๋‹จ์ˆœ ์ง€์ •

  • ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ์ง์ ‘ width์™€ height๋ฅผ ์ง€์ •ํ•ด์„œ ํ•˜๋Š” ๋ฒ•
  • ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ• ์‹œ ๋ฐ˜์‘ํ˜•์„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์• ๋กœ์‚ฌํ•ญ์ด ์žˆ๋‹ค.
/* ๋…ธ๋ž€์ƒ‰ ๋ฐ•์Šค css ๊ฐ’ */
width: 200px;
height: 100px;

See the Pen aspect-ratio-1 by barzz12 (@inpaSkyrim) on CodePen.

 

2. padding์„ ์‚ฌ์šฉํ•˜์—ฌ ๋น„์œจ ์กฐ์ •

  • padding-top ๋˜๋Š” padding-bottom์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
  • padding์˜ ๊ธฐ์ค€์„ ๋ถ€๋ชจ์˜ width๋กœ ๊ฐ€์ง€๊ธฐ ์œ„ํ•ด์„œ ๋ถ€๋ชจ์˜ container ์—˜๋ฆฌ๋จผํŠธ ์š”์†Œ๊ฐ€ ํ•„์š”
  • ๊ทธ ์™ธ์—๋Š” 50%์˜ ๋น„์œจ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด padding ๊ฐ’์„ 50%๋กœ ์„ค์ •
<div class="container-padding">
  <div class="inner"></div>
</div>
.container-padding {
  width: 200px; /* ๊ฐ€๋กœ๊ธธ์ด๋งŒ ์„ ์–ธํ•œ๋‹ค. ์ฆ‰ height๋Š” 0์ธ ์…ˆ์ด๋‹ค */
}

.inner {
  background-color: orange;
  padding-top: 50%; /* ๋ถ€๋ชจheight๊ฐ€ 0์ธ ์ƒํƒœ์—์„œ ์ž์‹์—์„œ padding-top์„ ์„ ์–ธํ•˜๋ฉด, ๋ฐ”๋กœ ์ด๊ฐ’์ด ๋†’์ด๊ฐ€ ๋˜๊ฒŒ ๋œ๋‹ค */
}

See the Pen aspect-ratio-2 by barzz12 (@inpaSkyrim) on CodePen.

 

3. calc()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋น„์œจ ์กฐ์ •

  • calc() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ด์™ธ์˜ ๋น„์œจ๋กœ๋„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฐ˜์‘ํ˜•์„ ์œ„ํ•ด์„œ calc ์ฒซ๋ฒˆ์งธ ์ธ์ž๋ฅผ 200px ๊ณ ์ •๊ฐ’์œผ๋กœ ์ฃผ๋Š”๊ฒƒ์ด ์•„๋‹Œ 100%๋กœ ์„ค์ •ํ•œ๋‹ค. (width๋Š” ์ž์‹์ด ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์ƒ์† ๋ฐ›๊ธฐ ๋•Œ๋ฌธ)
.inner {
  padding-top: calc(100% * 0.3);  /* 30% ์ ์šฉํ•˜๊ธฐ */
}

.inner {
  padding-top: calc(100% / 16 * 9); /* 16:9 ์ ์šฉ */
}

See the Pen aspect-ratio-3 by barzz12 (@inpaSkyrim) on CodePen.


์ตœ์‹  ์ข…ํšก๋น„ ์กฐ์ ˆ๋ฒ• (aspect-ratio)

css-aspect-ratio

  • ์œ„์™€ ๊ฐ™์€ ์˜›๋‚ ์‹์˜ ๋ฐฉ๋ฒ•์€, ์ง๊ด€์ ์ด์ง€๋„ ์•Š๊ณ  ๋ณต์žกํ•˜๋‹ค.
  • ๋”ฐ๋ผ์„œ css์ตœ์‹  ๊ธฐ๋Šฅ์œผ๋กœ ์ถ”๊ฐ€๋œ aspect-ratio ์†์„ฑ์œผ๋กœ ์—˜๋ฆฌ๋จผํŠธ์˜ ๋น„์œจ์„ ํ•œ๋ฐฉ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • aspect-ratio: 16 / 9 ํ•œ์ค„๋งŒ ๋„ฃ์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ 16:9 ๋น„์œจ์˜ div๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค.
  • ๋งŒ์•ฝ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์ง€์ •ํ•  ๊ฒฝ์šฐ, ๋†’์ด๋ฅผ 1์˜ ๋น„์œจ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.
  • div ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ด๋ฏธ์ง€, ํ…Œ์ด๋ธ” ๋“ฑ ๋ชจ๋“  ์—˜๋ฆฌ๋จผํŠธ์— ์ข…ํšก๋น„ ์ ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
.box { 
  width: 300px; /* ์š”์†Œ ๊ฐ€๋กœ๊ธธ์ด ์ง€์ • */
  background: #cccccc; /* ์š”์†Œ ๋ฐฐ๊ฒฝ์ƒ‰ */
  
  aspect-ratio: 16 / 9; /* 16:9 ๋น„์œจ ์ง€์ • */
}

See the Pen aspect-ratio-4 by barzz12 (@inpaSkyrim) on CodePen.

 

์ข…ํšก๋น„ auto ์ง€์ •

  • ์š”์†Œ๊ฐ€ ๊ณ ์œ ํ•œ ์ข…ํšก๋น„๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ auto๋กœ ํ•ด๋‹น ์ข…ํšก๋น„๋ฅผ ๋”ฐ๋ฅธ๋‹ค.
  • ์š”์†Œ๊ฐ€ ๊ณ ์œ ํ•œ ์ข…ํšก๋น„๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ง€์ •ํ•œ ๋น„์œจ ์†์„ฑ๊ฐ’์— ๋”ฐ๋ฅธ๋‹ค.
div {
	/* ๊ณ ์œ ํ•œ ์ข…ํšก๋น„๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ์ข…ํšก๋น„๋ฅผ ๋”ฐ๋ฅด๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ 1 : 1 ๋น„์œจ๋กœ ์ง€์ • */
	aspect-ratio: auto 1 / 1; 
}

See the Pen aspect-ratio-6 by barzz12 (@inpaSkyrim) on CodePen.

 

์ด๋ฏธ์ง€ ์ข…ํšก๋น„ ๊ฐ•์ œ ์ง€์ •

  • ์ด๋ฏธ์ง€๊ฐ€ ๋Š˜์–ด๋‚˜๊ฑฐ๋‚˜ ์ค„์–ด๋“ค์ง€ ์•Š๊ณ , ์ด๋ฏธ์ง€ ์ž์ฒด๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๋ฉฐ ์ข…ํšก๋น„๋งŒ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ๋จผ์ € ์ด๋ฏธ์ง€๋ฅผ ๋ถ€๋ชจ ์š”์†Œ๋กœ ๊ฐ์‹ธ๊ณ , ๋ถ€๋ชจ ์š”์†Œ์— aspect-ratio๋ฅผ ์ง€์ •ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋ฏธ์ง€์— object-fit: cover ์†์„ฑ์„ ์ ์šฉํ•œ๋‹ค.

See the Pen aspect-ratio-7 by barzz12 (@inpaSkyrim) on CodePen.

 

aspect-ratio ์ ์šฉ ์˜ˆ์™ธ ์ƒํ™ฉ

  1. ๋„ˆ๋น„, ๋†’์ด๊ฐ’์ด ์ง€์ •๋  ๊ฒฝ์šฐ ์ข…ํšก๋น„๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.
  2. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ min-width, min-height ์š”์†Œ๊ฐ’์ด ์ ์šฉ๋œ ๊ฒฝ์šฐ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.
  3. ๋‚ด์šฉ์ด ์š”์†Œ๋ฅผ ๋„˜๋Š” ๊ฒฝ์šฐ ์š”์†Œ๋Š” ํ™•์žฅ๋˜๋Š”๋ฐ ์ด ๊ฒฝ์šฐ์—๋Š” ์ข…ํšก๋น„๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.

See the Pen aspect-ratio-5 by barzz12 (@inpaSkyrim) on CodePen.


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

https://runebook.dev/ko/docs/css/aspect-ratio

https://webisfree.com/2019-03-11/css-%EB%B0%B1%EA%B7%B8%EB%9D%BC%EC%9A%B4%EB%93%9C-%EC%9D%B4%EB%AF%B8%EC%A7%80-%EB%B0%8F-%EC%97%98%EB%A6%AC%EB%A8%BC%ED%8A%B8-%EB%B9%84%EC%9C%A8%EB%A1%9C-%EC%A1%B0%EC%A0%88%ED%95%98%EA%B8%B0-aspect-ratio

https://mong-blog.tistory.com/entry/css-Aspect-Ratio-%EC%9A%94%EC%86%8C%EB%A5%BC-%EB%B9%84%EC%9C%A8%EB%8C%80%EB%A1%9C-%EC%A1%B0%EC%A0%95%ED%95%98%EA%B8%B0