0

I would like to display a series of thumbnails in a gallery and would like them to be displayed evenly in their container while fitting exactly the width of that container.

I found a very good solution here (the one from Marcelo Amorim, not the popular one):

Distributing images evenly & horizontally in a Div via CSS

This is basically the solution he came up with:

#container {
    text-align: justify;
}
.pic_bloc {
    width: 130px; 
    height:160px;
    display: inline-block;
    vertical-align: top;
    margin-bottom:30px;
}

.pic_bloc img{
    width:130px;
    height:160px;
}

#container:after {
    content: "";
    width: 100%;
    display: inline-block;
}

This is my HTML:

<div id="container">              
    <div class="pic_bloc"><img src="1.jpg"/></div>
    <div class="pic_bloc"><img src="2.jpg"/></div>
    <div class="pic_bloc"><img src="3.jpg"/></div>
    <div class="pic_bloc"><img src="4.jpg"/></div>
    <div class="pic_bloc"><img src="5.jpg"/></div>
    <div class="pic_bloc"><img src="6.jpg"/></div>
    <div class="pic_bloc"><img src="7.jpg"/></div>
</div>

The problem is that it is too effective.

For instance, if I have two rows and 7 thumbnails, the first row will display evenly 5 thumbnails and the second row will display two thumbnails dispatched at the extreme positions, leaving the middle of that row completely empty.

Here is the result:

Instance1

This is not what one intuitively expects from a standard gallery or list. So is there any way to get the same behavior but with the last row's thumbnails aligned to the left like this?

Instance2

This would make a lot more sense for a gallery.

As the script uses a justified aligning, I suspect that it would require a complete different approach but I didn't find a clean solution by myself or online.

PS: jQuery or CSS, anything that works would do.

Thank you.

http://jsfiddle.net/uG2U4/

Community
  • 1
  • 1
Baylock
  • 1,246
  • 4
  • 25
  • 49

2 Answers2

3

Updated answer with table layout

I haven't come up with anything better than using table structure and positioning your images accordingly.

Edit - > This example doesn't work with IE8 as the latter has no support of :last-child pseudo-class.

You can add support for IE8 using Javascript ->

Javascript

table_ = document.getElementById('tbl');
tr_ = table_.getElementsByTagName('tr');

for(var i=0;i<tr_.length;i++){

    tr_[i].lastChild.style.width = '1px';
    wrapperDiv = tr_[i].lastChild.getElementsByTagName('div')[0];
    wrapperDiv.style.position = 'relative';
    wrapperDiv.style.left = '1px';

    }

UPDATE


HTML

<table class='table' id='tbl' cellpadding='0' cellspacing='0'>
    <tr>

        <td><div class='parentWrapper'><img src='http://www.openvms.org/images/samples/130x130.gif'></div></td>
        <td><div class='parentWrapper centerDiv'><img src='http://www.openvms.org/images/samples/130x130.gif'></div></td>
        <td><div class='parentWrapper centerDiv'><img src='http://www.openvms.org/images/samples/130x130.gif'></div></td>
        <td><div class='parentWrapper centerDiv'><img src='http://www.openvms.org/images/samples/130x130.gif'></div></td>
        <td><div class='parentWrapper rightDiv'><img src='http://www.openvms.org/images/samples/130x130.gif'></div></td>

    </tr>  

    <tr>
       <td><div class='parentWrapper'><img src='http://www.openvms.org/images/samples/130x130.gif'></div></td>
        <td><div class='parentWrapper centerDiv'><img src='http://www.openvms.org/images/samples/130x130.gif'></div></td>
        <td></td>
             <td></td>
             <td></td>
        <td></td>
    </tr>  

</table>

CSS

.table{
    width:100%;
    height:auto;
    border:1px solid;    
}

.table td {
    width:auto;
    height:170px;
    vertical-align:top
}

.table td:last-child{
    width:1px;  
}

.table td:last-child div{
    position:relative;
    left:1px;  
}

.parentWrapper{
    width:50px;
    height:160px;
    border:1px solid brown;  
}

.table img {
    width:50px;
    height:160px;
    border:0px solid red;   
}

If you don't understand something, let me know.

