1

I want to put grid in center, but it is always going to left. My idea is to create music album display photos.

To explain what i want i display an image. The first photos are what are happening, and where it is called "jazz" is my objective.

enter image description here

enter image description here

The code is:

<style>
.grid { 
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  grid-gap: 20px;
  align-items: center;
  justify-items: center;
  }
.grid img {
  border: 1px solid #ccc;
  box-shadow: 2px 2px 6px 0px  rgba(0,0,0,0.3);
  max-width: 100%;
  margin: auto;
  justify-items: center;
}
</style>
<section class="grid">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
</section>
clod9353
  • 1,942
  • 2
  • 5
  • 20

2 Answers2

2

I have indicated in the code where I applied changes.

.grid {
  display: grid;
  grid-template-columns: repeat(3, 200px); /* Adjusted */
  grid-gap: 20px;
  align-items: center;
  justify-content: center; /* Adjusted */
}

.grid img {
  border: 1px solid #ccc;
  box-shadow: 2px 2px 6px 0px rgba(0, 0, 0, 0.3);
  max-width: 100%;
  margin: auto;
  justify-items: center;
}
<section class="grid">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
  <img src="https://images.shazam.com/coverart/t510017399-b1503746433_s400.jpg" alt="Sample photo">
</section>
Gerard
  • 15,418
  • 5
  • 30
  • 52
0

I believe you are centering the span rather than the images themselves. Just move the code to .grid img:

.grid img {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  grid-gap: 20px;
  align-items: center;
  justify-items: center;
}

You can also use flexbox instead:

.grid img {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}
kawwasaki
  • 91
  • 2