4

I'm trying to use the calc() function, but it return the wrong value.

I'm using this:

height: calc(100% - 45px);  
height: -webkit-calc(100% - 45px);  
height: -moz-calc(100% - 45px);  

..but it sets the height to 55%. Why? What am I doing wrong here?

http://jsfiddle.net/L7x9j/

12
  • 3
    Though I don't know the answer to your question, I do know that height: -webkit-height: calc(...); is not valid CSS. I think you want height: -webkit-calc(...);. Commented May 14, 2014 at 20:26
  • 1
    In my code it's just like @Jordan pointed... SOrry, my bad! I just wrote wrong here. =s Commented May 14, 2014 at 20:32
  • 1
    In which browser (and version) do you get the wrong result? And how did you check that the resulting height is 55%? Commented May 14, 2014 at 20:33
  • 3
    @PlayHardGoPro In the example, you explicityly set calc(55%). Did you add the wrong jsFiddle? Commented May 14, 2014 at 20:49
  • 2
    possible duplicate of Less Aggressive Compilation with CSS3 calc Commented May 14, 2014 at 21:18

2 Answers 2

10

I had a same problem, Calc() was calculating wrong %age.

Keep strict math set to on.

height: ~"calc(100% - 50px)";

This fixed the issue for me.

Sign up to request clarification or add additional context in comments.

Comments

4

There is nothing wrong with calc(), it works. You just need to set a height of 100% for the body/html elements in order for it to work as desired.

Compare this example (without html, body { height:100%; }) to this fixed example.

html, body {
    height:100%;
}
#mid-content {
    width: 100%;
    height: calc(100% - 50px);
    height: -webkit-calc(100% - 50px);
    height: -moz-calc(100% - 50px);
    border:1px solid red;
} 

Additionally, it's worth noting that the header is 50px, not 45px. I also added box-sizing:border-box in order to include borders/padding in the element's box model calculations.

You may notice that there is space at the bottom if the screen size is less than ~700px. That's because of the following:

#mid-content h1 {
    width: 300px;
    height: 250px;
    font-size: 58px;
    line-height: 1em;
    font-family:'Oswald', sans-serif;
    margin: 100px auto 70px auto;
    color: white;
}

Removal of the height/margin fixes it. (example)

7 Comments

I think the problem is with the .less file, isn't it? Or your answer is the reason to .less compiler set the calc(55%) ?
@PlayHardGoPro See the updated answer. Is that what you were after?
@JoshCrozier the problem is that the OP uses LESS to create the final css file and that LESS already produces the incorrect calc(55%).
The LESS problem is solvable and has been answered several times before, e.g.: stackoverflow.com/questions/11972084/…
@PlayHardGoPro No, you can still use LESS. Use height: ~"calc(100% - 50px)"; see: stackoverflow.com/questions/17904088/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.