1

I am using CSS flexbox to make a grid of items that wrap on page resize. I want the grid to be horizontally centered, which it is thanks to justify-content: center;. The problem is that the last box is (in some cases) centered instead of being aligned to the left. How can I make it aligned to the left?

I have tried to use align-content: flex-start; on the container, but it does not seem to work and I am not sure why.

@import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap');
@-ms-viewport {width: device-width;}

.flex-container {
  max-width: 100%;
  margin: 0 auto;
  background-color: whitesmoke;
}

.flex-row {
  display: flex;
  flex-wrap: wrap;
  overflow: hidden;
  justify-content: center;
  align-content: flex-start;
}

.flex-item {
  width: 300px;
  position: relative;
  box-sizing: border-box;
  border: dashed 1px navy;
  overflow-wrap: break-word;
  word-wrap: break-word;
  word-break: break-word;
  -webkit-hyphens: auto;
  -ms-hyphens: auto;
  hyphens: auto;
  padding: 1em;
}

.flex-item .flex-wrapper {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-color: crimson;
}

.flex-item a.flex-permalink {
  text-decoration: none;
  color: black;
  display: block;
  background-color: lightpink;
  height: 100%;
}

.flex-item img { width: 100%;}

.flex-item h1 {
  font-family: 'Roboto', sans-serif;
  font-size: 1.5em;
  font-weight: 500;
  margin: 0;
  padding: 1em;
}
<div class="flex-container">
 <div class="flex-row">
  <!-- ------ Let the loop begin ------ -->
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Exercitation cupidatat ex non aliqua dolore veniam veniam officia ex dolore.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Sint eiusmod est laborum reprehenderit.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Adipisicing quis tempor duis irure magna quis occaecat.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Culpa dolore sint sit non voluptate nostrud nulla dolor laborum.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Tempor consectetur do elit magna sunt cillum dolor.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>AEu eiusmod qui nostrud anim nulla dolore non veniam excepteur adipisicing sed.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Lorem ipsum duis non laboris</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Dolor occaecat laboris enim duis eiusmod</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Exercitation do enim sint pariatur consectetur</h1>
    </a>
   </div>
  </div>
  <!-- ------ Let the loop end ------ -->
 </div>
</div>

Edit: Illustration

Arete
  • 948
  • 3
  • 21
  • 48
  • `justify-content: center;` will center items in row. if you want to align them left you have to use `justify-content: flex-start;` – BeHappy Apr 08 '20 at 20:32
  • Yes, I do want to align the boxes to the left and center the whole grid, but no matter what I set `justify-content` to it does not change anything. – Arete Apr 08 '20 at 20:57
  • 1
    Maybe this helps https://stackoverflow.com/questions/32802202/how-to-center-a-flex-container-but-left-align-flex-items – Dmytro Cheglakov Apr 09 '20 at 08:13

3 Answers3

0

instead of justify-content:center; use justify-content:flex-start or space-between or space-evenly

if you want the whole thing centered on the x axis use margin:0 auto on the outer container

@import url('https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap');
@-ms-viewport {width: device-width;}

.flex-container {
  max-width: 100%;
  margin: 0 auto;
  background-color: whitesmoke;
}

.flex-row {
  display: flex;
  flex-wrap: wrap;
  overflow: hidden;
  justify-content: flex-start;
  align-content: flex-start;
}

.flex-item {
  width: 300px;
  position: relative;
  box-sizing: border-box;
  border: dashed 1px navy;
  overflow-wrap: break-word;
  word-wrap: break-word;
  word-break: break-word;
  -webkit-hyphens: auto;
  -ms-hyphens: auto;
  hyphens: auto;
  padding: 1em;
}

.flex-item .flex-wrapper {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-color: crimson;
}

.flex-item a.flex-permalink {
  text-decoration: none;
  color: black;
  display: block;
  background-color: lightpink;
  height: 100%;
}

.flex-item img { width: 100%;}

.flex-item h1 {
  font-family: 'Roboto', sans-serif;
  font-size: 1.5em;
  font-weight: 500;
  margin: 0;
  padding: 1em;
}
<div class="flex-container">
 <div class="flex-row">
  <!-- ------ Let the loop begin ------ -->
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Exercitation cupidatat ex non aliqua dolore veniam veniam officia ex dolore.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Sint eiusmod est laborum reprehenderit.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Adipisicing quis tempor duis irure magna quis occaecat.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Culpa dolore sint sit non voluptate nostrud nulla dolor laborum.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Tempor consectetur do elit magna sunt cillum dolor.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>AEu eiusmod qui nostrud anim nulla dolore non veniam excepteur adipisicing sed.</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Lorem ipsum duis non laboris</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Dolor occaecat laboris enim duis eiusmod</h1>
    </a>
   </div>
  </div>
  <div class="flex-item">
   <div class="flex-wrapper">
    <a class="flex-permalink" href="">
     <img src="SVG_fallback_image.svg" alt="">
     <h1>Exercitation do enim sint pariatur consectetur</h1>
    </a>
   </div>
  </div>
  <!-- ------ Let the loop end ------ -->
 </div>
