Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
Get value of the spacing between elements in justify-content:space-between

I need to get the amount of spacing between elements in my container which has the justify-content: space-between; property.

Is there any way to do it in Javascript/CSS ? I searched but found nothing...

Thanks !

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

You can get the offsetWidth of the container and your two elements. After that just do the maths

var diff = conatiner.offsetWidth - elementA.offsetWidth - elementB.offsetWidth
console.log(diff)

Check the working JSFiddle link here. Try resizing the screen width and you can see the distance between the blocks being changed. and it displays the new distance between the blocks

about 4 years ago · Santiago Gelvez Report

0

What I finally did is creating a function to calculate it :

This solution only works if all items in your container have the same size (which was my case)

const calculateSpaceBetween = () => {

    let container = document.querySelector("your container");
    let containerWidth = container.offsetWidth;
    let containerItems = container.children;
    let containerItemsWidth = containerItems[0].offsetWidth;
    let containerItemCount = containerItems.length;

    let spaceBetween = (containerWidth - containerItemsWidth * containerItemCount) / containerItemCount;

    return spaceBetween

To make sure it handles responsive, trigger it in an eventListener like so :

window.addEventListener("resize", calculateSpaceBetween);
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!