Home Web Front-end CSS Tutorial How to use CSS Viewport units vw and vmin to adapt layout to different screen widths

How to use CSS Viewport units vw and vmin to adapt layout to different screen widths

Sep 13, 2023 am 10:58 AM
css viewport vw unit vmin unit

如何使用 CSS Viewport 单位 vw 和 vmin 来实现适应不同屏幕宽度布局

How to use CSS Viewport units vw and vmin to implement layouts that adapt to different screen widths

With the popularity of mobile devices, responsive design has become the standard for today's web design one. Ensuring good presentation of web content becomes particularly important across different screen sizes and resolutions. The CSS Viewport units vw (viewport width unit) and vmin (the smaller unit of viewport width and height) can help us achieve the goal of adapting layout to different screen widths.

Use vw units for layout
Viewport (viewport) refers to the area where the user actually browses the web page in the browser. The vw unit is relative to the viewport width, with 1vw equal to 1% of the viewport width. By using vw units we can resize elements based on the width of the viewport.

For example, we can set the width of an element to half the width of the viewport:

.element {
  width: 50vw;
}
Copy after login

With a viewport width of 1000px, the width of the element will be 500px.

Use vmin units for layout
In addition to vw units, we can also use vmin units to adjust the size of elements based on the smaller of the width and height of the viewport. This is useful for implementing square or elements with a fixed aspect ratio.

For example, we can set the width and height of an element to 20% of the smaller of the viewport width and height:

.element {
  width: 20vmin;
  height: 20vmin;
}
Copy after login

If the viewport width is 1000px and the height is 800px , then the element's width and height will both be 160px.

Example of using vw and vmin units to implement layout that adapts to different screen widths
The following is an example showing how to use vw and vmin units to implement layout that adapts to different screen widths:

<!DOCTYPE html>
<html>
<head>
  <style>
    .container {
      display: flex;
      justify-content: space-between;
    }
    
    .box {
      width: 20vmin;
      height: 20vmin;
      background-color: orange;
      margin: 5px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
</body>
</html>
Copy after login

In In the above example, we used a container div and placed four box divs in a row with 5px spacing between them. The width and height of the box are both 20% of the smaller of the viewport width and height. This means that the boxes will fit proportionally regardless of screen width.

Summary
CSS Viewport units vw and vmin can help us achieve the goal of adapting to different screen width layouts when designing web pages. By using these units, we can adjust the size and layout of elements based on the dimensions of the viewport, enabling responsive design. Whether the user is using a large screen monitor, a laptop or a mobile device, we ensure that your web content looks beautiful and well presented.

Note: When using vw and vmin units, you need to consider the compatibility issues of different browsers to ensure correct display on various devices.

The above is the detailed content of How to use CSS Viewport units vw and vmin to adapt layout to different screen widths. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Apr 17, 2025 am 10:55 AM

In this week&#039;s roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

The Deal with the Section Element The Deal with the Section Element Apr 12, 2025 am 11:39 AM

Two articles published the exact same day:

How We Tagged Google Fonts and Created goofonts.com How We Tagged Google Fonts and Created goofonts.com Apr 12, 2025 pm 12:02 PM

GooFonts is a side project signed by a developer-wife and a designer-husband, both of them big fans of typography. We’ve been tagging Google

Some Hands-On with the HTML Dialog Element Some Hands-On with the HTML Dialog Element Apr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I&#039;ve been aware of it for a while, but haven&#039;t taken it for a spin yet. It has some pretty cool and

Multi-Thumb Sliders: General Case Multi-Thumb Sliders: General Case Apr 12, 2025 am 10:52 AM

The first part of this two-part series detailed how we can get a two-thumb slider. Now we&#039;ll look at a general multi-thumb case, but with a different and

Where should 'Subscribe to Podcast' link to? Where should 'Subscribe to Podcast' link to? Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

See all articles