Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html, body 100% causing scrollbar to appear

Tags:

html

css

http://codepen.io/basickarl/pen/Wrwdam?editors=110

html:

<div class="back">
</div>

css:

html,
body {
    height: 100%;
}

div.back {
    margin-top: 50px;
    display: absolute;
    width: 30px;
    height: 30px;
    background-image: url('http://simpleicon.com/wp-content/uploads/arrow-35.png');
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
}

The scrollbar on the right displays. I have to have html, body at 100% because of a sticky footer I'm using. Any ideas?

like image 480
basickarl Avatar asked Dec 18 '15 14:12

basickarl


People also ask

How do I make my body 100% in HTML?

Try setting the height of the html element to 100% as well. Body looks to its parent (HTML) for how to scale the dynamic property, so the HTML element needs to have its height set as well. However the content of body will probably need to change dynamically. Setting min-height to 100% will accomplish this goal.

How do I fix the scrollbar in HTML?

The easy fix is to use width: 100% instead. Percentages don't include the width of the scrollbar, so will automatically fit. If you can't do that, or you're setting the width on another element, add overflow-x: hidden or overflow: hidden to the surrounding element to prevent the scrollbar.

How do I force scrollbar to show in HTML?

To show the scrollbars always on the webpage, use overflow: scroll Property. It will add both horizontal and vertical scrollbars to the webpage. To add only horizontal scrollbar use overflow-x: scroll property and for vertical scrollbar use overflow-y: scroll property.


2 Answers

the body element has a default margin: 8px; in most major browsers so first you have to remove that by resetting the margin property on the body element

body {
    margin: 0;
}

Then, instead of using height: 100%; on the body use min-height: 100%; so that the body can also grow beyond 100% height when used in conjunction with overflow: hidden;

Since min-height doesn't work if the parent height isn't explicitly set, a height attribute has to be added to the html element as well.

html {
    height: 100%;
}

body {
    margin: 0;
    min-height: 100%;
    overflow: hidden;
}

Also, your div.back selector has an invalid value on the display property. the display property accepts inline, inline-block, block, none, flex, inline-flex, grid etc... whereas the position property accepts static (default for every element), absolute, relative or fixed. This is not a problem but rather something the browser just ignores as it doesn't understand it.

div.back {
    display: absolute; // <------ remove this line
    //... snipped ...
}
like image 68
SidOfc Avatar answered Oct 26 '22 09:10

SidOfc


It's partially because the default browser style sheets add some margins and/or padding. Resetting them to 0 will fix part of it (JSFiddle).

However you've also got a div with some margins applied (50px). The way the box-model works will mean you'll need to minus that from your 100%. You can use calc if you really need to (or just change it to padding):

height: calc(100% - 50px);

Example using the above code: http://codepen.io/anon/pen/mVPpYK

Example using padding: http://codepen.io/anon/pen/OMNzqB?

like image 44
Chuck Le Butt Avatar answered Oct 26 '22 09:10

Chuck Le Butt