Present a sequence of thumbnails in a nearly equal arrangement

Looking to create a visually appealing gallery layout with evenly spaced thumbnails that perfectly fit the width of their container? Check out this unique solution provided by Marcelo Amorim:

Distributing images evenly & horizontally in a Div via CSS

Here is the CSS 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;
}

Sample HTML structure:

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

However, there's an issue where the last row of thumbnails may not align as expected when there are multiple rows. A solution is needed for achieving consistent alignment.

Are you looking for a way to maintain even spacing while ensuring the last row's thumbnails align to the left, creating a better gallery layout? Share your thoughts and suggestions on how this can be achieved using CSS or jQuery.

View the code example here

Answer №1

Enhanced response featuring a structured layout

I have yet to discover a more effective method than utilizing a table format and positioning your images accordingly.

Note: This particular example is not compatible with IE8 due to the absence of support for the :last-child pseudo-class.

To provide compatibility with IE8, you can implement 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';

}

ENHANCEMENT


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 require further clarification on any aspect, please feel free to inquire.

Answer №2

Successfully implemented a solution using 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>

Adjust the window size to see the thumbnails display.

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

Appreciate your assistance.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Responsive HTML grid with a distinct line separating each row

My challenge is to create a layout that features a grid of floating divs with horizontal lines under intermediate rows. The catch is that there should not be a line under the last row, and if there is only one row of elements, no line should be displayed. ...

How to prevent JQueryUI dialog from drifting off the browser window

Currently, I am utilizing JQueryUI with dialogextend on a Bootstrap template called Shards UI. An issue that I have encountered is that the dialog has the ability to move outside of the window, resulting in scrollbars appearing on the webpage: https://i. ...

Resolve Bootstrap 4 responsive table header alignment glitch

Utilizing the d-none and d-md-block classes on a responsive table to hide certain columns at smaller screen sizes has been successful overall. However, there is one issue - the hidden columns seem to be slightly misaligned with the rest of the table. Despi ...

One common issue is being unable to target input[type=file] when multiple forms are present on a page using JavaScript

Question: I have multiple dynamic forms on a web page, each with a file input field. How can I specifically target the correct file input using $(this) in JavaScript? Here is an example of one of my forms: <form enctype="multipart/form-data" action="c ...

What could be causing the target to malfunction in this situation?

Initially, I create an index page with frames named after popular websites such as NASA, Google, YouTube, etc. Then, on the search page, <input id="main_category_lan1" value="test" /> <a href="javascript:void(0)" onmouseover=" window.open ...

Is the Addthis widget displaying incorrectly in a fixed element?

I'm currently using addthis as a vertical toolbox with the hover popup feature on a fixed element. However, when scrolling, the popup appears in a different location. I've tried adjusting the offset top and left configurations, but they haven&apo ...

What is the best way to define a:link style within a div class structure?

I am trying to include an anchor tag within my HTML... Inside the tag, I have the following CSS code: .foo { white-space:nowrap; text-shadow: 0 -1px 0 #000; color: #000; font-family: Helvetica Neue, Helvetica, arial; font-size: ...

The script initially fails to fetch the correct image height but is able to retrieve it successfully during subsequent calls

Hey there! So, I've been working on some jQuery code that's triggered by: <script type="text/javascript"> $(function () { $('#AdsLarge').adRotator({ num_li: 2, pauseTime: 50 ...

Tips for staying on the same page when hovering over a table

My hover table contains multiple pages, with each row offering various actions one can take with an item. After performing an action, the page refreshes and returns to the first page of the table. Is there a way to stay on the same page after executing a ...

Swapping out data within a container

I've been experimenting with creating a hangman game using JavaScript and HTML. However, I'm facing an issue where clicking on a letter doesn't replace the "_" placeholder. var myList=["Computer","Algorithm","Software","Programming","Develop ...

"Enhancing user experience by implementing Google Places Auto Complete feature that dynamically adjusts input

I have encountered an issue while using the react-places-autocomplete package. Every time I type something into the input field and suggestions appear, the entire input field jumps up, which disrupts the overall appearance. Is there a way to prevent the ...

Utilize Firebase Hosting to Host Your Vue Application

Having some trouble with hosting code on Firebase. Instead of displaying the value, {{Item.name}} is appearing :( Same code works fine on Codepen. Wondering if Firebase accepts vue.min.js? When deployed, the site is showing {{var}} instead of the table va ...

"Creating a 2-column layout for an unordered list using CSS Flex

After spending far too much time at work today pondering this issue, I've decided to turn to the collective wisdom of the internet. My dilemma involves a standard <ul> containing multiple <li> elements (currently 12, but this number could ...

Comparing adjust-leading-to with leader in Compass - what sets them apart?

Currently exploring Compass and feeling a bit puzzled about the functionality of two mixins: adjust-leading-to and leader. Would appreciate it if someone could clarify the distinction between them. ...

Here's a solution to prevent the "jump" effect when hiding elements in a navbar

Currently, I am facing an issue with my h5 elements within the navbar when scrolling. I am using Bootstrap 4 and jQuery for this project. The problem arises in my navbar where there are four sets containing an icon, followed by an "h5" and then a "p" elem ...

Divergent Bootstrap layouts for different devices

https://i.sstatic.net/g1YeJ.png I am working on creating a layout where I want to display certain content differently based on whether the user is viewing it on a mobile or desktop device. The current approach I have taken involves duplicating some divs ...

Customize the CSS property in a scss Page

Currently, I am diving into the world of ionic. Within my app, I have a linkbutton where I am attempting to set the font size to 12px. However, it seems that this style is being overridden by other CSS properties. The SCSS file associated with the linkbutt ...

Having issues with Bootstrap 5 modal not functioning correctly?

I'm facing an unusual issue that I'm struggling to resolve. I'm currently utilizing Bootstrap 5 and encountered the following problem: const reportBtns = Array.from(document.querySelectorAll('.report')) reportBtns.forEach((btn) ...

Why is there space between two divs if there are no `margin`, `padding`, or `border` properties set?

Using vue.js, I created a page called index.vue which includes the components header.vue and home.vue: The code for index.vue: <template> <div class="index"> <i-header></i-header> <router-view></router-view> ...

Tips for placing the html <a> tag above an image and centered

Here's the HTML code I'm struggling with, which is inside an ASP.NET Repeater: <div class="team_member"> <a class="teamMemberLink" href='<%# "Profile.aspx?uID=" + Eval("ID") %>' target="_blank"><%# Eval("Name") ...