4,094 Views

Viewport units in CSS are a handy way to create responsive designs that adjust to different screen sizes. These units—vh, vw, vmin, and vmax—are key for making fluid layouts that look great on any device. Let's break down these units and see how they work, along with some tips and tricks.

Understanding Viewport Units

Viewport units in CSS are relative measurements in CSS based on the size of the viewport, which is the visible part of a webpage on a screen. Here's a quick rundown of the four main viewport units:

  • vh (viewport height): 1vh equals 1% of the viewport height.
  • vw (viewport width): 1vw equals 1% of the viewport width.
  • vmin (viewport minimum): 1vmin equals 1% of the smaller dimension of the viewport (either height or width).
  • vmax (viewport maximum): 1vmax equals 1% of the larger dimension of the viewport (either height or width).

vh: Viewport Height

CSS Viewport Units vh, vw, vmin, and vmax Key Insights

The vh unit is great for setting sizes relative to the height of the viewport. For example, if you set an element’s height to 100vh, it'll take up the entire height of the viewport no matter what size screen you're using. This is perfect for full-screen backgrounds and hero sections.

.hero-section {
  height: 100vh;
  background-color: #3498db;
  display: flex;
  align-items: center;
  justify-content: center;
}

Here, the .hero-section class makes sure the element fills the entire height of the viewport, creating a striking and adaptable design.

vw: Viewport Width

The vw unit is ideal for setting widths based on the viewport width. It helps create layouts that smoothly adapt to different screen sizes. For example, setting an element’s width to 50vw will make it cover half the width of the viewport.

.banner {
  width: 50vw;
  background-color: #2ecc71;
  text-align: center;
  padding: 20px;
}

In this example, the .banner class ensures the element takes up half of the viewport width, keeping the design responsive as the viewport changes.

vmin: Viewport Minimum

CSS Viewport Units vh, vw, vmin, and vmax Key Insights

The vmin unit is based on the smaller dimension of the viewport, which is handy when you want an element to scale according to the smallest side. For instance, setting an element’s size to 10vmin makes it 10% of the viewport’s smallest dimension.

.square {
  width: 10vmin;
  height: 10vmin;
  background-color: #e74c3c;
}

Here, the .square class makes sure the element stays a square no matter the viewport size, making it versatile for responsive designs.

vmax: Viewport Maximum

The vmax unit is based on the larger dimension of the viewport. It's useful for making an element scale according to the largest side, offering flexibility in its size. For example, setting an element’s size to 10vmax makes it 10% of the viewport’s largest dimension.

.rectangle {
  width: 10vmax;
  height: 5vmax;
  background-color: #f39c12;
}

In this example, the .rectangle class ensures the element scales with the larger dimension of the viewport, maintaining its responsiveness across different devices.

Practical Uses of Viewport Units

Viewport units are super handy for making web designs flexible and responsive. Let's check out some common ways to use these units and some tips to make the most of them:

Creating Full-Screen Sections

Using vh units to make full-screen sections is a trendy move in web design. This way, each section takes up the whole height of the viewport, giving a smooth and engaging look.

CSS Viewport Units vh, vw, vmin, and vmax Key Insights

.section {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #34495e;
  color: white;
}

In this setup, the .section class ensures the section fills the screen height, making it visually appealing and immersive.

Responsive Typography

You can use viewport units for responsive text, so the font size changes with the screen size. This keeps text readable and looks good on any device.

.responsive-text {
  font-size: 3vw;
  color: #2c3e50;
}

Here, the .responsive-text class sets the font size to 3vw, making the text scale with the viewport width.

Dynamic Padding and Margins

Using viewport units for padding and margins helps keep spacing consistent across different screens. This keeps your layout neat and organized.

.container {
  padding: 5vh 5vw;
  margin: 2vmin 2vmax;
  background-color: #1abc9c;
  color: white;
}

In this example, the .container class uses vh and vw for padding, and vmin and vmax for margins, ensuring balanced spacing.

Background Images

Setting background images with viewport units makes sure they adjust to the viewport size. This keeps your background images looking good on all devices.

CSS Viewport Units vh, vw, vmin, and vmax Key Insights

.background-image {
  background-image: url('path/to/image.jpg');
  background-size: cover;
  height: 100vh;
}

With this .background-image class, the background image covers the entire viewport, creating a seamless visual effect.

Key Takeaways

Introduction to Viewport Units

Viewport units are flexible length units in CSS that change based on the browser's viewport size. They include vh, vw, vmin, and vmax, and make it easy to do things like set an element’s height to match the browser window.

Practical Uses of Viewport Units

Viewport units are great for various tasks like:

  • Creating full-screen backgrounds
  • Perfectly fitting headlines
  • Centering elements

They help your web pages adjust smoothly to different screen sizes, making your design more responsive.

Example Values

Here’s how viewport units work in different situations:

  • If the viewport is 1200px wide and 1000px high:

    • 10vw = 120px
    • 10vh = 100px
    • 10vmax = 120px (because width is larger)
    • 10vmin = 100px (because height is smaller)
  • If you rotate the device to 1000px wide and 1200px high:

    • 10vh = 120px
    • 10vw = 100px
    • 10vmax = 120px (now based on height)
    • 10vmin = 100px
  • If you resize the browser to 1000px wide and 800px high:

    • 10vh = 80px
    • 10vw = 100px
    • 10vmax = 100px
    • 10vmin = 80px

Comparing with Percentage Units

Viewport units might seem like percentages but they're quite different. Percentages set the size relative to the parent element, while viewport units set it based on the viewport.

Example:

<div class="parent">
  <div class="child-percent">Percent</div>
  <div class="child-vw">VW</div>
</div><br />
.parent {
  width: 500px;
  height: 500px;
}
.child-percent {
  width: 80%;
  background-color: lightblue;
}
.child-vw {
  width: 80vw;
  background-color: lightcoral;
}

In this example, the first child’s width is 80% of its parent’s width. The second child’s width is 80vw, making it wider than its parent.

Centering Elements with Viewport Units

To center an element on the screen using viewport units, set the top and bottom margins to half the leftover space after accounting for the element's height:

.centered {
  width: 60vw;
  height: 70vh;
  margin: 15vh auto;
}

Though nowadays, using Flexbox or CSS Grid is more common for centering elements.

Things to Keep in Mind with Viewport Units

  • Browser Scrollbars: Setting an element’s width with viewport units can cause issues if the browser has scrollbars. They aren’t taken into account, making elements slightly wider than expected. Use percentages for widths to avoid this problem.
div {
  height: 50vh;
  width: 50vw;
  float: left;
}
  • Mobile Devices: On mobile, the address bar’s presence can change the viewport height, causing content to jump. New viewport units like svw, svh, lvw, lvh, dvw, and dvh help deal with this by accounting for the dynamic nature of mobile viewports.

Conclusion

Viewport units (vh, vw, vmin, and vmax) are powerful for making responsive web designs. They help your design adapt to different screen sizes, whether you’re making full-screen sections, responsive text, or dynamic spacing.

Remember to test your designs on various devices and browsers to ensure they work well for everyone. By using viewport units wisely, you can create web designs that look great and work smoothly.