为什么100vw + 100vh可以水平滚动?

时间:2019-05-23 15:33:43

标签: html css

我有3个div 1 x 100%2 x 100vw

然后,如果我将100vh的高度设置为100vw div(黑色的),则将得到溢出-x,并且将得到水平滚动。

为什么会这样?根据我的理解,在这种情况下,100vw应该等于100%,因为它们位于body内,没有任何边距/填充,并且具有box-sizing: border-boxenter image description here

show100vhDiv = function() {
  document.querySelector('.height-100vh').classList.toggle('hidden');
}
@import url('https://fonts.googleapis.com/css?family=Lato&display=swap');
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: Lato;
  padding-top: 30px;
}

.test {
  height: 30px;
  margin: 20px 0;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  text-transform: uppercase;
  letter-spacing: 20px;
}

.width-100vw {
  background: salmon;
  width: 100vw;
  overflow: hidden;
}

.height-100vh {
  background: black;
  height: 100vh;
  color: white;
}

.width-100 {
  width: 100%;
  background: mediumvioletred;
}

.hidden {
  display: none;
}

button {
position: relative;
left: 50%;
transform: translateX(-50%);
}
<button onclick="show100vhDiv()">Toggle 100vh x 100vw element</button>
<div class="test width-100vw">100vw</div>
<div class="test width-100">100%</div>
<div class="test width-100vw height-100vh">100vh x 100vw</div>

0 个答案:

没有答案
相关问题