How to use css3's new units vw, vh, vmin, and vmax

php中世界最好的语言
Release: 2018-01-30 10:25:35
Original
2330 people have browsed it

This time I will bring you how to use the new css3 units vw, vh, vmin, and vmax. What are theprecautionsfor using the new css3 units vw, vh, vmin, and vmax. Here are Let’s take a look at practical cases.

This article introduces the detailed explanation of the use of css3 new units vw, vh, vmin, and vmax, and shares it with everyone. The details are as follows:

1, the meaning of vw, vh, vmin, and vmax

(1) vw, vh, vmin, and vmax are window units and relative units. It is not relative to the parent node or the root node of the page. It is determined by the size of the viewport. The unit is 1, which means something like 1%.
The viewport is the area where your browser actually displays content—in other words, your web browser without toolbars and buttons.

(2) The specific description is as follows:

vw: the percentage of the window width (1vw represents the width of the window is 1%)
vh: the percentage of the window height
vmin: current The smaller value of vw and vh
vmax: The larger value of the current vw and vh

2, the difference between vw, vh and % percentage

(1)% It is the ratio set relative to the size of the parent element, vw and vh are determined by the size of the window.
(2) The advantage of vw and vh is that they can directly obtain the height. However, using % cannot correctly obtain the height of thevisual areawithout setting the body height, so this is quite good. The advantages.

3, Uses of vmin and vmax

When developing mobile pages, if you use vw and wh to set the font size (such as 5vw), the font size displayed in portrait and landscape modes is Different.

Since vmin and vmax are the current smaller vw and vh and the current larger vw and vh. Here you can use vmin and vmax. Make the text size consistent in both horizontal and vertical screens.

4, Browser compatibility

(1) Desktop PC

Chrome: Perfectly supported since version 26 (February 2013)
Firefox: Since Perfectly supported since version 19 (January 2013)
Safari: Perfectly supported since version 6.1 (October 2013)
Opera: Perfectly supported since version 15 (July 2013)
IE: Since IE10 (including Edge) it is only partially supported (vmax is not supported, and vm replaces vmin)

(2) Mobile devices

Android: Since version 4.4 Perfect support (December 2013)
iOS: Perfect support since iOS8 version (September 2014)

2. A simple example

1, page code

In addition to setting the width and height of elements, the Viewport unit can also be used in text. Next, use vw to set the font size to implement responsive text.

    hangge.com  

宽度100%, 字体5%

宽度80%, 字体5%

宽度50%, 高度50%, 字体1%

Copy after login

3. Mask layer to achieve complete coverage

Sometimes in order to highlight the pop-up box or prevent page elements from being clicked. We need a translucent mask that covers the entire visible area, which can be easily achieved using vw and vh.

1, sample code

    hangge.com  
Copy after login

4. Implementing a centered pop-up box

1, the size of the pop-up box adapts to the content

(1) Sample rendering

After clicking the pop-up button, a pop-up box will be displayed centered on the entire screen.

The size of the pop-up box is adaptive according to the size of the content (logo picture). At the same time, there is a translucent mask layer behind the pop-up box that covers the entire screen.

After clicking the close button, the pop-up box will be hidden.

(2) Sample code

The mask layer uses vw and vh to achieve full-screen coverage. The popover is added to the mask layer and centered.

    hangge.com   
居中弹出框
关闭
Copy after login

2, the size of the pop-up box changes with the size of the window

(1) Sample rendering

After clicking the pop-up button, a pop-up box will be displayed centered on the entire screen Pop-up box.

The size of the pop-up box is no longer determined by the size of the content, but changes with the size of the window (both width and height are 80% of the visible area of the screen).

After clicking the close button, the pop-up box will be hidden.

(2) Sample code

The mask layer uses vw and vh to achieve full-screen coverage. The size and position of the pop-up box are also set using vw and vh.

    hangge.com   
居中弹出框
关闭
Copy after login

5. Limit the maximum size when displaying large images

We can also limit the maximum width or height of some elements through the view unit to avoid being too large and exceeding the screen.

(1) Click the button to display a large image of the original image in the center of the screen.
(2) If the original width and height of the image do not exceed 90% of the screen width and height, the default size of the image will be displayed.
(3) If the original width and height of the image exceed 90% of the screen width and height, it will be limited to 90% of the screen so that it can be fully displayed.

I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

html5 How to create a circle animation effect of pictures

How to use H5’s WebGL in the same The interface makes json and echarts charts

How to use the new semantic tag features of H5

The above is the detailed content of How to use css3's new units vw, vh, vmin, and vmax. 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!