Aspect ratio helps keep the shape of an element consistent, no matter the screen size. It stops images or boxes from looking stretched or squished.
Aspect ratio in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
selector {
aspect-ratio: width / height;
}The aspect-ratio property takes a ratio like 16 / 9 or 1 / 1.
This ratio controls the width to height proportion of the element.
Examples
CSS
img {
aspect-ratio: 16 / 9;
}CSS
div.square { aspect-ratio: 1 / 1; width: 200px; background-color: lightblue; }
CSS
video {
aspect-ratio: 4 / 3;
width: 100%;
}Sample Program
This example shows a coral colored box that stays in a 16:9 shape. The width is 300px, and the height adjusts automatically to keep the shape.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Aspect Ratio Example</title> <style> .box { aspect-ratio: 16 / 9; width: 300px; background-color: coral; color: white; display: flex; align-items: center; justify-content: center; font-family: Arial, sans-serif; font-size: 1.5rem; border-radius: 0.5rem; margin: 1rem auto; text-align: center; } </style> </head> <body> <div class="box">This box keeps a 16:9 shape</div> </body> </html>
Important Notes
If you set only width or height, the other dimension adjusts to keep the aspect ratio.
Aspect ratio works well with images, videos, and divs for consistent shapes.
Older browsers may not support aspect-ratio, so check compatibility if needed.
Summary
Aspect ratio keeps elements from stretching or squishing.
Use aspect-ratio: width / height; to set the shape.
It helps make responsive designs look neat and consistent.
Practice
1. What does the CSS property
aspect-ratio do for an element?easy
Solution
Step 1: Understand the purpose of
The property controls the ratio between width and height to keep the shape consistent.aspect-ratioStep 2: Identify the effect on element shape
It prevents the element from stretching or squishing by maintaining the ratio.Final Answer:
It keeps the element's width and height in a fixed ratio to avoid stretching. -> Option AQuick Check:
Aspect ratio = fixed width/height ratio [OK]
Hint: Aspect ratio locks shape, not color or visibility [OK]
Common Mistakes:
- Thinking it changes colors
- Assuming it hides elements
- Confusing with border properties
2. Which of the following is the correct syntax to set an aspect ratio of 16:9 in CSS?
easy
Solution
Step 1: Recall the correct syntax format
Theaspect-ratioproperty uses a ratio with a slash between numbers, no quotes or commas.Step 2: Match the correct option
aspect-ratio: 16 / 9; uses16 / 9which is the correct way to write the ratio.Final Answer:
aspect-ratio: 16 / 9; -> Option DQuick Check:
Use slash between numbers for aspect ratio [OK]
Hint: Use slash (/) between numbers, no quotes or commas [OK]
Common Mistakes:
- Using colon instead of slash
- Adding quotes around ratio
- Using parentheses or commas
3. Given this CSS code:
What will be the height of the
div {
width: 320px;
aspect-ratio: 4 / 3;
background-color: lightblue;
}What will be the height of the
div when rendered in the browser?medium
Solution
Step 1: Understand aspect ratio formula
The aspect ratio 4 / 3 means width divided by height equals 4/3.Step 2: Calculate height from width
Height = Width ÷ (4/3) = 320 ÷ (4/3) = 320 x (3/4) = 240px.Final Answer:
240px -> Option BQuick Check:
Height = width x (height/width) = 240px [OK]
Hint: Height = width x (height ÷ width) from ratio [OK]
Common Mistakes:
- Dividing height by width instead of width by height
- Using width as height
- Confusing ratio numbers
4. What is wrong with this CSS code if the element does not keep the expected 1:1 aspect ratio?
.box {
width: 200px;
aspect-ratio: 1 / 1;
height: 150px;
background: coral;
}medium
Solution
Step 1: Identify conflicting properties
The CSS sets bothwidthandheightexplicitly, which conflicts withaspect-ratio.Step 2: Understand aspect-ratio behavior
When height is fixed, the aspect ratio cannot adjust height automatically, so the shape distorts.Final Answer:
The fixed height overrides the aspect ratio, causing distortion. -> Option AQuick Check:
Fixed height blocks aspect ratio adjustment [OK]
Hint: Avoid fixed height when using aspect-ratio [OK]
Common Mistakes:
- Thinking syntax is wrong
- Believing background affects ratio
- Assuming width must be %
5. You want a responsive square
div that always stays square regardless of screen size. Which CSS setup achieves this best?hard
Solution
Step 1: Understand responsive sizing with aspect ratio
Usingwidth: 50vw;sets width to half the viewport width, andaspect-ratio: 1 / 1;keeps height equal to width, making a square.Step 2: Compare other options
width: 50vw; height: auto; usesheight: auto;, which depends on content. width: 50vw; height: 50vh; usesheight: 50vh;, half viewport height, so not square on widescreen monitors. width: 50vw; max-height: 50vw; usesmax-height: 50vw;, which caps but doesn't enforce square.Final Answer:
width: 50vw; aspect-ratio: 1 / 1; -> Option CQuick Check:
Aspect ratio + responsive width = perfect square [OK]
Hint: Use aspect-ratio with responsive width for perfect squares [OK]
Common Mistakes:
- Setting fixed height instead of aspect ratio
- Using height:auto which breaks square shape
- Confusing max-height with height
