skip to Main Content

Markup & Styling

* {
  box-sizing: border-box;
}
body {
  font-family: sans-serif;
  background: #dedede;
  padding: 48px;
}

#app {
  display: flex;
  width: 100%;
}
.box-1 {
  border: 1px solid red;
}
.box-2 {
  padding: 10px;
  border: 1px solid blue;
  flex: 1;
}
.overflow-container {
  padding: 10px;
  width: 100%;
  border: 1px solid green;
  overflow: auto;
}

.overflow-content {
  width: 100%;
  min-width: 2000px;
  border: 1px solid pink;
}
<div id="app">
      <div class="box-1">This is 1</div>
      <div class="box-2">
        This is 2
        <div class="overflow-container">
          <div class="overflow-content">This is my overflow content</div>
        </div>
      </div>
    </div>

When using the display: flex property, the overflow: auto property doesn’t function as expected. Instead, the size of the parent element expands to accommodate the width of the overflowing content. I’m seeking a solution to implement a horizontal scrollbar for the expanded content.
It does work as expected without flexbox

2

Answers


  1. write this code in your parent class

    #app {
          display: flex;
          width: 100%;
          overflow: auto;
        }
    
    Login or Signup to reply.
  2. You can use viewport width property of CSS.
    I have added viewport width propert in #app & .box-2 class.

    * {
      box-sizing: border-box;
    }
    body {
      font-family: sans-serif;
      background: #dedede;
      padding: 48px;
      margin: auto;
    }
    
    #app {
      display: flex;
      width: 90vw;
    }
    .box-1 {
      border: 1px solid red;
    }
    .box-2 {
      padding: 10px;
      border: 1px solid blue;
      flex: 1;
      width: 50vw;
    }
    .overflow-container {
      padding: 10px;
      width: 100%;
      border: 1px solid green;
      overflow: auto;
    }
    
    .overflow-content {
      width: 100%;
      min-width: 2000px;
      border: 1px solid pink;
    }
    <div id="app">
          <div class="box-1">This is 1</div>
          <div class="box-2">
            This is 2
            <div class="overflow-container">
              <div class="overflow-content">This is my overflow content</div>
            </div>
          </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search