CSS / Reference / aspect-ratio

개요

  • aspect-ratio는 선택한 요소의 가로 세로 비율을 정하는 속성입니다.
  • 접속하는 기기의 해상도가 변해도, 일정한 가로 세로 비율을 유지하고 싶을 때 유용하게 사용할 수 있습니다.

문법

aspect-ratio: auto | number / number | initial | inherit
  • auto : 요소가 고유의 가로 세로 비율을 가진 경우 그 값으로 설정합니다.
  • number / number : 가로 대 세로. 세로가 생략된 경우 1로 처리합니다.
  • initial : 기본값으로 설정합니다.
  • inherit : 부모 요소의 속성값을 상속 받습니다.

예제 - 고유 가로 세로 비율이 없는 요소

  • div 요소의 가로 크기를 600px로 정하고 가로 세로 비율을 4:3으로 합니다. 즉, 세로 크기는 300px가 됩니다.
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>CSS</title>
    <style type="text/css">
      div {
        background-color: #e3f2fd;
        width: 600px;
        aspect-ratio: 4 / 3;
      }
    </style>
  </head>
  <body>
    <div></div>
  </body>
</html>

  • div 요소의 세로 크기를 300px로 정하고 가로 세로 비율을 4:3으로 합니다. 즉, 가로 크기는 400px가 됩니다.
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>CSS</title>
    <style type="text/css">
      div {
        background-color: #e3f2fd;
        height: 300px;
        aspect-ratio: 4 / 3;
      }
    </style>
  </head>
  <body>
    <div></div>
  </body>
</html>

  • div 요소의 가로 크기를 600px로 정하고 가로 세로 비율을 4로 하면, 세로는 1로 처리합니다. 즉, 4:1이므로 세로 크기는 150px가 됩니다.
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>CSS</title>
    <style type="text/css">
      div {
        background-color: #e3f2fd;
        width: 600px;
        aspect-ratio: 4;
      }
    </style>
  </head>
  <body>
    <div></div>
  </body>
</html>

  • div 요소의 가로 크기와 세로 크기를 정하지 않으면, 가로 크기 100%로 작동합니다.
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>CSS</title>
    <style type="text/css">
      div {
        background-color: #e3f2fd;
        aspect-ratio: 16 / 9;
      }
    </style>
  </head>
  <body>
    <div></div>
  </body>
</html>

예제 - 고유 가로 세로 비율이 있는 요소

  • 이미지와 같이 고유 가로 세로 비율이 있는 요소에서 가로 크기 또는 세로 크기를 지정하지 않으면 aspect-ratio가 적용되지 않습니다.
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>CSS</title>
    <style type="text/css">
      img {
        aspect-ratio: 4 / 1;
      }
    </style>
  </head>
  <body>
    <img src="images/square-400x400.png" alt="">
  </body>
</html>

  • 가로 크기 또는 세로 크기를 설정해야 aspect-ratio가 적용됩니다.
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>CSS</title>
    <style type="text/css">
      img {
        width: 100%;
        aspect-ratio: 4 / 1;
      }
    </style>
  </head>
  <body>
    <img src="images/square-400x400.png" alt="">
  </body>
</html>

같은 카테고리의 다른 글
CSS / Reference / white-space

CSS / Reference / white-space

white-space는 스페이스와 탭, 줄바꿈, 자동줄바꿈을 어떻게 처리할지 정하는 속성입니다. 기본값 : normal 상속 : Yes 애니메이션 : No 버전 : CSS Level 1

CSS / Reference / word-break

CSS / Reference / word-break

word-break는 줄바꿈을 할 때 단어 기준으로 할 지 글자 기준으로 할 지 정하는 속성입니다. 기본값 : normal 상속 : Yes 애니메이션 : No 버전 : CSS Level 3

CSS / Reference / line-height

CSS / Reference / line-height

line-height 속성은 텍스트 줄 간격(줄 높이)을 조절하는 데 사용됩니다. 텍스트의 가독성을 높이고, 디자인적으로 더 일관된 레이아웃을 만들기 위해 자주 사용됩니다. 기본값 : normal 상속 : Yes 애니메이션 : Yes 버전 : CSS Level 1

CSS / Reference / background-repeat

CSS / Reference / background-repeat

background-repeat는 배경 이미지의 반복 여부와 반복 방향을 정하는 속성입니다. 기본값 : repeat 상속 : No 애니메이션 : No 버전 : CSS Level 1

CSS / Reference / font-size

CSS / Reference / font-size

font-size는 글자 크기를 정하는 속성입니다. 기본값 : medium 상속 : Yes 애니메이션 : Yes 버전 : CSS Level 1

CSS / Reference / border-width

CSS / Reference / border-width

border-width은 테두리(border)의 두께를 정하는 속성으로, 다음 속성의 단축 속성입니다. border-top-width border-right-width border-bottom-width border-left-width

CSS / Reference / align-content

CSS / Reference / align-content

flex-wrap 속성의 값이 wrap인 경우, 아이템들의 가로폭의 합이 콘테이너의 가로폭을 넘어가면 아이템이 다음 줄로 내려갑니다. 이때 여러 줄이 되어버린 아이템들의 정렬을 어떻게 할지 정하는 속성이 align-content입니다.

CSS / Reference / font-weight

CSS / Reference / font-weight

font-weight 속성은 CSS에서 글꼴의 굵기를 지정하는 데 사용됩니다. 텍스트의 가독성을 높이거나 디자인의 강조점을 표현할 때 유용합니다. 기본값 : normal 상속 : Yes 애니메이션 : Yes 버전 : CSS Level 1

CSS / Reference / text-shadow

CSS / Reference / text-shadow

text-shadow는 글자에 그림자 효과를 주는 속성입니다. 기본값 : none 상속 : Yes 애니메이션 : Yes 버전 : CSS Level 3 박스에 그림자 효과를 주고 싶다면 box-shadow 속성을 사용합니다.

CSS / Reference / color

CSS / Reference / color

color로 텍스트의 색을 정합니다. 상속 : Yes 애니메이션 : Yes 버전 : CSS Level 1