在vue.js中寫新的components的時候,若是在新頁面中的模板中設置height:100%的時候一直無效。html
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%就不起做用了。vue
此時應該在App.vue文件style中添加以下代碼:
html,body,#app{ height: 100%; }