Minegolfer
Minegolfer

Reputation: 374

Center responsive images

My images won't center with margin: 0 auto; or padding: 0 auto; Also it doesn't work if I do text-align: center; in the <div> around it. I think this is because the images are responsive. So how do I get these images centered?

@media only screen and (min-width: 1001px) {
  .gallerij {
    max-width: 825px;
    margin: 20px auto;
    padding: 0 10px 30px 200px;
  }
}

.gallerij h1 {
  margin-left: 10px;
}

.gallerij div.img {
  border: 1px solid #ccc;
  padding: 0;
  margin-top: 10px;
  background-color: rgba(211, 211, 211, 0.30);
}

.gallerij div.img:hover {
  border: 1px solid #000000;
}

.gallerij div.img img {
  display: block;
  width: auto;
  max-width: 100%;
  height: 200px;
  padding: 3px;
  margin-left: auto;
  margin-right: auto;
}

.gallerij div.desc {
  padding: 15px;
  text-align: center;
}

.gallerij * {
  box-sizing: border-box;
}

.responsive {
  padding: 0 6px;
  float: left;
  width: 24.9999%;
  max-width: 200px;
}

@media only screen and (max-width: 700px) {
  .responsive {
    width: 49.99999%;
  }
}

@media only screen and (max-width: 500px) {
  .responsive {
    width: 100%;
  }
}

.gallerij .clearfix:after {
  content: "";
  display: table;
  clear: both;
  margin-bottom: 30px;
}
<div class="gallerij">
  <h1 class="page-titel">All articles:</h1><br>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img2.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img4.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="clearfix"></div>
</div>

The HTML looks that long because I had to do more images so you could see what I mean. When you click on "full page" their will be 4 images next to eachother, in this little one (probably) 3 and I dont know if you can make it smaller but then it will go to 2 images and finally 1. When it's 3 and 4 it is centered but when it's at 2 or 1 it will not be centered anymore. Idk if you can see it not getting centered at the snippet so I will paste some print screens to show it.

2 images next to eachother:
2 images next to eachother

1 image:
1 image

The only problem is that they are not centered and I can't find how to do that.

Do you need more code or information?

Upvotes: 2

Views: 130

Answers (3)

Eamonn
Eamonn

Reputation: 418

Wrap your images in an overarching div, and center that with flexbox.

Alternatively, remove the float:left from your items, make them display:inline-block, and wrap them in a div with text-align:center.

Flexbox example:

<div style="
    display: flex;
    flex-flow: row wrap;
    justify-content: center;
">
    <div class="responsive">
        <div class="img">
            <a href="#">
                <img src="img/ERD_Easy/img2.jpg" alt="" class="article-img">
            </a>
            <div class="desc">Artikel naam + kleine bescrijving</div>
        </div>
    </div>
    ...
</div>

Without flexbox solution:

<div style="text-align:center;">
    <div class="responsive" style="float:none;display:inline-block;">
        <div class="img">
            <a href="#">
                <img src="img/ERD_Easy/img2.jpg" alt="" class="article-img">
            </a>
            <div class="desc">Artikel naam + kleine bescrijving</div>
        </div>
    </div>
    ...
</div>

Upvotes: 1

Andrew Ice
Andrew Ice

Reputation: 841

I'm thinking this might be what you want? Make sure that you remove the float in order to reset the images from floating to the left. Also added a margin: 0 auto; (Or text-align center on the image container) However you want to do that piece.

@media only screen and (min-width: 1001px) {
  .gallerij {
    max-width: 825px;
    margin: 20px auto;
    padding: 0 10px 30px 200px;
  }
}

.gallerij h1 {
  margin-left: 10px;
}

.gallerij div.img {
  border: 1px solid #ccc;
  padding: 0;
  margin-top: 10px;
  background-color: rgba(211, 211, 211, 0.30);
}

.gallerij div.img:hover {
  border: 1px solid #000000;
}

.gallerij div.img img {
  display: block;
  width: auto;
  max-width: 100%;
  height: 200px;
  padding: 3px;
  margin-left: auto;
  margin-right: auto;
}

.gallerij div.desc {
  padding: 15px;
  text-align: center;
}

.gallerij * {
  box-sizing: border-box;
}

.responsive {
  padding: 0 6px;
  float: left;
  width: 24.9999%;
  max-width: 200px;
}

@media only screen and (max-width: 700px) {
  .responsive {
    width: 49.99999%;
    float: none;
    margin: 0 auto;
  }
}

@media only screen and (max-width: 500px) {
  .responsive {
    width: 100%;
  }
}

.gallerij .clearfix:after {
  content: "";
  display: table;
  clear: both;
  margin-bottom: 30px;
}
<div class="gallerij">
  <h1 class="page-titel">All articles:</h1><br>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img2.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img4.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="responsive">
    <div class="img">
      <a href="#">
        <img src="img/ERD_Easy/img3.jpg" alt="" class="article-img">
      </a>
      <div class="desc">Artikel naam + kleine bescrijving</div>
    </div>
  </div>
  <div class="clearfix"></div>
</div>

Upvotes: 1

Vcasso
Vcasso

Reputation: 1348

This should work:

.gallerij {
    width: 420px;
    margin: 0 auto;
}

Upvotes: 0

Related Questions