body {
background-color: skyblue;
}
div {
width: 50%;
height: 50%;
background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
</div>
</body>
</html>
我了解到,百分比宽度表示与容器相比的比例。然而,当我给div的宽度和高度分配50%时,我看不到div。您能告诉我原因吗?这里的百分比意味着什么?
最佳答案
将 position:absolute
添加到 div
即可!
请了解 position
在 DOM 元素上的工作原理。
body {
background-color: skyblue;
}
div {
width: 50%;
height: 50%;
background-color: yellow;
position: absolute;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
</div>
</body>
</html>
关于html - 宽度 : 50% mean when the container is body? 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38743561/