Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7750535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:19:01+00:00 2026-06-01T11:19:01+00:00

Before suggesting bin packing algorithms, they assume you can re-order the elements and arrange

  • 0

Before suggesting bin packing algorithms, they assume you can re-order the elements and arrange them any way you please. Note as you read that I have a restriction on the ordering and arrangement.

So obviously there’s no such thing as kerning divs, but it’s the most appropriate term I could think of. Basically I have a lot of elements on a page, divs and imgs, with fixed width and height. They have to remain in order (they are chronological) and should be displayed left to right, top to bottom. I want to “kern” the elements in such a way as to make the whitespace as uniform as possible. It’s a very easy task to achieve horizontally, but vertical whitespace is more difficult. Uneven excess whitespace around the outermost edge of the packed elements is acceptable.

I was thinking as a really dumb solution, I could set the max width of the area to be packed to something like 1000px. Then I could keep an array with 1000 indices keeping track of the bottom of that column of pixels on the screen. When I add a new element, I see if it should be moved up a little into a gap and if shifting it left or right a little bit would allow it to be moved up.

As I said, that’s a really dumb solution. Are there any algorithms I could use or has anyone had to deal with a similar packing problem?

UPDATE:

Per questions in comments. The elements have random widths and heights. My test case is to create 100 image tags with images from placekitten.com with random widths and heights in the 200-300px range and arrange those images on the page in the order they are generated.

Create an html page and throw the below code in your head to get to where I am (please no comments on general code quality, I know there are some optimizations, I literally threw this together as a demo in under 10 minutes):

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var maxwidth = window.innerWidth - 80;
    function justify(row, filled)
    {
        var remaining = maxwidth - filled + 30,
            imgs = $('img',row),
            margin = Math.floor((remaining/imgs.length) / 2);
        $(imgs).each(function(){ this.style.marginLeft=margin+"px";this.style.marginRight=margin+"px"});
    }

    $(function()
    {
        var row = document.createElement('div'),
            filled = 0;
        document.body.appendChild(row);
        for (var i = 0; i < 100; ++i)
        {
            var img = document.createElement('img'),
                width = Math.floor(200 + Math.random() * 100),
                height = Math.floor(200 + Math.random() * 100);
            if ((filled + width) > maxwidth)
            {
                justify(row, filled);
                row = document.createElement('div');
                filled = 0;
                document.body.appendChild(row);
            }
            filled += width;
            img.src = "http://placekitten.com/" + width + "/" + height;
            row.appendChild(img);
        }
        justify(row,filled);
    })
</script>
<style>
    img{position:relative; vertical-align:middle}
</style>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:19:02+00:00Added an answer on June 1, 2026 at 11:19 am

    Take a look at the beautiful Masonry Plugin I used for a similar problem.

    Also, try similar plugins like Columnizer and jLayout.

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen this question asked before and tried following the solutions posted. Unfortunately, they
Is there any way of sharing column widths between controls, not just between multiple
Important: we really need the stated functionality, so please don't start suggesting alternatives. We're
Before you answer this I have never developed anything popular enough to attain high
Before I start, I know there is this post and it doesn't answer my
Before I do this I figured I would ask if it was the best
Before I jump headlong into C#... I've always felt that C, or maybe C++,
Before, I have found the Cost in the execution plan to be a good
Before moving on to use SVN, I used to manage my project by simply
Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.