</div>
DCR
  • 14,737
  • 12
  • 52
  • 115
  • yes that is what I want, the whole grid centered on the x-axis and I have already tried `margin: 0 auto;`, but it does not work. Your example also does not center the whole grid. – Arete Apr 08 '20 at 20:54
0

In your CSS you can specify that the last child align to the left; add this in your style:

.flex-row>*:last-child{
   margin-right: auto;
}

The only thing left to do is to adjust the space.

diegosmon
  • 3
  • 2
0

The problem with flexbox is that it is not a true grid, it relies on wrapping which leaves unwanted white space hoping that there might be a flex item small enough to fit within it, and so the container doesn't shrink.

Also since flexbox doesn't establish a true grid, there's no way of targeting the last row.

However, this can easily be done in CSS grid.

Using

grid-template-columns:repeat(auto-fit,300px);

We tell the grid to create columns the size of 300px width defined in .flex-item only if it can and only if there's more elements to put in that created column thanks to auto-fit

.flex-container {
  max-width: 100%;
  margin: 0 auto;
  background-color: whitesmoke;
}

.flex-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, 300px);
  overflow: hidden;
  justify-content: center;
}

.flex-item {
  width: 300px;
  position: relative;
  box-sizing: border-box;
  border: dashed 1px navy;
  overflow-wrap: break-word;
  word-wrap: break-word;
  word-break: break-word;
  -webkit-hyphens: auto;
  -ms-hyphens: auto;
  hyphens: auto;
  padding: 1em;
}

.flex-item .flex-wrapper {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-color: crimson;
}

.flex-item a.flex-permalink {
  text-decoration: none;
  color: black;
  display: block;
  background-color: lightpink;
  height: 100%;
}

.flex-item img {
  width: 100%;
}

.flex-item h1 {
  font-family: 'Roboto', sans-serif;
  font-size: 1.5em;
  font-weight: 500;
  margin: 0;
  padding: 1em;
}
<div class="flex-container">
  <div class="flex-row">
    <!-- ------ Let the loop begin ------ -->
    <div class="flex-item">
      <div class="flex-wrapper">
        <a class="flex-permalink" href="">
          <img src="SVG_fallback_image.svg" alt="">
          <h1>Exercitation cupidatat ex non aliqua dolore veniam veniam officia ex dolore.</h1>
        </a>
      </div>
    </div>
    <div class="flex-item">
      <div class="flex-wrapper">
        <a class="flex-permalink" href="">
          <img src="SVG_fallback_image.svg" alt="">
          <h1>Sint eiusmod est laborum reprehenderit.</h1>
        </a>
      </div>
    </div>
    <div class="flex-item">
      <div class="flex-wrapper">
        <a class="flex-permalink" href="">
          <img src="SVG_fallback_image.svg" alt="">
          <h1>Adipisicing quis tempor duis irure magna quis occaecat.</h1>
        </a>
      </div>
    </div>
    <div class="flex-item">
      <div class="flex-wrapper">
        <a class="flex-permalink" href="">
          <img src="SVG_fallback_image.svg" alt="">
          <h1>Culpa dolore sint sit non voluptate nostrud nulla dolor laborum.</h1>
        </a>
      </div>
    </div>
    <div class="flex-item">
      <div class="flex-wrapper">
        <a class="flex-permalink" href="">
          <img src="SVG_fallback_image.svg" alt="">
          <h1>Tempor consectetur do elit magna sunt cillum dolor.</h1>
        </a>
      </div>
    </div>
    <div class="flex-item">
      <div class="flex-wrapper">
        <a class="flex-permalink" href="">
          <img src="SVG_fallback_image.svg" alt="">
          <h1>AEu eiusmod qui nostrud anim nulla dolore non veniam excepteur adipisicing sed.</h1>
        </a>
      </div>
    </div>
    <div class="flex-item">
      <div class="flex-wrapper">
        <a class="flex-permalink" href="">
          <img src="SVG_fallback_image.svg" alt="">
          <h1>Lorem ipsum duis non laboris</h1>
        </a>
      </div>
    </div>
    <div class="flex-item">
      <div class="flex-wrapper">
        <a class="flex-permalink" href="">
          <img src="SVG_fallback_image.svg" alt="">
          <h1>Dolor occaecat laboris enim duis eiusmod</h1>
        </a>
      </div>
    </div>
    <div class="flex-item">
      <div class="flex-wrapper">
        <a class="flex-permalink" href="">
          <img src="SVG_fallback_image.svg" alt="">
          <h1>Exercitation do enim sint pariatur consectetur</h1>
        </a>
      </div>
    </div>
    <!-- ------ Let the loop end ------ -->
  </div>
</div>
Rainbow
  • 6,772
  • 3
  • 11
  • 28