Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

How to calculate size of navbar to subtract from div's 'min-height: 100vh' property...

I realize my title is a little confusing but basically it's pretty simple I imagine.

I have a navbar and then a div section that takes up the remaining space of the viewport.

I set the div's min-height: 100vh but that is a little too much given I need to account for the navbar. I'm writing in Sass so I know it allows for math operations I'm just not sure how.

Also, is this a good approach or would it make more sense to have the div's background-size be 'cover' even though all I want is a white background?

Thanks!

1 Answer

Jonathan Selwall
Jonathan Selwall
12,528 Points

So lets say you have your headers height in a variable like this:

$headerHeight: 100px;

Then on the div that's supposed to cover the rest do this.

.myclass{
  min-height: calc( 100vh - $headerHeight);
}

Just be aware that "calc" wont work in some old browsers, take a look at this link about support: http://caniuse.com/#search=calc

If that background is not and image and only a white color you wont need background-size: cover;

Oh, I should have mentioned I don't know the height of the navbar (I could inspect it, etc) but I was wondering if there was a dynamic way of coding it in case the height changes it automatically recalculates.

On further thinking though I think I'll just explicitly set the height of it.

Thanks Jonathan!

Jonathan Selwall
Jonathan Selwall
12,528 Points

Haha okay, well that should be possible to do with js without too much trouble! But if you dont have any real good reason for not setting the header height, I'd do that instead and save me the trouble!