Home > Web Front-end > CSS Tutorial > How to use css3's new units vw, vh, vmin, and vmax

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

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

This time I will bring you how to use the new css3 units vw, vh, vmin, and vmax. What are the precautions for 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 the visual area without 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.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hangge.com</title>
    <style>
      html, body, div, span, h1, h2, h3 {
        margin: 0;
        padding: 0;
        border: 0;
      }
  
      .demo {
       width: 100vw;
       font-size: 5vw;
       margin: 0 auto;
       background-color: #50688B;
       color: #FFF;
      }
  
      .demo2 {
       width: 80vw;
       font-size: 5vw;
       margin: 0 auto;
       background-color: #ff6a00;
      }
  
      .demo3 {
       width: 50vw;
       height: 50vh;
       font-size: 1vw;
       margin: 0 auto;
       background-color: #ff006e;
       color: #FFF;
      }
    </style>
  </head>
  <body>
      <div class="demo">
          <h1>宽度100%, 字体5%</h1>
      </div>
      <div class="demo2">
          <h2>宽度80%, 字体5%</h2>
      </div>
      <div class="demo3">
          <h3>宽度50%, 高度50%, 字体1%</h3>
      </div>
  </body>
</html>
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

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hangge.com</title>
    <style>
      html, body, div, span, button {
        margin: 0;
        padding: 0;
        border: 0;
      }
  
      button {
        width: 120px;
        height: 30px;
        color: #FFFFFF;
        font-family: "微软雅黑";
        font-size: 14px;
        background: #28B995;
      }
  
      #mask {
        width: 100vw;
        height: 100vh;
        position: fixed;
        top: 0;
        left: 0;
        background: #000000;
        opacity: 0.5;
        display: none;
      }
    </style>
  </head>
  <body>
      <button onclick="document.getElementById(&#39;mask&#39;).style.display=&#39;inline&#39;">点击显示遮罩</button>
      <div id="mask" onclick="document.getElementById(&#39;mask&#39;).style.display=&#39;none&#39;"/></div>
  </body>
</html>
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.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hangge.com</title>
    <script type="text/javascript" src="js/jquery.js"></script>
    <style>
      html, body, div, span, button {
        margin: 0;
        padding: 0;
        border: 0;
      }
  
      button {
        width: 120px;
        height: 30px;
        color: #FFFFFF;
        font-family: "微软雅黑";
        font-size: 14px;
        background: #28B995;
      }
  
      .dialog-container {
        display: none;
        width: 100vw;
        height: 100vh;
        background-color: rgba(0,0,0,.35);
        text-align: center;
        position: fixed;
        top: 0;
        left: 0;
        z-index: 10;
      }
  
      .dialog-container:after {
        display: inline-block;
        content: &#39;&#39;;
        width: 0;
        height: 100%;
        vertical-align: middle;
      }
  
      .dialog-box {
        display: inline-block;
        border: 1px solid #ccc;
        text-align: left;
        vertical-align: middle;
        position: relative;
      }
  
      .dialog-title {
        line-height: 28px;
        padding-left: 5px;
        padding-right: 5px;
        border-bottom: 1px solid #ccc;
        background-color: #eee;
        font-size: 12px;
        text-align: left;
      }
  
      .dialog-close {
        position: absolute;
        top: 5px;
        right: 5px;
        font-size: 12px;
      }
  
      .dialog-body {
        background-color: #fff;
      }
    </style>
  </head>
  <body>
      <button onclick="$(&#39;#dialogContainer&#39;).show();">点击显示弹出框</button>
      <div id="dialogContainer" class="dialog-container">
          <div class="dialog-box">
              <div class="dialog-title">居中弹出框</div>
              <a onclick="$(&#39;#dialogContainer&#39;).hide();" class="dialog-close">关闭</a>
              <div class="dialog-body">
                <img src="logo.png" class="demo-image" />
              </div>
          </div>
      </div>
  </body>
</html>
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.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hangge.com</title>
    <script type="text/javascript" src="js/jquery.js"></script>
    <style>
      html, body, div, span, button {
        margin: 0;
        padding: 0;
        border: 0;
      }
  
      button {
        width: 120px;
        height: 30px;
        color: #FFFFFF;
        font-family: "微软雅黑";
        font-size: 14px;
        background: #28B995;
      }
  
      .dialog-container {
        display: none;
        width: 100vw;
        height: 100vh;
        background-color: rgba(0,0,0,.35);
        text-align: center;
        position: fixed;
        top: 0;
        left: 0;
        z-index: 10;
      }
  
      .dialog-box {
        top:10vh;
        left:10vw;
        width: 80vw;
        height: 80vh;
        text-align: left;
        position: absolute;
        border: 1px solid #ccc;
        display: flex;
        flex-direction: column;
      }
  
      .dialog-title {
        line-height: 28px;
        padding-left: 5px;
        padding-right: 5px;
        border-bottom: 1px solid #ccc;
        background-color: #eee;
        font-size: 12px;
        text-align: left;
      }
  
      .dialog-close {
        position: absolute;
        top: 5px;
        right: 5px;
        font-size: 12px;
      }
  
      .dialog-body {
        background-color: #fff;
        flex:1;
        overflow: auto;
      }
    </style>
  </head>
  <body>
      <button onclick="$(&#39;#dialogContainer&#39;).show();">点击显示弹出框</button>
      <div id="dialogContainer" class="dialog-container">
          <div class="dialog-box">
              <div class="dialog-title">居中弹出框</div>
              <a onclick="$(&#39;#dialogContainer&#39;).hide();" class="dialog-close">关闭</a>
              <div class="dialog-body">
                <img src="logo.png" class="demo-image" />
              </div>
          </div>
      </div>
  </body>
</html>
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template