Need help aligning content with flexbox? Having trouble with align-content and justify-content not working correctly?

First and foremost, I want to express my gratitude in advance for any assistance you can provide. My goal was to neatly align three pieces of content within each row, similar to the image displayed here: https://i.sstatic.net/vtsRj.png

To achieve this layout, I attempted to utilize flexbox properties such as display: flex, flex-direction: column;, and experimented with align-items and justify-contents. Unfortunately, the result did not meet my expectations. Instead of the clear arrangement shown in the reference picture, my content appeared as depicted here: https://i.sstatic.net/QaQeY.png

If you could offer insight or guidance on how to address this issue, I would greatly appreciate it. Below is the code snippet I've been working with:

.contentsWrapper {
    display: flex;
    width: 100%;
    height: 1000px;
    // background-color: blue;
    padding: 5px;
    .contentsSide {
      width: 280px;
      padding-left: 48px;
      padding-right: 48px;
      .HorizontalLine {
        width: 160px;
        margin-bottom: 20px;
        border-top: 1px solid rgba(0, 0, 0, 0.3);
      }
      .colors {
        display: flex;
        flex-direction: column;
        span {
          margin-bottom: 20px;
        }
        .colorLists {
          margin-left: 10px;
          float: left;
          .colorLayout {
            float: left;
            // flex-wrap: wrap;
            display: flex;
            flex-direction: column;
            // justify-content: space-between;
            // align-content: space-around;
            position: relative;
            // display: grid;
            // grid-template-columns: repeat(3, 20%);
            margin-right: 15px;
            margin-left: 3px;
            label {
              background-color: beige;
              border: 1px solid #ccc;
              border-radius: 50%;
              cursor: pointer;
              height: 28px;
              left: 0;
              position: absolute;
              top: 40;
              width: 28px;
              &:after {
                border: 2px solid #fff;
                border-top: none;
                border-right: none;
                content: '';
                height: 6px;
                left: 7px;
                opacity: 0;
                position: absolute;
                top: 8px;
                transform: rotate(-45deg);
                width: 12px;
              }
            }
            input[type='checkbox'] {
              visibility: hidden;
            }
            input[type='checkbox']:checked {
              & + label {
                background-color: beige;
                border-color: beige;
                &:after {
                  opacity: 1;
                }
              }
            }

            .productColor {
              margin-top: 15px;
              font-size: 13px;
              width: 61.25;
              height: 25px;
            }
          }
        }
      }
      .sizes {
        .HorizontalLine {
        }
        .span {
        }
        .sizeLists {
          margin-top: 20px;
          margin-bottom: 20px;
          button {
            margin: 5px;
          }
        }

Here is the corresponding JavaScript file section:

<ul className="colorLists">
                  {COLOR_LISTS.map(color => {
                    return (
                      <li className="colorLayout" title="베이지">
                        <input type="checkbox" id="checkbox"></input>
                        <label
                          for="checkbox"
                          style={{ backgroundColor: color.colorProps }}
                        ></label>
                        <span className="productColor">{color.color_name}</span>
                      </li>
                    );
                  })}
                </ul>

Answer №1

It seems like you're in close proximity to finding the solution. Utilizing flexbox wrapping and setting a width for the elements on each row should help achieve the desired layout.

Consider implementing the following code snippet:

.colorLists {
  margin-left: 10px;
  display: flex; // ensuring the color list is displayed as flex
  flex-wrap: wrap; // allowing elements to wrap onto the next row
  .colorLayout {
    display: flex;
    flex-direction: column;
    width: 33%; // enabling colors to span one third of the row
    flex-shrink: 0; // triggering row wrapping after every third element
    align-items: center; // aiding in content centering
    justify-content: center; // further assisting in content centering
    label {
      // insert label styles here
    }
    input[type='checkbox'] {
      visibility: hidden;
    }
    input[type='checkbox']:checked {
      & + label {
        background-color: beige;
        border-color: beige;
        &:after {
          opacity: 1;
        }
      }
    }

    .productColor {
      margin-top: 15px;
      font-size: 13px;
      width: 61.25;
      height: 25px;
    }
  }
}

Omitting unnecessary styles, focus on making the parent container display: flex, flex-wrap: wrap, and adjusting the children divs with width: 33% along with potentially adding flex-shrink: 0. Tweak these settings to fine-tune your layout, primarily focusing on these styles for the parent and children divs to attain the desired outcome.

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

How to attach input to function invocation in Angular 2

Can we connect the @Input() property of a child component to a parent component's function call like this: <navigation [hasNextCategory]="hasNextCategory()" [hasPreviousCategory]="hasPreviousCategory()" (nextClicked)="next ...

A customizable and adaptable Tetris-inspired CSS design perfect for any screen size

If we imagine having a block like this: <div class="block"></div> There are different sizes of the block: <div class="block b1x1"></div> <div class="block b2x1"></div> <div class="block b1x2"></div> For i ...

Unable to refresh the fullcalendar section following an ajax post click

Currently developing a calendar using fullcalendar. I have created an ajax button that retrieves events from another php page. The first click on the ajax button works fine, displaying a nice month calendar with events. However, my issue arises when I cl ...

Interactive feature allowing all embedded YouTube videos on a webpage to play synchronously, with sound disabled, and on a continuous loop

I am in the process of developing a button that, when clicked by a user, will trigger all embedded YouTube videos on a specific webpage to start playing simultaneously, with sound muted, and set to loop. The target page for this button implementation can ...

The reflight response received an unexpected HTTP status code of 500

Recently, I've been working on utilizing the Yelp Fusion API by making a simple ajax call. I started off by using the client_id and client_secret provided by Yelp to successfully obtain an access token through a 'POST' request in Postman, fo ...

Exploring carousel elements in Selenium using Python

As a user of Selenium, I am trying to loop through the games displayed in the carousel on gog.com and print out all the prices. Below are two random XPaths that lead to the information within the carousel: /html/body/div[2]/div/div[3]/div/div[3]/div[2]/di ...

Firebug mistakenly detects phantom errors

<div id="video-player" data-src="http://www.youtube.com/embed..."></div> Inspect Element - Browser Developer Tools: Error: Access to property 'toString' denied I scanned the entire page (Ctrl+F) and could not find any reference t ...

Troubleshooting a Form Validation Issue with React's useState Hook

I am currently working on form validation for a project. The form includes two essential elements: a textbox for messages and a textbox for recipients. These elements are controlled by state.message (a string) and state.recipients (an array). The state var ...

What is the best way to set up secure client routes?

Welcome to my College Dashboard Project! :) I am currently utilizing Next.js to create a comprehensive dashboard system for Teachers, Students, and Administrators. The Home page serves as the central hub for logging in or signing up as any of the mention ...

I'm encountering an unexpected error while attempting to upload a file on Nextjs. What could be the issue?

I recently completed a small project that involves uploading a CSV file and processing its content to populate a MongoDB collection. However, I am encountering two errors: In the VS Code console: API resolved without sending a response for /api/upload, t ...

Ways to modify the content of the "file" <Input> component in Ant Design

It seems pretty clear from the title. Despite checking the documentation and other sources, I am unable to change both the texts of the label: neither "Choose file," nor "No file chosen." Here is what I have attempted so far. How can I modify those text ...

What is the best way to transmit a Blob object to the server without it arriving empty?

I'm currently facing an issue where I am attempting to send a Blob object containing video data to my server for upload to my S3 storage. However, the Blob is arriving as an empty object on the server side. const blob = new Blob(videoChunks, { t ...

Placing a logo to the left of a UL list

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> & ...

Take away the CSS class from an element after reCAPTCHA verification is complete in Next.js

I'm struggling with removing the CSS class btn-disabled from the input button element upon successful verification of a form entry. I have implemented a function called enableForm to remove the btn-disabled CSS class when reCAPTCHA is verified. Howe ...

Error encountered in creating dependency tree while utilizing create-react-app and storybook

TLDR: How can I configure storybook to use babel-loader v8.1.0 OR force react-scripts to use babel-loader v^8.2.2? Details I am developing a library with an example folder that is a project created with create-react-app. I wanted to integrate storybook i ...

Tips on moving the first item on the Bootstrap navigation bar to the left side

I have successfully implemented a Bootstrap navigation bar and created a container to display some descriptive text beneath it, as depicted in the image provided. Utilizing the most up-to-date version of Bootstrap has ensured smooth integration. https://i ...

Troubleshooting Node.js and Express: Adding to array leads to displaying "[object Object]"

As a newcomer to web development and currently enrolled in a course for it, I am in the process of creating a test web server before diving into my main project. In this test scenario, my goal is to extract the value from a text block and add it to a respo ...

Break up a list into separate paragraphs and style each word using individual CSS

I have a user-generated paragraph that consists of a list of words separated by commas, such as "dog, cat, hamster, turtle." I want to be able to individually assign attributes to each word in the list. Check out this image for reference In the example i ...

Stellar.js is malfunctioning

I've been attempting to implement a parallax effect using Stellar.js with two image tag elements, but I'm encountering issues. Despite trying various configurations, including following the Stellar.js creator's tutorial scripts closely, noth ...

Can an entire application built with a combination of PHP, JavaScript, and HTML be successfully converted to Angular 7?

After creating a complex application that heavily relies on JavaScript, PHP, HTML, and numerous AJAX calls, I am considering migrating the entire codebase to Angular 7. Is it feasible to achieve this transition without requiring a complete rewrite in Ang ...