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 / aspect-ratio

CSS / Reference / aspect-ratio

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

CSS / Reference / background-size

CSS / Reference / background-size

background-size는 배경 이미지의 가로 크기와 세로 크기를 정하는 속성입니다. 기본값 : auto 상속 : No 애니메이션 : Yes 버전 : CSS Level 3

CSS / Reference / text-align

CSS / Reference / text-align

text-align은 문단 정렬 방식을 정하는 속성입니다. 기본값 : 읽는 방향이 LTR(왼쪽에서 오른쪽)인 경우 left, RTL(오른쪽에서 왼쪽)인 경우 right 상속 : Yes 애니메이션 : No 버전 : CSS Level 1

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 / list-style-position

CSS / Reference / list-style-position

list-style-position으로 ul, ol 등의 목록의 마커(marker)의 위치를 정합니다. 기본값 : outside 상속 : Yes 애니메이션 : No 버전 : CSS Level 1

CSS / Reference / background-image

CSS / Reference / background-image

background-image는 이미지를 배경으로 사용하게 하는 속성입니다. 기본값 : none 상속 : No 애니메이션 : No 버전 : CSS Level 1

CSS / Reference / calc()

CSS / Reference / calc()

calc()는 괄호 안의 식을 계산한 결과를 속성값으로 사용하게 해주는 함수입니다. 예를 들어 다음은 는 글자 크기를 20px로 설정합니다. font-size: calc( 10px + 10px );

CSS / Reference / content

CSS / Reference / content

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

CSS / Reference / font-family

CSS / Reference / font-family

font-family는 글꼴을 설정하는 속성입니다. 기본값 : 웹브라우저의 기본 글꼴 상속 : Yes 애니메이션 : No 버전 : CSS Level 1

CSS / Reference / text-shadow

CSS / Reference / text-shadow

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