Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do mathematics inside CSS?

I have jquery accorion id #accordion and some of the content inside header class name .simpleColor. Now I want to give a calculated margin to .simpleColor. In pseudo...it looks something like this,

.simpleClass {     margin-left: ((#accordion.width/2) - (.simpleColor.width/2)); } 

I am open to use any other technology such as javascript to achieve this if it is possible.

like image 354
TeaCupApp Avatar asked Apr 30 '12 03:04

TeaCupApp


People also ask

Can you do math with HTML?

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

What is Calc function in CSS?

calc() The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used anywhere a <length> , <frequency> , <angle> , <time> , <percentage> , <number> , or <integer> is allowed.


1 Answers

There's a CSS function called calc that is starting to get pretty good support. The syntax works as followed:

width: calc(50% - 100px); 

(Note that the whitespace around the operators is significant)

This allows true dynamic computational support in CSS. With a preprocessor, you can only combine static lengths with static lengths, and relative lengths with relative ones.

At this point in time calc has achieved widespread support and should be safe to use unless you have specific needs for supporting legacy browsers.

like image 199
TimE Avatar answered Sep 19 '22 03:09

TimE