Aspect ratio in CSS refers to an element’s proportional relationship between its width and height.

Two of the most common video aspect ratios are 4:3, which is the universal video format of the 20th century, and 16:9, which is the default for HD television and European digital television format such as YouTube.

What aspect ratio does is that it preserves an element’s initial proportions so that it can scale nicely across different media sizes. We call this type of feature or behaviour responsiveness.

Responsiveness means that elements are both flexible and keep their aspect ratios when resized.

Implement Aspect Ratio in CSS

For us to implement aspect ratio in CSS, we must use the padding hack feature.

What this means is that we must set a padding-top or padding-bottom property on an element with the calculated percentage value that represents the aspect ratio.

How to Calculate Aspect Ratio

For us to calculate the value for the padding property, we have to divide the ratio values and multiply the result by 100%.

So that means …

  • 16:9 aspect ratio will be 9 / 16 * 100% = 56.25%
  • 4:3 aspect ratio will be 3 / 4 * 100% = 75%
  • 1:1 aspect ratio will be 1 / 1 * 100% = 100%

Now, we apply this to our CSS.

  .aspect-ratio--16-9 {
      overflow: hidden;
      padding-top: 56.25%;
      background: red;
      }

Result

16:9 Aspect Ratio

We can do the same thing for videos as well …

We’ll have a div container with a video element

<div class="video-container">
  <video class="video" src="Sheep.mp4" controls />
</div>

We’ll add the following CSS

.video-container {
  position: relative;
  padding-top: 56.25%;
}

.video {
  position: absolute;
  top: 0;
  left:0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Result

Video by Matthias Groeneveld from Pixabay

CSS Aspect Ratio Property

Instead of using the padding hack, we can effectively use the aspect-ratio property, and it does exactly what padding-top or padding-bottom does.

.aspect-ratio-container {
  aspect-ratio: 16 / 9;
}

The box’s preferred aspect ratio is the specified ratio of width / height. If height and the preceding slash character are omitted, height defaults to 1.

Conclusion

Using the aspect ratio property in your web or software development design allows for responsiveness.

It will always scale along with the user’s viewport and maintain a consistent aspect ratio across all browsers.

Recent Posts

Instagram Extends Reels Duration to 3 Minutes

Regardless of whether TikTok faces a U.S. ban, Instagram is wasting no time positioning itself…

1 day ago

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

5 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

7 days ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

1 week ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

1 week ago

Why Netflix Dumped React For its Frontend

Netflix, a pioneer in the streaming industry, has always been at the forefront of adopting…

1 week ago