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
0
votes
0 answers

How to use calc() to get the current height of this element?

I have a div inside another div. I want to make the inside one in the middle vertically. I can use this way to achieve it: calc(50% - 100px ) /* if the height of the inside box is 100px */ However, sometimes the inside box has a variable height, so…
AGamePlayer
  • 7,404
  • 19
  • 62
  • 119
0
votes
2 answers

Using percents as multipliers with CSS calc()

Let's say I define a CSS variable --half to be equal to 50%. :root { --half: 50%; } Then I can set the width of something to be 50% like this: .partial { width: var(--half); } So far so good. Let's say that I have a tall element that I want to…
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
0
votes
1 answer

CSS calc() not working for centring the td element

I want to create a table, the second column (which is represented by a single cell) is put in the center of a page. I am trying to use calc() function in order to do that, however it does not work. Here is my code: .bio { text-align: center; …
0
votes
0 answers

Can css variables be used between different selector?

I'm exploring css-variables and css calc(), so while the following code example is somewhat contrived, I'm trying to experiment with variables and calc using centering of one div withing another, which contains yet another div with the eventual goal…
user2505564
0
votes
2 answers

css variables calc var min, nested calculation based on viewport width and height

For a web app I want to calculate some elements width and height width pure css, based on vw (viewport width) and vh (viewport height), depending of which one is smaller. My approach is div#top_panel > ul > li { /*...*/ --w: calc(vw / 8); …
ddlab
  • 918
  • 13
  • 28
0
votes
1 answer

Adding a unit to calc

When using calc, how can you add a unit? I have tried the following: font-size: calc(((768 / 100) * 6) / 16)rem; font-size: calc(((768 / 100) * 6) / 16) rem; font-size: calc(((768 / 100) * 6) / 16) + rem; font-size: unit(calc(((768 / 100) * 6) /…
ccdavies
  • 1,576
  • 5
  • 19
  • 29
0
votes
0 answers

How can I create a css grid layout where only part of it is scrollable - without using a calc function?

I'm trying to make this view using grid layout where only a part of the content is scrollable. In the example below, only 'master-list' should be scrollable. The problem is in this line: grid-template-rows: 40px calc(100vh - 40px); I want to be able…
lilotop
  • 527
  • 6
  • 12
0
votes
1 answer

Zoom with transform:matrix(): how to stick to the left edge (alter zoom center)?

My client wants to have zoom functionality (I found that this is easy to implement with transform: scale() but after that he wants to have the top-left edge of a node at the same position. Sadly in school instead of math I played to WarCraft III and…
Vitaly Zdanevich
  • 13,032
  • 8
  • 47
  • 81
0
votes
1 answer

Calculate width in scss

I have a little question, I don't understand what does it mean in css file (100% - 60px) / 2) = ????? Thanks for help
0
votes
1 answer

How to correct CSS font-size calculation to limit font size

I am working on a class that I am applying to my header bar (header-top-container) in CSS, what I want to do is depending on the screen size have the header's h1 text size be dynamically adjusted with a max size of 28px. When I test it out, the…
psycoperl
  • 121
  • 1
  • 15
0
votes
0 answers

CSS calc values computed differently on Edge

I have this scss code snippet: @include flex(1 1 calc((1 * (100% + (2 * #{$spec_grid_outerMargin})) / 3) - #{$spec_grid_outerMargin})); In the dev console of Edge at my local machine this is translated to: flex: 1 1 calc((1 * (100% + (2 *…
GarfieldKlon
  • 11,170
  • 7
  • 31
  • 33
0
votes
0 answers

CSS calc does not work when subtracting from percentage [Opera]

A: width: calc((100% - SOME_INTEGER_NUMBERpx) / 2) B: width: calc((100%) / 2) Original style rule of the element is A. When inspecting element with DEV tools (Opera) A style is not marked as invalid but it does not work either. When changing calc…
user435421
  • 859
  • 2
  • 13
  • 31
0
votes
1 answer

Problems with Calc in Less

I try to define a variable to get dynamic height and do some operations on it; But the code failed to compile. How can I deal with the problem? Thanks. "less": "3.9.0", @block-height: calc(~"(100vh - 110px) / 3"); .block1{ height: @block-height -…
knyxs
  • 3
  • 1
0
votes
2 answers

Linear-gradient or calc() not working in IE11

I have a problem regarding the linear-gradient, IE11 and maybe calc() function. This code snippet acts different on Chrome and IE11. background: linear-gradient(to top right, transparent calc(50% - 1px), #ccc, transparent calc(50% +…
0
votes
0 answers

nesting multiple things in css-calc with multiple units

I've been trying to do a complicated calculation in CSS to get rid of media queries on a page, and not require too many of them. The second part of the code below with the long decimal number is me trying to use a set number to calculate the…