24

I'm experiencing strange behavior in Firefox (v35 v39 v52) regarding percentage padding. I cannot reproduce this issue in Chrome.

I have an element with top padding set as a percentage, like this:

p {
    padding:10% 0 0;
    margin:0 0 1em;
    background-color:#CCC;
}

Percentage padding on an element is relative to its parent's width. So, I expect that the padding at the top of the element will grow as the window's width is enlarged. This is indeed the result for my simple <p> tag.

However, when that element is floated or has width, the percentage padding does not behave as expected when the window is resized. The padding is calculated correctly upon load. But, as the window is resized, the total height of elements that are floated or have width seems to remain the same. Text in the element is inexplicably placed at the bottom of an area that gets mysterious height. This happens for elements like this:

p {
    padding:10% 0 0;
    margin:0 0 1em;
    background-color:#CCC;
    float:left;
}

p {
    padding:10% 0 0;
    margin:0 0 1em;
    background-color:#CCC;
    width:150px;
}

Here is an image to illustrate what I'm seeing. Color coding is added by Firebug; purple is padding, yellow is margin, and blue is the content of the element.

illustration of percentage padding problem in firefox

What causes this inconsistency? Can anyone else reproduce this issue in Firefox (or any other browser)?


Here's a fiddle to demonstrate. In Firefox, try expanding or contracting the result pane to see the elements resize.

I have not added a runnable code snippet, as I couldn't find an easy way of resizing the snippet area on-the-fly.

I've added a stack snippet to demonstrate the issue. Use the "Full page" button so you can stretch the window's width.

html,body {
  margin: 0;
}
div#container {
  width: 100%;
}
p {
  padding: 10% 0 0;
  margin: 0 0 1em;
  background-color: #CCC;
}
p.width_limited {
  width: 150px;
}
p.floated {
  float: left;
}
<div id="container">
  <p>NORMAL</p>
  <p class="floated">FLOATED</p>
  <div style="clear:both;height:0;"></div>
  <p class="width_limited">HAS WIDTH</p>
</div>
showdev
  • 25,529
  • 35
  • 47
  • 67
  • 3
    This appears to be a bug (or if you're a vendor these days, an "alternate implementation"). On page load the element padding will match appropriately (all the same), but it fails to update the padding on resize for some reason for those specific scenarios... weird. – Jesse Kernaghan Jan 16 '15 at 23:21
  • Shucks. I was afraid of that. I'll look around for a bug report. Thanks for verifying, Jesse. – showdev Jan 16 '15 at 23:23
  • 1
    Interesting finding, now that I understood the question. The _intended_ behavior should be outlined in [10.6 Calculating heights and margins](http://www.w3.org/TR/2011/REC-CSS2-20110607/visudet.html#Computing_heights_and_margins), but I don't have the time to figure out which rule it doesn't conform to. – Etheryte Jan 16 '15 at 23:33
  • @Nit That looks promising, thanks. I'll check it out. – showdev Jan 16 '15 at 23:38
  • Can't reproduce the bug in Firefox 39.0 – halfzebra Jul 12 '15 at 09:31
  • I see the issue in Firefox 39.0 on Mac. – showdev Jul 13 '15 at 16:19
  • Could you link me to your full CSS file? – DCdaz Jul 16 '15 at 21:27
  • @DCdaz The entirety of my CSS is shown in my question as well as in the linked [JSFiddle example](http://jsfiddle.net/vsvp71rw/1/). – showdev Jul 17 '15 at 16:45
  • Cool I think i know what it is then ill post an answer in a sec – DCdaz Jul 18 '15 at 09:20
  • 2
    The issue still persists in Firefox 50.1.0 – TylerH Jan 08 '17 at 23:26

2 Answers2

5

That's strange. I'm not sure if it is a bug. But, by changing the display to flex seems to solve the problem. http://jsfiddle.net/vsvp71rw/4/

p {
    display: -webkit-flex;
    display: -moz-flex;
    display: flex;
    padding:10% 0 0;
    margin:0 0 1em;
    background-color:#CCC;
}
Aakash
  • 1,641
  • 11
  • 21
5

you may use a pseudo element to avoid the bug and clear those height of 10%'s width you wish.

html,
body {
  margin: 0;
}

div#container {
  width: 100%;
}

p {
  margin: 0 0 1em;
  background-color: #CCC;
}

p:before {
  content: '';
  padding: 5% 0;
  display: block;
}

p.width_limited {
  width: 150px;
}

p.floated {
  float: left;
<div id="container">
    <p>NORMAL</p>
    <p class="floated">FLOATED</p>
    <div style="clear:both;height:0;"></div>
    <p class="width_limited">HAS WIDTH</p>
</div>
G-Cyrillus
  • 85,910
  • 13
  • 85
  • 110
  • I'm not sure I understand. It seems that you've removed the 10% top padding. This does not seem to have the desired effect of expanding the elements' heights relative to their widths. – showdev Jul 13 '15 at 16:19
  • @showdev the 10% vertical padding stands right there : :before {padding:5% 0;} , if you prefer , write it padding:10% 0 0 ; or padding:0 0 10% or padding-top:10%; or padding-bottom:10%, and so on. the before element displayed as a block will push anything down to 10% value of its own width / parents width wich is here p. Is it not what you look for ? – G-Cyrillus Jul 13 '15 at 20:53
  • No, it doesn't seem to work for me. [Here's what I see](http://i.stack.imgur.com/6Ojjt.jpg). Also, this doesn't explain the cause of the unexpected behavior. – showdev Jul 13 '15 at 20:58
  • @showdev well i see a padding of average 8px on the floatted where it is about 80px width, if it is not 10% it is very closed, for the has width, i mesure a padding of average 15px, here again we are very closed to 10% if not, behavior is the one that W3C tells it should be. Where do i misunderstand you ? Unexpected behavior when is a bug cannot be explained, it is just a bug that you can expect to see fixed in next updates ;) – G-Cyrillus Jul 13 '15 at 21:08
  • By "unexpected behavior", I mean that the behavior is not what I expect. Please revisit my diagram and code example above. Stretching the window's width changes the top padding of each element. Your example doesn't behave this way. The unexpected behavior is that certain elements retain inexplicable height (not padding) in Firefox. – showdev Jul 13 '15 at 21:14
  • 1
    @showdev the unexpected behavior is that FF doesn't refresh the layout on resize, you may force a reflow via js to awaken FF . Old IEs and old operas had this kind of behavior sometimes. YOU MAY as well use VW units to force calculation refresh onresize : http://jsfiddle.net/vsvp71rw/10/ should i update my answer since now i undestand that the width that mattered was parents not itself ? – G-Cyrillus Jul 13 '15 at 21:43
  • That does seem to work! Do you have any official reference about FF not refreshing the layout on resize? Also, can you direct me to official information that VW units force calculation refresh? Third, would you mind editing your answer to reflect this new information? – showdev Jul 13 '15 at 21:53
  • @showdev , nop there is no official référence about a bug behavior, but you can report it to mozilla – G-Cyrillus Jul 14 '15 at 07:33