Aligning a flex container that has wrapping enabled

I need help with displaying a list on the user's screen that can be reordered by drag and drop. Currently, the drag and drop functionality is working fine.

However, I want to display the list in multiple columns as sometimes there are too many options to fit in a single column, and the number of elements can vary.

I tried using flexbox to achieve this, but I am facing an issue where the list is not centered as the flex container dimensions are not adjusting to fit its content.

Here is the CSS code snippet I am using:

.params-order.overlay {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(30,30,30, 0.8);
    z-index: 999999;
    justify-content: center;
    align-items: center;
}

.overlay-title {
  margin: 5% 5% 2%;
  color: white;
  text-align: center;
  text-shadow: 2px 2px 2px black;
}

.params-list {
  display: inline-flex;
  margin: 5%;
  flex-flow: column wrap;
  justify-content: center;
  align-items: center;
}

.params-list-item {
  background-color: rgb(240, 240, 240);
  border-radius: 5px;
  padding: 5px 15px 5px 25px;
  margin: 10px;
  list-style-type: none;
  width: 200px;
  position: relative;
  font-size: 1.4em;
  cursor: move;
}

.params-list-item::before {
  content: '';
  position: absolute;
  left: 7px;
  top: 19%;
  height: 64%;
  width: 6px;
  border: 2px dotted #999;
  border-top-width: 0;
  border-bottom-width: 0;
  box-shadow: none;
}

Here is a snippet of the HTML code I am using:

<div class="overlay params-order">
  <h3 class="overlay-title">Overlay title</h3>
  <ul class="params-list">
    <li class="params-list-item">Draggable 1</li>
    <li class="params-list-item">Draggable 2</li>
    <li class="params-list-item">Draggable 3</li>
    <li class="params-list-item">Draggable 4</li>
    <li class="params-list-item">Draggable 5</li>
    <li class="params-list-item">Draggable 6</li>
    <li class="params-list-item">Draggable 7</li>
    <li class="params-list-item">Draggable 8</li>
    <li class="params-list-item">Draggable 9</li>
    <li class="params-list-item">Draggable 10</li>
    <li class="params-list-item">Draggable 11</li>
    <li class="params-list-item">Draggable 12</li>
    <li class="params-list-item">Draggable 13</li>
  </ul>
</div>

If you want to see the prototype in action, you can check it out on this CodePen: http://codepen.io/anon/pen/GpEGqd

I managed to fix the alignment issue by adding self-align: stretch; to the .params-list element. However, this causes it to take up the entire width, which is not desired as I need the params-list to track clicks and remove the overlay.

Can someone help me with fixing the alignment without causing the list to take up the full width?

Answer №1

When dealing with a list that has varying options and needs to be displayed in multiple columns, it can be tricky to keep everything centered. I've attempted to use flexbox to achieve this, but ran into issues with the centering and container dimensions.

Despite your list being horizontally centered as a single column, the challenge arises when the column wraps. This is due to the lack of a defined height for the container, causing the list to stack indefinitely in a single column.

To address this issue, try setting a height for the container and specifying the width of the container to achieve centering. Here's an example:

.params-list {
    display: flex;
    margin: 5%;
    flex-flow: column wrap;
    justify-content: center;
    align-items: center;
    height: 300px; /* Set a height */
    width: 100%; /* Specify the width */
}

By setting a height limit and defining the width, the list will wrap appropriately and remain centered within the container.

If you prefer the width of the container to fit its content, you may encounter challenges with centering multiple columns. In such cases, adjusting the flex-direction to row could be a viable solution. However, alternative approaches may also be worth exploring. You can find more information in the provided demo links and resources.


For further customization, consider the flex-direction options and experiment with different settings to achieve the desired layout for your list.

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

Wagtail Django TableBlock not showing up properly on the webpage

Recently delving into the world of Django and programming, I decided to incorporate Wagtail into my website. However, I've encountered a roadblock with the 'TableBlock' functionality Despite setting up the model and block correctly, I&apos ...

SCRIPT5007: The property 'href' cannot be assigned a value as the object is either null or undefined

This script is functioning properly in browsers like Chrome and Firefox, however it is encountering issues when used with Internet Explorer. An error message is displayed in the IE console: SCRIPT5007: Unable to set value of the property 'href': ...

When the last character in the route is a forward slash, the Express server will load the HTML page with CSS and JavaScript. Conversely, if the last route character

On my second html page model.html, I noticed the following issue: When I include a '/' at the end of my route address in the browser like this: http://localhost:3002/home/model/, the correct html page is loaded, but the css/js files do not load. ...

