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
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.
img {
aspect-ratio: 16 / 9;
}div.square { aspect-ratio: 1 / 1; width: 200px; background-color: lightblue; }
video {
aspect-ratio: 4 / 3;
width: 100%;
}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.
<!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>
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.
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
aspect-ratio do for an element?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]
- Thinking it changes colors
- Assuming it hides elements
- Confusing with border properties
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]
- Using colon instead of slash
- Adding quotes around ratio
- Using parentheses or commas
div {
width: 320px;
aspect-ratio: 4 / 3;
background-color: lightblue;
}What will be the height of the
div when rendered in the browser?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]
- Dividing height by width instead of width by height
- Using width as height
- Confusing ratio numbers
.box {
width: 200px;
aspect-ratio: 1 / 1;
height: 150px;
background: coral;
}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]
- Thinking syntax is wrong
- Believing background affects ratio
- Assuming width must be %
div that always stays square regardless of screen size. Which CSS setup achieves this best?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]
- Setting fixed height instead of aspect ratio
- Using height:auto which breaks square shape
- Confusing max-height with height
Ready to prove it?
Pass the test and this pattern moves unknown
