How to Maintain the Aspect Ratio of an Element using CSS?

Maintaining the aspect ratio of an element ensures that the element retains its width-to-height proportion regardless of screen size or resizing. This is particularly useful for responsive web design, where you need images, videos, and other elements to scale correctly without distortion.

There are several approaches to maintaining the aspect ratio of an element using CSS which are as follows:

Table of Content

  • Using the Padding Technique
  • Using the aspect-ratio Property
  • Using the object-fit Property

Using the Padding Technique

  • The padding technique leverages the fact that padding percentages in CSS are based on the width of the containing block, allowing you to create a box that scales with a fixed aspect ratio.
  • Create the basic structure of the web page using `<div>` elements in the HTML document file.
  • Set the width of the container to 100% and use padding to set the aspect ratio.
  • Use `position: relative` for the container and `position: absolute` for the content to ensure the content fits within the container.

Example: To demonstrate maintaining the aspect ratio of an element using the padding techinqiue in JavaScript.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width, initial-scale=1.0">
  <title>Padding Technique Aspect Ratio</title>
  <style>
    .aspect-ratio-box {
      position: relative;
      width: 100%;
      padding-top: 56.25%;
      /* 16:9 Aspect Ratio */
      background-color: #428f73;
    }

    .aspect-ratio-box-content {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 70%;
      height: 70%;
      background-color: #a6eebd;
      transform: translate(-50%, -50%);
    }
  </style>
</head>

<body>
  <div class="aspect-ratio-box">
    <div class="aspect-ratio-box-content"></div>
  </div>
</body>

</html>

Output:

The inner content will maintain a 16:9 aspect ratio as the box resizes in all screen sizes.

Using the aspect-ratio Property

  • The CSS aspect-ratio property directly specifies the aspect ratio of an element, making it the simplest method.
  • Create the basic structure of the web page using a `<div>` element in the HTML document file.
  • Apply the `aspect-ratio` property to the element.

Example: To demonstrate maintaining the aspect ratio of an element using the css aspect ratio property.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width, initial-scale=1.0">
  <title>Aspect Ratio Property</title>
  <style>
    .element {
      width: 100%;
      aspect-ratio: 16 / 9;
      background-color: #428f73;
    }
  </style>
</head>
<body>
  <div class="element"></div>
</body>
</html>

Output:

The element will maintain a 16:9 aspect ratio in all screen sizes.

Using the object-fit Property

  • The object-fit property works with images and videos, ensuring they fit within their container without distortion.
  • Create the basic structure of the web page using `<img>` and `<video>` elements in the HTML document file.
  • Apply the `object-fit` property to the media elements.

Example: To demonstrate maintaining the aspect ratio of an element using the css object-fit property.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0">
  <title>Object Fit Property</title>
  <style>
    .image {
      width: 100%;
      height: auto;
      object-fit: cover;
    }
  </style>
</head>
<body>
  <img src=
"https://media.w3wiki.net/wp-content/uploads/20240522090341/Aspect-ratio-16x9svg.png" 
       class="image" 
       alt="Responsive Image">
</body>
</html>

Output:

The image or video will scale to fit the container while maintaining its aspect ratio.