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 3609402
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:37:23+00:00 2026-05-18T21:37:23+00:00

I have a number of divs floated left. When browser is resized they move

  • 0

I have a number of divs floated left. When browser is resized they move down or up based on how many can fit on the line. I was wondering if there is a way to dynamically (with css) have those divs align (or have margin) in a way, that they would always fill the entire screen space by having their marhin resize? In other words margin between them would resize while browser is resized, but as soon as another div can fit it will be added in the line, or if minimum margin is reached and passed another div goes to next line while margins expand again. Here’s an example how it is now, resize the wondow to see he leftover space that I want to “fill”

<html>
<head>
<style>
.test {
float:left;
width: 100px; 
height:100px;
background-color: grey;
margin: 0 10px 10px 0;
}
</style>
</head>
<body>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
</body>
</html>
  • 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-05-18T21:37:24+00:00Added an answer on May 18, 2026 at 9:37 pm

    I’ve seen this question a few times, and the result always seems to be that css doesn’t (yet) support distribution of elements across a dynamic width. There are hacks out there, but honestly I believe the quickest way around this would be to use javascript for the positioning.

    something like this (written in the prototype library) should do what you want:

    $(document).observe('dom:loaded',function(){
        spaceSquares();
    });
    window.onresize = function(){spaceSquares();}
    function spaceSquares(){
        // this is the minimum margin per square.
        var minMargin = 20;
        // this tells you how many squares will fit on the top row
        var topRowSquares = Math.floor(document.viewport.getWidth() / ($$('.test')[0].getWidth()+minMargin));
        // this will tell you how much space is left over
        var remainderWidth = document.viewport.getWidth() - (($$('.test')[0].getWidth()+minMargin)*topRowSquares);
        // this will tell you how much margin to put on each item
        var marginWidth = (remainderWidth / topRowSquares) + minMargin;
        // this will put the margin on the top row
        for(var i=0;i<topRowSquares;i++){
            $$('.test')[i].setStyle({'margin-left':marginWidth/2+'px','margin-right':marginWidth/2+'px'});
        }
    }
    

    I’m sure there’s more elegant ways to do it, but essentially I’m just calculating the remaining space and dividing it between the squares. I’ve wrapped the first call to the function in an observer so that it won’t try to fire before the dom is loaded, and I’m calling it on the window.onresize event, so that if you resize the window, it will always keep the top row perfectly distributed.

    Hopefully that will get you headed in the right direction!

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

Sidebar

Related Questions

I have a container that contains a number of left-floated divs, something like: <div
I have infinite number of divs of a 100px width, which can fit into
Using jQuery 1.6.2 I have a variable number of floated divs inside a parent.
I have a number of divs floating in several rows. The divs contain text-previews
What I am looking for: I have a number of divs that are used
I have a number of dropdowns and divs that are hidden when the page
I have a jcycle plugin on a number of divs which are testimonials. Basically
I have number of line breaks in a string. But I only want it
I have a number of classes and they are quite close to each other
I use this to number divs that have the class number: $(#parent .number).each(function(i){ $(this).html((i+1)

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.