Maintain Aspect Ratio of Div While Filling Screen Width and Height in CSS
To maintain the aspect ratio of a div while filling the screen's width and height, we can utilize the CSS viewport units vw (viewport width) and vh (viewport height). This approach ensures that the element will always fill the maximum viewport size without breaking the ratio or creating scrollbars.
FIDDLE
Pure CSS
div { width: 100vw; height: 56.25vw; /* height:width ratio = 9/16 = .5625 */ background: pink; max-height: 100vh; max-width: 177.78vh; /* 16/9 = 1.778 */ margin: auto; position: absolute; top:0;bottom:0; /* vertical center */ left:0;right:0; /* horizontal center */ }
This CSS ensures that the div:
The above is the detailed content of How to Maintain a Div's Aspect Ratio While Filling the Entire Screen in CSS?. For more information, please follow other related articles on the PHP Chinese website!