Demo
Sometimes I create a square (or any rectangle, really) that will respect its ratio of any size, using a method similar to this method .
What I want:
- to prevent a square extending outside the viewing area on devices with low height, i.e. a mobile phone in landscape orientation.
Proposed solution
- limit the width of the square to a percentage of the height of the viewport using
max-width: 90vh - attitude is expected
CSS
.square { position: relative; height: 0; padding-bottom: 100%; overflow: hidden; } .square-inner { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .mw { max-width: 90vh;}
HTML
<div class="square mw"> <div class="square-inner"></div> </div>
What is going to happen
- in viewports with a low height, the square should be a maximum width of 90% of the viewing height.
What is actually going on:
- when the height of the viewport is less than the width of the square:
- Width is limited by
vh Height - calculated by the width of the square, if it was not limited to
vh - we get a vertically long rectangle
The specification states that the relative value is calculated from the "containing block", which for me seems to be the current width of the container.
browser behavior:
- Chrome 29.0.1547.65: as described
- Firefox 23.01: as described
Opera: does not respect vh at all. Not tested with Opera 16 +
Am I interpreting the specification incorrectly or is this a possible error in the implementation by browsers?
Larry source share