Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make overflow-x work in a full width flexbox column [duplicate]

Tags:

css

flexbox

I'm trying to create a 2 column layout with flexbox. One is fixed width on the side and the other one (main column) fills the remaining space.

It was working until I put some contents wider than the screen with a overflow-x: scroll parent. Now this wide content is not limited by its scroller parent.

Here is the sample:

.container {
  display: flex;
}
.container .col-main {
  background: gold;
  flex: 1 1 100%;
}
.container .col-side {
  background: cornflowerblue;
  flex: 0 0 15em;
}

.slider-container {
  overflow-x: scroll;
}
.slider-container .slider {
  background: repeating-linear-gradient(45deg, darkseagreen, darkseagreen 10px, dimgray 10px, dimgray 20px);
  width: 150em;
  height: 10em;
}
<div class="container">
  <div class="col-main">

    <div class=slider-container>
      <div class="slider">
        Foo
      </div>
    </div>

  </div>
  <div class="col-side">
    Side
  </div>
</div>

It should be rendered like this: enter image description here

like image 776
Yves Avatar asked Apr 05 '17 15:04

Yves


People also ask

Why overflow is not working in CSS?

One of the most common causes of overflow is fixed-width elements. Generally speaking, don't fix the width of any element that should work at multiple viewport sizes.

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% .


2 Answers

Try adding min-width: 0; to the flex item.

.container .col-main {
  ...
  min-width: 0;
}

jsFiddle

4.5. Implied Minimum Size of Flex Items

To provide a more reasonable default minimum size for flex items, this specification introduces a new auto value as the initial value of the min-width and min-height properties defined in CSS 2.1. [CSS21]

like image 148
Stickers Avatar answered Oct 02 '22 21:10

Stickers


You can add width: 0 to the container.

.container {
  display: flex;
}

.container .col-main {
  background: gold;
  flex: 1 1 100%;
  width: 0;
}

.container .col-side {
  background: cornflowerblue;
  flex: 0 0 15em;
}

.slider-container {
  overflow-x: scroll;
}

.slider-container .slider {
  background: repeating-linear-gradient(45deg, darkseagreen, darkseagreen 10px, dimgray 10px, dimgray 20px);
  width: 150em;
  height: 10em;
}
<div class="container">
  <div class="col-main">

    <div class=slider-container>
      <div class="slider">
        Foo
      </div>
    </div>

  </div>
  <div class="col-side">
    Side
  </div>
</div>
like image 22
sol Avatar answered Oct 02 '22 20:10

sol