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.

Official answer available

Preview the opening of the answer, then unlock the full walkthrough.

Candidate: "That's a great question, especially considering the importance of responsive design in today's multi-device world. To achieve a fixed aspect ratio for an element, regardless of the viewport size, one effective method involves using the CSS property padding-top or padding-bottom on a container element. Let me explain how this works and why it's a reliable choice for maintaining an aspect ratio."

"Firstly, the aspect ratio of an element is the ratio between its width and height. For example, a 16:9 aspect ratio is common for videos, meaning the width is 16 units wide while the height is 9 units tall. To maintain this ratio as the viewport changes, we can leverage the way CSS calculates padding."...

Related Questions