Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 842
Chris Spittles Avatar asked Nov 12 '14 13:11

Chris Spittles


People also ask

Why is my flexbox not wrapping?

If you are using flexbox and want the content to wrap, you must specify flex-wrap: wrap . By default flex items don't wrap. To have the images be three-across, you should specify flex-basis: 33.33333% .

How do I align my Flex container to the right?

In simple words, flex-items now expand from right to left as shown in the given figure. When justify-content is set to “flex-end”, it instantly shifts all the flex-items to the end of the flex-container along the main-axis, i.e flex items get right aligned.


2 Answers

You need to add another layer of wrapping, if you want to have both "overflow-x:auto" with scrollable padding at the end.

Something like this:

.scroll {      overflow-x: auto;      width: 300px;      border:1px #ccc solid;  }  .outer {      display: flex;      flex-direction: row;      box-sizing: border-box;      min-width: 100%;      height: 80px;      padding: 5px;      float: left; /* To size to content, & not be clamped to available width. (Vendor-prefixed intrinsic sizing keywords for "width" should work here, too.) */  }  .outer > div {      flex: 1 1 auto;      border: 1px #ccc solid;      text-align: center;      min-width: 50px;      margin: 5px;  }
<div class="scroll">    <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>  </div>
like image 142
dholbert Avatar answered Oct 13 '22 06:10

dholbert


Alternatively it's possible to create the margins with pseudo-elements:

.outer::before { content: ''; min-width: 5px; } .outer::after { content: ''; min-width: 5px; } 

.outer {     display: flex;     flex-direction: row;     width: 300px;     height: 80px;     border:1px #ccc solid;     overflow-x: auto;     padding: 5px; } .outer::after { content: ''; min-width: 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>
like image 31
Artur K. Avatar answered Oct 13 '22 08:10

Artur K.