2 votes

Centrer la mise en page flexible sans rétrécir la colonne

J'ai une grille à l'intérieur d'une boîte à outils avec flex-flow fixé à column nowrap . La grille n'a pas un nombre fixe de colonnes grid-template-columns: repeat(auto-fill, 270px) .

Mon problème est que lorsque j'essaie de centrer cette grille dans la boîte à outils, elle ne prend pas autant d'espace que possible et réduit simplement le nombre de colonnes à 1.

Je veux que la grille ait 4 colonnes sauf si elle est sur le point de déborder du conteneur. Comment y parvenir ? J'ai essayé de définir margin: 0 auto; sur la grille, mais le résultat est le même.

Résultat actuel : enter image description here

Résultat souhaité + les colonnes doivent s'enrouler au lieu de déborder : enter image description here

HTML/CSS
J'ai mis min-width: 300px pour la grille car elle contient des images d'une largeur de 270px + 10px écart. Et j'ai mis en place max-width: calc(4 * 280px) car je ne veux pas avoir plus de 4 colonnes.

.portfolio {
  min-height: 1004px;
  display: flex;
  flex-flow: column nowrap;
  justify-items: center;
  align-items: center;
}

.portfolio-heading {
  margin: 50px 0px 0px 0px;
  font-family: Lato;
  font-size: 30px;
  font-weight: 900;
  color: #616161;
  line-height: 2;
  text-align: center;
}

.portfolio-text {
  margin: 5px 0px 0px 0px;
  font-family: "Lato";
  font-size: 20px;
  font-weight: 300;
  color: #616161;
  text-align: center;
}

.portfolio-nav-row {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  margin: 60px 0px 15px 0px;
  padding-right: 10px;
}

.portfolio-nav-row-btn {
  padding: 10px 19px;
  margin-left: 10px;
  border: 1px solid #ff6760;
  border-radius: 5px;
  background-color: #ff6760;
  color: #ffffff;
  font-family: "Lato";
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
}

.portfolio-grid {
  max-width: calc(4 * 280px);
  min-width: 300px;
  display: grid;
  grid-template-columns: repeat(auto-fill, 270px);
  grid-gap: 10px;
  justify-items: center;
  overflow: hidden;
}

<section class="portfolio">
  <h1 class="portfolio-heading">Our Featured Works</h1>
  <p class="portfolio-text">Curabitur blandit tempus porttitor. Maecenas faucibus mollis interdum.</p>
  <div class="portfolio-nav-row">
    <button class="portfolio-nav-row-btn all-btn" type="button">All</button>
    <button class="portfolio-nav-row-btn graphic-btn" type="button">Graphic</button>
    <button class="portfolio-nav-row-btn illustration-btn" type="button">Illustration</button>
    <button class="portfolio-nav-row-btn motion-btn" type="button">Motion</button>
  </div>
  <div class="portfolio-grid">
    <img class="portfolio-grid-item portfolio-grid-row1-column1" src="pictures/portfolio-1.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column2" src="pictures/portfolio-2.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column3" src="pictures/portfolio-3.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column4" src="pictures/portfolio-4.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row2-column1" src="pictures/portfolio-5.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row2-column2" src="pictures/portfolio-6.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row2-column3" src="pictures/portfolio-7.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row2-column4" src="pictures/portfolio-8.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row3-column1" src="pictures/portfolio-9.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row3-column2" src="pictures/portfolio-10.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row3-column3" src="pictures/portfolio-11.png" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row3-column4" src="pictures/portfolio-12.png" alt="portfolio image">
  </div>
</section>

4voto

Yeasin01 Points 79

Vous devez utiliser width: 100% en .portfolio-grid . Il ne prend qu'une seule colonne parce que vous ne lui donnez pas de largeur. Il prend donc sa largeur en fonction de celle-ci min-width valeur. Je l'ai vérifié sur mon CodePen et cela fonctionne bien avec width: 100% .

.portfolio {
  min-height: 1004px;
  display: flex;
  flex-flow: column nowrap;
  justify-items: center;
  align-items: center;
}

.portfolio-heading {
  margin: 50px 0px 0px 0px;
  font-family: Lato;
  font-size: 30px;
  font-weight: 900;
  color: #616161;
  line-height: 2;
  text-align: center;
}

