Understanding of mathematical expressions calc CSS ()

Mathematical expressions calc () is a function of the CSS, mainly for math. Use calc () provides a convenient way of thinking and a new layout for the page elements. This article describes calc () related content

Defined
  mathematical expression Calc () is an abbreviation calculate calculated, which allows the use of +, -, *, / four operators, can mix%, px, em, rem other calculation units

Compatibility: IE8-, safari5.1-, ios5.1-, android4.3- does not support, android4.4-4.4.4 only supports the addition and subtraction. IE9 does not support backround-position for

Note: + and - sides operator must have whitespace

<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>

专门建立的学习Q-q-u-n: 731771211,分享学习方法和需要注意的小细节,不停更新最新的教程和学习技巧
(从零基础开始到前端项目实战教程,学习工具,全栈开发学习路线以及规划)

application

Mathematical expressions calc () operation commonly used in digital layout of different units

<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>

Published 267 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45761317/article/details/104025080