Tips for arranging Bootstrap cards in ReactJS so they wrap to a new line after three have been displayed

In my personal portfolio, there is a dedicated page for showcasing various projects using Bootstrap cards. These projects are fetched from a CardData.js file and then mapped to a ProjectCard component. Ideally, I want to display 3 cards per row with consistent margins and spacing. However, when the number of cards exceeds 3, they end up getting squeezed into a single row.

Any assistance or suggestions would be highly appreciated!

  • Projects.js (Page component):

import '../../App.css';
import './projects.css';

import { CardData } from './CardData.js';

// TODO: Update layout to ensure proper alignment of cards 

class Projects extends Component {
    render() {
        return (
            <div id="project-div">
                <Navbar ... />
                <h1 id="header-projects" className="font-weight-bold text-uppercase text-center ">Projects</h1>
                ...
            </div>
        )
    }
}
    
  • Projects.css:

#project-div {
    background-color: transparent;
    background-image: url("../../img/topography.png");
    width: 100vw;
    height: 100vh;
}

...

@media (min-width: 1000px)
{
    .project-cards {
        max-width: 40rem;
    }
}
    
  • ProjectCards.js (Component for actual cards):

class ProjectCards extends Component {
    render() {
        return (
            <div className="card mx-auto project-cards rounded border shadow">
                ...
            </div>
        )
    }
}
    
  • ProjectCard.css:

.project-cards {
    overflow: hidden;
    display: inline-block;
    text-align: center;
    ...
}
    

Answer №1

Here is an example of how you can achieve this layout using the Bootstrap Grid System:

<Row>
    {CardData.map((item, index) => {
            return (
                <Col sm={4}><ProjectCard cardImg={item.cardImg} cardURL={item.cardURL} cardTitle={item.cardTitle} cardText={item.cardText} /></Col>
            )
        })}
    
</Row>

Answer №2

Utilize the Bootstrap grid system Grid to arrange items on a webpage. For example, if you need to display 3 cards in each row, follow these steps:

<div className="container">
  <div className="row">
    <div className="col-12 col-lg-4"><ProjectCard/></div>
    <div className="col-12 col-lg-4"><ProjectCard/></div>
    <div className="col-12 col-lg-4"><ProjectCard/></div>
    <div className="col-12 col-lg-4"><ProjectCard/></div>
    <div className="col-12 col-lg-4"><ProjectCard/></div>
    <div className="col-12 col-lg-4"><ProjectCard/></div>
  </div>
</div>

The above code will position each ProjectCard in 1 row on mobile devices using col-12, and will place 3 ProjectCards in 1 row on desktops using col-lg-4.

This grid system can accommodate any number of ProjectCards, with the 4th card moving to the next line automatically.

One important point to note is not to specify the width for ProjectCard when using the grid system, as the system will manage the widths for you.

Updated code:

