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

Ensure that the HTML input element only accepts positive values, including decimal numbers

I need to build an input field that only accepts positive numbers, including decimal values. Leading zeros are not allowed, and users should not be able to paste invalid values like -14.5 into the field. Here is my current implementation: <input type= ...

Effective implementation of the useReducer hook across various React components to manage form state

My current project is proving to be quite challenging as I navigate through the step-by-step instructions provided. Initially, I was tasked with creating a BookingForm.js component that houses a form for my app. The requirement was to utilize the "useEffec ...

Enhancing React Flow to provide updated selection and hover functionality

After diving into react flow, I found it to be quite user-friendly. However, I've hit a roadblock while attempting to update the styles of a selected node. My current workaround involves using useState to modify the style object for a specific Id. Is ...

How to place a button in SwiperJs using React

Can anyone provide guidance on how to position the navigation buttons in a manner similar to the image below? The desired layout is for the buttons to appear on the outer edges of the swiper component. You can refer to this demo for more information. htt ...

Center contents of Bootstrap row vertically

I have a simple property grid layout in Bootstrap 3 structured as follows: <div class="row"> <div class="col-md-3"> <div id="propGrid"> <div class="row pgTable"> <div class="col col-md-12"> <d ...

The sub menu in IE 8 does not stay open while hovering over it

My website has a navigation menu with a sub-menu that appears when hovering over the parent element. While this works smoothly on modern browsers, Internet Explorer 8 presents an issue. When hovering over the parent li element in IE 8, the sub-menu is disp ...

Focus Google Map on Selected Option using AngularJS

I'm attempting to center the map based on a selection in a drop-down select option box. Despite trying various examples, I haven't been successful in getting the map to recenter to the new latitude and longitude coordinates. I'd like to ach ...

Attempting to determine income in JavaScript with the inclusion of two distinct rates

Trying to come up with a salary calculation for different rates within the same timeframe is proving to be quite challenging. For instance, between 10:00-15:00 the rate is $20 per hour and from 15:00 onwards it drops to $15 per hour. Check out the entire ...

Merging pertinent information from separate arrays

Seeking assistance in merging data from two distinct arrays with varying structures. Data is acquired via API, the initial request: [ { "category_id": "12345", "category_name": "itemgroup 1", "cat ...

Generate a revised object from a function and return it to the caller

What is the most efficient way to update and return a new object from a function? I came up with this function: export const addItemToCart = (currentCart, item) => { const { name, ...otherProps } = item; // if the item is already in the cart if ...

Tips for creating an infinite repeating loop in jQuery

I'm attempting to create a jQuery infinite loop featuring images within a div. I've experimented with using the setInterval function, but encountered an issue where the animation starts after a 3-second delay and does not seamlessly repeat itself ...

enhancing the functionality of appended children by including a toggle class list

I'm having trouble toggling the classList on dynamically appended children. The toggle works fine on existing elements, but not on those added through user input. Any tips on how I can achieve this? I'm currently stumped and would appreciate any ...

Ways to distinguish jquery response apart from using $.post method?

Using jquery $.post method: $.post( 'includes/studiesAjax/addTryb.php', {inputVL: inputVL}, function(res){ $("#container").html(res); } ); The response contains a long html code. I am interested in extracting data from ...

Display a webpage in thumbnail form when hovering the mouse over it

I'm in the process of creating a website that contains numerous sub-pages. I want to display all the links on a single page and when a user hovers over a link, I want to show a thumbnail of the corresponding webpage within a tooltip. I've experi ...

What is the best way to include a new user in my list of friends within the User schema?

Working on my customized social media platform, I have implemented a feature where users can send friend requests by clicking on a button. <form action="/requests" method="POST"> <input type="hidden" name="send ...

Custom template for prefetched data is not displaying in Typeahead.js

I'm currently utilizing Twitter's typeahead.js plugin for displaying autocomplete suggestions. Here is the code snippet: $.ajax({ url:'/getlocals/', success: function(data){ $('.local-type').typeahead({ ...

I'm confused as to why only one of my HTML pages is accepting my CSS styling - not sure what's going on

I am facing an issue where the CSS styles are only being applied to one of the HTML pages I created. Despite checking my code multiple times, everything seems to be correct. All the necessary files are saved on my laptop in the same folder for the website. ...

Toggle switch unable to reset upon reloading the page

Issue with Toggle Switch Not Resetting on Page Reload I am encountering a problem with a toggle switch I implemented from the W3schools example (link here). Unlike in the W3schools editor where it resets its state upon reload, my toggle switch remains in ...

Unable to retrieve the complete count of invitations made by a user

My goal is to retrieve the invites of the author of a specific command within one server. I have experimented with various solutions, but many appear outdated or incorrect. Here is my current approach: exports.run = async (client, message, args) => { ...

Enhance your Morris.js charts by incorporating detailed notes and annotations

Is there a way to include annotations in my morris.js charts? I couldn't find any information about this on their official website. I specifically need to add notes to certain dates. ...