Questions tagged [css-calc]

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

The calc() CSS function can be used anywhere a <length>, <frequency>, <angle>, <time>, <number>, or <integer> is required. With calc(), you can perform calculations to determine CSS property values.

It is possible to nest calc() function, the internal ones being considered as simple parenthesis ().

Syntax

/* property: calc(expression) */
width: calc(100% - 100px);

Expressions

The expression can be any simple expression combining the following operators, using standard operator precedence rules:

+ Addition.

- Subtraction.

* Multiplication. At least one of the arguments must be a <number>.

/ Division. The right-hand side must be a <number>.

The operands in the expression may be any length syntax value. You can use different units for each value in your expression, if you wish. You may also use parentheses to establish computation order when needed.

You can see a couple of use cases for calc()

189 questions
1593
votes
7 answers

Sass Variable in CSS calc() function

I'm trying to use the calc() function in a Sass stylesheet, but I'm having some issues. Here's my code: $body_padding: 50px body padding-top: $body_padding height: calc(100% - $body_padding) If I use the literal 50px instead of my…
GJK
  • 33,798
  • 7
  • 52
  • 72
453
votes
5 answers

Disable LESS-CSS Overwriting calc()

Right Now I'm trying to do this in CSS3 in my LESS code: width: calc(100% - 200px); However, when LESS compiles it is outputting this: width: calc(-100%); Is there a way to tell LESS not to compile it in that manner and to output it normally?
AJStacy
  • 5,035
  • 3
  • 14
  • 31
343
votes
4 answers

Less aggressive compilation with CSS3 calc

The Less compilers that I'm using (OrangeBits and dotless 1.3.0.5) are aggressively translating body { width: calc(100% - 250px - 1.5em); } into body { width: calc(-151.5%); } Which is obviously not desired. I'm wondering if there is a way to…
Nick Babcock
  • 5,842
  • 3
  • 24
  • 39
70
votes
6 answers

CSS Calc alternative

I am trying to dynamicly change the width of a div using CSS and no jquery. The following code will work in the following browsers: http://caniuse.com/calc /* Firefox */ width: -moz-calc(100% - 500px); /* WebKit */ width: -webkit-calc(100% -…
H3AP
  • 998
  • 1
  • 8
  • 16
67
votes
2 answers

How to use a Stylus variable in calc?

In Stylus, how do I use a variable in a calc expression? For example, the following doesn't work (arrow-size being a variable): arrow-size = 5px left calc(50% - arrow-size)
aknuds1
  • 57,609
  • 57
  • 177
  • 299
63
votes
4 answers

CSS Calc Viewport Units Workaround?

From what I've seen in other answers, CSS viewport units can't be used in calc() statements yet. What I would like to achieve is the following statement: height: calc(100vh - 75vw) Is there some workaround way I can achieve this using purely CSS…
golmschenk
  • 9,361
  • 17
  • 69
  • 117
61
votes
1 answer

Is there a way to use variables in Less for the ~ operator, like ~"calc(100% - @spacing)";

Is there a way to use variables in less ~ operator, like ~"calc(70% - @spacing)"; When I have tried it it only works with static values like ~"calc(70% - 10px)"; Can I get the string to be evaluated prior to beeing set as a property.
patricjansson
  • 703
  • 1
  • 6
  • 8
51
votes
3 answers

Why must a + or - be surrounded with whitespace from within the Calc() method?

Recently I've started using the calc(...) method within CSS. I quickly learned that code such as: width: calc(100%-2) will not work, though adding white-space before and after the - operator will fix the problem and the calc method will function…
RLH
  • 13,948
  • 19
  • 85
  • 175
49
votes
4 answers

calc() not working within media queries

@media screen and (max-width: calc(2000px-1px)) { .col { width: 200px; } } The value after subtraction should be 1999px, however it does not seem to be working. If I manually change it to 1999px it works fine, so I know it's not a problem with…
AndSmith
  • 613
  • 1
  • 6
  • 10
48
votes
3 answers

Combine calc() with attr() in CSS

I was wondering whether I can combine the calc() function with the attr() function to achieve something like the following:
This box should have a width of 100px
This…
Loupax
  • 4,046
  • 4
  • 36
  • 63
44
votes
3 answers

calc() function inside another calc() in CSS

How do I use a CSS calc function inside another CSS calc function? According to this post it is possible but there is no example of that.
Raghavendra Prasad
  • 519
  • 1
  • 4
  • 10
42
votes
3 answers

css calc - round down with two decimal cases

I have the following situation: div { width: calc((100% / 11) - 9.09px); } In the context, 100% = 1440px, and 9.09px is generated in mathematics with sass. The results is: 94.55px, because calc rounds it up, but I need 94.54px (round down). How…
Cleiton Souza
  • 720
  • 1
  • 9
  • 21
40
votes
2 answers

CSS calc() - Multiplication and Division with unit-ed values

Is it possible to use calc() to multiply or divide with unit-based values (like 100%, 5px, etc). For example I was hoping to do something like this: width: calc(100% * (.7 - 120px / 100%)); Hoping it resolved to something like (assuming 100% =…
samanime
  • 21,211
  • 7
  • 69
  • 122
33
votes
1 answer

How can I use CSS calc() with inherit?

I would like to use inherit with calc() like this: #foo { animation-duration: 10s; } #foo > .bar { animation-duration: calc(inherit + 2s); /* =12s */ } But it don't seem to works. Is it browsers bug or specs?
Yukulélé
  • 11,464
  • 8
  • 52
  • 76
29
votes
2 answers

Can I use CSS calc within Javascript?

Can I use the css calc() function when setting positions in JavaScript? ePopup.style.top = "calc(100px - 1.5em)";
NickC
  • 953
  • 4
  • 13
  • 22
1
2 3
12 13