Achieve equal distribution of divs with a specified size

After searching through various questions, none of the answers provided a solution that worked for my specific situation.

Within my project, I have a series of divs, each with a fixed width of 210px.

The container holding these divs is able to resize based on the user's screen size. It is crucial that the divs maintain even spacing horizontally and wrap onto a new line if necessary.

For a visual representation, please see the diagram linked below: https://i.sstatic.net/KUJrJ.jpg

While this JS fiddle demonstrates the desired outcome, I am unsure how to adapt it for divs with fixed widths.

width: calc(100% / 6);

EDIT:

The issue with the JS Fiddle arises when the screen size has space, but not enough to accommodate another div. At this point, the last div should align closer to the right side.

https://i.sstatic.net/lN5Z4.jpg

Answer №1

Flexbox is the solution you need by simply adjusting the justify-content to space-around (or space-between if necessary for your design). Take a look at this example:

body{
  display:flex;
  flex-wrap:wrap;
  justify-content:space-around;
  margin:0;
  padding:5px;
}
div{
  background:#000;
  flex:0 0 210px;
  height:210px;
  margin:5px;
  width:210px;
}
<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>

Visit caniuse.com for more information on browser support and prefixing.

Answer №2

Here is a helpful article that delves into achieving visual layouts without relying on Flexbox. Flexbox can be tricky to work with due to various bugs and inconsistent browser support, so it's good to explore alternative methods.

If you're facing challenges with Flexbox, you can try the following approach:

.container {
  padding: y%; /* Using percentage-based padding for the container creates a scalable layout as the screen width increases. */
}

.box {
  width: 210px; /* Setting a fixed width for the box. */
  margin-right: x%; /* Adjust this value to evenly distribute containers across the screen and allow wrapping when needed. Experiment with different values until it works for you. */
}

Give this method a try and see if it resolves your layout issue!

Answer №3

Upon reviewing the screen shot and gathering input from various sources, it seems that flexbox can be a valuable tool (I was particularly interested in receiving feedback on my comments regarding http://jsfiddle.net/bvgn46hu/108/ & http://jsfiddle.net/bvgn46hu/114/ )

#container {
  margin:1em;
  color:turquoise;
  display:flex;
  flex-wrap:wrap;
  border: solid;
  padding:2vh;
  align-items:center;
  justify-content:space-between;
}
#container > div:before {
  content:'boxe ';
  padding-right:0.25em;;
}
#container > div {
  display:flex;
  
  align-items:center;
  justify-content:center;
  min-height:25vh;
  border: solid;
  margin:1vh 0;
  width:400px;
  min-width:calc(100vw  / 4);
  max-width: 45%;
  
}
<div id="container">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
</div>

check out the codepen here

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

Tips for excluding a CSS property from a class override

I have a file called _buttons.scss: .btn { display: inline-block; font-family: $btn-font-family; font-weight: $btn-font-weight; color: $body-color; text-align: center; text-decoration: if($link-decoration == none, null, none); white-space: $b ...

Safari causing misalignment in Bootstrap column layout

I am currently experiencing an issue with the alignment of Bootstrap columns in Safari. Despite being fine on Chrome and IE, the columns appear to be shifted to the right in Safari. https://i.sstatic.net/13Ci4.png https://i.sstatic.net/EiTie.png Below i ...

choose courses using jQuery

<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <p class="myChild">one</p> <p class="myChild">two</p> <p class="myChild">th ...

Deploying an Azure Blob Trigger in TypeScript does not initiate the trigger

After successfully testing my Azure function locally, I deployed it only to find that it fails to trigger when a file is uploaded to the video-temp container. { "bindings": [ { "name": "myBlob", "type&qu ...

My HTML loop is functioning properly with a variable, however, it is not working as expected

Hi there! Here is a piece of code that includes a loop with a timer. I am trying to change a variable using a button, however, it's not working as expected. <!doctype html> <html> <body> <script> var timer = 1000; var counter ...

Is there a way to align input fields alongside the center of their corresponding labels?

Let me illustrate the issue I am encountering: https://jsfiddle.net/v10un11s/1/ Here is the snippet of my HTML code: <div> <div class="component"> <div> <label for="test1" id="short-label">Short label: </label&g ...

Alter the top property of CSS using JavaScript

Hey there! I'm currently working on a project where I want to gradually change the background when a button is clicked on my website. Below you'll find snippets of JavaScript and HTML code that I'm using for this purpose. Can you take a look ...

Substitute the images with links provided in an external text file

I have a function that I use to replace avatars of players with custom images. Currently, I have three replacement links hardcoded into a Chrome extension. However, I want the function to read an external txt file to dynamically build an array so that I ca ...

bootstrap 4 - create a responsive two-column layout where the second column displays additional details specifically designed for mobile devices

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" crossorigin="anonymous"> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-xs-12"> ...

Minimize the empty area separating two adjacent <sup> components

Here is the code snippet I am working with: <p>Test <sup>1</sup> <sup>, 2</sup> </p> The output of this code is "1 , 2". I am having trouble applying styles to the sup elements, as there are no margins or paddings ...

Menu navigation - ensuring the background color extends to cover the entire page, not just the viewport

I'm facing an issue with the left navigation bar's design. Specifically, I'd like the grey color of the bar to extend vertically across the entire page. Currently, the gray bar ends at the viewport's edge. Is there a way to make the ba ...

Leverage scope variables and dynamic CSS properties when using ngClass

Is there a way to manipulate the CSS dynamically in HTML using a scope variable? <div ng-class="myClassScope, { 'dynamic-class': !ifIsNot }"> ...

Using ngb-carousel to display embedded YouTube iframe videos

Can anyone help me figure out how to embed an Iframe into a ngb-carousel? The carousel is displaying correctly, but the iframe tab is not showing up as expected. <ngb-carousel *ngIf="project.images"> <ng-template class="item" n ...

Attempting to achieve the desired grid layout with Bootstrap 4

Need help achieving this Grid layout Hello, I am new to Bootstrap 4 and I am trying to achieve the layout shown in the image linked above. Here is what I have tried so far... <div class="container"> <div class="row"> <div class="col-md-4 ...

Troublesome Appearance of Datatables in Bootstrap 4

I've been trying to integrate Datatables with Bootstrap 4, but the end result isn't visually appealing. Even after following the basic example provided on the Datatables site, the table still looks off (refer to the screenshot below). I have inc ...

Angular 2: Applying class to td element when clicked

I am working with a table structured like this <table> <tbody> <tr *ngFor="let row of createRange(seats.theatreDimension.rowNum)"> <td [ngClass]="{'reserved': isReserved(row, seat)}" id={{row}}_{{sea ...

Display the latest tooltip in a Tree view

I have developed a unique custom component to incorporate tooltips for each individual node within a Tree. function StyledTreeItem(props) { const { nodeId, labelText, tooltipTitle, ...other } = props; return ( <Tooltip title={tooltipTitle} placem ...

display the table without providing a preview

Hey everyone, I am currently utilizing vue.js to develop a table and I am looking for a way to add a button that can print the data in the table without displaying a preview dialog. What modifications should I make to my javascript code in vue? Here is an ...

Exploring the power of add() method in getStyleClass() along with some code snippets

Recently, I successfully created a custom button in JavaFX by tweaking the style of a simple button using the setStyle() method with different String parameters based on whether the button was clicked or not. I wanted to convert this customized button int ...

How about using a circle inset clip path technique?

Can a unique inset circle clip path be created to cut a hole through the center of a div instead of just showing the center? The entire div should be visible except for a hole cut out in the center to achieve a design similar to this: https://i.sstatic.n ...