Arrange four Divs next to each other with flexbox styling

I've been struggling with aligning my cards side by side. They are a series of divs nested in lists under a <ul>

Changing the positioning is not resolving the issue, and I'm hesitant to alter the display as it's crucial for responsiveness.

I want to maintain the current mobile layout as it complements my other elements well. Are there any resources you can suggest to assist me?

This is the HTML code:

        <ul class="cards">
          <li class="cards_item">
            <div class="card">
              <div class="card_image"><img src="./assets/Blog-post/post-5.jpg"></div>
              <div class="card_content">
                <h2 class="card_title">Card Grid Layout</h2>
                <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>
                <button class="btn card_btn">Read More</button>
              </div>
            </div>
          </li>
          <li class="cards_item">
            <div class="card">
              <div class="card_image"><img src="./assets/Blog-post/post-2.jpg"></div>
              <div class="card_content">
                <h2 class="card_title">Card Grid Layout</h2>
                <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>
                <button class="btn card_btn">Read More</button>
              </div>
            </div>
          </li>
          <li class="cards_item">
            <div class="card">
              <div class="card_image"><img src="./assets/Blog-post/post-6.png"></div>
              <div class="card_content">
                <h2 class="card_title">Card Grid Layout</h2>
                <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>
                <button class="btn card_btn">Read More</button>
              </div>
            </div>
          </li>
          <li class="cards_item">
            <div class="card">
              <div class="card_image"><img src="./assets/Blog-post/post-1.jpg"></div>
              <div class="card_content">
                <h2 class="card_title">Card Grid Layout</h2>
                <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>
                <button class="btn card_btn">Read More</button>
              </div>
            </div>
          </li>


        </ul>
      </div>

Here is the corresponding CSS:

/* Design */
*{
  box-sizing: border-box;
}

.main-card{
  max-width: 900px;
  margin: 0 auto;
  position: relative;
 right: 10%;
}

img {
  height: auto;
  max-width: 100%;
  vertical-align: middle;
}

