我在试着理解对我来说似乎出乎意料的行为:
我在一个容器中有一个max-height为100%的元素,该容器也使用了max-height,但出乎意料的是,子元素溢出了父元素:
.container {
背景:蓝色;
填充:10 px;
max-height: 200 px;
max-width: 200 px;
}
img {
显示:块;
max-height: 100%;
max-width: 100%;
}
< div class = "容器" >
<img src="http://placekitten.com/400/500" />
< / div >
但是,如果父节点被指定了显式的高度,这个值是固定的:
.container {
背景:蓝色;
填充:10 px;
max-height: 200 px;
max-width: 200 px;
身高:200 px;
}
img {
显示:块;
max-height: 100%;
max-width: 100%;
}
< div class = "容器" >
<img src="http://placekitten.com/400/500" />
< / div >
有人知道为什么在第一个例子中子节点不尊重父节点的max-height吗?为什么需要显式高度?
.container {
background: blue;
padding: 10px;
max-height: 200px;
max-width: 200px;
float: left;
margin-right: 20px;
}
.img1 {
display: block;
max-height: 100%;
max-width: 100%;
}
.img2 {
display: block;
max-height: inherit;
max-width: inherit;
}
<!-- example 1 -->
<div class="container">
<img class='img1' src="http://via.placeholder.com/350x450" />
</div>
<!-- example 2 -->
<div class="container">
<img class='img2' src="http://via.placeholder.com/350x450" />
</div>
我玩了一下。在firefox中使用较大的图像时,我使用继承属性值得到了很好的结果。这对你有帮助吗?
.container {
background: blue;
padding: 10px;
max-height: 100px;
max-width: 100px;
text-align:center;
}
img {
max-height: inherit;
max-width: inherit;
}
.container {
background: blue;
padding: 10px;
max-height: 200px;
max-width: 200px;
float: left;
margin-right: 20px;
}
.img1 {
display: block;
max-height: 100%;
max-width: 100%;
}
.img2 {
display: block;
max-height: inherit;
max-width: inherit;
}
<!-- example 1 -->
<div class="container">
<img class='img1' src="http://via.placeholder.com/350x450" />
</div>
<!-- example 2 -->
<div class="container">
<img class='img2' src="http://via.placeholder.com/350x450" />
</div>
我玩了一下。在firefox中使用较大的图像时,我使用继承属性值得到了很好的结果。这对你有帮助吗?
.container {
background: blue;
padding: 10px;
max-height: 100px;
max-width: 100px;
text-align:center;
}
img {
max-height: inherit;
max-width: inherit;
}