To address the issue at hand, a media query combined with some mathematical calculations can provide a solution. Below is an approach tailored for a portrait orientation:
@media (max-device-aspect-ratio: 3/4) {
height: calc(100vw * 1.333 - 9%);
}
@media (max-device-aspect-ratio: 2/3) {
height: calc(100vw * 1.5 - 9%);
}
@media (max-device-aspect-ratio: 10/16) {
height: calc(100vw * 1.6 - 9%);
}
@media (max-device-aspect-ratio: 9/16) {
height: calc(100vw * 1.778 - 9%);
}
When the url bar disappears and affects the viewport's vertical height (vh), an alternative method must be devised to determine the correct height. By leveraging the constant width of the viewport and the limited range of mobile device aspect ratios, one can calculate the precise vh value using the following steps:
1) Establish a set of media queries targeting specific aspect ratios.
Utilize 'device-aspect-ratio' instead of 'aspect-ratio' to account for url bar disappearance.
Incorporate 'max' in the device-aspect-ratio to cover any unforeseen aspect ratios between popular ones. Though less exact, it will still yield satisfactory results for most users.
Remember to adjust the numbers when switching from landscape to portrait orientations.
2) For each media query, multiply the desired percentage of vertical height in vw by the inverse of the aspect ratio.
- Calculate the product of the desired percentage (e.g., 100%) and the height-to-width ratio to obtain the correct vh value.
3) Account for the impact of the url bar height by subtracting it from the calculated height. While exact measurements may vary, using 9% as a standard for mobile devices in landscape orientation generally yields satisfactory outcomes.
While this solution may not be the most elegant, other alternatives such as encountering user bugs, incorrectly sized elements, or resorting to unnecessary JavaScript for basic styling are far less desirable. Despite potential variations in url bar heights among devices, adopting this methodology ensures a consistent experience compared to indiscriminate resizing during swiping interactions across all platforms.
For convenience, a SASS mixin has been created to streamline the process:
@mixin vh-fix {
@media (max-device-aspect-ratio: 3/4) {
height: calc(100vw * 1.333 - 9%);
}
@media (max-device-aspect-ratio: 2/3) {
height: calc(100vw * 1.5 - 9%);
}
@media (max-device-aspect-ratio: 10/16) {
height: calc(100vw * 1.6 - 9%);
}
@media (max-device-aspect-ratio: 9/16) {
height: calc(100vw * 1.778 - 9%);
}
}