0

Embedded scroll bars do not work within a simple flexbox container. The browser automatically displays the horizontal scroll bar.

What am I doing wrong on this simple example?

When I remove "display: flex;" on the flexContainer, the embedded scrollbar works. Of course, my entire layout is then destroyed.

I only found the following, but the solution relates to a vertical scrollbar and does not work here: Stackoverflow nesting-flexbox-inside-flexbox-overflow-how-to-fix-this

.flexContainer {
  display: flex;
}

.sidebar {
  flex: 1 0 auto;
  min-width: 150px;
  max-width: 190px;
  color: white;
  background: blue;
}

.content {
  flex: 1 0 auto;
  background: green;
}

.scrollableElement {
  display: flex;
  overflow-x: scroll;
}

.textElement {
  color: white;
  background: grey;
}
<html>
  <body>
    <div class="flexContainer">
      <div class="sidebar">
        <div>Sidebar</div>
      </div>
      <div class="content">
        <div class="scrollableElement">
          <div class="textElement">
            <div>Content11111111111111111111111111111111111111111111111111111111111111111111111</div>
            <div>Content22222222222222222222222222222222222222222222222222222222222222222222222</div>
            <div>Content33333333333333333333333333333333333333333333333333333333333333333333333</div>
            <div>Content44444444444444444444444444444444444444444444444444444444444444444444444</div>
            <div>Content55555555555555555555555555555555555555555555555555555555555555555555555</div>
            <div>Content66666666666666666666666666666666666666666666666666666666666666666666666</div>
            <div>Content77777777777777777777777777777777777777777777777777777777777777777777777</div>
            <div>Content88888888888888888888888888888888888888888888888888888888888888888888888</div>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>
Volker Fried
  • 411
  • 6
  • 14
  • You can adapt the vertical solution to horizontal layout. Overflow-y will not work on horizontal layout, but there is an equivalent horizontal property. – Félix Adriyel Gagnon-Grenier Nov 04 '20 at 15:59
  • Thank you for your quick response. With 'overflow-x: auto' on the flexContainer, the browser scrollbar is no longer displayed, but the scrollbar of the flexContainer is displayed. Actually, however, the scrollableElement scrollbar should be used. – Volker Fried Nov 04 '20 at 16:13

2 Answers2

2

You need to allow .content to shrink then add min-width:0 or overflow:hidden so its size is calculated at screen inside its parent so overflow works on the children:

.content {
  flex: 1 1 auto;/* updated*/
  background: green;
  min-width:0;/* added */
}

.flexContainer {
  display: flex;
}

.sidebar {
  flex: 1 0 auto;
  min-width: 150px;
  max-width: 190px;
  color: white;
  background: blue;
}

.content {
  flex: 1 1 auto;
  background: green;
  min-width:0;
}

.scrollableElement {
  display: flex;
  overflow-x: scroll; 
}

.textElement {
  color: white;
  background: grey;
}
<html>
  <body>
    <div class="flexContainer">
      <div class="sidebar">
        <div>Sidebar</div>
      </div>
      <div class="content">
        <div class="scrollableElement">
          <div class="textElement">
            <div>Content11111111111111111111111111111111111111111111111111111111111111111111111</div>
            <div>Content22222222222222222222222222222222222222222222222222222222222222222222222</div>
            <div>Content33333333333333333333333333333333333333333333333333333333333333333333333</div>
            <div>Content44444444444444444444444444444444444444444444444444444444444444444444444</div>
            <div>Content55555555555555555555555555555555555555555555555555555555555555555555555</div>
            <div>Content66666666666666666666666666666666666666666666666666666666666666666666666</div>
            <div>Content77777777777777777777777777777777777777777777777777777777777777777777777</div>
            <div>Content88888888888888888888888888888888888888888888888888888888888888888888888</div>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
0

Your flex container has no set width, so it is taking up the entire width of the screen.

You could try setting the width to achieve the behavior you are looking for, for example:

.flexContainer {
  display: flex;
  width: 500px;
}

OR you could set the width of the .scrollableElement to resize based on the screen:

.scrollableElement {
  overflow-x: scroll;
  width: 65%;
}
Glen Carpenter
  • 392
  • 2
  • 9