.btn {
    margin: 10px;
    font-family: "Arial Black", Gadget, sans-serif;
    font-size: 15px;
... (CSS code continues)

If you have any suggestions or tips to help me resolve this alignment issue while maintaining responsiveness, please share.

Answer №1

After carefully reviewing your issue, my suggestion is to eliminate the line display: flex; flex-direction: column; from

.card {
  background-color: white;
  border-radius: 0.25rem;
  box-shadow: 0 20px 40px -14px rgba(0, 0, 0, 0.25);
   display: flex;
  flex-direction: column; 
  position: relative;
}

Instead, you can add

ul {
    display: flex;
    flex-flow:row wrap;
    flex-direction:row;
}


Answer №2

Make your design better by incorporating rows and columns as shown below.

/* Improved Design */

.row {
    display: flex;
    flex-wrap: wrap;
    max-width: 100%;
    flex: 50%;
}
.column {
    flex: 50%;
    padding: 20px;
}

*{
    box-sizing: border-box;
}

.main-card{
    max-width: 900px;
    margin: 0 auto;
    position: relative;
    right: 10%;

}

img {
    height: auto;
    max-width: 100%;
    vertical-align: middle;
}

.btn {
    margin: 10px;
    font-family: "Arial Black", Gadget, sans-serif;
    font-size: 15px;
    padding: 10px;
    text-align: center;
    text-transform: uppercase;
    transition: 0.5s;
    background-size: 200% auto;
    color: #FFF;
    box-shadow: 0 0 20px #eee;
     border-radius: 10px; 
    width: 200px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
    transition: all 0.3s cubic-bezier(.25,.8,.25,1);
    cursor: pointer;
    display: inline-block;
     border-radius: 25px;
     background-image: linear-gradient(to right, #89B0D5  0%,  #E677F1   51%,  #9A669F   100%);
}


.btn:hover {
    box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
    margin: 8px 10px 12px;
}

.cards {
   display: flex;
  flex-wrap: wrap; 
  list-style: none;
 position: relative;

}

.cards_item {
    display: inline-block;
    padding: 1rem;
}

@media (min-width: 40rem) {
  .cards_item {
    width: 50%;
  }
}

@media (min-width: 56rem) {
  .cards_item {
    width: 33.3333%;
  }
}

.card {
    background-color: white;
    border-radius: 0.25rem;
    box-shadow: 0 20px 40px -14px rgba(0, 0, 0, 0.25);
    display: flex;
    flex-direction: column; 

    position: relative;

}

.card_content {
    padding: 1rem;
    background: white
}

.card_title {
    color: black;
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: 1px;
    text-transform: capitalize;

}

.card_text {
    color: black;
    font-size: 0.875rem;
    line-height: 1.5;
    margin-bottom: 1.25rem;    
    font-weight: 400;
}
 
 <ul class="cards">
  <div class="row">
          <div class="column">
          <li class="cards_item">
            <div class="card">
              <div class="card_image"><img src="./assets/Blog-post/post-5.jpg"></div>
              <div class="card_content">
                <h2 class="card_title">Card Grid Layout</h2>
                <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>

                <button class="btn card_btn">Read More</button>
              </div>
            </div>
            
          </li>
          </div>
<div class="column"> 
          <li class="cards_item">
            <div class="card">
              <div class="card_image"><img src="./assets/Blog-post/post-2.jpg"></div>
              <div class="card_content">
                <h2 class="card_title">Card Grid Layout</h2>
                <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>

                <button class="btn card_btn">Read More</button>
              </div>
            </div>
             
          </li>
          </div>
                <div class="column"> 
          <li class="cards_item">
            <div class="card">
              <div class="card_image"><img src="./assets/Blog-post/post-6.png"></div>
              <div class="card_content">
                <h2 class="card_title">Card Grid Layout</h2>
                <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>

                <button class="btn card_btn">Read More</button>
              </div>
            </div>
            
          </li>
         
</div>
<div class="column"> 
          <li class="cards_item">
            <div class="card">
              <div class="card_image"><img src="./assets/Blog-post/post-1.jpg"></div>
              <div class="card_content">
                <h2 class="card_title">Card Grid Layout</h2>
                <p class="card_text">Demo of pixel perfect pure CSS simple responsive card grid layout</p>

                <button class="btn card_btn">Read More</button>
              </div>
            </div>
            </div>
          </li>
</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

Solutions for resolving the error "Unable to access property 'value1' because it is undefined"

constructor() { super(); this.state = { value1 : Math.floor(Math.random() * 100), value2 : Math.floor(Math.random() * 100), value3 : Math.floor(Math.random() * 100), proposedAnswer : Math.floor(Math.random() * 3) + this.state.value1 + t ...

How can I use AJAX to remove a record from a MySQL table?

I am looking to create a button with an image X that, when clicked, will delete a row from the database at that exact moment. teste-checkbox.php <style> table { font-family: verdana; font-size: 12px; } table th { text-align: left; backgrou ...

What is the best way to cut and combine multiple array elements in the right positions?

let result = response.data; console.log(result); const newTemp = result.ssps_with_scated.splice(5,1).concat(result.ps_with_ed.splice(2,1))[0]; result.ps_with_ed.push(newTemp); After multiple attempts, I finally achieved my goal. However, the array values ...

Expanding a list of object arrays through iteration: A step-by-step guide

// Initializing with one object let posi_val=[{top: '3px', left: '2px'}]; // Adding another object in the loop for (let i = 1; i < n; i++) { posi_val.push({ top: `${posi_val[i - 1].top.slice(0, -2) * 2}px`, l ...

Using Sequelize to send data from the client-side to the server-side

I am currently developing a project for a fictional library database and website interface. I am facing an issue where only 2 out of the 4 new loan form inputs are being passed to the req.body. Even though all items have a name attribute, it seems like onl ...

Creating a TypeScript generic type for the "pick" function to define the types of values in the resulting object

I am facing an issue while writing the type for the pick function. Everything works smoothly when picking only one key or multiple keys with values of the same type. However, if I attempt to pick a few keys and their values are of different types, I encoun ...

Is it possible for node-java to accept anonymous functions as parameters in Java?

I am looking to pass an anonymous function from JavaScript to Java using node-java (https://github.com/joeferner/node-java). Below is a snippet of the Java code for reference: public class Example { public Example() { } public interface Callb ...

Transform a global JavaScript function into a global Vue.js function, compatible with the Vue Laravel framework

My JavaScript function displays a color border when required and changes the color if anything is inputted. It works fine in plain JavaScript but not in Vue. I need to use this function in Vue, on any place or component. app.js $('.req' ).on(&a ...

Using jQuery to retrieve the TD value

I'm attempting to retrieve the TD value using the Value attribute.... Let's say I have the following HTML markup: <td nowrap="nowrap" value="FO2180TL" class="colPadding" id="salesOrderNumber1">bla bla </td> So, I tried this- v ...

I'm currently attempting to set up the React JS package, but unfortunately encountering some errors in the process

I'm having trouble installing react as I keep receiving errors. My system has npm version 8.12.1 installed. I attempted to downgrade react, but that didn't resolve the issue. I tried the following steps: Update npm using: npm install npm -g Dow ...

Changing direction of arrow upon dropdown menu opening with JavaScript

I have developed a piece of code that enables users to click on a div in order to reveal a dropdown menu containing radio buttons. My goal is to make the arrows rotate 180 degrees when the dropdown menus are opened, and then return to their original positi ...

Is a Bootstrap 3 Calendar Available?

I am attempting to design a calendar using Bootstrap 3.3.7 that features consistent square dates, similar to this example: It is important to me that each date is displayed in a square format. Here is what I have come up with so far: I am getting close ...

Is it possible to store dat.gui presets for controls that are dynamically added?

I have a dynamic dat.gui interface where I add controls, but the "save settings" feature doesn't seem to recognize them. var mygui = new dat.GUI(); mygui.remember(mygui); // Example of adding a control in the standard way mygui.control1 = 0.0; var c ...

Transfer information from an array to a Vue function

Having some difficulties passing data to the function myChart within the mounted section. As a beginner in vuejs, I'm struggling with identifying the issue. I am trying to pass data in labels and datasets, which are called from my function. Can anyone ...

Unable to make a POST request to the GitHub v3 API

I'm attempting to generate a public gist using JavaScript without any authentication - all purely client-side. var gist = { "description": "test", "public": true, "files": { "test.txt": { "content": "contents" ...

NextJS encountered a JavaScript heap out of memory issue

While developing my NextJs project, it suddenly encountered an unknown issue that resulted in the following error message being logged: <--- Last few GCs ---> [8728:000001A567CE5290] 8719226 ms: Mark-sweep (reduce) 1827.7 (1944.2) -> 1827.7 (189 ...

Reading data from Firestore in Next.js

I came across a Next.js starter that retrieves data from Firestore v9, but it only displays the data after a click event. How can I modify this code in Next.js to automatically show the data without needing to click? import { db } from '@/lib/firebase ...

Exploring the Fusion of Different Styles in Material-UI Using React

I have two different styles that I use in my code. One style is specific to certain components, while the other style is global and used across various components. For example, consider the following file tree: index.tsx -App.tsx -globalConstants.ts In ...

Encountering a JSLint error while attempting to import the 'aws-amplify' package in my main file

Recently, I installed the latest version of aws-amplify using npm. After running npm init with 'entryPoint.js' as the entrypoint file, I encountered an issue when pasting the following code at the top of entryPoint.js: import Amplify, {Auth} from ...

Differences between the application/xml and text/xml MIME types

How can I receive an XML response from a servlet? The servlet returns a content type of "application/xml". When using XmlHttpRequest, I am able to get responseText, but not responseXml. Could this be related to the content type or the request type (I' ...