html - 宽度 : 50% mean when the container is body? 是什么意思

标签 html css width css-position percentage

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/

相关文章:

javascript - jquery - 鼠标松开后查找 2 个不同的元素

javascript - 通过 AJAX 将数据插入 div 后的 jQuery scrollTop

ios - 将标签宽度增加到左侧 Titanium

html - 在 css 中重新创建动态表的最佳方法

javascript - 根据内容更改样式

HTML 表格列宽实践

javascript - 屏幕宽度检测在 Internet Explorer 中不起作用

javascript - Angular 指令 DOM 操作行为

iPhone 上的 HTML5 视频元素有边框

html - Bootstrap4 测试版 : tables and d-inline-block