What is the best way to align an equal number of divs in each row?

I need help arranging my divs in rows, with 3 divs in each row. Can someone assist me with this?

Here is the code I have written so far:

.container {
  display: flex;
  flex-wrap: wrap;
}

.item {
  border: 2px solid black;
  width: 100px;
  height: 100px;
}
<div class="container">
  <div class="item"> test </div>
  <div class="item"> test </div>
  <div class="item"> test </div>
  <div class="item"> test </div>
  <div class="item"> test </div>
  <div class="item"> test </div>
</div>

Answer №1

If you want to achieve this, follow these steps:

body {margin: 0}

.wrapper {
  column-count: 3; /* display three columns/divs per row */
  column-gap: 0; /* add some gaps between items if desired */
  page-break-inside: avoid;
  break-inside: avoid-column;
}

.wrapper > .item {
  border: 2px solid black;
  width: 100px;
  height: 100px;
}
<div class="wrapper">
  <div class="item"> test </div>
  <div class="item"> test </div>
  <div class="item"> test </div>
  <div class="item"> test </div>
  <div class="item"> test </div>
  <div class="item"> test </div>
</div>

Answer №2

If you're looking to improve your layout design, I recommend exploring Flexbox. By simply applying display: flex to the parent container, you'll notice how the child elements automatically adjust their positioning. There is a wealth of helpful documentation available on websites like Stack Overflow. Take some time to dive into it and discover how straightforward and effective it can be. Hopefully, this suggestion proves useful for you!

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

Displaying background images as list items on a web page outside of

I am facing an issue with an unordered list within a div where the list item images are floating outside the div. Does anyone have a solution for this? You can see an example of the problem on Any assistance would be greatly appreciated! ...

Embracing JQuery for Frontend Development with Node.js

I'm currently enhancing my node.js skills by utilizing express, and I've encountered a situation that is causing me some confusion. Essentially, when a user requests a webpage and the backend sends them the HTML page, how can I incorporate a Java ...

Incorporating JavaScript to dynamically load an HTML page into an iframe

I am attempting to have each option load a unique page within a frame <!DOCTYPE html> <html> <head> <script> function selectCountry() { var mylist=document.getElementById("country"); document.getElementById("frame").src=mylist.opti ...

Issue with manipulating background color with JQuery in CSS

Currently, I have a simple navigation menu consisting of five links. The navigation bar is styled with a background image and changes to a darker shade when hovered over. My goal is to implement a jQuery script that dynamically changes the color of the act ...

Tips for aligning bootstrap labels in one line while allowing horizontal scrolling within a column in Bootstrap

Check out this code snippet here. <div class="row"> <div class="col-xs-6"> <div class="input-group"> <div type="text" class="form-control"> <span class="label label-default">Default</span> &l ...

What is the best way to shorten string values that exceed a certain length?

Here is an array list I have: $myarray = array( array( 'name' => 'File name 1 - type.zip', 'size' => '600KB', ), array( 'name' => 'File name 2 - type.pdf&a ...

Setting character limits within textareas based on CSS classes in a dynamic manner

I'm looking to develop a JavaScript function that can set text limits for multiple textareas at once using a class, allowing for flexibility in case specific textareas need to be exempt. However, I'm facing issues with my debuggers - Visual Studi ...

Using HTML and CSS to Position Text Within an Image

Is there a way to position an image next to specific points on the screen so that it remains in the same place regardless of screen size? Here is what I am trying to achieve: https://i.stack.imgur.com/A5cop.png Using HTML, this is my desired setup: < ...

What are the steps for positioning tables using HTML?

As a newcomer to this field (literally just started two days ago), I have managed to generate tables from an XML file using my XSL file. However, aligning these tables properly has become a bit of a challenge. I initially used the align attribute of <ta ...

javascript Click to add a tag

I am currently using a rich text editor and I'm attempting to add a code tag feature to it. Currently, the editor has two buttons - one for underline and one for code. When I select text and click the underline button, the selected text will be under ...

Send folder to the homepage of the website

I want to implement a redirect using .htaccess to prevent people from snooping into my directories and index files. For instance, if someone tries to access a specific image file like site.com/pics/1.jpg by trying to see the entire "pics" folder at site.co ...

Issue with absolute positioning occurs when the screen resolution or window size is altered, causing the feature to

Take a look at the code snippet below: <div style="position: relative;"> <div style="position: absolute; left: 10px; top: 18px;"> <img id="ImgTachoMetre" src="/Images/u174_normal.png"> </div> </div> I have wr ...

PHP code that prevents undefined index when uploading photos. Ensure photos are successfully inserted into an SQL

Currently, I am dealing with a registration form that includes an option for users to upload a photo for their profile picture. Everything works smoothly when trying to submit the form except for the errors related to the picture index. I strongly believe ...

Information is cleared before it is transmitted

Let's begin with the following code: JavaScript: function keyPressed(e) // function for key press event { if (e.keyCode == 13) // 13 represents the enter key { $(this).val(""); } } $(document).ready(function () { $(' ...

Winning opportunities created by using credits in the slot machine

**Greetings, I have created a slot machine using JavaScript, but I am looking to enhance the player's chances based on their credits. Can anyone guide me on how to achieve this? Essentially, I want the player's odds to increase proportionally wit ...

Displaying divs of varying heights in a line

I'm facing a challenge in creating floating divs, just like illustrated in the image provided in the link below. Currently, I am employing the float left property to align all the divs next to each other. The first picture displays the default layout ...

The button in Bootstrap 4 that triggers the modal stays in a highlighted or clicked state even after the modal has been

I am currently utilizing the button below to trigger the opening of a modal window However, upon closing the modal, it seems to maintain a highlighted or clicked appearance with a shadow effect. Initially, I assumed I could address this issue by implement ...

Message: Unable to locate the requested resource for Paypal verification with the information provided (email, first name,

My email, firstname, lastname are all functioning correctly for verification on the PayPal website. You can check it out here: I am encountering an issue after submitting my form. The email, firstname, lastname in my controller are being automatically cal ...

Displaying two images side by side in HTML using Bootstrap

As a beginner in HTML, I am faced with the challenge of placing two images of different sizes side by side on the same row using HTML and Bootstrap. Below is my current code: <div class="row"> <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6"> ...

What is the best way to close the space between a box-shadow and a div background when incorporating semi-transparent rgba() values?

There seems to be an issue arising when applying partially transparent rgba() background-color and box-shadow color values to an element with a non-fixed (percent-based) border-radius. This combination results in a minuscule transparent gap appearing at th ...