<div id="card-div" className="row">
  {CardData.map((item, index) => {
    return (
      <div className="col-12 col-lg-4">
        <ProjectCard
          cardImg={item.cardImg}
          cardURL={item.cardURL}
          cardTitle={item.cardTitle}
          cardText={item.cardText}
        />
      </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

Exploring hover-effects in Next JS

I am currently facing a challenge while trying to utilize the npm package hover-effect within a functional component. Despite my previous experience with similar packages in create-react-app, I am relatively new to next.js. Below is an example of how the p ...

What does the concept of "installing packages globally or locally" entail in React JS?

I recently began learning React.js and as I was setting up the environment, I came across a confusing term: installing packages globally versus locally. What exactly does it mean to install packages globally versus locally in this context? ...

What's preventing me from using just one comparison condition in TypeScript?

The issue at hand is quite simple: An error occurred because I tried to compare a number with a 'Ref<number>' object. It seems ridiculous that I can't compare two numbers, but as I am new to Typescript, I would greatly appreciate some ...

The onWrite cloud function does not activate upon the creation of a new document

I have a collection called 'Users' that stores documents with user objects as well as a sub-collection named 'Notifications'. When a new notification is generated for a user, a corresponding document is created in the 'Notification ...

The dropdown CSS menu appears to be malfunctioning

I've been struggling with creating a dropdown menu for some time now and I've reached a point where I need assistance. I've tried various solutions I found online, including watching tutorials on YouTube, but I can't seem to make it wor ...

Is there a way to trim the string after the second occurrence of an underscore?

I am faced with the task of extracting file names and types from a list of files in an object and displaying them in a table. The list of files is returned by the server in the format: timestamp_id_filename. For example: 1568223848_12345678_some_document. ...

Extract the title of a research paper from the personnel website

I am looking to extract the title and authors of journal articles from the official web pages of all staff members. For example: https://eps.leeds.ac.uk/civil-engineering/staff/581/samuel-adu-amankwah The specific section I need to access is this: https: ...

Python HTMLParser: Extracting HTML tag content made easy!

After working through an HTML page, I have now reached a point where I have lines that look like this: <td class="border">AAA</td><td class="border">BBB</td> I am trying to extract AAA and BBB into variables using HTMLParser, but ...

Tips for effectively waiting in Selenium for a list to finish scrolling

For my project, I decided to write Selenium code using Javascript in order to automatically scroll down a list by simulating the Down key on the keyboard. Although there are simpler ways to achieve scrolling with Javascript commands, I specifically wanted ...

Unable to establish a connection with the Mysql database using Node JS

My Class is responsible for handling a mysql connection, and every time I access the path /cargos, it should retrieve data and then close the connection. However, every time I try to Query, I encounter this error message: TypeError: conexao.query is not a ...

Tips for ensuring an image remains consistently positioned alongside another element

I have a request - In my code, I have multiple h2 headings that I want to display with an image of the Orioles Logo on both sides. There are about 100 of them, and now I need to change all of them to show the Ravens Logo instead. Doing this manually would ...

Ensure that the div spans the entire width of the page, prioritize maintaining the aspect ratio, but allow for variable

I have a dilemma with a div containing multiple elements. My goal is to make the div 100% in width, while also attempting to preserve the aspect ratio if feasible. Should the enclosed elements exceed the div's boundaries, then the height will adjust a ...

Tips for toggling password visibility by clicking outside a password field

As I work on creating a password field that allows users to toggle the visibility of their password by clicking an eye icon, I am facing a challenge. I want the password to be hidden automatically when the user clicks outside the field, which requires a co ...

Issue with UI inconsistency due to Jotai's useHydrateAtoms hook in Next.js version 13

In the midst of developing a Next.js 13 project with the new app directory and utilizing jotai as a global state manager. I have encountered an issue while attempting to pass an initial state to my atoms using the useHydrateAtoms hook, following the guidan ...

Show and hide the div by clicking a button

Looking at the image below, my goal is to display the div under the reply button. (The div consists of a text box and send button) https://i.stack.imgur.com/jnaV4.png The code I have currently functions correctly. However, when clicking any reply button ...

What is the best way to display label information on a Google line chart?

line graph column graph My graph continuously calls the Controller to fetch recent data from the Database. There are two lines on the graph, and I would like to display the names of each line (column) such as red=counts of something // brown=counts of so ...

Displaying an ejs file on the client's side

Struggling to implement AJAX functionality for cloning an 'item' template. I am attempting to insert an EJS template containing information about the newly cloned item into the main EJS page after each POST request. However, I am having difficult ...

Importing classes in ECMAScript 6 does not work properly, causing issues when running scripts

I am currently learning Selenium Webdriver. I am facing an issue where I can't run a script with imported classes, but I am able to run it without classes using import functions only. To execute it, I use babel-cli in the following manner: node ./babe ...

Creating multiple dynamic routes in Next.js based on specific IDs

Hey there! Currently tackling a challenge in my Nextjs project involving Events->Event->Schedule. My goal is to simply click on an event and be directed to the corresponding event details. Within the event, there should be a schedule that links to th ...

Should we consider packaging the npm dependencies code along with our code as a best practice?

What is the best way to handle npm dependencies in our code bundle? If it's preferable to include the npm dependency code in our bundle, does it make sense to add it as a separate module or package? If not, how can I prevent bundling my dependencie ...