Ways to showcase a collection of divs in a dual-column layout while minimizing any unnecessary vertical spacing

Is there a way to create two columns without changing the HTML structure? The content within each child element is dynamic, so there needs to be dynamic vertical spacing as well. I'm experimenting with different CSS properties for both the parent and child elements to achieve this.

.parent{

}

.child{
  display:flex;
  float:left;
  width: 48%;
  padding: 3px;
  border: 1px solid red;
  background-color: gray;
}


/** Ignore below */
.child1{
  background-color: green; 
}
.child2{
  background-color: blue; 
}
.child3{
  background-color: darkblue;
}
.child4{
  background-color: lightgreen; 
}
.child5{
  background-color: yellow; 
}
.child6{
  background-color: #989898; 
}
.child7{
  background-color: #545454; 
}
<div class="parent">
  <div class="child child1">Child 1 text here Child 1 text here Child 1 text here Child 1 text here 
  </div>
  <div class="child child2">Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here 
  </div>
  <div class="child child3">Child 3 text here Child 3 text here Child 3 text here Child 3 text here Child 3 text here Child 3 text here 
  </div>
  <div class="child child4">Child 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4...
  </div>
  <div class="child child5">Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here 
  </div>
  <div class="child child6">Child 6  text here Child 6  text here Child 6  text here Child 6  text here 
  </div>
  <div class="child child7">Child 7  text here 
  </div>
</div>

Answer №1

Another alternative approach could involve utilizing the display: inline-block; property.

.parent {
  background-color: #aaa;
  margin: 0 auto;
  height: 100%;
  width: 500px;
  column-count: 2;
  gap: 10px;
  padding: 5px;
}

.child {  
  padding:10px;
  background-color: white;
  border: 1px solid black;
  width: 100%;  
  display: inline-block;
  box-sizing: border-box;
  margin-bottom: 5px;
}

child1{
  background-color: green;
}
.child2{
  background-color: blue;
}
.child3{
  background-color: darkblue;
}
.child4{
  background-color: lightgreen;
}
.child5{
  background-color: yellow;
}
.child6{
  background-color: #989898;
}
.child7{
  background-color: #545454;
}
<div class="parent">
  <div class="child child1">Child 1 text here Child 1 text here Child 1 text here Child 1 text here 
  </div>
  <div class="child child2">Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here Child 2 text here 
  </div>
  <div class="child child3">Child 3 text here Child 3 text here Child 3 text here Child 3 text here Child 3 text here Child 3 text here 
  </div>
  <div class="child child4">Child 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text hereChild 4 text here 
  </div>
  <div class="child child5">Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here Child 5  text here 
  </div>
  <div class="child child6">Child 6  text here Child 6  text here Child 6  text here Child 6  text here 
  </div>
  <div class="child child7">Child 7  text here 
  </div>
</div>

Answer №2

To achieve a responsive layout, utilize flex-direction: row along with flex-wrap: wrap while eliminating the float: left

This will allow the content to span the entire width of the page until reaching capacity, at which point it will shift to a new line

Given your specified 48% width, this configuration will result in two columns with 4% remaining space

Answer №3

This is the rewritten version.

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

Is there a way for me to merge the specifications of two itemscopes in order to depict a

Adding microdata to a page can be challenging when the data for an item is spread across different parts of the page. If there are two span elements with an itemscope attribute, it would be beneficial if search engines could merge these two itemscopes into ...

The issue of the keyboard disappearing on Chrome for Android while switching between different input

Issue with Input Switching on Chrome for Android I am facing difficulty when trying to switch between two input fields. The HTML code is as follows: <input type="text"/> <input type="text"/> When I enter text in the first input and click on ...

Exploring Angular's Animation Feature for HTML Element Movement

I'm currently using @angular/animations module to animate HTML objects and I want to move an object when a button is clicked. For example: * The object initially has top: 800px, left: 800px * Clicking the 'W' button should move the obje ...

Styling a webpage with a two-row layout using just CSS

I am looking to design a 2-row layout for a webpage where one row will contain filters and the other row will display results. The layout should meet the following criteria: The page height should not exceed 100% The first row's height will vary bas ...

