构建响应式图像网格

时间:2014-04-07 19:52:53

标签: html wordpress css3

我使用以下代码构建响应式图像网格:

.gallery {
    width: 100%;
    height: auto;
    position: relative;
    float: left;
    margin: 5px 0 40px 0;
    padding: 5px;
    background-color: rgba(28, 28, 28, 0.8);
    -webkit-box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, 0.6);
    -moz-box-shadow:    0px 0px 15px 0px rgba(0, 0, 0, 0.6);
    box-shadow:         0px 0px 15px 0px rgba(0, 0, 0, 0.6);
    border: 1px solid #333;
}

.box {
    float: left;
    position: relative;
    width: 33%;
    padding-bottom: 20%;
    background-color: #093;
}

.boxInner {
       position: absolute;
       left: 5px;
       right: 5px;
       top: 5px;
       bottom: 5px;
       overflow: hidden;
    }
    .boxInner img {
       width: 100%;
    }
    .boxInner .titleBox {
       position: absolute;
       bottom: 0;
       left: 0;
       right: 0;
       margin-bottom: -50px;
       background: #000;
       background: rgba(0, 0, 0, 0.5);
       color: #FFF;
       padding: 10px;
       text-align: center;
       -webkit-transition: all 0.3s ease-out;
       -moz-transition: all 0.3s ease-out;
       -o-transition: all 0.3s ease-out;
       transition: all 0.3s ease-out;
    }

<section class='gallery'><div class='box'>
      <div class='boxInner'>
        <a class="fancybox" rel="gallery-1" href='http://ng1club.everythingcreative.co.uk/?attachment_id=202'><img src="http://ng1club.everythingcreative.co.uk/wp-content/uploads/2014/04/Club-Soda-400x260.jpg" class="attachment-thumbnail" alt="Club Soda" /></a>
      </div></div><div class='box'>
      <div class='boxInner'>
        <a class="fancybox" rel="gallery-1" href='http://ng1club.everythingcreative.co.uk/?attachment_id=233'><img src="http://ng1club.everythingcreative.co.uk/wp-content/uploads/2014/04/Main-Room-400x260.jpg" class="attachment-thumbnail" alt="Main Room" /></a>
      </div></div><div class='box'>
      <div class='boxInner'>
        <a class="fancybox" rel="gallery-1" href='http://ng1club.everythingcreative.co.uk/?attachment_id=232'><img src="http://ng1club.everythingcreative.co.uk/wp-content/uploads/2014/04/Upstairs-Arena-400x260.jpg" class="attachment-thumbnail" alt="Upstairs Arena" /></a>
      </div></div><div class='box'>
      <div class='boxInner'>
        <a class="fancybox" rel="gallery-1" href='http://ng1club.everythingcreative.co.uk/?attachment_id=231'><img src="http://ng1club.everythingcreative.co.uk/wp-content/uploads/2014/04/The-Gallery-400x260.jpg" class="attachment-thumbnail" alt="The Gallery" /></a>
      </div></div><div class='box'>
      <div class='boxInner'>
        <a class="fancybox" rel="gallery-1" href='http://ng1club.everythingcreative.co.uk/homepage/reception-2/'><img src="http://ng1club.everythingcreative.co.uk/wp-content/uploads/2013/11/Reception-400x260.jpg" class="attachment-thumbnail" alt="Reception" /></a>
      </div></div><div class='box'>
      <div class='boxInner'>
        <a class="fancybox" rel="gallery-1" href='http://ng1club.everythingcreative.co.uk/?attachment_id=230'><img src="http://ng1club.everythingcreative.co.uk/wp-content/uploads/2014/04/The-Terrace-400x260.jpg" class="attachment-thumbnail" alt="The Terrace" /></a>
      </div></div>
  </section>

然而,它在右边留下了一个空隙,但我看不出我做错了什么。它很可能很简单,但我无法弄清楚:

这是一个JSfiddle: http://jsfiddle.net/wMNNB/

2 个答案:

答案 0 :(得分:1)

我能看到的两件事:

  1. 宽度为100%,填充量为5px。这意味着它将与容器一样宽100%+ 10像素。您可以在现代浏览器中使用box-sizing: border-box;来使用在确定宽度时不包含填充的框模式。

  2. 您使用的是33%的宽度。这留下了1%的3个元素。尝试33.3334%。

答案 1 :(得分:1)

由于您要添加填充并使用不精确的值(33%),因此它将无法正确对齐。您可以使用以下方法解决填充问题:(Fiddle

* {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}

这使宽度包括填充(和边框)。