81

I can’t get padding-bottom to work when I use overflow-y: auto on a box. I use Firefox.

#container {
  padding: 3em;
  overflow-x: hidden;
  overflow-y: auto;
  width: 300px;
  height: 300px;
  background: red;
}

#some_info {
  height: 900px;
  background: #000;
}
<div id="container">
  <div id="some_info"></div>
</div>

See the JSFiddle.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Philip
  • 6,827
  • 13
  • 75
  • 104

13 Answers13

73

One more solution without extra DIVs.

#container:after {
  content: "";
  display: block;
  height: 50px;
  width: 100%;
}

Working in FF, Chrome, IE8-10.

isHristov
  • 1,546
  • 2
  • 16
  • 18
  • 8
    Any way to make it work for the right side? In Firefox the horizontal scroll ends when the content is right on the edge of the box, and it ignores extra padding. – Nick Feb 12 '17 at 19:46
  • If you wouldn't mind explaining, why the 50px? Is this value specific to the css in the question or could it work for anyone? – Ethan Aug 28 '17 at 08:55
  • @Booligoosh, the 50px is the size for the "padding". To be more accurate, this should be 3em to reflect the example within the question. – isHristov Aug 30 '17 at 07:30
  • @isHristov so as I understand it 50px is just a demo value? – Ethan Aug 30 '17 at 11:05
  • @Booligoosh, that is correct. You can set any value you want and that will be the "padding". – isHristov Aug 31 '17 at 11:49
  • 1
    This works only if you remove the bottom padding from #container, otherwise you get a doubled bottom padding in Chrome. – kol Feb 24 '19 at 12:35
  • This did not work for me. I tried it in the same fiddle provided in the question – Aseem Jul 25 '20 at 22:01
  • Does not work on FireFox 89.0.2 - still doesn't respect bottom-margin. – TetraDev Jun 30 '21 at 23:20
19

I'm late to the party, but I thought it was worth adding a different solution that addresses some of the concerns raised above.

I came here because of exactly the kind of situation that @Philip raised in response to Alexandre Lavoie's solution: I have dynamically generated content inside the container, so I can't just apply styling to a specific div name like #some_info.

Happily, there's a simple solution for browsers that support CSS3: instead of applying bottom padding to the container, apply a bottom margin to the last child element inside the container.

#container > :last-child {
    margin-bottom: 3em;
}

As long as the last child element in the container div is a block-level element, this should do the trick.

DEMO: http://jsfiddle.net/rwgZu/240/

P.S. If Firefox's failure to scroll to the bottom of the padding is indeed a bug (as suggested by @Kyle), it still hasn't been fixed as of Firefox 47.0. Frustrating! Internet Explorer 11.0.9600.17843 exhibits the same behavior. (Google Chrome, in contrast, shows the bottom padding as expected.)

Dan Robinson
  • 457
  • 4
  • 10
8

The solutions above were not working for my needs, and I think I stumbled on a simple solution.

If your container and overflowing content share the same background color, you can add a top and bottom border with the color matching the background color. To create equal padding all around, set the border width equal to the left and right padding of the container.

Link to modified version of OP's fiddle: http://jsfiddle.net/dennisoneil/rwgZu/508/

A simple example below.

Note: Stack Overflow puts the snippet results into an overflow scroll, which makes it a little harder to see what's going on. The fiddle may be your best preview option.

#container {
  background: #ccc;
  overflow-y: scroll;
  height: 190px;
  padding: 0 20px;
  border-top: 20px solid #ccc;
  border-bottom: 20px solid #ccc;
}
#overflowing {
  background: #ccc;
}
<div id="container">
  <div id="overflowing">
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>
    This is content<br/>    
  </div>
</div>
Dennis O'Neil
  • 491
  • 5
  • 5
  • This kind of works, but it ends up pushing the scrollbar up from the bottom by the amount of the border thickness. So it looks weird. – TetraDev Jun 30 '21 at 23:21
4

Here is a possible approach that is working perfectly :

#container {
    overflow-x: hidden;
    overflow-y: auto;
    width: 300px;
    height: 300px;
}

#some_info {
    height: 900px;
    background: #000;
    border: 3em solid red;
}
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
3

Style the parent div normally and make the inner div do what you want it to do.

Remove overflow-x and overflow on #container, change height to 100% and add overflow-y:scroll; on #some_info

#container {
    padding: 3em;
    width: 300px;
    height: 300px;
    background: red;
}

#some_info {
    height: 100%;
    background: #000;
    overflow-y:scroll;
    width:100%;
}

Working Demo: http://jsfiddle.net/9yuohxuh/

Barrett Kuethen
  • 1,864
  • 7
  • 25
  • 33
2

For those who are looking for a simple solution and can change the DOM, put the overflow on the outer element and the padding on the inner element.

