How to use CSS Viewport units vw and vh to implement layout that adapts to tablet and mobile screens

PHPz
Release: 2023-09-13 12:26:00
Original
1290 people have browsed it

如何使用 CSS Viewport 单位 vw 和 vh 来实现适应平板和手机屏幕的布局

How to use CSS Viewport units vw and vh to implement layout adapted to tablet and mobile phone screens

When designing responsive web layout, we often need to consider different device screens Size adaptation issue. The CSS Viewport units vw (viewport width) and vh (viewport height) provide a simple way to achieve layout adaptability to tablet and mobile screens.

Viewport units vw and vh are calculated relative to the width and height of the viewport, where 1vw is equal to 1% of the viewport width and 1vh is equal to 1% of the viewport height. By using these units appropriately, we can easily control the size and position of elements in different viewport sizes.

The following will introduce in detail how to use CSS Viewport units vw and vh to implement layout that adapts to tablet and mobile phone screens.

  1. Set basic styles

Before starting the layout, we need to set some basic styles to ensure that the default style of the page adapts to different device screens. First, we can add the following style to the body element:

body { margin: 0; padding: 0; box-sizing: border-box; }
Copy after login

This eliminates the default margins and padding and sets the box model to the border box model.

  1. Use vw and vh units to set the element size

When designing the layout, we need to take into account the size changes of page elements under different view window sizes. At this time, you can use vw and vh units to set the size of the element.

.element { width: 50vw; /* 宽度为视窗宽度的 50% */ height: 30vh; /* 高度为视窗高度的 30% */ }
Copy after login

By setting the width and height to relative unit values, we can ensure that the element maintains appropriate proportions across different viewport sizes.

  1. Set element position using vw and vh units

In addition to setting the size of the element, we also need to consider the position of the element on the page. At this time, you can use vw and vh units to set the position of the element.

.element { position: absolute; left: 50vw; /* 左边距为视窗宽度的 50% */ top: 25vh; /* 上边距为视窗高度的 25% */ }
Copy after login

By setting the left and top properties to relative unit values, we can ensure that the relative position of the element remains unchanged under different viewport sizes.

  1. Use media queries to adjust layout

By using media queries, we can apply different styles or layouts according to different device screen sizes.

For example, when the screen width is less than 768px, we can adjust the size and position of the element:

@media (max-width: 768px) { .element { width: 80vw; height: 25vh; left: 10vw; top: 15vh; } }
Copy after login

In this way, when the screen width is less than 768px, the size and position of the .element element will be according to the media The styles in the query are adjusted.

To sum up, by using CSS Viewport units vw and vh, we can easily achieve layout adaptability to tablet and mobile screens. By setting the size and position of elements to relative unit values and combining media queries to adapt to different device screen sizes, we can ensure that web pages present the best user experience on different devices.

Sample code:

    
Copy after login

The above is the method and sample code on how to use CSS Viewport units vw and vh to implement layout that adapts to tablet and mobile phone screens. I hope it will be helpful to your layout adaptation work!

The above is the detailed content of How to use CSS Viewport units vw and vh to implement layout that adapts to tablet and mobile screens. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!