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-weight

CSS / Reference / font-weight

font-weight 속성은 CSS에서 글꼴의 굵기를 지정하는 데 사용됩니다. 텍스트의 가독성을 높이거나 디자인의 강조점을 표현할 때 유용합니다. 기본값 : normal 상속 : 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 / overflow

CSS / Reference / overflow

overflow는 내용이 요소의 크기를 벗어났을 때 어떻게 처리할지를 정하는 속성입니다. 기본값 : visible 상속 : No 애니메이션 : No 버전 : CSS Level 2

CSS / Reference / background-position

CSS / Reference / background-position

background-position은 배경 이미지의 위치를 정하는 속성입니다. 기본값 : 0% 0% 상속 : No 애니메이션 : 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 / empty-cells

CSS / Reference / empty-cells

empty-cells은 표(table)에서 빈 셀의 테두리를 표시할지 말지는 정하는 속성입니다. 기본값 : show 상속 : Yes 애니메이션 : No 버전 : CSS Level 2

CSS / Reference / word-break

CSS / Reference / word-break

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

CSS / Reference / color

CSS / Reference / color

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

CSS / Reference / opacity

CSS / Reference / opacity

opacity로 요소의 투명도를 정할 수 있습니다. 기본값 : 1 상속 : No 애니메이션 : Yes 버전 : CSS Level 3

CSS / Reference / accent-color

CSS / Reference / accent-color

accent-color는 HTML의 폼 요소에 사용되는 강조 색상을 정하는 속성입니다. 기본적으로 브라우저는 사용자 인터페이스 요소에 특정한 시스템 색상을 사용하지만, accent-color를 사용하면 이러한 요소들의 강조 색상을 커스터마이즈할 수 있습니다.