.scroll {
    overflow-x: hidden;
    overflow-y: auto;
}

.scroll__inner {
    padding: 3em;
}

In the example from the original question, it would look like this:

#container {
    overflow-x: hidden;
    overflow-y: auto;
    width: 300px;
    height: 300px;
    background: red;
}

#some_info {
    height: 900px;
    background: #000;
    padding: 3em;
    box-sizing: border-box; /* only needed if wanting padding to not be added to height */
}

Note the use of box-sizing: border-box here, which is only needed as the OP has a hardcoded height (generally bad practice but could be needed in edge cases), so adding this border-box enables the 3em padding to not increase the height, but pad inside the 900px.

A final note, I'd advise avoiding ID's for styling, mostly due to their extremely high specificity, see this post for more info on that.

fredrivett
  • 5,419
  • 3
  • 35
  • 48
1

Demo

Hi now used to this css

#container {
    padding: 3em;
    overflow-x: hidden;
    overflow-y: auto;
    width: 300px;
    height: 300px;
    background: red;
    padding-bottom:0; // add this line in your css
}

#some_info {
    height: 900px;
    background: #000;
    margin-bottom:3em; // add this line in your css
}

Demo

Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97
  • This works because there's no content. When you add content, it overlaps everything in there, even the padding! Any border, margin or other even container without content is collapsed inside the scrolling div (at least in chrome 43) – sergio Jul 04 '15 at 20:45
  • Doesn't work if you use `height: 100vh`, which is my case. – alejnavab Oct 07 '16 at 18:49
1

It's not only with bottom padding. Right padding/border/spacing is also ignored (you can't see it in your example because it has no content, and the width is not scrolling)

All the answers above fail in chrome 43, generating up to 3 scrollbars! or if the content overflows #some_info.

Example: http://jsfiddle.net/LwujL3ad/

If it worked for you, it's probably because the content was not as wide as the scrolling element, or fixed sized.

The right solution is:

Set #some info to display:table, and add padding or border to it, not to the scrolling container.

#container {
    overflow: scroll;
    width: 300px;
    height: 300px;
    background: red;
    padding-bottom:0;
}

    #some_info {
    display:table;
    border: solid 3em red;
    height: 900px;
    background: #000;
    margin-bottom:3em;
    color: white;
}

DEMO: http://jsfiddle.net/juh7802x/

The only element that doesn't fail, and respects ANY border and padding you add in there as separator is a TABLE.

I tried, and no matter if it's the next direct child or it's nested many items deep, any non-content styling will NOT expand to wrap the content, and will stay 100% width of the parent. Which is nonsense, because having content BIGGER than the parent is EXACTLY the scenario in which a scrolling div is required!

For a dynamic solution (both the container and the content) set the container of the elements inside the scrolling container to display:table.

sergio
  • 997
  • 1
  • 11
  • 15
1

Based on isHristov's answer:

#container {
    padding: 3em 3em 0 3em; /* padding-bottom: 0 */
    overflow-x: hidden;
    overflow-y: auto;
    width: 300px;
    height: 300px;
    background: red;
}

#container:after {
    content: "";
    display: block;
    height: 0;
    width: 100%;
    margin-bottom: 3em; /* length you wanted on padding-bottom */
}

However, his solution adds extra space in browsers that handle this situation properly.

Dan Robinson's answer is great too unless you have multiple elements, in #container, that are dynamically shown/hidden. In that case :last-child might target a hidden element and have no effect.

Community
  • 1
  • 1
LGT
  • 4,957
  • 1
  • 21
  • 22
1

You just need to add box-sizing: border-box to the same element where you applied the overflow rule.

Subbre
  • 75
  • 3
0

I think @-moz-document url-prefix() is what you need.

#container {
    padding: 3em;
    overflow-x: hidden;
    overflow-y: auto;
    width: 300px;
    height: 300px;
    background: red;
}

#some_info {
    height: 900px;
    background: #000;
}

@-moz-document url-prefix() {
  #container > :last-child {
    margin-bottom: 3em;
  }
}
<div id="container">
  <div id="some_info"></div>
</div>
dabeng
  • 1,340
  • 17
  • 7
0

The top answers did not work in FireFox 89. The only sensible solution I could think of is to use a div containing only a non-breaking space and with a fixed height set.

HTML

<div className="spacer">&nbsp;</div>

CSS

.spacer {
    height: 30px;
}

This works as it does not utilize margin or padding.

TetraDev
  • 16,074
  • 6
  • 60
  • 61
0

I have just faced this issue, it persists even in Firefox 87, version being released in 2021.

But it is finally fixed very recently. After update to Firefox 93 bottom padding with scroll works normally.

Mastikosa
  • 51
  • 2
  • 5