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

  • Home
  • SEARCH
  • 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 6538567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:45:03+00:00 2026-05-25T10:45:03+00:00

Using jQuery 1.6.2 I have a variable number of floated divs inside a parent.

  • 0

Using jQuery 1.6.2

I have a variable number of floated divs inside a parent. The parent may have a fixed width but also may not.

I want to use jQuery to work out what number of divs are in one row, then insert a div with clear property set to both, so that the children may have variable heights and not make a mess of everything, and the parent may have a variable width.

It’s a simple thing to do either in the markup or server side scripting if you know the width of the div and if you know how many children you want in a row – but what if everything is a bit more liquid?

Is there a simple way to do this, or am I going to have to do some working out of all the widths involved and a bit of clever maths to decide where to put the clear divs?

  • 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-25T10:45:04+00:00Added an answer on May 25, 2026 at 10:45 am

    Try this:

    Sample jQuery plugin (couldn’t come up with a better plugin name):

    (function ($) {
        $.fn.extend({
            addLineBreak: function (elementToInject) {
                var minLeft = $(this).position().left;
    
                return $(this).each(function () {
                    var position = $(this).position();
                    if (position.left > minLeft && $(this).prev().position().left >= position.left) {
                        $(elementToInject).insertBefore(this);
                    }
                });
            }
        });
    })(jQuery);
    

    Usage:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>jQuery</title>
            <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
            <script type="text/javascript" src="jquery.addLineBreak.js"></script>
            <script type="text/javascript">
                $(function () {
                    // '.inner' is the selector for the child elements!
                    $('.inner').addLineBreak('<div class="clear"></div>');
    
                    // on resize, remove the clear-elements, and inject new ones
                    $(window).resize(function () {
                        $('#tmp .clear').remove();
                        $('.inner').addLineBreak('<div class="clear"></div>');
                    });
                });
            </script>
    
            <style type="text/css">
                body {
                    background: black;
                }
    
                .inner {
                    float: left;
                    margin: 5px;
                    padding: 5px;
                    border: 1px solid #efefef;
                }
    
                .clear {
                    clear: both;
                }
            </style>
        </head>
    
        <body>
            <div id="tmp">
                <?php for ($i = 0; $i < 25; $i += 1): ?>
                    <div class="inner" style="width: <?php echo rand(100, 150); ?>px; height: <?php echo rand(100, 150); ?>px"><?php echo $i; ?></div>
                <?php endfor; ?>
            </div>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some tabs using jQuery UI which work just fine, but I want
I have a JQuery (using JQuery 1.4.2) problem that exhibits only in IE8 in
Using jQuery UI's Droppable I have made a shelf type thing where the items
I have problem while using jquery maskedinput with asp.net textbox. I have a check
I have been using Jquery alot lately an was wondering if I should use
I have just started using jQuery and although following code gets the job done,
I have started using jQuery and rails. I have made a simple form that
So I have a system using jQuery AJAX to load the content dynamically. I
I have a MVC app and using JQuery. I have anchor that I setup
I am using jquery UI tabs I have three ajax tabs like so: <div

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.