.portfolio-text {
  margin: 5px 0px 0px 0px;
  font-family: "Lato";
  font-size: 20px;
  font-weight: 300;
  color: #616161;
  text-align: center;
}

.portfolio-nav-row {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  margin: 60px 0px 15px 0px;
  padding-right: 10px;
}

.portfolio-nav-row-btn {
  padding: 10px 19px;
  margin-left: 10px;
  border: 1px solid #ff6760;
  border-radius: 5px;
  background-color: #ff6760;
  color: #ffffff;
  font-family: "Lato";
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
}

.portfolio-grid {
  max-width: calc(4 * 280px);
  min-width: 300px;
  display: grid;
  grid-template-columns: repeat(auto-fill, 300px);
  grid-gap: 10px;
  justify-content: center;
  overflow: hidden;
  width: 100%;
}

<section class="portfolio">
  <h1 class="portfolio-heading">Our Featured Works</h1>
  <p class="portfolio-text">Curabitur blandit tempus porttitor. Maecenas faucibus mollis interdum.</p>
  <div class="portfolio-nav-row">
    <button class="portfolio-nav-row-btn all-btn" type="button">All</button>
    <button class="portfolio-nav-row-btn graphic-btn" type="button">Graphic</button>
    <button class="portfolio-nav-row-btn illustration-btn" type="button">Illustration</button>
    <button class="portfolio-nav-row-btn motion-btn" type="button">Motion</button>
  </div>
  <div class="portfolio-grid">
    <img class="portfolio-grid-item portfolio-grid-row1-column1" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column2" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column3" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column4" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column1" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column2" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column3" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column4" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column1" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column2" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column3" src="http://placehold.it/270x270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column4" src="http://placehold.it/270x270" alt="portfolio image">
  </div>
</section>

1voto

Michael_B Points 15556

Passer de column nowrap a row wrap sur le conteneur principal. Ensuite, forcez chaque élément à occuper toute la rangée avec flex-basis: 100% . Le centrage horizontal devient alors simple et facile.

Voici votre code avec les révisions. J'ai supprimé certains styles visuels qui n'étaient pas pertinents pour le problème.

.portfolio {
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  text-align: center;
}
.portfolio-heading { flex-basis: 100%; }
.portfolio-text    { flex-basis: 100%; }

.portfolio-nav-row {
  flex-basis: 100%;
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
}
.portfolio-nav-row-btn {
  padding: 10px 19px;
  background-color: #ff6760;
  color: #ffffff;
  cursor: pointer;
}
.portfolio-nav-row-btn+.portfolio-nav-row-btn {
  margin-left: 10px;
}
.portfolio-grid {
  max-width: calc(4 * 280px);
  display: grid;
  grid-template-columns: repeat(auto-fill, 270px);
  grid-gap: 10px;
  flex-basis: 100%;
  justify-content: center;
}
body { margin: 0; }

<section class="portfolio">
  <h1 class="portfolio-heading">Our Featured Works</h1>
  <p class="portfolio-text">Curabitur blandit tempus porttitor. Maecenas faucibus mollis interdum.</p>
  <div class="portfolio-nav-row">
    <button class="portfolio-nav-row-btn all-btn" type="button">All</button>
    <button class="portfolio-nav-row-btn graphic-btn" type="button">Graphic</button>
    <button class="portfolio-nav-row-btn illustration-btn" type="button">Illustration</button>
    <button class="portfolio-nav-row-btn motion-btn" type="button">Motion</button>
  </div>
  <div class="portfolio-grid">
    <img class="portfolio-grid-item portfolio-grid-row1-column1" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column2" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column3" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row1-column4" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row2-column1" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row2-column2" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row2-column3" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row2-column4" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row3-column1" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row3-column2" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row3-column3" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
    <img class="portfolio-grid-item portfolio-grid-row3-column4" src="http://i.imgur.com/60PVLis.png" width="270" alt="portfolio image">
  </div>
</section>

Démonstration de jsFiddle

0voto

Suresh Points 272

Dans votre conteneur flexbox ( .portfolio ), supprimer align-items:center et notez que flexbox utilise justify-content et non justify-items . Ceci semble faire l'affaire.

.portfolio {
    min-height: 1004px;
    display: flex;
    flex-flow: column nowrap;
    justify-content: center;
}

Violon : https://jsfiddle.net/osayky9b/

Edit : Centrer la grille : https://jsfiddle.net/osayky9b/8/

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X