Html set body height to 100

Making html and body stretch to 100% height and more if the page scrolls

Which works a treat if the web page isn’t tall enough for the user to scroll, however on a long webpage it only goes to the height of the viewport on the users browser. I also tried:

I guess you’re doing that because you want a footer that will always be at the bottom of the page? If so, just google «sticky footer».

Nope, I just have a sidebar and content div which is meant to be 100% of the page height, but since I have to add height: 100% to make it the entire height of the users viewport, it also cuts off when the user scrolls.

6 Answers 6

It sounds like your goal is for the body to

  • always cover the screen (with minimal or no content)
  • be able to stretch (if there is a ton of content)

if that’s the case the following snippet should help

/* this looks like it should work html, body < min-height: 100%; >but this ↓ does the trick */ html, body < padding: 0; margin: 0; >html < height: 100%; >body

That should set the height of the elements to the viewport and if they go beyond, it will have a scrollbar, here is an example:

Читайте также:  Function with if else in php

(or add a container div with an ID, etc if you have issues with some browsers.)

EDIT: Adding full code example below:

  
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Источник

HTML vs Body: How to Set Width and Height for Full Page Size

Dave Gray

Dave Gray

HTML vs Body: How to Set Width and Height for Full Page Size

CSS is difficult but also forgiving. And this forgiveness allows us to haphazardly throw styles into our CSS.

Our page still loads. There is no «crash».

When it comes to page width and height, do you know what to set on the HTML element? How about the body element?

Do you just slap the styles into both elements and hope for the best?

If you do, you’re not alone.

The answers to those questions are not intuitive.

I’m 100% guilty of applying styles to both elements in the past without considering exactly which property should be applied to which element. 🤦‍♂️

It is not uncommon to see CSS properties applied to both the HTML and body elements like this:

Does It Matter?

The above style definition creates a problem:

Setting min-height to 100% on both elements does not allow the body element to fill the page like you might expect. If you check the computed style values in dev tools, the body element has a height of zero.

Meanwhile, the HTML element has a height equal to the visible part of the page in the browser.

Look at the following screenshot from Chrome Dev Tools:

empty_body

Why Does This Happen?

Using a percentage as a size value requires the element to reference a parent to base that percentage on.

The HTML element references the viewport which has a height value equal to the visible viewport height. However, we only set a min-height on the HTML element. NOT a height property value.

Therefore, the body element has no parent height value to reference when deciding what 100% is equal to.

And The Problem May Be Hidden

If you started out with enough content to fill the body of the page, you might not have noticed this issue.

And to make it more difficult to notice, if you set a background-color on both elements or even on just one of them, the viewport is full of that color. This gives the impression the body element is as tall as the viewport.

It’s not. It’s still at zero.

The image above is taken from a page with the following CSS:

Reverse-inheritance?

In a strange twist, the HTML element assumes the background-color of the body element if you don’t set a separate background-color on the html element.

So What is the Ideal Height Setting for a Full Responsive Page?

For years, the answer was the following:

This allows the HTML element to reference the parent viewport and have a height value equal to 100% of the viewport value.

With the HTML element receiving a height value, the min-height value assigned to the body element gives it an initial height that matches the HTML element.

This also allows the body to to grow taller if the content outgrows the visible page.

The only drawback is the HTML element does not grow beyond the height of the visible viewport. However, allowing the body element to outgrow the HTML element has been considered acceptable.

The Modern Solution is Simplified

This example uses vh (viewport height) units to allow the body to set a minimum height value based upon the full height of the viewport.

Like the previously discussed background-color, if we do not set a height value for the HTML element, it will assume the same value for height that is given to the body element.

Therefore, this solution avoids the HTML element overflow present in the previous solution and both elements grow with your content!

The use of vh units did cause some mobile browser issues in the past, but it appears that Chrome and Safari are consistent with viewport units now.

Page Height May Cause a Horizontal Scrollbar

Shouldn’t this say «Page Width»?

In another strange series of events, your page height may activate the horizontal scrollbar in your browser.

When your page content grows taller than the viewport height, the vertical scrollbar on the right is activated. This can cause your page to instantly have a horizontal scrollbar as well.

So What is the Fix?

You may sleep better knowing it starts with a page width setting.

This problem arises when any element — not just the HTML or body element — is set to 100vw (viewport width) units.

The viewport units do not account for the approximate 10 pixels that the vertical scrollbar takes up.

Therefore, when the vertical scrollbar activates you also get a horizontal scrollbar.

How to Set the Page for Full Width

Not setting a width on the HTML and body elements will default to the full size of the screen. If you do set a width value other than auto, consider utilizing a CSS reset first.

Remember, by default the body element has 8px of margin on all sides.

A CSS reset removes this. Otherwise, setting the width to 100% before removing the margins will cause the body element to overflow. Here’s the CSS reset I use:

How to Set Width to Your Preference

While it may not always be necessary to set a width, I usually do.

If you set the width to 100% on the body element you will have a full page width. This is essentially equivalent to not setting a width value and allowing the default.

If you want to use the body element as a smaller container and let the HTML element fill the page, you could set a max-width value on the body.

Conclusion

With no height value provided for the HTML element, setting the height and/or min-height of the body element to 100% results in no height (before you add content).

