html 学习 day3 css 的层叠 父子size 的继承
今天遇到一个问题, css 的父子继承关系在一种case 下不生效。
:子节点 无法 继承 父节点的 width 和 height 的设定
下面代码的原始版本 img 无法继承 div 的 宽和高的设定。
当 img 的 css 设定为:
img {
height: 100%;
width: 100%;
margin-right: 120px;
}
它 会超出 div的宽高, 直接继承 html 的宽高设定。
这是因为 div 继承来自 section 的 宽高都是 百分比, 子节点在这种情形下, 没法继承, mdn web docs 说 : 这种属性是不能父子间传递的,否则 css 将会很难用。(https://developer.mozilla.org/zh-CN/docs/Learn/CSS/Building_blocks/Cascade_and_inheritance)
<!DOCTYPE >
<html>
<head>
<style>
html {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
font-size: 18px;
}
body {
height: 90vh;
width: 90vw;
min-height: 80vh;
margin: 20px auto;
}
h1 {
font-size: 5rem;
margin: 0px 10%;
}
h1:nth-child(3) {
color: rgb(159, 63, 115);
text-shadow: 2px 2px rgb(248, 169, 173);
padding: 5px;
-webkit-text-stroke: 2px rgb(248, 169, 173);
-webkit-text-fill-color: rgb(159, 63, 115);
}
section {
background-image: linear-gradient(
45deg,
RGB(153, 187, 233),
rgb(247, 174, 189)
);
width: 100%;
min-height: 100%;
background-size: 100% 100%;
background-repeat: no-repeat;
background-position: center;
background-size: 100% 100%;
display: flex;
flex-direction: column;
justify-content: space-around;
}
#image_container {
display: flex;
justify-content: flex-end;
}
img {
max-height: 100%;
max-width: 100%;
margin-right: 120px;
}
div {
padding: 20px;
}
</style>
</head>
<body>
<section>
<div>
<h1>Web Development</h1>
<h1>Roadmap</h1>
<h1>2023</h1>
</div>
<div id="image_container">
<img src="p1.png" />
</div>
</section>
</body>
</html>

浙公网安备 33010602011771号