calc( Expression );
算术运算符加法(+)和减法(-)应该始终被空格包围。否则,该表达式将被视为无效表达式。例如, calc(60%-4px)将无效,因为它被解析为百分比,后跟负数。另一方面,表达式 calc(60%-4px)将被解析为减法运算符和长度。
尽管运算符 * 和/不需要空格,但是建议添加空格以保持一致性。
可以嵌套 calc()函数。
<!DOCTYPE html>
<html>
<head>
<title> calc() function </title>
<style>
.jtp {
width: calc(150% - 75%);
height: calc(350px - 75px);
background-color: lightblue;
padding-top: 50px;
}
.jtp1 {
font-size: 30px;
font-weight: bold;
color: blue;
}
h1 {
color: blue;
}
h2 {
color: green;
}
</style>
</head>
<body>
<center>
<div class="jtp">
<div class="jtp1"> Welcome to the bianchenghao6.com </div>
<h1> 这是calc()函数的示例 </h1>
<h2> width: calc(150% - 75%); </h2>
<h2> height: calc(350px - 75px); </h2>
</div>
</center>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> calc() function </title>
<style>
.jtp {
width: calc(40% + 10em);
height: calc(350px + 75px);
background-color: lightblue;
padding-top: calc(10% - 10px);
padding-left: calc(10% + 10px);
}
.jtp1 {
font-size: 30px;
font-weight: bold;
color: blue;
}
h1 {
color: blue;
}
h2 {
color: green;
}
</style>
</head>
<body>
<div class="jtp">
<div class="jtp1"> Welcome to the bianchenghao6.com </div>
<h2> width: calc(40% + 10em); </h2>
<h2> height: calc(350px + 75px); </h2>
<h2> padding-top: calc(10% - 10px); </h2>
<h2> padding-left: calc(10% + 10px); </h2>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> calc() function </title>
<style>
.jtp {
width: calc( calc(40em / 3) * 2);
height: calc(350px + 75px);
background-color: lightblue;
}
.jtp1 {
font-size: 30px;
font-weight: bold;
color: blue;
}
h1 {
color: blue;
}
h2{
color: green;
}
</style>
</head>
<body>
<div class = "jtp">
<div class = "jtp1"> Welcome to the bianchenghao6.com </div>
<h1> This is an example of nested calc() Function </h1>
<h2> width: calc( calc(40em / 3) * 2); </h2>
<h2> height: calc(350px + 75px); </h2>
</div>
</body>
</html>