Home Web Front-end CSS Tutorial About the use of css3 units vw and vh

About the use of css3 units vw and vh

Jun 20, 2018 am 11:44 AM
css3 vh vw

This article mainly introduces the usage tutorial of css3 new units vw and vh. This article introduces the meaning of vw, vh, vmin, vmax and the difference between vw, vh and % percentage through example code. Friends who are interested can join us. Let’s take a look

For responsive layout units, we will first think of using rem units to implement adaptation, but it also needs to embed a script to dynamically calculate and element size.

For example:

(function (doc, win) {
  let docEl = doc.documentElement
  let resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'
  let recalc = function () {
    var clientWidth = docEl.clientWidth
    if (!clientWidth) return
    docEl.style.fontSize = 14 * (clientWidth / 320) + 'px'
  }
  if (!doc.addEventListener) return
  win.addEventListener(resizeEvt, recalc, false)
  doc.addEventListener('DOMContentLoaded', recalc, false)
})(document, window)
Copy after login

Is there a unit that does not require JS and CSS to be coupled together? The answer is yes, it is vw/vh.

vw = view width
vh = view height
Copy after login

These two units were introduced in CSS3. The above called viewport units allow us to define the size closer to the browser window.

The meaning of vw, vh, vmin, vmax

(1) vw, vh, vmin, vmax is a window unit and a relative unit. 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 as 1%)

  • vh: The percentage of the window height

  • vmin: The smaller of the current vw and vh

  • vmax: The current vw and vh The larger value in vh

The difference between vw, vh and % percentage

(1)% is the size relative to the parent element The set ratios, vw and vh, are determined by the window size.

(2) The advantage of vw and vh is that they can directly obtain the height. However, using % cannot correctly obtain the height of the visible area without setting the body height, so this is a good advantage.

Use of vmin and vmax

When doing mobile page development, if you use vw and wh to set the font size (such as 5vw), in the vertical screen The font size displayed in landscape mode 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.

Browser Compatibility

(1) Desktop PC

  • Chrome: since version 26 Perfectly supported since version 19 (February 2013)

  • Firefox: Perfectly supported since version 19 (January 2013)

  • Safari : Fully supported since version 6.1 (October 2013)

  • Opera : Fully 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 since (December 2013)

iOS: Perfect support since iOS8 version (September 2014)

How to use viewport unit adaptation Page

Only use vw as CSS unit

1. Convert to vw unit according to the size of the design draft (SASS function compilation)

//iPhone 6尺寸作为设计稿基准
$vm_base: 375;
@function vm($px) {
    @return ($px / 375) * 100vw;
}
Copy after login

2. Use vw

< p class="mod_nav">
            < nav class="mod_nav_list" v-for="n in 5">
                < a href="#" class="mod_nav_list_item">
                    < span class="mod_nav_list_item_logo">
                < img src="http://jdc.jd.com/img/80">
                    < /span>
                    < p class="mod_nav_list_item_name">导航入口< /p>
                < /a>
            < /nav>
< /p>
.mod_nav {
    background: #fff;
    &_list {
        display: flex;
        padding: vm(15) vm(10) vm(10);
        &_item {
            flex: 1;
            text-align: center;
            font-size: vm(10);
            &_logo {
                display: block;
                margin: 0 auto;
                width: vm(40);
                height: vm(40);
                img {
                    display: block;
                    margin: 0 auto;
                    max-width: 100%;
                }
            }
            &_name {
                margin-top: vm(2);
            }
        }
    }
}
Copy after login

for both text and layout height, width, spacing, etc. The best practice is to match vw and rem

Using vm as the css unit does reduce the amount of code a lot, but you will find that it is implemented using the viewport unit, automatically scaling depending on the viewport size, and losing the maximum and minimum width restrictions.

So, I thought it would be better to combine the rem unit to realize the layout? The core of rem flexible layout is to dynamically change the size of the root element. Then we can set the vw unit for the size of the root element that changes with the change of the viewport through:

