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

214 questions
43
votes
3 answers

How can I get a negative value of CSS variables (aka Custom Properties)

Let me start by showing you how I would do this in SCSS: $submenu-padding-left: 1.5em; transform: translateX(calc(-#{$submenu-padding-left} + .5em)); which would compile to: transform: translateX(calc(-1.5em - .5em)) Basically SCSS allows me to…
IOIIOOIO
  • 3,899
  • 4
  • 14
  • 19
40
votes
6 answers

css calc invalid property value

Can someone tell me why this CSS calc function isn't working? When I inspect element on Chrome, it says 'Invalid property value'. width: calc((100vw - 14px * 2) / (270px + 11px * 2));
Josh
  • 614
  • 1
  • 10
  • 17
31
votes
3 answers

Nested calc operations

Hopefully this is quite simple. I want to use the CSS calc operation to perform two calculations: I want to set my width to the equivalent of (100% / 7) - 2 However if I try to perform more than one operation in a CSS calc operation, it…
Liftoff
  • 24,717
  • 13
  • 66
  • 119
29
votes
3 answers

Can I use the auto value with the calc() property?

I wanted to set margin to auto plus some pixel amount using calc(), but my code doesn't seem to work. selector { width: 200px; height: 200px; margin-left: auto; margin-right: auto; background: red; margin: calc(auto +…
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
24
votes
8 answers

Using modulus in css calc function

Is there any way to use or implement modulus operator in css calc function? I know there is mod operator and IE supports it, but what about other browsers? For example #element-id{ width: calc( 100% mod 5 ); }
Hike Nalbandyan
  • 994
  • 1
  • 11
  • 28
23
votes
2 answers

Using calc() with tables

I'm trying to get a table with fixed-width tds and variable-width tds. Im using the CSS calc() function, but somehow it seems like I can't use % in tables. So that is what I have so far:
Marc Becker
  • 523
  • 2
  • 7
  • 18
21
votes
3 answers

How do I debug CSS calc() value?

I have relatively complex formulas e.g. transform: scale(var(--image-scale)) translateY(calc((1px * var(--element-height) * (var(--image-scale) - 1)) / 2 * var(--scrolled-out-y))) how do I debug calculated value? moreover is there a way to…
godblessstrawberry
  • 4,556
  • 2
  • 40
  • 58
21
votes
3 answers

Using n inside calc

can I use the n variable inside a calc expression? For example: .child:nth-child(n) { margin-left: calc(n * 46px); } My Code:
20
votes
5 answers

Can I use Calc inside Transform:Scale function in CSS?

I'm trying to calculate the right scale to apply to children inside a parent but i can't use calc() inside transform: scale CSS function. I've done this function with javascript but i'm interested in a pure CSS solution to avoid bloating the page…
Andu
  • 209
  • 1
  • 2
  • 5
18
votes
2 answers

jQuery animate() and CSS calc()

I tried to set CSS calc() using jQuery animate, for example: $element.animate({ height: 'calc(100% - 30px)' }, 500); and I noticed that calc() is not working with jQuery animate... I hope that there is a way to do it, I don't want a similar way…
neoDev
  • 2,879
  • 3
  • 33
  • 66
17
votes
2 answers

calc not working ie 11

After researching I confirmed that calc should work for ie8+ but it is not working for me. Here is a JS fiddle I made: http://jsfiddle.net/75tzyLoo/ here is the code: HTML:
CSS: #outer{ …
Ahmed-Anas
  • 5,471
  • 9
  • 50
  • 72
16
votes
2 answers

How to use calc() inside another function

Is it possible to use calc() inside of a CSS function like transform or translate? I cannot seem to get it working. Here is a demo so you guys can play around: http://jsfiddle.net/qdJwY/1/
cmplieger
  • 7,155
  • 15
  • 55
  • 83
16
votes
1 answer

calc() 100% + #px

I'm trying to achieve something I'm not even too sure is possible here. I'm trying to place a particular drop shadow PNG image under the nav for my site. However I also want to reuse this ability for any object I wish on the site, be it an image, a…
user1597713
  • 287
  • 1
  • 3
  • 7
14
votes
1 answer

CSS calc() in border-width?

Can I use calc() with border-width? I would like the following CSS to work: .my-element { border-left-width: calc(10% + 10px); border-right-width: calc(10% + 20px); } But for whatever reason, any value I provide with calc() results in no border…
MW.
  • 12,550
  • 9
  • 36
  • 65
13
votes
1 answer

Using CSS Variables with Calc() not working when 2nd parameter

I'm new to using CSS variables with calc() so forgive me if this is obvious, but I'm trying to perform the following calculation and can't for the life of me figure out why it's not working: $h2-font-size: 21px; --padding-top:…
Kessa
  • 133
  • 1
  • 1
  • 6
1
2
3
14 15