简体   繁体   English

CSS `height: calc(100vh);` VS`height: 100vh;`

[英]CSS `height: calc(100vh);` Vs `height: 100vh;`

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?) (这是问这个问题的正确地方吗?)

VH VH
height: 100vh; means the height of this element is equal to 100% of the viewport height.表示此元素的高度等于视口高度的 100%。

example: height: 50vh;例如: height: 50vh;
If your screen height is 1000px, your element height will be equal to 500px (50% of 1000px).如果您的屏幕高度为 1000 像素,则您的元素高度将等于 500 像素(1000 像素的 50%)。

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).如果您的屏幕高度为 1000 像素,则您的元素高度将等于 900 像素(1000 像素的 100% 减去 100 像素)。

*I think your former developer didn't need to use calc() if he/she didn't want to calculate value. *我认为如果你的前开发人员不想计算价值,他/她不需要使用calc()

没有区别,因为无论calc(100vh)计算表达式calc(100vh) ,它总是以100vh结束。

The calc() CSS function lets you perform calculations when specifying CSS property values calc() CSS 函数可让您在指定 CSS 属性值时执行计算

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) (前开发人员使用它的原因可能是他通常在任何地方都使用它,之后添加计算会更容易)

Basically calc() function allows mathematical expressions with addition (+), subtraction (-), multiplication (*), and division (/) to be used as component values.基本上 calc() 函数允许使用加法 (+)、减法 (-)、乘法 (*) 和除法 (/) 的数学表达式作为分量值。

Now in your case, both are same as you have not used any calculation.现在在您的情况下,两者都与您没有使用任何计算相同。 So you can use height: 100vh所以你可以使用高度:100vh

A common use case is that at first he substracted the header or any other element.一个常见的用例是,首先他减去标题或任何其他元素。 calc(100vh - 80px).计算(100vh - 80px)。

calc() is just used to calculate the value inside the parenthesis. calc()仅用于计算括号内的值。

Example height: calc(100vh - 60px);示例高度: calc(100vh - 60px);

Here the height will be lesser by 60px in viewport height.这里的高度将比视口高度小 60px。

Note: Remember to use space before the operator and after the operator if its not working.注意:如果操作符不工作,请记住在运算符之前和运算符之后使用空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM