
height、max-height、min-height 同时使用时,谁优先?
在本文开头,我们将通过一个具体的 html 代码示例来说明这个问题。
html 代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>document</title>
</head>
<body>
<!-- 父元素 -->
<div style="max-height: 100px; height: 300px; min-height: 200px;width: 200px;background-color: red;">
<!-- 子元素 -->
<div style="height: 300px; background-color: aqua;"></div>
</div>
</body>
</html>
登录后复制
从代码中可以看出,父元素设置了 max-height、height、min-height 三个高度属性值,分别为 100px、300px、200px。子元素设置了 height 为 300px。
立即学习“”;
那么,问题来了,父元素最后的高度是多少呢?
优先级判断规则
首先,我们了解一下 height、max-height、min-height 的优先级判断规则:
- 若 height 的值大于 max-height,则 height 的值为 max-height。
- 若 height 的值小于 min-height,则 height 的值为 min-height。
具体分析
对于给定代码,先比较 height 和 max-height:
height(300px) > max-height(100px)
登录后复制
因此,height 的值为 max-height,即 100px。
然后,比较 max-height 和 min-height:
max-height(100px) < min-height(200px)
登录后复制
因此,height 的值为 min-height,即 200px。
因此,父元素的最终高度为 200px。
以上就是CSS 中 height、max-height、min-height 同时使用时,谁的优先级最高?的详细内容,更多请关注米云其它相关文章!
