vue中设置height:100%无效的问题及解决方法

在vue.js中写新的components的时候,如果在新页面中的模板中设置height:100%的时候一直无效,

在App.vue中:

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
export default {
   name: 'App'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>

 

这时候设置height: 100%;是无效的,在chrome的Elements中发现height仍然是0px.

设置高度100%时,div的高度会等同于其父元素的高度。而上面中id为app的div(vue挂载的div)的父节点是body标签,body标签的父节点是html标签。在默认情况下html和body标签的高度为auto,而浏览器是不会自动给标签添加高度的,所以html和body标签就为0,自然子div的高度设置为100%就不起作用了。

此时应该在App.vue文件style中添加如下代码:

html,body,#app{

 height: 100%;

}

  

 原文链接:https://segmentfault.com/a/1190000015789623

猜你喜欢

转载自www.cnblogs.com/zeng91/p/9775043.html