Creating equal spacing between divs and the edges of the container

I need to arrange a set of divs equidistant from each other. My goal is to have the maximum number of divs fit side by side with equal spacing between them and the container edge.

While I almost achieved this with the code below (credit to another post), there is an issue when resizing the page as shown on where the items on the edges do not adjust their distance from the sides.

<body>
    <div id="container">
        <div class='item'><a href='#'><img src='test.png' alt='1' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='2' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='3' height='150' width='150' /></a><br />test</div>
        ...
        <div class='item'><a href='#'><img src='test.png' alt='10' height='150' width='150' /></a><br />test</div>
    </div>
</body>

The CSS used for this layout:

#container {
    text-align: justify;
}
#container:after{
    content: '';
    width: 100%;
    display: inline-block;
}

.item {
    text-align: center;
    width: 200px;
    display: inline-block;
    box-sizing: border-box;
    padding:25px;
}

.item img {
    border-radius: 25px;
}

If anyone has suggestions on achieving what I need, please share. Keep in mind that the number of items is variable, so manual specification of distances is not viable.

Your help would be greatly appreciated.

Answer №1

Check out this flexbox layout and see if it suits your needs.

#container {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
}
.item {
    flex: 0 0 150px;
    margin: 25px;
}
<body>
    <div id="container">
        <div class='item'><a href='#'><img src='test.png' alt='1' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='2' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='3' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='4' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='5' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='6' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='7' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='8' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='9' height='150' width='150' /></a><br />test</div>
        <div class='item'><a href='#'><img src='test.png' alt='10' height='150' width='150' /></a><br />test</div>
    </div>
</body>

jsfiddle

Answer №2

If you want to create a layout using flexbox (though it may not work on older browsers), here is one way to do it:

Styles in CSS:

.wrapper {
  display: flex;
  justify-content: space-between;
}

.box {
  flex: 1 0 200px;
}

HTML structure:

<div class="wrapper">
  <div class="box">Box 1</div>
  <div class="box">Box 2</div>
</div>

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

Alter the color of the initially chosen option in the dropdown selection

I'm struggling to modify the color of the initial selection while keeping the remaining selection-options at their default black color. Unfortunately, I haven't been successful in achieving this. Currently, all colors are #ccc. What I want is fo ...

Having trouble with the javascript dropdown functionality

I am currently facing an issue with my Javascript menu. The hover and click functionality for the main menu items is working perfectly fine, but I am struggling to make the dropdown feature work. Here is the code snippet from menu.js: /* When the user cl ...

Encountering an issue with Nuxt 3.5.1 during the build process: "ERROR Cannot read properties of undefined (reading 'sys') (x4)"

I am currently working on an application built with Nuxt version 3.5.1. Here is a snippet of the script code: <script lang="ts" setup> import { IProduct } from './types'; const p = defineProps<IProduct>(); < ...

How to make an image expand to fit the screen when clicked using HTML and CSS

Is there a way to make the images on this page grow to screen size when clicked, rather than extending past the boundaries of the screen? Currently, the images enlarge to twice their original size on click. How can I ensure that they expand responsively to ...

Placing a div underneath a different element on a webpage

I am struggling with the placement of a div on my webpage. I have tried various methods to position it beneath two other elements without success, despite following advice from online resources. Here is the code I have been working with: ...

Eliminate list items with a keyboard stroke

I am currently developing a straightforward todo list application using JavaScript. The main functionality I am trying to implement is the ability to add new items from an input field to a list, as well as the option to remove items from the list. While ...

Utilizing Angular controllers to access data attribute values from child elements

Today I embarked on the journey of learning AngularJs through online tutorials. Excited about my new project, I started working on creating some useful features using Angular. Here is a snippet of my progress so far: The HTML Part <div data-ng-control ...

What is the best way to target only the immediate children of a div when using tailwindcss?

Here is the HTML code snippet I am working with: <div class="section"> <div class="header">header</div> <div class="content"> <div>sub contents 1</div> <di ...

How to wrap a narrower column around a shorter column using Bootstrap 4

Incorporating Bootstrap 4 into my website design, I have created a simple two column layout. The right column houses a table of contents while the left column is filled with various markup elements such as paragraphs, lists, and images. My goal is to have ...

Divide information in the chart

<table id="tab"> <tr><td class="sub">mmmmm</td><td class="sub">jjjjj</td></tr> <tr><td class="sub">lllll</td><td class="sub">wwwww&l ...

Combining strings with JQuery

Looking for help with a code snippet <script> function goToDetails($i){ $(function(){ var transValue = $('#trans'+$i).html(); var mileageValue = $('#mileage'+$i).html(); var engineValue = $('#eng'+$i).html ...

rectifying file extension hyperlinks

While designing a webpage on my MacBook's local server, I unintentionally omitted the ".css" file extension in the href attribute of a link to my locally stored stylesheet. The mistake went unnoticed until I transferred my files to an externally hoste ...

Easy Access Form

I am working on creating a straightforward login form using HTML and JavaScript. The goal is to verify the entered username and password against a set list of authorized credentials upon submission. If the input credentials are correct, I want to direct t ...

What is the process for outputting JSON data from a script page?

I had a JSON file named results.json which is displayed below. Additionally, I had an HTML file containing some script that is used to retrieve data from this JSON file. Upon entering the HTML page, a script function get_machFollow(que_script) is called to ...

Accelerate blinking timer by utilizing CSS3 animations

I'm currently developing a quiz application that includes a timer counting down from 10 seconds to 0 seconds. As the timer reaches closer to 0 seconds, I am looking to implement a feature where my text blinks faster and faster. Additionally, I would l ...

Having trouble capturing Ajax calls with Selenium? Reach out for assistance with HTML coding or Selenese scripting

While using Selenium IDE to record a .Net application, I encountered a scenario where a textbox is present and I need to enter a name to search for. Once I start typing, matching options appear in a dropdown below that I must select before the application ...

What is the best way to create dynamic transparency based on cursor position?

Is there a way to create an animation like the one on https://meetwalter.com, where the transparency changes with the cursor's position? I previously attempted a similar implementation using CSS, but it seems that the website accomplishes this effect ...

Incorporating photos within the layout of a Twitter Bootstrap grid

Looking to showcase images like the ones found here: https://i.stack.imgur.com/eD4GD.jpg These images are original and come in various sizes. I want a hover effect that includes blur, fogging effects, and text placed in the middle of the picture. Check ou ...

The depth order of overlapping elements and the rotation effect achieved through

I have created some interactive flip cards using CSS and HTML with transitions Currently, when you hover over a card, it flips and enlarges. However, I am facing an issue with positioning the hovered card on top using z-index and relative position due to ...

Creating a Stylish Web Design with Bootstrap HTML - Tips for Perfecting Your Output

My code is producing the following output: https://i.sstatic.net/LVda1.png But I want to achieve this instead: https://i.sstatic.net/OUlOP.png How can I modify my code to get the desired layout? <link rel="stylesheet" href="https://cdn.jsdelivr.n ...