Arrange all absolute divs equidistant from each other

I'm struggling with positioning multiple absolute divs inside a relative container using the CSS 'left' property. I want these inner divs to be evenly spaced regardless of how many there are, without setting fixed values for each.

Currently, I'm stuck on the calculations needed to achieve this. I have provided a picture of the desired result below, along with the code I have so far.

Any help or guidance on this would be greatly appreciated!

https://i.sstatic.net/0nwpM.jpg

HTML

<div id="bubbles-container" populate-bubbles></div>

CSS

#bubbles-container{
   position: relative;
   width: 100%;
   height: 300px;
}
.bubble {
   position: absolute;
   border: 1px solid rgba(0, 0, 0, 0.2);
   bottom: 0;
   border-radius: 10px;
   height: 15px;
   width: 15px;
}

JS/ANGULAR

app.directive('populateBubbles', [function(){
    return function(scope, element, attr){
        console.log(element);
        for (i = 1; i <= 10; i++){
            element.append('<div class="bubble bubble' + i + '"></div>');
        }

        element.find('.bubble').each(function(){
            var bubbleLength = $(this).length;
            var bubbleWidth = $(this).width();
            var containerWidth = element.width();

            ...
        })
    }
}])

Answer №1

Avoid the hassle of dealing with precise positions by utilizing flexbox. No need to manually calculate:

#bubbles {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

Check out this example -> https://jsbin.com/gepufuy/1/edit?html,css,js,output

Answer №2

If flexbox is not an option for you, here is an alternative approach using

display: table;
display: table-cell;

This method is compatible with IE10.
-> Check out this example

Answer №3

To achieve this effect using jQuery, you can use the following code snippet:

let index = 0;
let containerWidth = $('#bubbles-container').width();
let numberOfBubbles = $('.bubble').length;

$('.bubble').each(function() {

    let leftSpace = (containerWidth / numberOfBubbles) * index;
    $(this).css({left: leftSpace + 'px'});

    index++;
});

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

Text field auto-saving within an iFrame using localStorage is not functioning as expected

My goal is to create a rich text editor with an autosave feature using an iframe. Although each code part works individually, I am struggling to combine them effectively. View LIVEDEMO This graphic illustrates what I aim to accomplish: The editable iFram ...

Automatically resizing textures in Three.JS with WebGL

Seeking assistance to overcome a challenge :) I am using Three.JS to showcase high-resolution equirectangular images on a sphere (20000x10000 pixels). Image quality is crucial for my web application, and bandwidth is not a concern. My issue is that ThreeJ ...

The $watch feature in AngularJS does not function properly within a directive when a controller is used to update data

I created a custom directive and also have a controller to bind data to the directive. The data is retrieved from the server and bound to the directive. However, I noticed that the data in the directive on the page does not update when I change the scope ...

Tips for utilizing the React Page Scroller with a webpage exceeding 100vh in size

I'm currently facing an issue with React Page Scroller. While most components I'm using are set to take up 100vh and work smoothly with React Page Scroller, the problem arises with the footer, which exceeds the 100vh limit. I attempted to place t ...

JavaScript array displaying excess whitespace in its presentation

https://i.sstatic.net/5AdA7.png One issue I am facing is that when I use console.log(a), it adds extra spaces which then flow through to the backend. This leads to each element breaking after the first one. How can I resolve this problem and why does it o ...

Error in thread : TagNameUnexpectedException

I encountered an issue while trying to find a dropdown using Select: An error occurred: Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input" Even when attempting to use ...

Where is the best place to import Bootstrap in my SCSS file when customizing it with Sass?

When attempting to customize bootstrap using Sass, I discovered that overriding default bootstrap variables can be quite confusing. I am curious if someone could provide an explanation for the inconsistent behavior. Some variables only seem to be overridd ...

Extract the text enclosed by two specific symbols within a string and add it to a new array

I have a string formatted as follows: var str = "-#A This text belongs to A. Dummy Text of A. -#B This text belongs to B. Dummy Text of B. -#C This text belongs to C. Dummy text of C. -#Garbage This string should be ignored" I am looking to convert this ...

Interactive Vue components with dynamic children and sub-children

In my Vue application, I have a component called Address.vue which contains a child component called Contact.vue. One address can contain multiple components What I have accomplished: I have implemented the functionality in the Address.vue component t ...

I'm curious, which effect or plugin is being used here?

Do you know the name of this particular parallax effect or the plugin used for it? I'm also interested in finding more examples with a similar type of effect, so if you have any tutorials on how to achieve it, that would be greatly appreciated. ...

Resizing Images with Bootstrap

While working with twitter bootstrap 2.2.2, I have noticed that the image in the "hero" div resizes correctly when the page loads initially. However, it then unexpectedly expands beyond the div boundaries and gets cut off. You can view the page here: It ...

Run code snippets within summaries

I am seeking assistance with JQuery. Currently, I have checkboxes populated in the following manner: <span class="checked"> <input class="cbid" type="checkbox" name="cb1"> </span> <span > <input class="cbid" type="checkbox" ...

Working with Three.js: Utilizing the SpotLight and dat.gui

I have implemented a SpotLight in my scene along with an OctahedronGeometry as a visual aid for the user. The SpotLight can be moved using transformControls by selecting it, which is working properly. However, the issue arises when I try to edit the setti ...

Retrieving the checkbox's status from the database

I have implemented a basic checkbox that updates its state in the database when clicked or unclicked. Is there a way to retain this state and have it displayed as checked if the page is refreshed? Essentially, I want the checkbox to remember its last state ...

Having trouble getting the dropdown button to function properly in my Bootstrap navigation bar implementation

Can anyone help me troubleshoot why the dropdown button is not functioning in my code? I've tried following tutorials and searching for solutions online, but nothing seems to work. Any assistance would be greatly appreciated. <!DOCTYPE html> & ...

Error in JSON access permissions while using AJAX call to Webmethod in ASP.NET webforms with jquery Datatables

My current project involves implementing server-side processing for jQuery Datatables.NET in an ASP.NET webforms test application. Here is the code I am using: $(document).ready(start); function start(){ $('#PersonsTable').DataTable({ ...

Use Jquery to apply quotation marks within a blockquote

Here is the code I am working with: $("input[id="+id.slice(0,-1)+"-br-"+brand+"].qnt_to_cart").show(); This code generates: input[id=02620-br-FEBI BILSTEIN].qnt_to_cart However, I need it to look like this instead: input[id="02620-br-FEBI BILSTEIN"].q ...

Ways to showcase multiple elements using introjs

I am attempting to highlight multiple DOM elements using the JS library intro.js, but I am encountering an issue. It seems that I can only define one element to be highlighted at a time. introjs.setOptions({ steps: [ { ...

How can we enable Material UI tooltip based on certain conditions?

In my React component utilizing Material UI, I have the code snippet below: const MyButton = ({ warningText }) => ( <Tooltip title={warningText}> <Button>Do action</Button> </Tooltip> ) At present, an empty tool ...

Issue with Dropdown functionality in Navigation Bar when using Bootstrap and Angular

Currently, I am delving into the realms of Angular and Bootstrap in my learning journey. The challenge I am facing involves code generated by Yeoman. In this code, normal links in my Nav bar function properly, but the drop down items are not behaving as ex ...