However, with no width value provided for the HTML element, setting the width of the body element to 100% results in full page width.

This can be counterintuitive and confusing.

For a responsive full page height, set the body element min-height to 100vh.

If you set a page width, choose 100% over 100vw to avoid surprise horizontal scrollbars.

I’ll leave you with a tutorial from my YouTube channel demonstrating the CSS height and width settings for an HTML page that is full screen size and grows with the content it contains:

Do you have a different way of setting the CSS width and height that you prefer?

Источник

How to fill 100% of remaining height?

Contents of (1) as shown above are unknown, as it may increase or decrease in dynamically generated pages. The second div (2) as shown above, should fill the remaining space. here is an example of my html

Do you want the height of 1 + 2 to be the exact height of the viewport? Or 2 is the same size as the viewport and it scrolls down equal to the height of 1?

8 Answers 8

You should be able to do this if you add in a div ( #header below) to wrap your contents of 1.

  1. If you float #header , the content from #someid will be forced to flow around it.
  2. Next, you set #header ‘s width to 100%. This will make it expand to fill the width of the containing div, #full . This will effectively push all of #someid ‘s content below #header since there is no room to flow around the sides anymore.
  3. Finally, set #someid ‘s height to 100%, this will make it the same height as #full .
 
Contents of 1
Contents of 2
html, body, #full, #someid < height: 100%; >#header

I think it’s worth mentioning that flexbox is well supported across modern browsers today. The CSS could be altered have #full become a flex container, and #someid should set it’s flex grow to a value greater than 0 .

html, body, #full < height: 100%; >#full < display: flex; flex-direction: column; >#someid

I think you are right but my problem couldn’t be solved but anyway this helped me to understand about this. So I’m accepting your answer.

Your answer works beautifully, but I am slightly confused as to why it works: If you play with the background colors, you can see that #someid is ALWAY equal to the height of #full , despite the height of #header . Logically, the content of #someid should start at the top of #full . Why does #header push the content down?

The problem with this is it makes #someid the same height as #full , so #someid overflows #full by the height of #header . It doesn’t set #someid to 100% of the remaining space. If you set overflow: scroll; on #someid you would see the problem.

Yeah, this answer should be updated. It was a hack that abused the fact that float pushed content down, which was a «good enough» at the time when dealing with browsers that didn’t support things like calc and flexbox layouts weren’t widely supported. Look how far we’ve come in less than 3 years!

To get a div to 100% height on a page, you will need to set each object on the hierarchy above the div to 100% as well. for instance:

Although I cannot fully understand your question, I’m assuming this is what you mean.

This is the example I am working from:

Style is just a replacement for the CSS which I haven’t externalised.

if you want someid to also be 100% then you have to add 100% to it, and set the container to 100% too. I’ve edited my question to show this.

Try the example I posted, You might just be missing some thing along the hierarchy which is screwing it up.

 html, body < height: 100%; >.parent < display: flex; flex-flow:column; height: 100%; background: white; >.child-top < flex: 0 1 auto; background: pink; >.child-bottom
  
This child has just a bit of content
And this one fills the rest

Hello @arkascha, I’ve just updated my post with a working code example. Thanks for the suggestion. Cheers.

This can be done with tables:

Then apply css to make someid fill the remaining space:

Now, I can just hear the angry shouts from the crowd, «Oh noes, he’s using tables! Feed him to the lions!» Please hear me out.

Unlike the accepted answer which accomplishes nothing aside from making the container div the full height of the page, this solution makes div #2 fill the remaining space as requested in the question. If you need that second div to fill the full height allotted to it, this is currently the only way to do it.

But feel free to prove me wrong, of course! CSS is always better.

I know this is a late entry but even in 2016 I am surprised by the complete lack of IE support for flex (currently 11 is the only one to support it and its majorly buggy at that http://caniuse.com/#feat=flexbox) which from a business perspective is not great! So I think until IE is shut down the best solution and most cross-browser friendly one surely must be a JS/Jquery one?

Most sites already use Jquery and a very simple example (for my code) is:

$('#auto_height_item').height($(window).height() - $('#header').height()); 

You can obviously replace window and header and let the basic math do the work. Personally I’m still not convinced about flex yet.

Источник

body height not filling 100% page height

I can’t figure this one out. I’ve got a simple set of divs with a header, sidebar and content area. The header is full width, the side and content are floated left. I need the sidebar (for the background) to fill 100% of the page height, but when I inspect element in chrome, the is actually ending long before the bottom of the page, which seems to be limiting the height of my sidebar. What is keeping the from filling the full page here?

  • homeHome
  • homeUsers
    • Manage users
    • Add a user
  • homeClients
    • Manage clients
    • Add a client
  • homeProjects
    • Manage projects
    • Create a project
    • Submitted projects
    • Closed projects
  • homeAccount
  • helpHelp

homeHelp

Curabitur blandit tempus porttitor. Cras mattis consectetur purus sit amet fermentum. Cras mattis consectetur purus sit amet fermentum. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Maecenas faucibus mollis interdum. Maecenas sed diam eget risus varius blandit sit amet non magna.

8 Answers 8

Looks like you have float:left applied on your children. Use this code :

I have the same problem; its so annoying.. I tried the overflow but it did not work. Can you take a look omarhabash.com/wp/?page_id=54.

Источник

Оцените статью