Maintaining Aspect Ratios for HTML Videos with CSS

Maintaining aspect ratios for HTML videos is crucial for responsive design. CSS provides two main approaches: using the padding-top property with percentage values or the modern aspect-ratio property to ensure videos display correctly across different screen sizes.

Syntax

/* Method 1: Using padding-top */
.container {
    padding-top: percentage;
    height: 0;
    position: relative;
}

/* Method 2: Using aspect-ratio property */
.container {
    aspect-ratio: width / height;
}

Method 1: Using Padding-Top Property

The padding-top method works by setting the container's height to 0 and using percentage-based padding to create the desired aspect ratio. The percentage is calculated as (height / width) × 100.

Aspect Ratio 16:9

For a 16:9 aspect ratio, calculate 9/16 = 0.5625 = 56.25% −

<!DOCTYPE html>
<html>
<head>
<style>
    .video-container {
        margin: 20px auto;
        width: 80%;
        padding-top: 56.25%; /* 16:9 aspect ratio */
        height: 0;
        position: relative;
        background-color: #f0f0f0;
        border: 2px solid #ddd;
    }
    
    .video-placeholder {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: #333;
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 18px;
    }
</style>
</head>
<body>
    <h3>16:9 Aspect Ratio Video Container</h3>
    <div class="video-container">
        <div class="video-placeholder">Video Content (16:9)</div>
    </div>
</body>
</html>
A responsive container with 16:9 aspect ratio appears, maintaining its proportions when the browser is resized. The container shows "Video Content (16:9)" centered inside a dark rectangle.

Aspect Ratio 4:3

For a 4:3 aspect ratio, calculate 3/4 = 0.75 = 75% −

<!DOCTYPE html>
<html>
<head>
<style>
    .video-container-4-3 {
        margin: 20px auto;
        width: 60%;
        padding-top: 75%; /* 4:3 aspect ratio */
        height: 0;
        position: relative;
        background-color: #e8f4f8;
        border: 2px solid #4CAF50;
    }
    
    .video-placeholder {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: #2196F3;
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 16px;
    }
</style>
</head>
<body>
    <h3>4:3 Aspect Ratio Video Container</h3>
    <div class="video-container-4-3">
        <div class="video-placeholder">Video Content (4:3)</div>
    </div>
</body>
</html>
A responsive container with 4:3 aspect ratio appears with a blue background. The container maintains its square-like proportions and displays "Video Content (4:3)" in white text.

Method 2: Using Aspect-Ratio Property

The modern aspect-ratio property provides a cleaner approach to maintain aspect ratios without the padding-top hack −

<!DOCTYPE html>
<html>
<head>
<style>
    .modern-video-container {
        margin: 20px auto;
        width: 70%;
        aspect-ratio: 16 / 9;
        background-color: #ffebee;
        border: 2px solid #e91e63;
        position: relative;
    }
    
    .video-content {
        width: 100%;
        height: 100%;
        background-color: #9c27b0;
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 18px;
    }
</style>
</head>
<body>
    <h3>Modern Aspect Ratio with aspect-ratio Property</h3>
    <div class="modern-video-container">
        <div class="video-content">16:9 Video Using aspect-ratio</div>
    </div>
</body>
</html>
A purple container with 16:9 aspect ratio created using the modern aspect-ratio property. The text "16:9 Video Using aspect-ratio" is centered in white within the purple background.

Browser Support

The aspect-ratio property has excellent modern browser support but may need fallbacks for older browsers. The padding-top method works in all browsers as a reliable fallback.

Conclusion

Both methods effectively maintain video aspect ratios. Use aspect-ratio for modern browsers and simpler code, or padding-top for broader compatibility. Choose the approach that best fits your project's browser support requirements.

Updated on: 2026-03-15T15:30:08+05:30

624 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements