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 want to give border on these divs. I want to write my rule like this.
#container { width: 50% - 1px; // I know this does not work. }
Why do browsers not support such arithmetic operations in CSS ?
And, How can I get this to work ?
You can perform addition, subtraction, multiplication, and division in CSS property values. This function is very useful for developing responsive web pages and applications.
calc() is a native CSS way to do simple math right in CSS as a replacement for any length value (or pretty much any number value). It has four simple math operators: add (+), subtract (-), multiply (*), and divide (/).
No. HTML is not a "programing" language. It has no math capabilities like that.
Arithmetic Operations are used to perform operations like addition, subtraction etc. on the values. To perform arithmetic operations on the data we need at least two values. Addition: It performs the sum of given numbers.
We are using HTML form to take the input values and choose an option to perform particular operation using Switch Case. Arithmetic Operations are used to perform operations like addition, subtraction etc. on the values. To perform arithmetic operations on the data we need at least two values.
JavaScript Arithmetic 1 Operators and Operands. The numbers (in an arithmetic operation) are called operands. ... 2 Adding 3 Subtracting. The subtraction operator ( -) subtracts numbers. 4 Multiplying. The multiplication operator ( *) multiplies numbers. ... 5 Remainder. The modulus operator ( %) returns the division remainder. ...
To perform arithmetic operations on the data we need at least two values. Addition: It performs the sum of given numbers. Subtraction: It performs the difference of given numbers.
It already exists; You can use the CSS3 calc()
notation:
div { background: olive; height: 200px; width: 200px; } div > div { background: azure; height: calc(100% - 10px); width: 100px; }
http://jsfiddle.net/NejMF/
Note: It's only supported in modern browsers (IE9+) and has only recently been adopted by mobile browsers.
Use box-sizing: border-box;
on your <div>
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With