40

I'm working on a project where the former developer used:

.main-sidebar {
    height: calc(100vh);
}

I have no way to contact him/her anymore, and I would like to understand what is the difference (if any) between the two methods.

(Is this the right place to ask this question?)

Adriano
  • 3,788
  • 5
  • 32
  • 53
  • Possible duplicate of [Unnecessary use of calc() in MDN example?](https://stackoverflow.com/questions/47888375/unnecessary-use-of-calc-in-mdn-example) (this isn't an MDN example but it features the same use of calc() with only one value) – BoltClock Oct 23 '18 at 04:50
  • 2
    "(Is this the right place to ask this question?)" Yes, don't worry. – BoltClock Oct 23 '18 at 04:51

6 Answers6

57

VH
height: 100vh; means the height of this element is equal to 100% of the viewport height.

example: height: 50vh;
If your screen height is 1000px, your element height will be equal to 500px (50% of 1000px).

CALC
height: calc(100% - 100px); will calculate the size of the element by using the value of the element.

example:
height: calc(100% - 100px); If your screen height is 1000px, your element height will be equal to 900px (100% of 1000px and minus 100px).

*I think your former developer didn't need to use calc() if he/she didn't want to calculate value.

Adriano
  • 3,788
  • 5
  • 32
  • 53
36

There's no difference, since any time the expression calc(100vh) is calculated, it always ends up being 100vh.

jameslittle230
  • 3,098
  • 1
  • 14
  • 7
9

calc() is just used to calculate the value inside the parenthesis.

Example height: calc(100vh - 60px);

Here the height will be lesser by 60px in viewport height.

Note: Remember to use space before the operator and after the operator if its not working.

Elikill58
  • 4,050
  • 24
  • 23
  • 45
8

The calc() CSS function lets you perform calculations when specifying CSS property values

you may want to refer this Path

(The reason former developer used this may be that he's conventionally using it everywhere and it'd be easier to add calculations afterwards)

Kondal
  • 2,870
  • 5
  • 26
  • 40
3

A common use case is that at first he substracted the header or any other element. calc(100vh - 80px).

3

Basically calc() function allows mathematical expressions with addition (+), subtraction (-), multiplication (*), and division (/) to be used as component values.

Now in your case, both are same as you have not used any calculation. So you can use height: 100vh

DPS
  • 943
  • 11
  • 22