详细介绍css中的数学表达式calc()


本文摘自PHP中文网,作者V,侵删。

数学表达式calc()是CSS中的函数,主要用于数学运算。使用calc()为页面元素布局提供了便利和新的思路。

定义

数学表达式calc()是calculate计算的缩写,它允许使用+、-、*、/这四种运算符,可以混合使用%、px、em、rem等单位进行计算

兼容性: IE8-、safari5.1-、ios5.1-、android4.3-不支持,android4.4-4.4.4只支持加法和减法。IE9不支持用于backround-position

(推荐教程:CSS教程)

注意:+和-运算符两边一定要有空白符

实例如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<style>

.test1{

    border: calc( 1px + 1px ) solid black;

    /* calc里面的运算遵循*、/优先于+、-的顺序 */

    width: calc(100%/3 - 2*1em - 2*1px);

    background-color: pink;

    font-style: toggle(italic, normal);

}

.test2{

    /* 由于运算符+的左右两侧没有空白符,所以失效 */

    border: calc(1px+1px) solid black;

    /* 对于,不能小于0的属性值,当运算结果小于0时,按0处理 */

    width: calc(10px - 20px);

    padding-left: 10px;

    background-color: lightblue;

}

</style>

<div class="test1">测试文字一</div>   

<div class="test2">测试文字二</div>

应用

数学表达式calc()常用于布局中的不同单位的数字运算

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<style>

p{margin: 0;}

.parent{overflow: hidden;zoom: 1;}

.left{float: left;width: 100px;margin-right: 20px;}   

.right{float: left;width: calc(100% - 120px);}

</style>

<div class="parent" style="background-color: lightgrey;">

    <div class="left" style="background-color: lightblue;">

        <p>left</p>

    </div>

    <div class="right"  style="background-color: lightgreen;">

        <p>right</p>

        <p>right</p>

    </div>

</div>

相关视频教程推荐:css视频教程

以上就是详细介绍css中的数学表达式calc()的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

asp怎样引入css样式

详解css3+svg滤镜实现不规则边框的方法

如何使用css解决浮动元素父元素的高度塌陷问题?(代码示例)

css怎样缩进文本?

css怎么设置字体为楷体?

css怎么把div设置成圆角

使用html+css制作一些动画提示工具

html和css重点难点问题

css在html中三种实现方式是什么

不能忘却的悼念:将网页背景变为黑白色调教程

更多相关阅读请进入《css》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...