CSS3: calc计算属性

前言

正如其名,calc是css3中新增的计算属性,让很多属性增加了一个表达式的说法;

标准的写法

  .class{
       /*
		 area: expression;
       */
	   width:calc();
	   padding:calc();
	   margin-top:calc();
	   ...
   }

兼容性

这里写图片描述

基本理论

  • calc可以做用于任何具有大小的东东,比如border、margin、pading、font-sizewidth等属性设置动态值
  • 支持的运算单位: rem , em , percentage , px
  • 计算优先级别和数学一致

注意点:

calc 内部的表达式,在使用运算符号时,两遍必须加上空格(虽然乘除可以无视,但还是建议带上)!!!!!,不然会解析错误!!,看演示写法

	width:calc(10 * 10px);
	width:calc(50% - 50px);
	width:calc(50% + 5em);
	width:calc(10% / 1rem);

实战效果图

仅仅作为演示,响应伸缩

这里写图片描述

实战代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>CSS3 Calc</title>
    <style type="text/css" media="screen">
    	html{
    		font-size:62.5%;
    	}
		.wrapper{
			width:100%;
			border:5px solid #f00;
			margin:10px ;
			box-sizing:border-box;
			height:200px;
			clear:b;
		}
		.items{
			height:calc(100% - 40px);
			display:inline-block;
			border:1px solid #f70;
			text-align:center;
		}

		.w1 .items{

			float:left;
			margin-top:calc( 5 * 4px  );
			border:3px solid #0F16C6;
			width:calc(100% / 3 - 6px)
		}

		.w2 .items{
			float:left;
			margin-top:calc(200px - 20px * 9);
			width:calc(100% / 3 - 2px)
		}

		.w3 .items{
			float:left;
			width:calc(100% / 3 - (3 * 6px));
			margin:calc(2px * 4 );
		}

		.w3 .items:first-child{
			padding:calc(5 * 1rem - 3rem);
			box-sizing:border-box;
		}

    </style>
</head>

<body>
    <div class="wrapper w1">
        <div class="items">margin-top:calc( 5 * 4px  );</div>
        <div class="items">margin-top:calc( 5 * 4px  );</div>
        <div class="items">margin-top:calc( 5 * 4px  );</div>
    </div>
    <div class="wrapper w2">
        <div class="items">margin-top:calc(200px - 20px * 9);</div>
        <div class="items">margin-top:calc(200px - 20px * 9);</div>
        <div class="items">margin-top:calc(200px - 20px * 9);</div>
    </div>
    <div class="wrapper w3">
        <div class="items">width:calc(100% / 3 - (3 * 6px));<br>margin:calc(2px * 4 );<br>padding:calc(5 * 1rem - 3rem);</div>
        <div class="items";>width:calc(100% / 3 - (3 * 6px));<br>margin:calc(2px * 4 )</div>
        <div class="items";>width:calc(100% / 3 - (3 * 6px));<br>margin:calc(2px * 4 )</div>
    </div>
</body>

</html>


总结

calcflexbox搭配,用来写流式布局非常好;

  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

crper

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值