为什么CSS width:calc(inherit)做的事情不同于width:继承?

问题描述 投票:0回答:2

[我是一名中学生,正在尝试CSS和HTML,我注意到width:calc(inherit)所做的事情与width:Inherit有所不同。

以下代码段是什么宽度:继承;做。

.header {
	background-color: #77a4ed;
	min-width: 200px;
	width: fit-content;
	border-top-left-radius: 50px;
	border-top-right-radius: 50px;
	height: 30px;
	text-align: center;
	height: 20px;
}

.header > div {
	background-color: whitesmoke;
	border-right: solid 2px black;
	border-left: solid 2px black;
	border-top: solid 2px black;
	min-width: 200px;
	width: inherit;
	text-align: center;
	margin: auto;
	padding-left: 10px;
	padding-right: 10px;
	padding-top: 3px;
	padding-bottom: 3px;
}

.header > div:last-of-type {
	background-color: whitesmoke;
	border-right: solid 2px black;
	border-left: solid 2px black;
	min-width: 200px;
	width: fit-content;
	margin: auto;
	padding-left: 10px;
	border-bottom: solid 2px black;
	border-bottom-left-radius: 10px;
	border-bottom-right-radius: 10px;
}
<div class='container'>
<div class='header'>
&nbsp;
<div>
test
</div>
<div>
test..........................................................
</div>
</div>
</div>

此代码段的宽度为:calc(inherit);做。

.header {
	background-color: #77a4ed;
	min-width: 200px;
	width: fit-content;
	border-top-left-radius: 50px;
	border-top-right-radius: 50px;
	height: 30px;
	text-align: center;
	height: 20px;
}

.header > div {
	background-color: whitesmoke;
	border-right: solid 2px black;
	border-left: solid 2px black;
	border-top: solid 2px black;
	min-width: 200px;
	width: calc(inherit);
	text-align: center;
	margin: auto;
	padding-left: 10px;
	padding-right: 10px;
	padding-top: 3px;
	padding-bottom: 3px;
}

.header > div:last-of-type {
	background-color: whitesmoke;
	border-right: solid 2px black;
	border-left: solid 2px black;
	min-width: 200px;
	width: fit-content;
	margin: auto;
	padding-left: 10px;
	border-bottom: solid 2px black;
	border-bottom-left-radius: 10px;
	border-bottom-right-radius: 10px;
}
<div class='container'>
    <div class='header'>
    &nbsp;
    <div>
    test
    </div>
    <div>
    test..........................................................
    </div>
    </div>
    </div>

我不明白他们为什么会给出不同的结果。有人可以解释吗?

html css width
2个回答
0
投票

如果检查console.log,您将看到calc无法与继承一起使用。

#a1{
width:inherit;
}

#a2{
width: calc (inherit);
}
<div id='a1'>xxx</div>
<div id='a2'>xxx</div>

0
投票

calc()假定使用-,+,*或/等数学表达式进行计算例子

width: calc(100% - 100px); 

您没有任何表达式,因此它可能无效,但如果使.header宽度具有表达式,则使用可能起作用的继承

.header{
    width: cal(100% - 100px);
}
.header{
    width: inherit;
}

这可能有效,但也不确定我不知道100%-100px是否是您要查找的确切宽度

© www.soinside.com 2019 - 2024. All rights reserved.