CSS: Fixed Aspect Ratio
Problem Description
- You want a box of fixed aspect ratio of 4:3.
- However, you want the box size to be larger when the screen is larger.
How to specify this in CSS?
Solution
The box above has aspect ratio of 4/3, while the width is 25% of window size.
Try resize window to see it change.
Here is the CSS
.fixratio50249 { width: 25vw; height: calc( 25vw * 0.75); border: solid thick red; }
The vw is a CSS unit that means 1 percent of viewport width.
[see CSS: Length Units]
calc is used to compute 75 percent of 25vw
[see CSS: calc]
CSS, misc, advanced
- CSS: Case Sensitivity
- CSS: Declare Charset
- CSS: Comment Syntax
- CSS: Default Unit
- CSS: Computed Style
- CSS: Pseudo Element
- CSS: Pseudo Class
- CSS: Specificity
- CSS: Data URI Scheme
- HTML: Protocol-Relative URL
- CSS: Browser Default Style Sheet (2025-12)