Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

horizontally scrolling flex child

Tags:

css

flexbox

I have been scowering the web, but can not seem to get a solution to work.

Here is an example codepen:

http://codepen.io/anon/pen/Wxjjqp

.container {
  display: flex;
}

.horizontally-scrolled-items {
  display: flex;
  background: lightblue;
  overflow-x: scroll;
}
.item {
  width: 1000px;
  border: 1px solid blue;
}

html:

<div class="container">
  <div class="horizontally-scrolled-items">
    <div class="item">item1</div>
    <div class="item">item2</div>
    <div class="item">item3</div>
  </div>
  <div class="aside">
    <button>keep me on screen</button>
  </div>
</div>

The idea is for horizntally-scrolled-items to be flex:1. If the items are greater than the width of the container, for them to scroll, leaving aside in the view.

like image 572
Stepan Parunashvili Avatar asked Dec 17 '25 22:12

Stepan Parunashvili


1 Answers

You can achieve this with min-width. Give your .item class a min-width with a flex-grow: 1;. Then set your .horizontally-scrolled-items div to width: 100%;.

CSS

.horizontally-scrolled-items {
  width: 100%;
}

.item {
  min-width: 400px;
  flex-grow: 1;
}

CodePen

like image 193
Hunter Turner Avatar answered Dec 20 '25 19:12

Hunter Turner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!