css box-sizing以及calc()

box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box。

content-box,border和padding不计算入width之内

padding-box,padding计算入width内

border-box,border和padding计算入width之内,其实就是怪异模式了~

ie8+浏览器支持content-box和border-box;

ff则支持全部三个值。

使用时:

-webkit-box-sizing: 100px; // for ios-safari, android

-moz-box-sizing:100px; //for ff

box-sizing:100px; //for other

例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.demo{300px; padding:20px; background-color:#00F;}
.box{100%; box-sizing:border-box; -webkit-box-sizing:border-box; -moz-box-sizing:border-box; height:50px; background-color:#F00; border:5px solid #0F0; padding:20px;}
</style>
</head>

<body>
<div class="demo">
<div class="box">dd</div>
</div>
</body>
</html>

关于calc:

calc(expression);

calc()的运算规则

calc()使用通用的数学运算规则,但是也提供更智能的功能:

  1. 使用“+”、“-”、“*” 和 “/”四则运算;
  2. 可以使用百分比、px、em、rem等单位;
  3. 可以混合使用各种单位进行计算;
  4. 表达式中有“+”和“-”时,其前后必须要有空格,如"widht: calc(12%+5em)"这种没有空格的写法是错误的;
  5. 表达式中有“*”和“/”时,其前后可以没有空格,但建议留有空格。

浏览器的兼容性

浏览器对calc()的兼容性还算不错,在IE9+、FF4.0+、Chrome19+、Safari6+都得到较好支持,同样需要在其前面加上各浏览器厂商的识别符,不过可惜的是,移动端的浏览器还没仅有“firefox for android 14.0”支持,其他的全军覆没。

例子:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>无标题文档</title>
<style>
.demo {
300px;
background: #60f;
padding: 3px 0;
}
.box {
background: #f60;
height: 50px;
padding: 10px;
border: 5px solid green;
90%;/*写给不支持calc()的浏览器*/
-moz-calc(100% - (10px + 5px) * 2);
-webkit-calc(100% - (10px + 5px) * 2);
calc(100% - (10px + 5px) * 2);
}
</style>
</head>

<body>
<div class="demo">
  <div class="box">dd</div>
</div>
</body>
</html>

 
原文地址:https://www.cnblogs.com/luoluo8/p/5401623.html