Stack div in a vertical orientation

I am dealing with a div tag that has a set height of 300px. Inside this parent container, there are multiple child divs and the content within them is dynamic so I cannot predict how many child elements there will be. How can I make sure that the parent container wraps the children vertically? In other words, if the combined height of all child containers exceeds 300px, the excess should flow into the next column. Below is an example of the code:

<div class="parent_container">
    <div class="child" id="child_one">... <!-- Content contributing to element height --></div>
    <div class="child" id="child_two">... <!-- Content contributing to element height --></div>
    <div class="child" id="child_three">... <!-- Content contributing to element height --></div>
</div>

Now, let's assume the following dynamically generated heights for the elements:

  1. parent_container = height: 300px;
  2. child_one = height: 120px;
  3. child_two = height: 100px;
  4. child_three = height: 100px;

Given that the total height of the parent is only 300px and placing all three child elements would exceed this height, I need a solution to wrap these elements vertically. This means that when the height of a child element surpasses that of the parent, it automatically moves to the next column. For this scenario, the child_three should be positioned in the second column.

Answer №1

Indeed, it is possible by using the following CSS properties:

flex-direction: column; flex-wrap: wrap;

.parent_container {
  height: 300px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: baseline;
}
.child {
  width: 50px;
  border: 1px solid black;
  box-sizing: border-box;
}
#child_one {
  height: 120px;
}
#child_two {
  height: 100px;
}
#child_three {
  height: 100px;
}
<div class="parent_container">
    <div class="child" id="child_one">... <!-- Multiple element inside that contribute to this element height --></div>
    <div class="child" id="child_two">... <!-- Multiple element inside that contribute to this element height --></div>
    <div class="child" id="child_three">... <!-- Multiple element inside that contribute to this element height --></div>
    <div class="child" id="child_three">... <!-- Multiple element inside that contribute to this element height --></div>
    <div class="child" id="child_three">... <!-- Multiple element inside that contribute to this element height --></div>
    <div class="child" id="child_three">... <!-- Multiple element inside that contribute to this element height --></div>
    <div class="child" id="child_three">... <!-- Multiple element inside that contribute to this element height --></div>
    <div class="child" id="child_three">... <!-- Multiple element inside that contribute to this element height --></div>
    <div class="child" id="child_three">... <!-- Multiple element inside that contribute to this element height --></div>
    <div class="child" id="child_three">... <!-- Multiple element inside that contribute to this element height --></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

I'm having trouble getting my modal to open, it just displays the information on my page instead

I am encountering some difficulties with my model window. I would like it to open a modal that shows a larger version of the photo and description when the thumbnail is clicked. However, I have not been able to get it to work properly. The only time it pop ...

How to format numbers in JavaScript post calculations

Struggling to find a solution to format calculation results with commas for thousand separators (e.g., 10,000). After implementing the .toLocaleString('en-US', {maximumFractionDigits:1}); method to format numbers in the output, I encountered unex ...

Content and footer being covered by the sidebar

I have an illustration here to help explain my issue. The problem I am facing is that the Sidebar is overlapping both my Content and Footer when the content consists of only small items. In my _layout file, I'm referencing the sidebar like thi ...

Limiting the click event to the parent div only

I've encountered a problem with my overlay setup. I have an overlay with a child div inside, and I want the overlay to close only when the user clicks on the overlay itself, not the child div. Even though I've implemented the closing function, it ...

Tips for centering text on a webpage using the div element in HTML

I need help aligning the text "Monitoring Engineering Dashboard" to the right side of the image in the top-left corner. Currently, it is positioned below the image (Refer to the image below) <div style="width:1200px; margin:0 auto ...

Issue with concealing floated elements within a container div

