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 / font-style

CSS / Reference / font-style

font-style은 글자 모양을 정하는 속성으로, 기울임 여부를 정합니다. 기본값 : normal 상속 : Yes 애니메이션 : No 버전 : CSS Level 1

CSS / Reference / font-weight

CSS / Reference / font-weight

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

CSS / Reference / background-color

CSS / Reference / background-color

background-color로 배경의 색을 정합니다. 그 색으로 border와 padding을 포함한 영역을 칠합니다. margin 영역은 칠하지 않습니다. 기본값 : transparent 상속 : No 애니메이션 : Yes 버전 : CSS Level 1

CSS / Reference / word-wrap

CSS / Reference / word-wrap

word-wrap은 띄어쓰기가 없는 긴 단어를 어떻게 처리할지 정하는 속성입니다. 기본값 : normal 상속 : Yes 애니메이션 : No 버전 : CSS Level 3

CSS / Reference / content

CSS / Reference / content

content는 선택한 요소의 앞이나 뒤에 텍스트, 이미지 등을 추가하는 속성입니다. 기본값 : normal 상속 : No 애니메이션 : No 버전 : CSS Level 2

CSS / Reference / background-position

CSS / Reference / background-position

background-position은 배경 이미지의 위치를 정하는 속성입니다. 기본값 : 0% 0% 상속 : No 애니메이션 : Yes 버전 : CSS Level 1

CSS / Reference / font-size

CSS / Reference / font-size

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

CSS / Reference / text-indent

CSS / Reference / text-indent

들여쓰기와 내어쓰기는 text-indent 속성으로 만듭니다. 값이 양수이면 들여쓰기, 값이 음수이면 내어쓰기가 됩니다. 내어쓰기를 할 때는 왼쪽에 여백을 적절히 줍니다.

CSS / Reference / background-origin

CSS / Reference / background-origin

HTML 요소는 박스로 이루어져 있고, 바깥 여백 영역(Margin Area), 테두리 영역(Border Area), 안쪽 여백 영역(Padding Area), 내용 영역(Content Area)으로 구분합니다. background-origin으로 배경 이미지를 어느 영역부터 채워나갈지를 정합니다. 기본값 : padding-box 상속 : No 애니메이션 : No 버전 : CSS Level 3

CSS / Reference / box-shadow

CSS / Reference / box-shadow

box-shadow는 선택한 요소에 그림자 효과를 만들어주는 속성입니다. 기본값 : none 상속 : No 애니메이션 : Yes 버전 : CSS Level 3 글자에 그림자 효과를 주고 싶다면 text-shadow 속성을 사용합니다.