-
Setting the height of a hero section to fill the screen:
.hero { height: 100vh; /* or 100dvh, 100lvh, 100svh */ width: 100vw; background-image: url('hero-image.jpg'); background-size: cover; }In this example, the
.heroelement will always fill the entire viewport, regardless of the screen size. Using100dvhensures that the hero section adapts correctly even when the address bar on mobile devices is visible. -
Creating a full-screen modal:
.modal { position: fixed; top: 0; left: 0; width: 100vw; height: 100dvh; /* or 100svh */ background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; }Here, the
.modalelement covers the entire screen, and using100dvhensures it adapts to the dynamic viewport height, preventing any content from being obscured by the address bar. -
Setting the font size to scale with the viewport width:
h1 { font-size: 5vw; text-align: center; }In this case, the
font-sizeof theh1element will scale proportionally with the viewport width, making it larger on wider screens and smaller on narrower screens. This ensures that the heading remains readable and visually appealing on all devices. -
Creating a responsive grid layout:
.grid-container { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 2vw; }Here, the
gapbetween the grid items is set to2vw, which means it will scale proportionally with the viewport width. This ensures that the spacing between the grid items remains consistent across different screen sizes. - Address Bar Behavior: On mobile devices, the address bar can appear and disappear, affecting the actual visible viewport height. Use
dvh,lvh, andsvhto handle these dynamic changes. - Zooming: Users can zoom in and out, which can affect the size of the viewport. Be mindful of how zooming impacts your layouts and adjust your styles accordingly.
- Initial-Scale: The
initial-scaleproperty in the viewport meta tag can affect the initial size of the viewport. Ensure that it is set correctly to avoid any unexpected sizing issues. - Testing: Always test your designs on a variety of devices and browsers to ensure they behave as expected.
Hey guys! Let's dive into the fascinating world of CSS units, specifically focusing on vh, dvh, lvh, svh, and vw. These units are incredibly useful for creating responsive designs that adapt to different screen sizes and devices. Understanding how they work will give you a serious edge in front-end development, allowing you to build layouts that look great no matter where they're viewed. So, grab your favorite code editor, and let's get started!
What are CSS vh, dvh, lvh, svh, and vw Units?
When we talk about responsive web design, viewport units in CSS are essential. These units allow you to size elements relative to the viewport, which is the visible area of the user's screen. The primary viewport units are vh (viewport height) and vw (viewport width). However, with the introduction of mobile devices and their unique behaviors, especially how the address bar interacts with the screen, new units like dvh, lvh, and svh have emerged to provide more precise control over sizing.
vh (Viewport Height)
The vh unit represents 1% of the viewport's height. This means that if your viewport is 800 pixels tall, 1vh equals 8 pixels. Using vh is fantastic for creating elements that take up a specific portion of the screen height, regardless of the device. For example, you might want a full-screen hero section that always fills the entire visible height without requiring scrolling. You can achieve this by setting the height of the hero element to 100vh. However, keep in mind that the standard vh doesn't account for the dynamic nature of mobile browser UI, such as the address bar, which can lead to issues where content gets obscured or layouts break when the address bar appears or disappears. Understanding how viewport height affects layout is crucial. The vh unit is widely supported across modern browsers, making it a reliable choice for many responsive design scenarios. When using vh, it’s important to test your designs on various devices to ensure they behave as expected, especially considering the variations in viewport sizes and browser behaviors.
For instance, setting a div’s height to 50vh will make it half the height of the viewport. This unit is especially useful for creating full-screen layouts or sections that always occupy a consistent portion of the screen. Using vh can simplify your CSS and make your designs more adaptable. However, be mindful of how the address bar on mobile browsers can affect the actual visible area, which is where the newer units like dvh, lvh, and svh come into play to offer more accurate control.
vw (Viewport Width)
Similar to vh, the vw unit represents 1% of the viewport's width. If your viewport is 1200 pixels wide, 1vw equals 12 pixels. The vw unit is perfect for creating elements that scale proportionally with the screen width. For example, you can create a heading that always takes up a certain percentage of the screen width, ensuring it remains readable and visually appealing on different devices. By setting the font size to, say, 5vw, the text will scale up or down as the screen size changes. However, just like vh, the standard vw unit doesn't account for the dynamic UI elements on mobile browsers. It’s important to consider how the viewport width impacts element sizing when using vw.
vw is also useful for creating horizontal scrolling sections or galleries where the width of each item is relative to the viewport width. For example, setting the width of a gallery item to 80vw will make it occupy 80% of the screen width. This can create a visually engaging experience, especially when combined with CSS scroll snapping. Understanding the nuances of vw can greatly enhance your ability to create responsive and dynamic layouts. The support for vw is excellent across modern browsers, making it a dependable unit for responsive web design. When implementing vw, always test your designs on various screen sizes to ensure elements scale appropriately and maintain their intended appearance.
dvh (Dynamic Viewport Height)
The dvh unit represents 1% of the dynamic viewport height. The dynamic viewport takes into account the presence or absence of dynamic browser UI elements like the address bar on mobile devices. This means that dvh adjusts as the address bar appears or disappears, providing a more stable and accurate representation of the visible screen height. This unit is incredibly useful for ensuring that your layouts don't get obscured by the address bar or have unexpected gaps when the address bar retracts. When the address bar is visible, the dynamic viewport height is smaller; when it's hidden, the dynamic viewport height is larger. Understanding dynamic viewport changes is crucial for creating a seamless mobile experience.
Using dvh ensures that elements that are supposed to fill the screen, such as full-screen modals or hero sections, adapt correctly to the changing viewport height. For example, setting the height of a modal to 100dvh will make it fill the entire visible screen, regardless of whether the address bar is visible or not. This prevents content from being cut off or having unnecessary whitespace. While dvh is a relatively new unit, it is becoming increasingly supported in modern browsers, making it a valuable tool for responsive design. When using dvh, it’s essential to test your designs on mobile devices to see how they respond to the dynamic changes in viewport height. This will help you fine-tune your layouts and ensure a consistent user experience.
lvh (Large Viewport Height)
The lvh unit represents 1% of the largest possible viewport size. The largest viewport is determined when the browser UI elements (like the address bar) are fully retracted or hidden. This unit is useful when you want to ensure that an element always covers the maximum possible height of the screen, even if the address bar is likely to appear at some point. Using lvh can be beneficial for creating immersive experiences or layouts where you want to avoid any potential gaps or sizing issues caused by dynamic UI elements. For instance, if you have a full-screen image or video background, using 100lvh will ensure it always covers the entire screen, providing a consistent visual experience. The large viewport offers a consistent sizing reference, especially when dealing with elements that need to cover the full screen.
However, it's important to note that lvh might result in elements being slightly larger than the actual visible area when the address bar is visible. This can sometimes lead to content being slightly cut off or requiring scrolling. Therefore, it's crucial to test your designs thoroughly on various devices to ensure they behave as expected. Support for lvh is growing, but it may not be universally available across all browsers, so always check compatibility before relying on it. When implementing lvh, consider the potential for content overflow and adjust your layout accordingly to maintain a seamless user experience.
svh (Small Viewport Height)
The svh unit represents 1% of the smallest possible viewport size. The smallest viewport is determined when all dynamic browser UI elements (like the address bar) are fully visible. This unit is particularly useful when you want to ensure that an element always fits within the visible screen area, even when the address bar is present. Using svh can help prevent content from being obscured or cut off by the address bar, providing a more consistent and user-friendly experience. For example, if you have a fixed-position element at the bottom of the screen, using 100svh will ensure it always remains visible, even when the address bar is fully displayed. The small viewport ensures content is always visible, regardless of the browser UI state.
svh is especially useful for creating layouts where you want to guarantee that all content is accessible without requiring users to scroll excessively. It ensures that elements sized with svh will never exceed the visible screen height, even in the most constrained viewport conditions. While svh is gaining traction, it's essential to check browser compatibility before using it extensively. As with other viewport units, thorough testing on different devices is crucial to ensure your designs behave as expected. When using svh, consider how it interacts with other layout elements and adjust your styles accordingly to achieve the desired visual outcome.
How to Use These Units in CSS
Using vh, dvh, lvh, svh, and vw in CSS is straightforward. You simply specify the unit after the numerical value, just like you would with pixels (px) or percentages (%). Here are a few examples to illustrate how these units can be used in practice:
Browser Support and Considerations
While vh and vw have excellent browser support, the newer units like dvh, lvh, and svh are still gaining traction. It's essential to check the compatibility of these units on websites like Can I use before implementing them in your projects. Additionally, always test your designs on various devices and browsers to ensure they behave as expected.
When using viewport units, keep the following considerations in mind:
Conclusion
Viewport units like vh, dvh, lvh, svh, and vw are powerful tools for creating responsive designs that adapt to different screen sizes and devices. Understanding how these units work and how to use them effectively can greatly enhance your front-end development skills. While vh and vw provide basic viewport-relative sizing, the newer units like dvh, lvh, and svh offer more precise control over sizing in dynamic environments, especially on mobile devices. By leveraging these units and keeping browser support and considerations in mind, you can build layouts that look great no matter where they're viewed. So go ahead, experiment with these units, and take your responsive designs to the next level!
Lastest News
-
-
Related News
Used Sedan Reliability Ratings In 2023: Which Cars To Trust
Alex Braham - Nov 14, 2025 59 Views -
Related News
Popular Basketball & Volleyball Players
Alex Braham - Nov 9, 2025 39 Views -
Related News
Utah NHL Vs New Jersey: A New Era On Ice
Alex Braham - Nov 9, 2025 40 Views -
Related News
Nasi Lemak Ice Cream: A Delicious Dive In
Alex Braham - Nov 16, 2025 41 Views -
Related News
Choline: Manfaat, Dosis, Efek Samping, Dan Penggunaannya
Alex Braham - Nov 9, 2025 56 Views