Home  >  Article  >  Web Front-end  >  Css3 calc可使用简单的数学运算计算宽度_html/css_WEB-ITnose

Css3 calc可使用简单的数学运算计算宽度_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:29:131086browse

Css3 calc可使用简单的数学运算计算宽度

 

calc是单词calculate的缩写,是Css3的一个新的长度单位函数,可以使用简单的数学运算.

Firefox要使用-moz-calc()私有属性,

Chrome要使用-webkit-calc()私有属性,

IE9原生支持标准的不带前缀的写法,

Opera暂还不支持.

运算规则

可以使用 “+” “-” “*” “/” 四则运算(”+”、”-”两个符号边上必须要有空格,而”*”、”/”符号则不是必须的),

可以使用百分比、px、em、rem等单位,

可以混合使用各种单位进行计算.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

.content {

     border:1px solid #000;

     /* ‘+’ ‘-’ 符号左右两边加空格 */

     width:calc(100% - 2px);

}

 

.content2 {

     /* ‘+’ ‘-’ 符号左右两边加空格 */

     width:calc(10em + 10px);

}

 

/*三栏等宽布局*/

.content3 {

     margin-left:20px;

     /* ‘+’ ‘-’ 符号左右两边加空格, ‘*’ ‘/’ 符号可不加 */

     width:calc(100%/3 - 20px);

}

/* n为从0开始的乘数,依次递增1,如:3*0,3*1,3*2等相乘结果后的子元素 */

.content3:nth-child(3n){

     margin-left:0;

}

calc是单词calculate的缩写,是Css3的一个新的长度单位函数,可以使用简单的数学运算.

Firefox要使用-moz-calc()私有属性,

Chrome要使用-webkit-calc()私有属性,

IE9原生支持标准的不带前缀的写法,

Opera暂还不支持.

运算规则

可以使用 “+” “-” “*” “/” 四则运算(”+”、”-”两个符号边上必须要有空格,而”*”、”/”符号则不是必须的),

可以使用百分比、px、em、rem等单位,

可以混合使用各种单位进行计算.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

.content {

     border:1px solid #000;

     /* ‘+’ ‘-’ 符号左右两边加空格 */

     width:calc(100% - 2px);

}

 

.content2 {

     /* ‘+’ ‘-’ 符号左右两边加空格 */

     width:calc(10em + 10px);

}

 

/*三栏等宽布局*/

.content3 {

     margin-left:20px;

     /* ‘+’ ‘-’ 符号左右两边加空格, ‘*’ ‘/’ 符号可不加 */

     width:calc(100%/3 - 20px);

}

/* n为从0开始的乘数,依次递增1,如:3*0,3*1,3*2等相乘结果后的子元素 */

.content3:nth-child(3n){

     margin-left:0;

}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn