Flex items refusing to wrap in a flexbox with a column-direction orientation

In my code, I am using an outerWrapper, innerWrapper, and children. The outerWrapper has a fixed height of 300px and is styled with display: flex. Similarly, the innerWrapper also has a style of display: flex with flex-direction: column.

However, when I apply the align-items property to the outerWrapper with anything other than stretch, the children display in a single long column, disregarding the specified 300px height. See the image here:

https://i.sstatic.net/X5ieN.png

Instead, I want them to be displayed as shown in this image:

https://i.sstatic.net/osL0x.png

The desired outcome can be achieved by simply applying align-items: flex-end.

To address this issue, the solution involves setting a height of 210px for the innerWrapper. However, this value needs to be dynamically calculated using JavaScript due to the variable number of child elements.

I attempted the following code snippet:

innerWrapper.style.height = (lastChild.offsetTop - innerWrapper.offsetTop + lastChild.offsetHeight) + 'px';

Unfortunately, this did not resolve the issue as it only resulted in a height calculation based on the individual box dimensions.

How can I accurately set the height of innerWrapper using JavaScript while preserving the flexibility needed for aligning items?

JSFiddle

Answer №1

You have specified a height for #outerWrapper: height: 300px.

To ensure the child element – #innerWrapper – has an equal height, set it to height: 100%. This will make them wrap properly.

If you want the items to be positioned at the bottom of the container, apply flex auto margins to the odd-numbered items.

Include an invisible pseudo-element to ensure that the last odd-numbered item is always aligned with the top row.

#outerWrapper {
    height: 300px;
    background-color: lightgreen;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: flex-end;
}

ul {
    padding: 0;
    margin: 0;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    flex-direction: column;
    height: 100%;                           /* NEW */
}

li {
    list-style-type: none;
    width: 100px;
    height: 100px;
    background-color: lightblue;
    border: 1px solid;
}

li:nth-child(odd) { margin-top: auto; }      /* NEW */

ul::after {
    content: "";                              /* NEW */
    width: 100px;                             /* NEW */
    height: 100px;                            /* NEW */
    border: 1px solid;                        /* NEW */
    visibility: hidden;                       /* NEW */
}
<div id="outerWrapper">
    <ul id="innerWrapper">
        <li class="child">I'm #01</li>
        <li class="child">I'm #02</li>
        <li class="child">I'm #03</li>
        <li class="child">I'm #04</li>
        <li class="child">I'm #05</li>
    </ul>
</div>

Revised Fiddle

Answer №2

To maximize the layout of the flexbox container, consider incorporating the height attribute:

ul {
    ...
    height: 100%;
}

Answer №3

To achieve the desired layout, adjustments need to be made to both the outerWrapper and menu element which have flex positioning. I have updated a fiddle to demonstrate the behavior you seek in the second image:

Updated Fiddle Link

#outerWrapper {
  width: 100%;
  height: 300px;
  background-color: lightgreen;
  display: flex;
  justify-content: center;
  align-items: center;
}

ul {
  height: 250px;
  width: 300px;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-items: center;
}

li {
  list-style-type: none;
  width: 100px;
  height: 100px;
  background-color: lightblue;
  border: 1px solid;
}

I set specific dimensions for the ul to ensure proper wrapping of children elements. Feel free to reach out if you have any questions!

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 extracting values from a JSON object

I have been attempting to retrieve the value from an array without success. Here is the current array: [{0: {value: 2, label: "ARKANSAS"}}] When I try to use JSON.parse(object), I encounter the error VM20617: 1 Uncaught SyntaxError: Unexpected ...

Tips for incorporating a hyperlink into a pop-up box

I need some assistance with adding a link to a pop-up window. Here's the HTML code I currently have: HTML <div id="popup"> <a href="#"><div id="popup-headline">News 1</div><div id="local">News Author</div> <di ...

Guide for implementing async/await in conjunction with the eval() function within JavaScript

I'm currently using the eval function to evaluate strings and adding await to it to ensure all values are obtained, but unfortunately the await is not functioning correctly. Here is a snippet of my code: if (matchCard.card.status != "notstarted& ...

React with Typescript: Issue with mapping array not displaying all elements

I am facing an issue with my Table / Array. If I click on the blue button, all items in the same group as the selected record should have their status changed to "Gratis". However, currently, only the value of the selected record and those above it are cha ...

