css - 为什么绝对定位的img不能自动拉伸?
问题描述
如果绝对定位元素的height
&没有显式声明,并且所有 4 个方向都是,那么该元素将获得与其包含块相同的& :width
0
height
width
CSS
section {
width: 400px;
height: 400px;
background-color: teal;
position: relative;
}
div {
background-color: pink;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
HTML
<body>
<section>
<div></div>
</section>
</body>
但是,当涉及到时,事情似乎很奇怪img
:
CSS
section {
width: 400px;
height: 400px;
background-color: teal;
position: relative;
}
img {
background-color: pink;
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
HTML
<body>
<section>
<img src="" alt="">
</section>
</body>
除了我div
用img
. 我什至给出了img
一个明确的display: block
,这显然是多余的,因为绝对定位的元素被计算为block
。但是img
并没有像以前那样被拉伸div
。检查它,发现它img
恰好得到了height
它的包含块的 400px section
,但它width
是 0。
所以这是我的问题:为什么绝对定位img
不能自动拉伸?
注意:img
没有内容。
有一个类似的问题,但我想要最本质和理论上的解释,而不是解决方法。
解决方案
推荐阅读
- python-2.7 - Selenium webdriver - Python 不会点击网站上的按钮
- python - VSCode中的python错误未解决的导入和未使用的导入
- ios - 如何将自定义元素添加到 UIAlertController?
- javascript - 我将有效负载中的正文传递给 POST 方法,这些方法是选定的过滤器。如果它是空的,现在是否可以发送任何数组类别
- spring - 模拟基本抽象类的自动装配对象
- android - 从片段写入 Firebase 数据库
- spring-boot - 为什么我总是在 zuul api 网关中遇到连接超时异常?
- javascript - 使用 Probot 创建关于快速路由响应的 Github 问题
- azure - heptio velero 无法备份 azure AKS 中的持久卷
- visual-studio-code - 是否有将当前行移动到当前窗口顶部的命令,类似于简要编辑器 Ctrl-T?