Home > Web Front-end > CSS Tutorial > Respective introduction of 'vh” & 'vw” in mobile css units

Respective introduction of 'vh” & 'vw” in mobile css units

高洛峰
Release: 2017-03-17 09:47:32
Original
2809 people have browsed it

1. Foreword:

Responsive web design is inseparable from percentages. However, CSS percentages are not the best solution for all problems. CSS width is relative to the width of the nearest parent element that contains it. But what if you just want to use the width or height of the viewport instead of the parent element?

2. "vh" & "vw":

vh: relative to the height of the window: the height of the window is 100vh.

vw: relative to the width of the window: the window width is 100vw.

Respective introduction of vh” & vw” in mobile css units

100% height of the window
(Look at me!!!)

Look at me! ! ! Look at me...! ! ! I am the width of the browser window (you can see my changes by changing the browser width): 0

3. Source code:

CSS:

  .demo-1,.demo-2,.demo-3{margin-bottom:10px; padding:10px 0; line-height: 30px; color: #fff; text-indent: 10px;}
  .demo-1 strong,.demo-2 strong,.demo-3 strong{color:#fff !important;}
  .demo-1{width:10vw; background: #1ab5e3;}
  .demo-2{width:25vw; background: #FF5F09;}
  .demo-3{width:50vw; background: #28AF59;}
  .demo-4{position: fixed; z-index: 10; top: 0; left: 0; width: 150px; height: 100vh; color: #fff; background: rgba(0,0,0,.5);}
  .demo-4 span{position:absolute; top:50%; display:block; padding: 0 10px; -webkit-transform: translateY(-50%); transform: translateY(-50%);}
Copy after login

HTML:

  <p>视窗的10%: <strong>0</strong></p>
  <p>视窗的25%: <strong>0</strong></p>
  <p>视窗的50%: <strong>0</strong></p>
  <p><span>视窗的100%高度<br>(看我!!!)</span></p>
  <p>看我!!!看我...!!!我是浏览器视窗的宽度(你可以通过改变浏览器宽度看我的变化):<span>0</span></p>
Copy after login

JS:

$(function(){
  //视窗宽度改变函数
  function resizeWindow(){
    var viewWidth = window.innerWidth;
    $('.js-viewWidth').html(viewWidth);
    $('.js-getVW3').html(viewWidth/2);
    $('.js-getVW2').html(viewWidth/4);
    $('.js-getVW1').html(viewWidth/10);
  }
  //初始化
  resizeWindow();
  
  //浏览器视窗改变时调用上面定义的函数
  $(window).resize(function(event) {
    resizeWindow();
  });
});
Copy after login

The above is the detailed content of Respective introduction of 'vh” & 'vw” in mobile css units. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
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