What is the best way to create equal space between divs while also adjusting the left and right space based on the width of the

Is it possible to create equal space between div elements while also ensuring that the spacing adjusts based on the width of the browser?
The goal is to center the divs and have consistent left and right margins that change depending on the browser width.
Additionally, the number of columns should adapt according to the browser width.

The height of each div with the class .box varies, indicated by the use of the <br> tag for demonstration purposes.

.under {
width:70%;
margin:auto;
padding-top:40px;
padding-bottom:40px;
text-align:center;
border: 1px solid green;
overflow:auto; 
justify-content: center;
position:relative;
column-count: 5;
}

.box {
text-align:center;
width:314px;
padding:10px;
margin:10px;
display:inline-block;
box-shadow: 0px 5px #1c1c1c, 0px  -2px #1c1c1c;
border-bottom-right-radius: 10px;
border-top-left-radius: 10px;
background-color: #363636;
border: 0px solid red;
}

@media screen and (max-width: 2450px) {
.under {
column-count: 4;
}
}

@media screen and (max-width: 1900px) {
.under {
column-count: 3;
}
}

@media screen and (max-width: 1350px) {
.under {
column-count: 2;
}
}

@media screen and (max-width: 900px) {
.under {
column-count: 1;
}
}

body {
height:100%;
padding:0px;
margin:0px;
flex-direction:column;
justify-content:center;
background-color:#262626!important;
color:#ddd!important;
font-family: 'Jost', sans-serif;
}
<div class="under">
  <div class="box">Flex item 1</div>
  <div class="box">Flex item 2<br><br><br>end</div>
  <div class="box">Flex item 3</div>
  <div class="box">Flex item 4</div>
  <div class="box">Flex item 5<br><br/>end</div>
  <div class="box">Flex item 6</div>
  <div class="box">Flex item 7</div>
  <div class="box">Flex item 8</div>
  <div class="box">Flex item 9<br><br><br><br>end</div>
  <div class="box">Flex item 10</div>
  <div class="box">Flex item 11</div>
  <div class="box">Flex item 12</div>
</div>

Answer №1

To achieve the desired layout, you can simply utilize basic flexbox properties. By applying flex box with flex wrap and fixing the width of the content, the layout will automatically adjust accordingly.

.under {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
}

.box {
  text-align: center;
  width: 314px;
  padding: 10px;
  height: auto;
  margin: 10px;
  background-color: #363636;
  color: #fff;
}
<div class="under">
  <div class="box" style="height:50px;">Flex item 1</div>
  <div class="box" style="height:90px;">Flex item 2</div>
  <div class="box">Flex item 3</div>
  <div class="box">Flex item 4</div>
  <div class="box">Flex item 5</div>
  <div class="box">Flex item 6</div>
  <div class="box">Flex item 7</div>
  <div class="box">Flex item 8</div>
  <div class="box">Flex item 9</div>
  <div class="box">Flex item 10</div>
  <div class="box">Flex item 11</div>
  <div class="box">Flex item 12</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

What is the best way to retrieve multiple image file names using JavaScript?

I attempted to use the code snippet below to retrieve the names of all the images I selected, however when I used 'alert', I only received one name even though I selected multiple. My goal is to have all the names of the selected photos saved in ...

Display a tooltip when you click on a different element

I have a unique feature that I want to implement on my website. The idea is to display a tooltip when a user clicks on a specific div with the same ID as the tooltip. This will be particularly useful for interactive questions where users can click and reve ...

The website created with Mobirise's HTML is experiencing display issues after being uploaded to NGINX through PuTTY

Recently, I embarked on building a website using NGINX but realized it needed a major revamp. That's when I turned to Mobirise as my HTML expertise is limited. Here's the initial site I created: https://i.stack.imgur.com/IsCg4.jpg However, upon ...

Choosing a color while hovering over the navigation bar

I'm trying to modify the color of my navigation bar when hovering over it with the mouse. Currently, it's black but I want it to change to a light grey or white color. What am I missing here? HTML: <!DOCTYPE html> <html> <head ...

Problem with special characters in Localstorage for Internet Explorer 8

