0

Assuming I have the following HTML

<div className="cards-container">
  <div className="card" />
  <div className="card" />
  <div className="card" />
  <div className="card" />
  <div className="card" />
  <div className="card" />
</div>

Now I have 6 Cards. These cards for instance appear on the top of each other in my screen on the left side.

Card1
Card2
Card3
Card4
Card5
Card6

I'd like to make them appear as follows:

Card1 Card2 Card3
Card4 Card5 Card6

And if the width of the screen is smaller, it would go like this

Card1 Card2
Card3 Card4
Card5 Card6

css:

.cards-container {
   height: 100%;
   overflow: auto;

  .card {
     border-radius: 7px;
     height: 228px;
     width: 361px;
     min-width: 361px;
     margin-left: 23px;
     margin-top: 8px;
   }
}

I thought about splitting the cards into 2/3 arrays with Javascript based on the screen width then render each row with flex-direction: column; but now sure if that's right.

Something like

 const Array1 = [Card1, Card4]
 const Array2 = [Card2, Card5]
 const Array3 = [Card3, Card6]

Then add them into 1 array as follows Arrays = [Array1, Array2, Array3]

then render them on sparate divs.

Ziko
  • 919
  • 2
  • 10
  • 22
  • Any reason you are not using any UI libraries like bootstrap, foundation etc? – Lukman Jun 20 '21 at 13:55
  • @kinglish I guess yes!!! I believe I was missing the flex-wrap: wrap; Let me poke around more and see if that will do what I really want. Please submit as an answer if you want so that I can approve. – Ziko Jun 20 '21 at 13:57
  • @Lukman I haven't done a lot of research, I believe css tricks could be enough.. – Ziko Jun 20 '21 at 13:58
  • You can use the Display Grid property to achieve what you need I believe. – Shrish Sharma Jun 20 '21 at 14:00
  • @ShrishSharma Display: flex; flex-wrap: wrap; is all you need. I missed the flex-wrap property which prevented the trick – Ziko Jun 20 '21 at 14:00
  • @Ziko that's cool..glad that your problem's fixed ;) – Shrish Sharma Jun 20 '21 at 14:02

0 Answers0