I am currently facing an issue with trying to hide floated divs within a parent div, but unfortunately my code is not working as expected. Here is the code snippet: div.scrollarea { overflow: scroll; width: 400px; ...

The use of CSS float creates spaces between elements

Is there a way to stack the green DIVs on top of each other without any space between them in the given simple HTML code? I'm puzzled by the gap between the third and fourth green DIVs. * { margin: 0; } .left { width: 20%; background-color: # ...

Fine-tuning the design of the Box Model layout

I can't seem to get the table in my code to center properly and not be stuck against the left border. Despite trying various solutions, the table in column 2 is floating off to the left and touching the border. I need it to be more centered within the ...

Styling a numerical value to appear as currency with CSS

Is there a way to style the content of an element as currency using just CSS? It would be great to know if we can control how the value is displayed with CSS. I haven't found anything yet, so not getting my hopes up :) <!DOCTYPE html> <html& ...

Creating CSS styles for fluid width divs with equal height and background images

Looking to create a layout with two side-by-side divs of equal width - one containing an image and the other dynamic text that can vary in height from 400px to 550px based on input. The goal is for the image to align flush at the top and bottom with the te ...

What are some ways I can ensure that my personalized bootstrap navbar adjusts to different viewport sizes?

I've created a custom navbar that fits perfectly on a screen that is 1300 pixels wide by 700 pixels high. https://i.sstatic.net/nOm0c.png However, when the viewport size is reduced, the navbar elements become misaligned: https://i.sstatic.net/zBqpQ ...

Discovering how to adjust the "border-spacing" in a React Material-UI Table to ensure each row is nicely separated

I am looking to create Material-UI Collapsi Table Rows with separate styling. Check out the link for more information: https://material-ui.com/components/tables/#CollapsibleTable.tsx const theme = createMuiTheme({ overrides: { MuiTable: { root ...

Centered CSS navigation bar without any spacing between the buttons

Good evening everyone, I am looking to create a navigation bar that is centered on the screen with no gaps between the buttons. I have tried using 'float:left' to close the gaps, but this aligns the navigation bar to the left. Without 'floa ...

HTML animation effect on subpage with a URL modification [LATEST UPDATE]

Can you smoothly load a new subpage in the background, update the URL, and fade it in while an animation is playing on the previous page? Check out this example (jsFiddle) to see a simple animation: $(document).ready(function() { 'use strict&apo ...

Achieve a stylish layout by displaying three cards per row using Vue.js and Bootstrap 4

I'm facing an issue with my code where I am trying to display Bootstrap cards in rows of three, but the layout is not rendering correctly. It starts with one card, then two, and eventually adds a new column, resulting in some empty spaces with offsets ...

Incorporating additional options onto the menu

I recently designed a menu on https://codepen.io/ettrics/pen/ZYqKGb with 5 unique menu titles. However, I now have the need to add a sixth title to the menu. After attempting to do so by adding "strip6" in my CSS, the menu ended up malfunctioning and looki ...

Customize the Emotion Styled Material UI Tab Indicator Overwrite

Looking for a way to customize the appearance of tab indicators using Emotion styled components. I'm struggling to target nested classes in my code. Here's what I have so far: const StyledTabs = styled(Tabs)( { classes: { indicator: ...

What causes two identical pages to display fonts in different ways?

Two identical pages, hosted on the same server with the exact same Apache configuration and home directory. Each page contains only one sentence of text, unstyled. The strange thing is, when viewed in Chrome and Safari, the fonts render differently, while ...

The issue with the max-height transition not functioning properly arises when there are dynamic changes to the max-height

document.querySelectorAll('.sidebarCategory').forEach(el =>{ el.addEventListener('click', e =>{ let sub = el.nextElementSibling if(sub.style.maxHeight){ el.classList.remove('opened&apos ...

Enable the ability to upload multiple images on a single page using droparea.js

I am currently utilizing droparea js for uploading images. However, I have encountered an issue where if I try to upload an image in one of the upload menus, it ends up changing all four upload menus simultaneously. <div class="col-md-12"> ...