How can you achieve a fixed aspect ratio for a responsive element?

Instruction: Describe a method to maintain an element's aspect ratio as the viewport size changes.

Context: This question tests the candidate's ability to implement responsive design techniques for maintaining aspect ratios of elements.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

Use aspect-ratio with one automatic dimension. For a responsive 16:9 presentation area, let its width follow the available space and derive its height from that width.

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS example</title> <style> .frame { width: 100%; max-width: 640px; aspect-ratio: 16 / 9; position: relative; background: #dde9f3; } .frame > span { position: absolute; inset: 0; display: grid; place-items: center; } </style>...

Related Questions