Make content scroll horizontally within nested flex containers with position sticky

Viewed 82

I have the following layout (see snippet below).

This is the expected behavior.

The problem is: Once the extra-large-content is simulated (by removing the comment on the extra-large-content CSS rule), it breaks the layout.

I would like the extra-large-content to scroll horizontally while staying inside column-3.

Is this even possible?

(the code is also available here https://codepen.io/Ploddy/pen/NWXOgMG?editors=1100)

body {
  height: 1920px;
  margin: 0;
}
.container {
  display: flex;
  border: 1px solid #ccc;
  margin: 1rem;
}
.container > * {
  border: 1px solid #ccc;
  position: sticky;
  top: 0;
  align-self: flex-start;
  flex-grow: 1;
  margin: 1rem;
}
#column-3 {
  height: 300px;
}
#extra-large-content {
  background-color: lightgreen;
  /*width: 3000px;*/
}
<div class="container">
  <div>
    column-1
  </div>
  <div class="container">
    <div>
      column-2
    </div>
    <div id="column-3">
      column-3
      <div id="extra-large-content">
        extra-large content
      </div>
    </div>
  </div>
</div>

2 Answers

This should work nicely for you. Essentially, I just specified width's on the .container elements. In theory, you could put overflow-x: scroll; on the .container, however, this would break your sticky positioning.

Edit ~ OP wants the extra-large content to scroll horizontally, not the entire column-3.

Set overflow-x: scroll; on the new parent wrapper of the div that has the 3000px static width.

body {
  height: 1920px;
  margin: 0;
}

.container:first-child {
  max-width: 100%;
}

.container:first-child > div:first-child {
  width: 40%;
}

.container:nth-child(2) {
  width: 60%;
}

.container:nth-child(2) > div:first-child {
  margin: 1em 0em 1em 1em;
}

.container {
  display: flex;
  border: 1px solid #ccc;
  margin: 1rem;
}

.container>* {
  border: 1px solid #ccc;
  position: sticky;
  top: 0;
  align-self: flex-start;
  flex-grow: 1;
  margin: 1rem;
}

.wrapper {
  display: flex;
  flex-direction: column;
  width: 40%;
}

#column-3 {
  background-color: salmon;
}

#extra-large-content {
  height: 300px;
  width: 3000px;
  background-color: lightgreen;
}

.xl-content-wrapper {
  overflow-x: scroll;
}
<div class="container">
  <div>column-1</div>
  <div class="container">
    <div>column-2</div>
    <div class="wrapper">
      <div id="column-3">column-3</div>
      <div class="xl-content-wrapper">
        <div id="extra-large-content">extra-large content</div>
      </div>
    </div>
  </div>
</div>

The issue comes from using flexbox. Switching to grid fixes the problem.

body {
  height: 1920px;
  margin: 0;
}
#primary-container {
  position: relative;
  display: flex;
  margin: 1rem;
}
#secondary-container {
  position: relative;
  display: grid;
  grid-template-columns: max-content 1fr;
  align-items: start;
}
#column-3 {
  display: grid;
  grid-auto-rows: min-content;
  height: 200px;
}
#content-wrapper {
  overflow: auto;
}
#extra-large-content {
  width: 3000px;
  background-color: lightgreen;
}
.sticky {
  position: sticky;
  top: 0;
  align-self: flex-start;
}
.border {
  border: 1px solid #ccc;
}
<div id="primary-container" class="border">
  <div class="sticky">
    column1
  </div>
  <div id="secondary-container" class="border">
    <div class="sticky">
      column2
    </div>
    <div id="column-3" class="sticky border">
      column3
      <div id="content-wrapper">
        <div id="extra-large-content">
          extra-large content
        </div>
      </div>
      ...
    </div>
  </div>
</div>

Related