After developing a jQuery script that retrieves data from a service, stores it in local storage, and displays it on the page upon a specific event, I encountered an issue with displaying Chinese characters only on IE8+ browser. Unfortunately, IE is unabl ...

Incorporate a sleek Vueitfy user interface seamlessly into your current website

Recently, I put together a sleek user interface for a web application with the help of Nuxt.js and Vuetify.js (which utilizes Vue.js code). Although my design is top-notch, I now face the challenge of integrating it into an existing website that does not ...

Modifying the CSS based on the SQL data retrieved

I'm currently diving into the world of php and jquery, attempting to build a webpage that displays player information and their status fetched from my Mysql server. Although the core code is functional, it's a mashup of snippets gathered from va ...

When clicking initially, the default input value in an Angular 2 form does not get set

I am currently learning angular2 as a beginner programmer. My goal is to create a form where, upon clicking on an employee, an editable form will appear with the employee's current data. However, I have encountered an issue where clicking on a user f ...

Unable to attach a tooltip to a list item

As an Angular developer, I am working on a list element that displays all the cars and I am looking to add a tooltip to enhance its visual appeal. Here is what I have attempted so far: I created a span tag with the class "tooltip" to wrap around the ul el ...

Are there any open source libraries in jQuery or HTML that can be used for creating av

I have been searching extensively for an open source avatar library that can perform basic functions like choosing gender, eyes, and clothing. It was surprising to me that I couldn't find any open source framework for avatars on the web, especially c ...

Attach to dynamically generated elements by event listeners that bind on mouseover or hover

I am looking to implement a hover effect for dynamically generated items in JavaScript. Specifically, when I hover over an icon element, I want a unique text to appear related to that icon: for (var i = 0; i < items.length; i++) { $('.items&ap ...

Tips for utilizing bootstrap.css to position a web design form in the center of the page and place the copyright at the bottom

I have spent several hours trying to figure out how to achieve this, but still haven't found the solution. I have a form that I believe doesn't look very good: https://i.sstatic.net/t1MRd.png So, I attempted to center the form on the page and ...

Is there a way to position the logo icons on the right side of the footer?

My goal is to connect social media platforms with their respective logos, and I'm encountering an issue. The problem arises when attempting to align the list items to the right of the footer. <footer> <div class="footer-wrapper"> ...

What is the best way to create grid designs using less in bootstrap?

When using Bootstrap without a preprocessor, we include classes directly into our opening tags like this: <div id="El" class="col-md-1"></div> Personally, I usually work with Bourbon Neat and Sass. With Sass, I can import mixins in the rules ...

css boxShadow merger

Looking to create a sleek horizontal navigation bar using an unordered list with list items representing each element: To ensure the elements fit perfectly within the bar, I set the width of each at 25% and added a box-shadow for a border. Using a traditi ...

Retrieving information from a JSON file using AngularJS

Hello, I am a beginner in Angularjs and I am facing an issue while trying to retrieve data from a JSON file. The output I am getting is quite strange. Below are snippets from my controller.js and services.js files: angular .module('app') .contro ...

Expand the image size when hovering using CSS

I want to make a simple image link button that enlarges when I hover over it. The code below accomplishes this, but I also want to move the paragraph when the image gets larger. Any suggestions on how I can select and move the paragraph when I hover over ...

Retrieving random HTML content with an AJAX request

Solving this technical issue has been quite a challenge for me. Running on Ubuntu 14.04 with Apache and PHP5, I initially suspected my NetBeans IDE to be the culprit, but then I directly ran it from the web root /var/www/html. PHP is functional as I'v ...

The overflow hidden property does not seem to be effective when used in conjunction with parallax

The issue arises when I attempt to use the overflow hidden property in combination with parallax scrolling. Although everything seems to be working correctly with JavaScript for parallax scrolling, setting the overflow to hidden does not contain the image ...

Tips for maintaining the particles.js interaction while ensuring it stays under other elements

I have incorporated particle.js as a background element. By adjusting the z-index, I successfully positioned it in the background. While exploring solutions on the Github issues page, I came across a suggestion involving the z-index and this code snippet: ...