htmlcsswinjsflexbox

Display: Flex loses right padding when overflowing?


I have an issue with a CSS3 flexbox.

If I set the flexbox element to overflow and set a min-width value for the children, the right padding on the parent is lost? This is consistent on all supporting browsers.

Here is an example of the error. If you scroll to the right of the container you will see the last child is hard up against the right edge of the container instead of honoring the padding value.

.outer {
    display: flex;
    flex-direction: row;
    width: 300px;
    height: 80px;
    border:1px #ccc solid;
    overflow-x: auto;
    padding: 5px;
}
.outer > div {
    flex: 1 1 auto;
    border: 1px #ccc solid;
    text-align: center;
    min-width: 50px;
    margin: 5px;
}
<div class="outer">
    <div>text1</div>
    <div>text2</div>
    <div>text3</div>
    <div>text4</div>
    <div>text5</div>
    <div>text6</div>
    <div>text7</div>
    <div>text8</div>
    <div>text9</div>
    <div>text10</div>
</div>

Does anyone know why this is and how I would go about correcting it? I've messed around with padding and margin values in different combinations without success.


Solution

  • Update 2023

    This seems to have been fixed in recent browser versions, as right padding is clearly visible here:

    .outer{width:500px; display:flex; padding:20px; overflow:auto; background:#ccc;}
    .inner{width:600px; height:50px; flex-shrink:0; background:darkblue;}
    <div class="outer">
      <div class="inner"></div>
    </div>

    The problem still exists though in cases where the overflow is applied to a wrapping element (note also how the gray background doesn't expand to fill the wrapper when scrolled to the right side):

    .wrapper{width:500px; overflow:auto; border:1px solid red;}
    .outer{display:flex; padding:20px; background:#ccc;}
    .inner{width:600px; height:50px; flex-shrink:0; background:darkblue;}
    <div class="wrapper">
      <div class="outer">
        <div class="inner"></div>
      </div>
    </div>

    But this can be fixed by adding width:fit-content to the outer element:

    .wrapper{width:500px; overflow:auto; border:1px solid red;}
    .outer{width:fit-content; display:flex; padding:20px; background:#ccc;}
    .inner{width:600px; height:50px; flex-shrink:0; background:darkblue;}
    <div class="wrapper">
      <div class="outer">
        <div class="inner"></div>
      </div>
    </div>