使用css的calc() 函数计算宽高
【摘要】
茫茫人海中我还是只看到了你。
什么是calc()?
calc() 函数用于动态计算长度值。
需要注意的是,运算符前后都需要保留一个空格,例如:width: calc(100% - 10px...
茫茫人海中我还是只看到了你。
什么是calc()?
calc() 函数用于动态计算长度值。
- 需要注意的是,运算符前后都需要保留一个空格,例如:width: calc(100% - 10px);
- 任何长度值都可以使用calc()函数进行计算;
- calc()函数支持 “+”, “-”, “*”, “/” 运算;
- calc()函数使用标准的数学运算优先级规则;
语法
calc(expression)
- 1
举例
加法+

<template>
<div class="demo">
<div class="item"></div>
</div>
</template>
<style lang="scss">
.demo{
width: 400px;
height: 400px;
background: #999;
.item{
width: calc(100% + 200px);
height: 100px;
background: red;
}
}
</style>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
减法-

<template>
<div class="demo">
<div class="item"></div>
</div>
</template>
<style lang="scss">
.demo{
width: 400px;
height: 400px;
background: #999;
.item{
width: calc(100% - 200px);
height: calc(100% - 100px);
background: red;
}
}
</style>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
乘法 *

<template>
<div class="demo">
<div class="item"></div>
</div>
</template>
<style lang="scss">
.demo{
width: 400px;
height: 400px;
background: #999;
.item{
width: calc(100px * 2);
height: calc(100px * 2);
background: red;
}
}
</style>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
除法 /

<template>
<div class="demo">
<div class="item"></div>
</div>
</template>
<style lang="scss">
.demo{
width: 400px;
height: 400px;
background: #999;
.item{
width: calc(100% / 2);
height: calc(100% / 2);
background: red;
}
}
</style>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
calc及大的增加了了宽高计算的便利性。
文章来源: root181.blog.csdn.net,作者:前端纸飞机,版权归原作者所有,如需转载,请联系作者。
原文链接:root181.blog.csdn.net/article/details/121688171
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
- 点赞
- 收藏
- 关注作者
评论(0)