23

This question is related to these 2:
1. css - applying padding to box with scroll, bottom-padding doesn't work
2. Bottom padding not working on overflow element in non-chrome browsers

But I didn't find anywhere as to why it happens, meaning, why in Chrome(31) and Opera(18) the padding does appear, and in Firefox(26) and IE(9-10) it doesn't.

Here's my test case:
http://jsfiddle.net/eW39h/4/

A simpler example from the related question #1:
http://jsfiddle.net/rwgZu/

<div id="container">
    <div id="innerBox"></div>
</div>

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

#innerBox{
    height: 400px;
    background: #000;
}

I'm not really looking for a fix, but to understand what exactly is the correct implementation (and which browsers got it wrong :-)).

EDIT Dec 18th, 2013

Based on the answer by Marc Audet, I dug into the specs and made a new test case.
http://jsfiddle.net/rwgZu/79/

Here it's evident that all browsers clip the overflowing box at the same point, which is the padding-edge", which is indeed in accordance to the spec:

Whenever overflow occurs, the 'overflow' property specifies whether a box is clipped to its padding edge

And still, in Chrome there's an extra padding after the inner box.

Interesting though, that adding overflowing content inside the inner box leads to unified results on all browsers:
http://jsfiddle.net/uPY8j/1/

I could not find in the specs the rules for this type of conditions, so I'm leaving the question still open for now.

Community
  • 1
  • 1
Alex Ilyaev
  • 1,222
  • 1
  • 13
  • 17

2 Answers2

29

I have the same issue here and instead of using :last-child div (what if last child is hidden?) and margin-bottom trick (not so nice, the scroll bar will not reach the bottom) I use this:

#container {
  padding: 20px;
  padding-bottom: 0;
  overflow: auto;
}

#container:after {
  content: "";
  height: 20px;
  display: block;
}

So inserting a pseudo element will ensure my extra space, so I can use it for simulating my padding bottom value. What do you think?

JSFIDDLE HERE: http://jsfiddle.net/z72sn0p2/2/

ans777
  • 436
  • 5
  • 13
  • I don't remember what I did back then to overcome it, but using a pseudo element feels kinda hacky, though, I guess it's not so bad. I didn't use `:last-child`, not sure how it's related. Any way, would be nice if you'd add a Fiddle fork with your solution. Though it still doesn't answer why the difference between Chrome and Firefox, even today. – Alex Ilyaev Jun 27 '15 at 10:55
  • This is a very helpful answer - worked for me in FF, IE, Chrome, ...but not in Edge. Still trying to figure out a solution for Edge. – Ender2050 Sep 07 '15 at 14:40
  • Super helpful, confirmed working in FF, IE, Chrome and Edge – DannyB Jun 23 '16 at 16:06
  • Nice tweak ! Works ad advertised. – Matthieu Riegler Jan 29 '18 at 13:20
5

According to the W3 specification, overflowing content will be clipped to the edge of the padding box:

http://www.w3.org/TR/CSS21/visufx.html

FF takes the edge of the padding box to be the outer edge, which seems to be in accordance to the definition of the padding box:

http://www.w3.org/TR/CSS21/box.html

That being the case, FF seems to be closer to the spirit of the CSS specification wording, whereas Chrome seems to have decided to clip to the edge of the content box, which is the inner edge of the padding box.

To quote the specification:

The padding edge surrounds the box padding.

Does this mean the edge closer to the content box for the edge closer to the border?

I think that there is some ambiguity, leading to two interpretations. I suspect readers with an inclination towards pure mathematics and geometry may see it one way, and readers with a legal background may argue an alternative viewpoint.

In my opinion, the description of the box model is worded such that the progression of thought is from the inner content area towards the outer margin area. That being the case, I would think that the word "surrounds" would mean to enclose the outer edge of the area. Thus, I think FF is perhaps more right, but other developers at Chrome think otherwise.

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
Marc Audet
  • 46,011
  • 11
  • 63
  • 83
  • Thanks for the answer, definitely gave me a better direction to dig in. Though I think the spec is quite clear on what "padding-edge" means. Any way, I edited my question with new test cases and renamed the title. – Alex Ilyaev Dec 18 '13 at 14:26
  • 1
    Any thoughts on the new findings? – Alex Ilyaev Dec 25 '13 at 10:15
  • 1
    No thoughts today, busy with Santa on Skype! – Marc Audet Dec 25 '13 at 11:41
  • 2
    I'm more concerned from a "what makes sense" or "what is useful" perspective. Chrome's implementation is useful to a developer, as you have the option of adding extra space to the bottom of an overflowing div or remove it by controlling the padding. Otherwise, padding-bottom is useless in this scenario. – user2867288 Sep 08 '16 at 21:47