Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Why is the start of the flexbox hidden ? And how to solve keeping items centered? [duplicate]

Tags:

css

flexbox

In the following snippet, you won't be able to see the first items of the flexbox.
Why ? How to solve this problem so the user can scroll horizontally to see all items ?

<div style="display: flex; justify-content: center; overflow: auto; padding: 30px 0">
  <div style="padding: 0 15px">AAAAAA</div>
  <div style="padding: 0 15px">BBBBBB</div>
  <div style="padding: 0 15px">CCCCCC</div>
  <div style="padding: 0 15px">DDDDDDD</div>
  <div style="padding: 0 15px">EEEEEEE</div>
  <div style="padding: 0 15px">FFFFFFF</div>
  <div style="padding: 0 15px">GGGGGGG</div>
  <div style="padding: 0 15px">HHHHHHH</div>
  <div style="padding: 0 15px">iiiiiii</div>
  <div style="padding: 0 15px">JJJJJJJ</div>
  <div style="padding: 0 15px">KKKKKKK</div>
  <div style="padding: 0 15px">LLLLLLL</div>
  <div style="padding: 0 15px">MMMMMMM</div>
  <div style="padding: 0 15px">OOOOOOO</div>
</div>

<div style="text-align: center">Scroll horizontally the letters above.</div>
like image 303
Yairopro Avatar asked Dec 30 '25 23:12

Yairopro


1 Answers

Flex tries to fit every item in the same row. If you want to keep items centered add flex-wrap: wrap to the container. If you want to keep them in the same row though you have to changejustify-content to flex-start. Here is the example of the flex-wrap solution.

<div style="display: flex; flex-wrap: wrap; justify-content: center; overflow: auto; padding: 30px 0">
  <div style="padding: 0 15px">AAAAAA</div>
  <div style="padding: 0 15px">BBBBBB</div>
  <div style="padding: 0 15px">CCCCCC</div>
  <div style="padding: 0 15px">DDDDDDD</div>
  <div style="padding: 0 15px">EEEEEEE</div>
  <div style="padding: 0 15px">FFFFFFF</div>
  <div style="padding: 0 15px">GGGGGGG</div>
  <div style="padding: 0 15px">HHHHHHH</div>
  <div style="padding: 0 15px">iiiiiii</div>
  <div style="padding: 0 15px">JJJJJJJ</div>
  <div style="padding: 0 15px">KKKKKKK</div>
  <div style="padding: 0 15px">LLLLLLL</div>
  <div style="padding: 0 15px">MMMMMMM</div>
  <div style="padding: 0 15px">OOOOOOO</div>
</div>

<div style="text-align: center">Scroll horizontally the letters above.</div>
like image 71
Bikas Avatar answered Jan 01 '26 14:01

Bikas