, so that its size can be dynamically changed.

Limit the maximum and minimum font size of the root element, and use the body plus the maximum width and minimum width

In this way, we can achieve the maximum and minimum restrictions on the layout width. Therefore, based on the above conditions, we can conclude that the code implementation is as follows:

// rem 单位换算:定为 75px 只是方便运算,750px-75px、640-64px、1080px-108px,如此类推
$vm_fontsize: 75; // iPhone 6尺寸的根元素大小基准值
@function rem($px) {
     @return ($px / $vm_fontsize ) * 1rem;
}
// 根元素大小使用 vw 单位
$vm_design: 750;
html {
    font-size: ($vm_fontsize / ($vm_design / 2)) * 100vw;
    // 同时,通过Media Queries 限制根元素最大最小值
    @media screen and (max-width: 320px) {
        font-size: 64px;
    }
    @media screen and (min-width: 540px) {
        font-size: 108px;
    }
}
// body 也增加最大最小宽度限制,避免默认100%宽度的 block 元素跟随 body 而过大过小
body {
    max-width: 540px;
    min-width: 320px;
}
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

关于CSS代码如何书写规范

The above is the detailed content of About the use of css3 units vw and vh. 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 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)

How to achieve wave effect with pure CSS3? (code example) How to achieve wave effect with pure CSS3? (code example) Jun 28, 2022 pm 01:39 PM

How to achieve wave effect with pure CSS3? This article will introduce to you how to use SVG and CSS animation to create wave effects. I hope it will be helpful to you!

Use CSS skillfully to realize various strange-shaped buttons (with code) Use CSS skillfully to realize various strange-shaped buttons (with code) Jul 19, 2022 am 11:28 AM

This article will show you how to use CSS to easily realize various weird-shaped buttons that appear frequently. I hope it will be helpful to you!

How to hide elements in css without taking up space How to hide elements in css without taking up space Jun 01, 2022 pm 07:15 PM

Two methods: 1. Using the display attribute, just add the "display:none;" style to the element. 2. Use the position and top attributes to set the absolute positioning of the element to hide the element. Just add the "position:absolute;top:-9999px;" style to the element.

How to implement lace borders in css3 How to implement lace borders in css3 Sep 16, 2022 pm 07:11 PM

In CSS, you can use the border-image attribute to achieve a lace border. The border-image attribute can use images to create borders, that is, add a background image to the border. You only need to specify the background image as a lace style; the syntax "border-image: url (image path) offsets the image border width inward. Whether outset is repeated;".

How to enlarge the image by clicking the mouse in css3 How to enlarge the image by clicking the mouse in css3 Apr 25, 2022 pm 04:52 PM

Implementation method: 1. Use the ":active" selector to select the state of the mouse click on the picture; 2. Use the transform attribute and scale() function to achieve the picture magnification effect, the syntax "img:active {transform: scale(x-axis magnification, y Axis magnification);}".

It turns out that text carousel and image carousel can also be realized using pure CSS! It turns out that text carousel and image carousel can also be realized using pure CSS! Jun 10, 2022 pm 01:00 PM

How to create text carousel and image carousel? The first thing everyone thinks of is whether to use js. In fact, text carousel and image carousel can also be realized using pure CSS. Let’s take a look at the implementation method. I hope it will be helpful to everyone!

How to set animation rotation speed in css3 How to set animation rotation speed in css3 Apr 28, 2022 pm 04:32 PM

In CSS3, you can use the "animation-timing-function" attribute to set the animation rotation speed. This attribute is used to specify how the animation will complete a cycle and set the speed curve of the animation. The syntax is "element {animation-timing-function: speed attribute value;}".

What is the unit of vh in css? What is the unit of vh in css? Dec 07, 2023 pm 02:49 PM

vh is a relative length unit in CSS, representing a percentage of the viewport height. 1vh is equal to 1% of the viewport height. The viewport height refers to the height of the visible part of the browser window, excluding the browser toolbar, address bar and Non-page parts such as scroll bars.

See all articles