Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform arithmetic operations in CSS?

Tags:

css

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 ?

like image 305
Sachin Avatar asked Apr 23 '13 04:04

Sachin


People also ask

Can I do arithmetic in CSS?

You can perform addition, subtraction, multiplication, and division in CSS property values. This function is very useful for developing responsive web pages and applications.

Can you do addition in CSS?

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 (/).

Can we perform arithmetic operations in HTML?

No. HTML is not a "programing" language. It has no math capabilities like that.

What is the use of arithmetic operations?

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.

How to perform arithmetic operations using switch case in HTML?

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.

What are the arithmetic operators in JavaScript?

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

How many values are needed to perform arithmetic operations?

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.


2 Answers

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.

like image 157
Adrift Avatar answered Sep 21 '22 16:09

Adrift


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

like image 41
Ross Allen Avatar answered Sep 22 '22 16:09

Ross Allen