ChessboardJs: JavaScript Boards Function Properly on Initial Use Only

Update: The page code can be accessed via my page URL. I'm unsure where the issue lies. Your assistance is appreciated. Issue: Initially, when clicking on the chess puzzles page, everything works fine. However, upon re-clicking from the homepage, th ...

unwelcome tab spacing in CSS

I am facing an issue with unwanted spacing in my lists. I have a code that generates three-column lists, each containing about eight rows. However, the first list item of the last row is causing an unwanted space. It seems to shift entirely to the next col ...

Is there a method to prompt the browser to retain form data when using the back button?

Having a puzzling problem. I've developed a sophisticated quote form for our company that utilizes ajax to display prices for products based on user input. There are 6 hidden fields set up as follows: productname1 productname2 productname3 and the ...

Are you looking for methods to alter the elements of a webpage prior to viewing it?

I have created a page with the intention of using it as a tool, but I am facing some challenges due to my limited experience in this field. As a newcomer, I am struggling to achieve certain goals: - My objective is to modify the values of an element on a p ...

What is the best way to continuously add items to a div until their heights are equal?

In my layout, I have two divs positioned next to each other. Typically, the left div displays n+2 items while the right div displays n items. The value of n changes depending on the category and is already set. However, there are instances where certain ...

When implementing CSS object-fit: contain on an image, it may result in empty spaces surrounding the image

Within my image-wrapper container, I have an image with a variable resolution. The container itself has fixed dimensions for height and width. My goal is to center the image within the container both horizontally and vertically while adding a box-shadow e ...

Enhance your bootstrap accordion by incorporating stylish up and down arrows using the <accordion> element

I am currently developing a sophisticated web application using Angular and TypeScript, and I have decided to incorporate accordions in some sections. The setup for these accordions involves TypeScript/Bootstrap as shown below: <accordion> <acco ...

At what point does a box create an inline formatting context?

According to my research, I came across a situation where a block formatting context is generated as explained in MDN:Block formatting context. I am curious about the scenarios that lead to the establishment of an inline formatting context within a box. ...

What is the method for creating a hovering triangle on a particular element?

My navigation bar consists of two parts. On the left side, there is a logo image, and on the right side, there are 4 list elements. I am attempting to make a triangle appear at the bottom of the element when hovered. I have floated the list to the right an ...

Connect to a point on the leaflet map using an external <a> tag reference

I have a leaflet map and I am trying to create a link that, when clicked, will activate a specific marker on the map. Essentially, I want the linked marker to simulate being clicked when the link is clicked. Here is an example of the link: <a href="#" ...

Exploring the utility of the load_file function in the simple HTML DOM method

After writing this code and it was functioning properly. <?php include ('require/simplehtmldom_1_5/simple_html_dom.php'); echo $html=file_get_html('http://www.site.com'); ?> Now I want to make it work using the object method. He ...

Using JQuery Mobile to Incorporate Links into List Elements

Creating a mobile webpage tailored for college students involves assigning each student a unique id and an additional field. Both fields can be treated as strings, with the unique id being guaranteed to be unique, while the second field may or may not be u ...

Creating dynamic links between two cells of two HTML tables within an HTML email using JavaScript

A recent project requires the creation of HTML-based emails with tables for each automation script within the test automation framework. Additionally, there is a request for an extra HTML table to provide a quick overview. The cells in the first column of ...

Is there a way to incorporate an 'ended' feature into the .animate() function?

I'm currently working on a jQuery statement where I want to activate pointer events once the button animation is finished. However, I'm unsure how to incorporate an ended or complete function. Any suggestions on how to approach this? $('#ce ...

What is the best way to incorporate an input bar in a Bootstrap panel header?

When designing a search box in my panel header, I floated both elements to get them onto the same line. Although functional, the vertical alignment of the title is displeasing and I am seeking a way to improve this without drastically changing its size. I ...

One-Time Age Verification Popup Requirement

Hi there! I'm facing a challenge with an age verification pop up on my webpage. Currently, the pop up appears on every page a user lands on, but I only want it to show on their first visit. I've tried using cookies to achieve this but haven' ...