How to perform arithmetic operations in CSS?

后端 未结 3 480
情话喂你
情话喂你 2020-12-25 11:30

I want to know how can I achieve an arithmetic operation in CSS.

For example: I want to align two divs side by side each having width of 50% and I w

3条回答
  •  不思量自难忘°
    2020-12-25 12:07

    Use box-sizing: border-box; on your

    to make borders part of the width calculation. The default value for box-sizing is content-box, which does not include padding or border in the width attribute.

    #container {
      border: 1px solid black;
      -moz-box-sizing: border-box;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      width: 50%;
    }
    

    Paul Irish comments on the use of calc() and suggests using border-box because it better matches our mental model of "width".

提交回复
热议问题