The Most Secure Cross Browser Testing Platform since 2012

Cross Browser Viewport units: vw, vh, vmin, vmax

BLOG / BrowseEmAll / Cross Browser Testing

Cross Browser Viewport units: vw, vh, vmin, vmax

Viewport units are one of the cornerstones of responsive design, allowing us to create dynamic measurements based on the size of the screen. Units like vw, vh, vmin, and vmax help ensure that our designs appear proportionate and consistent across all devices. However, especially on mobile browsers, the behavior of these units doesn’t always match our expectations. Cross-browser inconsistencies—particularly those related to the vh unit and how some user interface elements are included or excluded from the calculation—are important details that designers and developers need to be aware of. In this article, we’ll explore what viewport units are, how they behave differently across browsers, and how to deal with those differences effectively.

What Are Viewport Units?

Viewport units are special measurement units in CSS that are calculated based on the visible area of the browser window. The vw (viewport width) unit represents 1% of the viewport’s width, while the vh (viewport height) unit represents 1% of the viewport’s height. In addition, vmin is calculated as 1% of the smaller of the width or height, and vmax as 1% of the larger. These units allow element sizes to adjust proportionally regardless of the screen size. Because they offer flexibility in responsive design, viewport units are frequently used in mobile-friendly interfaces. However, their behavior can vary between devices and browsers, so they should be used with care.

Browser Support and Compatibility

Viewport units (vw, vh, vmin, vmax) are now supported by all major modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. While they generally work consistently across desktop browsers, there can be some compatibility issues on mobile browsers—particularly with the vh unit. For instance, when the address bar is visible on mobile devices, 100vh might not actually cover the full height of the screen, which can negatively affect the user experience. That’s why it’s crucial to test how these units behave across different browsers and devices, and to implement alternative solutions when necessary. Taking browser compatibility into account is especially important for mobile-friendly projects to ensure a smoother and more reliable design.

vh Issues on Mobile Browsers

The vh (viewport height) unit on mobile browsers can cause problems, especially when the address bar is visible. Mobile browsers dynamically adjust the screen size as they show and hide the address bar and other UI elements. As a result, 100vh usually doesn’t cover the entire screen height, because browser UI elements are included in the calculation. This leads to the page content appearing shorter than expected, causing layout issues. To overcome this, it may be necessary to calculate the height dynamically using JavaScript or use custom CSS variables like –vh to correctly adjust the viewport height.

Modern CSS Viewport Units: svh, lvh, dvh

Modern CSS introduces new viewport units like svh, lvh, and dvh, aiming to provide more accurate and consistent viewport calculations. These new units were developed to solve the issues faced by the traditional vh unit.

  • svh (small viewport height): Calculates based on the smallest height of the screen, using the lowest height considering the browser UI elements.
  • lvh (large viewport height): Calculates based on the largest height of the screen, typically the height during fullscreen mode.
  • dvh (dynamic viewport height): Represents the dynamically changing screen height, such as when the address bar is visible or hidden, reflecting the current screen height.

These units allow for more flexible and accurate measurements, especially on mobile browsers. However, as these new units are not yet fully supported by all browsers, it’s important to conduct compatibility testing.

Best Practices for Using Viewport Units

Paying attention to best practices when using viewport units is crucial for ensuring that designs look consistent and smooth across all devices. First, it’s best to use viewport units for fundamental sizing, such as for width, height, and font sizes. However, when working with vh and vw units, it’s important to consider the impact of browser UI elements, such as the address bar, on mobile devices. By performing dynamic height calculations when using the vh unit, you can improve the user experience, especially on mobile. Additionally, it’s important to test the browser compatibility of viewport units, as some older browsers or specific mobile browsers may not fully support them. To achieve the best results, it’s recommended to combine viewport units with media queries and other CSS properties to create a flexible and adaptable design.

Framework Support and Tools

Modern CSS viewport units (vw, vh, vmin, vmax) are supported by many popular front-end frameworks, such as Bootstrap, Foundation, and Tailwind CSS, allowing developers to easily incorporate responsive design principles into their projects. These frameworks often provide built-in utilities and components that leverage viewport units for fluid layouts and scaling. Additionally, CSS preprocessors like Sass or Less can be used to manage viewport unit values dynamically, making the design more flexible and easier to maintain. For testing and optimizing designs, tools like BrowserStack, Sauce Labs, and Chrome DevTools can simulate how viewport units behave across different browsers and devices, helping developers ensure compatibility and performance. Using these tools in conjunction with responsive design frameworks ensures a seamless user experience across a wide range of screen sizes.

In conclusion, viewport units provide great flexibility when creating responsive designs and offer a consistent user experience across different screen sizes. However, while these units are widely supported by modern browsers, compatibility issues and browser differences should be taken into account. Especially on mobile browsers, attention should be paid to potential behavior discrepancies, and dynamic solutions should be implemented when necessary. When used correctly, viewport units simplify the design process and ensure that projects work seamlessly across all devices. Therefore, to achieve the best results, it is crucial to test browser compatibility, use the right tools, and adhere to best practices.