How can I make this flexbox container scroll instead of resizing the width?


feast

I want to make the flexbox container 100% width and have a scrollbar to see the overflowing content. Now, it seems that the content is resized to fit the width of the window, and it always takes 100% of the width without the content overflowing at all.

https://codepen.io/UrsuGrizzly/pen/BmqPvm https://jsfiddle.net/duvq2e42/

<div id="topimg">
 <a href="#" id="st1"></a>
 <a href="#" id="second"></a>
 <a href="#" id="first"></a>
 <a href="#" id="third"></a>
 <a href="#" id="fourth"></a>
 <a href="#" id="fifth"></a>
 <a href="#" id="sixth"></a>
 <a href="#" id="nd2"></a>
 <a href="#" id="rd3"></a>
 <a href="#" id="th4"></a>
</div>

#topimg {
 display: flex;
 min-width:100%;
 overflow-x:scroll;
}

#topimg > * {
 margin: 0 4px 0 0;
 box-shadow: 0 1px 6px rgba(32, 33, 36, 0.28);
}

#topimg #st1 {
 background-color: yellow;
 width: 125px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: center;
}

#topimg #nd2 {
 background-color: orange;
 width: 100px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: center;
}

#topimg #rd3 {
 background-color: cyan;
 width: 100px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: center;
}

#topimg #th4 {
 background-color:green;
 width: 100px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: center;
}

#topimg #first {
 background-color: purple;
 width: 100px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: center;
}

#topimg #second {
 background-color: red;
 width: 250px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: center;
}

#topimg #third {
 background-color: blue;
 width: 200px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: top;
}

#topimg #fourth {
 background-color: fuchsia;
 width: 250px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: center;
}

#topimg #fifth {
 background-color: brown;
 width: 100px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: center;
}

#topimg #sixth {
 background-color: coral;
 width: 100px;
 height: 144px;
 display: block;
 background-size: cover;
 background-position: center;
}
a son

flex-shrinkDue to the default value of a flex item 1, it can shrink, and can shrink unless its content prevents shrinking.

add flex-shrink: 0;to your #topimg > *rules

stack fragment

    #topimg {
     display: flex;
     min-width:100%;
     overflow-x:scroll;
    }

    #topimg > * {
     margin: 0 4px 0 0;
     box-shadow: 0 1px 6px rgba(32, 33, 36, 0.28);
     flex-shrink: 0;                                    /*  added  */
    }

    #topimg #st1 {
     background-color: yellow;
     width: 125px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: center;
    }

    #topimg #nd2 {
     background-color: orange;
     width: 100px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: center;
    }

    #topimg #rd3 {
     background-color: cyan;
     width: 100px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: center;
    }

    #topimg #th4 {
     background-color:green;
     width: 100px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: center;
    }

    #topimg #first {
     background-color: purple;
     width: 100px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: center;
    }

    #topimg #second {
     background-color: red;
     width: 250px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: center;
    }

    #topimg #third {
     background-color: blue;
     width: 200px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: top;
    }

    #topimg #fourth {
     background-color: fuchsia;
     width: 250px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: center;
    }

    #topimg #fifth {
     background-color: brown;
     width: 100px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: center;
    }

    #topimg #sixth {
     background-color: coral;
     width: 100px;
     height: 144px;
     display: block;
     background-size: cover;
     background-position: center;
    }
<div id="topimg">
          <a href="#" id="st1"></a>
          <a href="#" id="second"></a>
          <a href="#" id="first"></a>
          <a href="#" id="third"></a>
          <a href="#" id="fourth"></a>
          <a href="#" id="fifth"></a>
          <a href="#" id="sixth"></a>
          <a href="#" id="nd2"></a>
          <a href="#" id="rd3"></a>
          <a href="#" id="th4"></a>
</div>


As a comparison, if the link is the content of the wrapper, it will work as-is.

stack fragment

#topimg {
  display: flex;
  min-width: 100%;
  overflow-x: scroll;
}

#topimg > * {
  margin: 0 4px 0 0;
  box-shadow: 0 1px 6px rgba(32, 33, 36, 0.28);
}

#topimg #st1 {
  background-color: yellow;
  width: 125px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: center;
}

#topimg #nd2 {
  background-color: orange;
  width: 100px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: center;
}

#topimg #rd3 {
  background-color: cyan;
  width: 100px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: center;
}

#topimg #th4 {
  background-color: green;
  width: 100px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: center;
}

#topimg #first {
  background-color: purple;
  width: 100px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: center;
}

#topimg #second {
  background-color: red;
  width: 250px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: center;
}

#topimg #third {
  background-color: blue;
  width: 200px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: top;
}

#topimg #fourth {
  background-color: fuchsia;
  width: 250px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: center;
}

#topimg #fifth {
  background-color: brown;
  width: 100px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: center;
}

#topimg #sixth {
  background-color: coral;
  width: 100px;
  height: 144px;
  display: block;
  background-size: cover;
  background-position: center;
}
<div id="topimg">
  <span>
    <a href="#" id="st1"></a>
  </span>
  <span>
    <a href="#" id="second"></a>
  </span>
  <span>
    <a href="#" id="first"></a>
  </span>
  <span>
    <a href="#" id="third"></a>
  </span>
  <span>
    <a href="#" id="fourth"></a>
  </span>
  <span>
    <a href="#" id="fifth"></a>
  </span>
  <span>
    <a href="#" id="sixth"></a>
  </span>
  <span>
    <a href="#" id="nd2"></a>
  </span>
  <span>
    <a href="#" id="rd3"></a>
  </span>
  <span>
    <a href="#" id="th4"></a>
  </span>
</div>

Related


How to make flexbox container scroll?

Gipf I have a vertical flexbox container on which I set a specific height and set it to scroll vertically on overflow. The problem is that if the content of the flex container overflows, the flex container will not scroll. Here's a plugin that demonstrates wha

How to make flexbox container scroll?

Gipf I have a vertical flexbox container on which I set a specific height and set it to scroll vertically on overflow. The problem is that if the content of the flex container overflows, the flex container will not scroll. Here's a plugin that demonstrates wha

How to make a flexbox container scroll horizontally

Rahul Kumar I have this HTML and CSS #container{ border:1px solid red; display:flex; overflow:auto; } .item{ border:1px solid green; margin:5px; width:700px; } <div id="container"> <div class="item"> ... </div> <div class="item"> ... </di

flexbox width inside horizontal scroll container

Oleglov I am trying to create a layout for a virtual table. The virtual table will be placed inside the .table-bodyelement . Each table cell has the same Flex layout style as the cell, .table-headerso it will look the same with the same parent element. The pro

flexbox width inside horizontal scroll container

Oleglov I am trying to create a layout for a virtual table. The virtual table will be placed inside the .table-bodyelement . Each table cell has the same Flex layout style as the cell, .table-headerso it will look the same with the same parent element. The pro

flexbox width inside horizontal scroll container

Oleglov I am trying to create a layout for a virtual table. The virtual table will be placed inside the .table-bodyelement . Each table cell has the same Flex layout style as the cell, .table-headerso it will look the same with the same parent element. The pro