My objective is to set an aspect ratio (for example, 4:3) for a DIV and all its children with the styles WIDTH:100% and HEIGHT:100%. Initially, this method works well by setting the parent's
WIDTH:100%
and then adding
PADDING-BOTTOM: 75%; // (3/4)*100
to the first child element. However, when I maximize the window on a 1080p monitor, the inner box will correctly expand to 100% width and 75% of the width in height, resulting in a 1920x1440 resolution. As a result, scrollbars are required to view the complete content within the div.
I would prefer the inner box to be equivalent to (window.innerHeight / 3) * 4 in width with black bars on either side. To achieve this, I tried setting the height on the parent and adding padding-right to the child, expecting black bars on the right instead of the bottom, but unfortunately, it didn't work as expected.
In an ideal scenario, I want my div to maintain a 4:3 aspect ratio regardless of the parent's dimensions, with black bars if necessary, without exceeding the parent's size and causing scrollbars. Can this be achieved solely with CSS, or will JavaScript be required?
Edit:
Using
<div style="width:133vmin;height:100vmin;margin-left:-66vmin;left:50%;background: #f00; overflow:hidden;">
I managed to create a div with a 4:3 aspect ratio and black bars on the sides. However, if the height is insufficient, the div does not resize accordingly. It seems that I need to combine both approaches for a comprehensive solution.