What is preventing me from setting flex when I add display flex to the parent element?

Currently, I am developing components in React and experimenting with styling using index.css (although I understand this is not the recommended approach). Below is the current structure: https://i.sstatic.net/8euhM.png

I am looking to display a specific number of productContainer components (let's say 3) out of a total of 24. Currently, all items are appearing on the same line and I have attempted using flex-wrap without success. Adjusting the width also results in everything being grouped on the same line.

Although inline block or grid could resolve this issue, I prefer finding a solution via flexbox layout techniques.

CSS:

.productlist {
display: flex;
}
.productContainer {
padding: 10px;
width: 340px;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-items: flex-start;
border-style: ridge;
border-color: black;
border-width: 1px;
}
.item {
width: 100px;
word-wrap: break-word;
}

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

Answer №1

To achieve the desired outcome, it is essential to utilize both wrap and basis. In the context of flex properties, flex-basis serves the same purpose as width, while flex-wrap prevents elements from overflowing. When flex-grow is not specified, the last elements that do not neatly fit into a row will maintain their size. This is particularly useful for scenarios where there are an odd number of elements.

.productlist {
  display: flex;
  flex-wrap: wrap; /* wrap items in container */
}

.productContainer {
  padding: 10px;
  box-sizing: border-box;
  flex-basis: 33%;  /* 3 containers per row */
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border: 1px ridge black;
}
<div class="productlist">
    <div class="productContainer">
        <div class="item title">Title-1</div>
        <div class="item price">Price</div>
        <div class="item company">Company 1</div>
    </div>
    <div class="productContainer">
        <div class="item title">Title-2</div>
        <div class="item price">Price</div>
        <div class="item company">Company 2</div>
    </div>
    <div class="productContainer">
        <div class="item title">Title-3</div>
        <div class="item price">Price</div>
        <div class="item company">Company 3</div>
    </div>
    <div class="productContainer">
        <div class="item title">Title-4</div>
        <div class="item price">Price</div>
        <div class="item company">Company 4</div>
    </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

Algorithm MinMax is not functioning as originally anticipated

In the process of creating a tic tac toe game for a project on Free Code Camp, I have successfully added a minmax algorithm to determine the best square for the computer player's next move. While the algorithm performs as intended in most cases I&apo ...

The Query Parameters in NextJS are currently blank and are being utilized with the isReady

Hey there, I'm new to NextJS and have a query about accessing query params without using server-side rendering. I am trying to retrieve the params from the URL below: This is the URL: https://localhost:3000/test?querytest=Test export default function ...

Utilize the accurate information retrieved from the API and store it in MongoDB

I have a functioning script that automatically sends data to my MongoDB every second, but I am struggling with figuring out how to send the exact data from an API to MongoDB. FULL CODE var requestPromise = require('request-promise'); const ...

Navigating through various tables (documents) using angularjs and mongoose

I am working towards implementing a setup in mongoDB where each user has their own table (collection of documents). How can I dynamically switch between tables during runtime, based on the user who is logged in? I currently have this code snippet... var m ...

deciding on the axis for flipping a div with CSS

Is there a way to change the setting so that when using -webkit-transform: rotateY(180deg), it rotates from either the far left or right edge of the div, similar to how a door swings open? ...

Guidelines for importing Json information onto a Charts.js visualization

I am looking to integrate the "temp" data variable, identified by ID within (.list), from the provided Json () as an x-axis variable in the chart below: var ctx = document.getElementById('myChart').getContext('2d'); var chart = new Char ...

In search of a comprehensive AJAX-enabled content management system

Is there a Content Management System (CMS) available that can create a fully ajax-driven website, allowing for a persistent Flash component without the need to reload it with each page navigation? ...

Enhancing User Experience with AJAX-loaded Navigation Bar

I have set up the following structure: A main-page.php consisting of: 1) A header 2) A navigation bar 3) Content (loaded inside a <div id="dynamic-content"></div>) 4) Footer The navigation bar contains various options (e.g. About Us, Cont ...

What is the best way to set up a loop for displaying Chat Bubbles?

Is there a way to create a loop for chat bubbles? I want them to appear on the right when I send a message and on the left when the chat partner replies. How can I implement this loop? This is the code I currently have: <div class="Webview"> &l ...

Can a DjangoREST+React project be deployed on a single Google Cloud App Engine instance?

My setup includes a Django backend that interacts with the React.js frontend through REST APIs created with the DjangoREST Framework. In past projects, I've always used separate gcloud projects/app engine instances for Django and React. However, this ...

Incorporating an if condition within the componentDidUpdate method in React resulted in an unforeseen infinite loop

I have a link tag that I am sending to the same component but with a different parameter. To achieve this, I am utilizing componentDidUpdate. If I had used an anchor tag, this step would not have been necessary. However, I encountered an issue where adding ...

Utilizing Unique Icons for HTML Button Design

Currently, I am in the process of developing a mobile application with IONIC 2. Within this app, there is a group of buttons present, three of which utilize the standard Ionicons resources. However, one button stands out as it incorporates a custom image o ...

Challenges with Table and <p> Spacing in HTML and CSS

My website is centered around tables. The body section of my site aligns with the bottom of the header nav bar. However, when I begin typing text in the body, it shifts down by a few pixels. Here are some examples: Before typing any text: After entering ...

Using React TypeScript: maximize the efficiency of sending multiple JSON objects to the frontend with the useData hook

Currently, I am experimenting with a customized useData hook based on the React Beginner course by CodingWithMosh. This modified hook contains multiple objects within it. In the original course content, all data was stored in the results JSON object. I am ...

The useMutation function from react-apollo-hooks is failing to pass variables as intended

Currently in the process of transitioning my code base from apollo-boost to react-apollo-hooks, encountering recurring errors: Error: Required type "String!" variables "$email" and "$password" not provided Updated code snippet: const [email, setEmail] = ...

Using jQuery to load content with a dynamic variable instead of specific element IDs

I am facing a minor jQuery issue. I am trying to load a specific area of an external HTML document into a div, but instead of loading just that particular area, the entire document is being loaded. Here's the code snippet for the trigger: $('a& ...

An alternative in the Android platform that simulates the functionality of CompositeOperation in

When working with HTML5/JavaScript, you may come across the compositeoperation property, which allows you to set the mode for placing graphics on a canvas (add/xor/over etc.). Is there an equivalent behavior for manipulating graphics in Android's Can ...

Is it possible to author TypeScript modules in a format other than ES6?

Is it possible to utilize AMD for writing code? define([ 'hb!./some/file.hb' ], function(template) { // }) ...

Encountered a peculiar problem with nested $.ajax calls within another $.ajax call

Starting from this point - I've set up the base configuration for all upcoming AJAX requests as follows: $.ajaxSetup({ beforeSend : function(){ $("#ajax-loader").dialog({ modal : true }); }, complete : function(){ $( ...

Modifying the content of a webpage with the help of Tampermonkey

I'm working on changing a W Text into B using the code below. It's functional, but I'm experiencing two issues. First, there is a lag when refreshing the page where it briefly shows the W text before replacing it with a B. Second, every 20 o ...