How to Build Two Navbars in BootstrapVue with a Sticky Bottom Navbar at the Top

My current project involves creating two navigation bars. The first navbar at the top consists of just a brand logo, while the second navbar at the bottom serves as the main navigation menu. I want the top navbar to scroll off the screen when users scroll ...

Transform HTML entities to an escaped string in CSS content dynamically

I am aware that special html-entities like &nbsp; or &#246; or &#x0F0; cannot be incorporated within a css block like the following: div.test:before { content:"text with html-entities like `&nbsp;` or `&#246;` or `&#x0F0;`"; } ...

Customizing Semantic UI Navigation Menu Styles

For the first time, I am incorporating semantic-ui into my React application. The specific package I am working with can be found here: To display my header, I am using the following code: <Menu className='header' > <Containe ...

What steps can I take to ensure that my server is accessible to all users?

After successfully creating a chat server using Node.JS and hosting it on my LocalHost (127.0.0.1), I realized that only I have access to the chat. To make the chat server accessible to everyone, I want to deploy it on my real server. The real server URLs ...

Expanding the outer div with Jquery's append() function to accommodate the inner div elements perfectly

I am facing an issue where my outer div does not expand automatically to fit the elements I append inside it using jQuery. The structure of my div is as follows: <div class="well" id='expand'> <div class="container"> < ...

Embed an external website within a div element

Is there a way to embed an entire external website onto another site, without scroll bars or borders? I attempted this method but the entire page did not load, and there were still scroll bars and borders present. <!DOCTYPE HTML> <html> <b ...

Generate spacing between rows of content in HTML or CSS without affecting the spacing between lines of text

In my R Markdown document in R, I want to replace the header labeled "Preface" with grey lines for visual indication. Here's the code snippet I've tried: :::{#preface} <p> Text </p> ::: However, there seems to be no margin at the beg ...

Troubleshooting a Vue.js formatting problem in Visual Studio 2019

Encountering an issue with VS2019 while attempting to format this code section. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="milestone.ascx.cs" Inherits="uc.dms.milestone" %> <section class="content-header"> <h1> ...

Issue arising from CSS and div elements

I am facing some challenges in making certain divs behave as desired. Take a look at the following references: Link 1: - contains two divs Link 2: - has only one div I am trying to utilize two divs (.content) without them being positioned incorrectly ...

Update the CSS property according to the selected list item in the slider using Glider JS

Looking to dynamically change the background image in CSS based on the active slide value in Glider.js Here is the CSS: html { background-image: url("https://images.unsplash.com/photo-1496518908709-02b67989c265?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEy ...

Is Material-ui's sx feature able to produce optimized utility classes like tailwindcss?

When utilizing Tailwind CSS, it is compiled in the following manner: <div class="font-bold"></div> <div class=".font-bold"></div> This compiles to: .font-bold{ font-weight: bold; } So... when using SX like thi ...

When HTML is outside of the root web directory, it is unable to access any files

My files for html, css, and javascript are structured within the /www/ directory in their own respective folders (/www/html, /www/css, /www/js). The web root is located at /www/www/ When using the readfile function in my PHP code like this: readfile(" ...

Unlocking the res property in index.js from an HTML script tag: A step-by-step guide

Can anyone help me with the access variable issue I am facing? I have two files, index.js and page.ejs. These files require me to create a timer linked with datetimes stored on my local server. //index.js.. router.get('/mieiNoleggi', functio ...

Validating Color Properties with CSS 3.0

When using Visual Studio 2012 (Ultimate), a red squiggly may appear indicating that rgba(255, 255, 255, 0.4) is not a valid color property value However, according to the W3C, it is considered a valid value. Could this be a bug in Visual Studio, or am ...

"Troubleshooting jQuery html() Function Issue in Firefox: Dealing with an Unterminated

Trying to insert the following string into a <p> element: (without specified charset) Цена: 4,80 BGN Поддръжка: (directly from .XML file) &#1062;&#1077;&#1085;&#1072;: 4,80 BGN Encountering ...

Issue with jQuery animation: Background color does not change upon scrolling

Here is my fiddle link: https://jsfiddle.net/jzhang172/owkqmtcc/5/ I am attempting to change the background color of the "content" div when scrolling anywhere within it. The goal is for the background color to change when scrolling and revert back to its ...

How to enable multiple selections in a Bootstrap 5 form-select, but restrict users to selecting only one

I am looking to create a modal with a list of options where only one option can be selectable. The default form-select is a dropdown menu that requires an unnecessary click, which I want to avoid. I would like all options to be displayed without the need f ...