Search code examples
htmlcsswordpresscss-gridfancybox

Building a responsive image grid


I'm building a responsive image grid using the following code:

CSS:


.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;
}

HTML:

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

However, it's leaving a gap on the right but I can't see what I'm doing wrong. It is most likely something simple but I can't figure it out:

Heres a JSfiddle


Solution

  • 2 things I can see:

    1. You have width 100% and padding of 5px. That means it's going to be 100% + 10 pixels as wide as the container. You can use box-sizing: border-box; in modern browsers to use a box moden that does not include padding when determining width.

    2. You are using 33% width. That leaves 1% with 3 elements. Try 33.3334%.