Home > Mobile >  How to make content display in three columns instead of one column?
How to make content display in three columns instead of one column?

Time:01-08

I am a beginner and learning from a tutorial using an API. I would like to change the layout of the design, and instead of having one column, I would like a three column layout. I don't know if I am using flexbox correctly.

I have tried using row and then having three columns, but somehow its not displaying correctly.I am also using bootstrap.

jsfiddle.net/e5bqsywk/2/

CodePudding user response:

What you're looking for is CSS Grid, which lets you display elements in rows and columns.

Just change your flexbox to a grid with 3 equal columns:

/* display: flex; */
display: grid;
grid-template-columns: repeat(3, 1fr);

CodePudding user response:

first of all, we need to clean up your HTML structure.

since in your javascript you are appending all of the cards in a class "images-container" we have to remove the duplicate divs.

<div >
    <div >
      <div >
        <div ></div>
      </div>
      <div >
        <div ></div>
      </div>
      <div >
        <div ></div>
      </div>
    </div>
  </div>

and turned them into. so it will only appended into a single container and do our flex there.

  <div >
    <div >
      <div ></div>
    </div>
  </div>

The next one is CSS, I added a "flex-wrap: wrap" on the "images-container" so that the cards will go into the next line whenever there is no space left.

/* Images Container */
.images-container {
  width: 100%;
  margin-top: 50px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

and since you are following bootstrap class names you have to add media queries on classnames with breakpoints so. it says the card will take 12 columns on xs "smallest screen" and 4 columns on medium screen.

.col-xs-12 { 
  width: 100%;
}

@media onyl screen and (min-width: 767px) {
  .col-md-4 {
    width: 33% !important;
  }
}

now on javascript. since we got our cards inside "images-container". then we need to add the classname "col-md-4" and "col-xs-12" on the card directly so it will have it's own width

    // Card Container
    const card = document.createElement("div");
    card.classList.add("card");
    card.classList.add("col-md-4");
    card.classList.add("col-xs-12");

sorry for the long explanation I'm still improving my explanation skills hopefully it will help you out.

  •  Tags:  
  • Related