Focus on targeting dynamic IDs such as #event_rules_attributes_0_interval, #event_rules_attributes_1_interval, etc. using CSS3 styling

Styling input tags using their IDs can be very convenient due to the higher precedence weight they hold compared to classes. For instance, I set a default width for input.text elements within a specific container: .details .metadata input.text { width: 2 ...

What is the best way to retrieve the value of a textbox in AngularJS?

Trying my hand at creating a basic web page using angular. I've got 2 textboxes and 2 buttons - one to set a predefined value in a textbox, and the other to add some text. Here's the code snippet: <!DOCTYPE html> <html lang="en" ng-app ...

What is the reason behind Angular not allowing users to define @Output events that begin with 'on'?

While developing a component, I defined an output EventEmitter named onUploaded. However, Angular flagged an error instructing me to use (uploaded) instead. This restriction is due to security concerns, as bindings starting with 'ono' pose risks. ...

Is there a way to display one of the divs in sequence each time the page is reloaded?

Is there a way to display the divs sequentially when the page is refreshed? For instance, on the initial load, only div 1 should appear, and with each refresh it should cycle through divs 2, 3, 4, and 5 before starting over. Below is an example I'm c ...

Is there a way to retrieve the values of all identical properties within an object?

I am currently running npm test on the object books. My objective is to retrieve the value of the title property from the books object. When I invoke the function getTheTitles(books), both 'Book' and 'Book2' should be returned. However, ...

Identify the mouse's location in relation to its parent element, not the entire webpage

A script I've been working on detects the mouse position relative to the page, taking into account a movement threshold of 100px before triggering certain actions. // Keep track of last cursor positions var cursorDistance = 0; var lastCursorX = null; ...

Can the identical script be implemented by utilizing two separate JavaScript files?

My knowledge of JavaScript is limited, so I often rely on resources like JavaScript.com and Dynamic Drive for coding. Recently, I came across some scripts that reference two different .js files. <script type="text/javascript" src="lib/prototype.js"> ...

Utilize a unique CSS selector and content property to insert a space character between two inline-block elements

I have a group of connected span elements with the class tag from bootstrap v4. Because they are generated from a react.js array of JSX elements, they sit adjacent to each other without any spacing between them: <span class="tag tag-primary">text< ...

Updating parts of a list using AJAX

Recently, I've encountered a challenge where I need to enable editing functionality on a table column from a database. The goal is to make the column editable when clicked and update the value when clicked out. To achieve this, I decided to utilize A ...

Why does my paragraph consistently fall behind my button no matter what?

Whenever I resize the window, the text in my footer overlaps with my button: Any suggestions on how to resolve this issue? Here is the HTML code snippet: <div data-role="footer" class="footer2"> <a href="#seiteHome" class="ui-btn-left ui-bt ...

Is it a common issue that sound.stop() doesn't seem to function properly, while sound.play() operates successfully within Howler

I am having trouble utilizing Howler.js in Reactjs with typescript. While I can successfully play the sound, pausing or stopping it seems to be an issue. Below is the code snippet: This component takes all the audio details as props. I used console.log() ...

Unable to retrieve an array in SAPUI5 and assign it to a JSONModel

I encountered an issue with accessing an array where I stored data from a model read operation. The array, named aData, contains row data for a table and everything seems to be working correctly as all the data is present. This code snippet is executed af ...

Display the quantity of stock in WooCommerce as separate numerical values

I am looking to display the stock quantity of products on a specific page of my website. The code I am currently using to retrieve the stock quantity is as follows: <p class="stock-m13"><?php echo $product->get_stock_quantity(); ?></p> ...

Trouble displaying certain HTML code and its output on the browser?

I am in the process of developing a user profile page. I have designed a form with various fields, but the code within the section is not displaying correctly in the browser. I attempted to inspect the elements, but the code within that section remains inv ...

How to obtain the value of TR in JavaScript?

Objective: Extract the value "2TR" from "MARSSTANDGATA132TR" using JavaScript. Need to determine the location of the digit 2 within the extracted string. Issue: Uncertain about the correct syntax to achieve this task. Additional Details: *The cha ...

"Enhance your Vue.js application with the powerful capabilities of vue3-easy

I am currently working on a Vue.js project utilizing the "vue3-easy-data-table" library and following the style recommendations outlined on this particular webpage: Despite attempting to apply the following CSS properties: --easy-table-body-even-row-font ...