Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a child element overflows horizontally, why is the right padding of the parent ignored?

Given this simple structure:

<div id="parent">     <div id="child">Lorem ipsum</div> </div> 

with this CSS:

#parent {     width: 200px;     height: 200px;     padding: 20px;     overflow-x: scroll; }  #child {     width: 500px;       } 

Live demo: http://jsfiddle.net/523me/5/

Notice that the parent has a 20px padding and that the child overflows horizontally (because it is wider). If you scroll the parent all the way to the right, you'll see that the child touches the right edge of the parent.

So, the parent should have a right padding, but it is ignored. It seems that when the child has a fixed width, the right padding of the parent does not apply. (Is this specified by a standard? I would love to know. Please let me know if you find anything!)

Is there a way to force the right padding to be applied in this scenario without having to remove any of the elements from the flow (by floating or positioning)?

enter image description here

Screenshot 1 - The right padding is ignored. This is how all current browsers behave.

Screenshot 2 - The right padding applies. This is what I'm trying to accomplish. (Btw, the screenshot is from IE7, which is the only browser which does not ignore the right padding.)

like image 689
Šime Vidas Avatar asked Apr 07 '12 13:04

Šime Vidas


People also ask

How do you override padding in CSS?

Just remove the space between the class names: . sapUiRespGridHSpace1. noPaddingRightLeft {padding: 0; } this tells the browser it's a single element with both of these classes.


2 Answers

You're suffering from this problem.

I would solve it by giving a margin to the child (and not a padding to the parent):

body {    padding: 2em;  }    #parent {    width: 200px;    height: 200px;    overflow-x: scroll;    background: gray;  }    #child {    width: 500px;    background: yellow;    margin: 20px;    display: inline-block;  }
<div id="parent">    <div id="child">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras et turpis eu lorem consectetur blandit sed vel ligula. In lorem ligula, lacinia sed aliquet sed, congue quis tortor. In sed magna eros, eget blandit arcu. Nulla sit amet volutpat ipsum. Duis      quis nisl massa. Sed ipsum magna, tempus non malesuada in, gravida et sapien. Fusce a odio nulla, quis ultrices mauris. Maecenas in tellus id massa fringilla molestie.</div>  </div>
like image 137
Joao Avatar answered Oct 13 '22 05:10

Joao


Dunno but adding:

#child{   display: inline-block; } 

Seems to fix it: http://jsfiddle.net/523me/6/

I've only tested in latest Chrome, may not be cross-browser

like image 35
stephenmurdoch Avatar answered Oct 13 '22 06:10

stephenmurdoch