html,body{ width: 100%;height: 100%;}的解释

按道理,子元素百分比是以父元素为标准,那么问题来了,html有父元素吗?

html{ width: 100%;height: 100%;}拿谁的宽高?

经过测试,是当前浏览器窗口的大小

看图

<body>
		<div onclick="func()">xxx</div>
	</body>
	<script>
		function func(){
			var div = document.getElementsByTagName("div")[0];
			alert(parseInt(getStyle(div,"width"))+"--"+parseInt(getStyle(div,"height")));
		}
		function getStyle(ele,attr){
				if(window.getComputedStyle){
					return getComputedStyle(ele,null)[attr];
				} 
				else{
					return ele.currentStyle[attr];
				}
		} 		
	</script>

在这里插入图片描述

全屏幕下,是1506-686

缩放下

在这里插入图片描述

其实可以写成

	    html{ width: 100%;height: 100%}
		body{height:100%}
		div{
			height: 100%;/* 再次证明高度不能继承 */
			background-color: salmon;
		}

宽度100%不用写,可以继承,但是如果不写height,就会得不到可视区域的高度

猜你喜欢

转载自blog.csdn.net/qq_26239917/article/details/88543304