中心响应图像

时间:2017-02-20 15:16:22

标签: html css responsive-design responsive

我的图片不会以margin: 0 auto;padding: 0 auto;为中心 如果我在text-align: center;周围进行<div>,它也无法工作。 我认为这是因为图像是响应式的。 那么如何让这些图像居中呢?

&#13;
&#13;
@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;
}
&#13;
<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>
&#13;
&#13;
&#13;

HTML看起来很长,因为我必须做更多的图像,所以你可以看到我的意思。当您点击&#34;整页&#34;他们将是彼此相邻的4张图片,在这个小的一个(可能)3中我不知道你是否可以让它变小但是它会转到2张图片,最后是1张。 当它的3和4处于中心位置但是当它处于2或1时它将不再居中。如果你能看到它没有集中在片段上,那么我会粘贴一些打印屏幕来显示它。

彼此相邻的2张图片:
2 images next to eachother

1张图片
1 image

唯一的问题是他们没有居中,我无法找到如何做到这一点。

您需要更多代码或信息吗?

3 个答案:

答案 0 :(得分:1)

将图像包裹在一个总体div中,并将其与flexbox对齐。

或者,从您的商品中删除float:left,将其display:inline-block设为text-align:center,然后将其换成包含<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> 的div。

Flexbox示例:

<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>

没有flexbox解决方案:

function red_on(){
   $('#red_light').css('opacity', 1);
}

setInterval(function () {
   red_on();
}, 4000);

setInterval(red_on, 4000);

答案 1 :(得分:1)

我认为这可能是你想要的? 确保移除浮动以重置浮动到左侧的图像。 还添加了保证金:0自动; (或图像容器上的文本对齐中心)但是你想要做那件事。

&#13;
&#13;
@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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这应该有效:

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