W.D.
  • 1,033
  • 1
  • 7
  • 12
  • Thank you for your answer. Unfortunately no because in your example everything is aligned to the left (so there's a gap between the last thumbnail of the first row and the container). In the previous example, the first row is perfect as the thumbnails are just matching exactly the width of the container. That's the catch: if it is justified, all the rows are perfect but the last one and if it is aligned to the left, the last row is perfect and all the others are not… – Baylock Apr 20 '14 at 22:12
  • Thank you. It's almost it and if it's the best that can be achieved I'll take it as a valid answer. The last issue is: the first and last thumbnail of the first row are not horizontally touching the container like in the other script. Your script is impressive enough but it doesn't fit the bill yet. – Baylock Apr 20 '14 at 22:39
  • I'm pushing it, I'm sorry! In the last edit, the pictures and their pic_bloc don't have the same width and it's the margin between each picture and its pick_bloc that changes. I would like each picture and its pic_bloc to have both the same width and what varies is the margin between two adjacents pic_blocs. I know it's tricky as it's not easy to describe it. Summary: evenly dispatch the thumbs in a div while the first and last thumb of each row touches the div AND where the last row has its thumbnails aligned to the left (if there is not enough thumbnails to complete that row). Thanks! – Baylock Apr 20 '14 at 23:01
  • Can you use a table layout? – W.D. Apr 20 '14 at 23:33
  • Anything is ok as long as it is cross-browser, IE8 (included) and above. – Baylock Apr 20 '14 at 23:38
  • Just so you know: my container is not fluid. It has a fixed width but I'm building an adaptive website with media queries and I display a container of a specific width according to the device (but the html remains the same whatever the device). This is why I still need the gallery to be fluid as it should work on any container even if each one of these containers has in fact a fixed width. This extra information may clarify the purpose of the request... – Baylock Apr 20 '14 at 23:46
  • This time it's visually perfect but you are cheating though: you inserted the notion of left/centered/right thumbnail. If you read my note here above, it should work on any container's width. With the first script it will fill the space with as many thumbnails per row as possible whatever the width of the container but with your last update you are limiting a row to a fixed number of thumbnails which are already determined to go on a specific location. I really appreciate that you tried so many times but it's still not closing the case. – Baylock Apr 21 '14 at 08:23
  • 1
    @Baylock, I believe now you can add as many elements to the container as you want and they use no alignment whatsoever, just like you wanted. One note though: this example won't work with IE8 as that browser has no support of `:last-child pseudo class`. I did my best. – W.D. Apr 21 '14 at 09:07
  • @Baylock, this is another link with more elements inside http://jsfiddle.net/BLU3s/11/ – W.D. Apr 21 '14 at 09:14
  • I know you did your best and even more, thank you. Anyway, the number of boxes per row is still static. Can I ask you to go back to my jsfiddle and shrink/expand the result section? Of course, for the example sake I used a fluid container but if it is fixed, whatever the container width, it will put as many boxes as possible inside a row and there will never be any overflow. In your update, if the container is smaller than the number of boxes expected, there is an overflow. Thank you anyway. – Baylock Apr 21 '14 at 09:43
0

Got it to work with some jQuery!

    <script>

    $(document).ready(function() 
    {
         var containerWidth       = $('.container').width();
         var thumbWidth           = $('.pic_block').width();
         var thumbsPerLine        = Math.floor(containerWidth/thumbWidth);
         var marginTotalWidth     = (containerWidth-(thumbsPerLine*thumbWidth));
         var tempMarginR          = Math.floor(marginTotalWidth/(thumbsPerLine-1));
         var extraTotalSpaceWidth = ((marginTotalWidth/(thumbsPerLine-1)) - tempMarginR) *thumbsPerLine;
         var i                    = Math.floor(extraTotalSpaceWidth); 
         var n                    = 1;           

         $('.pic_block').css('margin-right',tempMarginR);

         while( n <= thumbsPerLine) 
         {      
             if(n == thumbsPerLine)
             {
                 $('.pic_block:nth-child('+n+'n)').css('margin-right',0); 
             }
             else
             {
                 if(i>0)
                 {
                     $('.pic_block:nth-child('+thumbsPerLine+'n+1)').css('margin-right','+=1');  
                     i--;
                 };
             };
             n++; 
         }       
      });

  </script>

Resize the window and watch the display of the thumbnails.

http://jsfiddle.net/Baylock/ZkCgv/7/

Thank you for your help.

Baylock
  • 1,246
  • 4
  • 25
  • 49