React Cards

1/1/1970

React Cards

How to Create React Card UX Component with Bootstrap - React Project

1. Set Up the React App

Create a new React application:

npx create-react-app my-cards

This command initializes a new React project in a directory named my-cards.

2. Directory Structure

Create the following directory structure inside src:

src
 |-- Cards
    |--- CardUI.jsx
    |--- Cards.jsx
    |--- card-style.css
|--- index.js

3. Install Necessary Packages

Install Bootstrap for styling:

npm install bootstrap@latest

This command installs the latest version of Bootstrap to style your React components.

Install Webpack service worker plugin:

npm install serviceworker-webpack-plugin

This plugin helps in setting up service workers for offline capabilities.

4. Card Component (CardUI.jsx)

// CardUI.jsx
 
import React from 'react'; // Import React library
import './card-style.css'; // Import custom styles
 
const Card = (props) => {
	return (
		<div className="card text-center shadow">
			<div className="overflow">
				<img src={props.imgsrc} alt="Image" className='card-img-top' />
			</div>
			
			<div className="card-body text-dark">
				<h4 className="card-title">{props.title}</h4>
				<p className="card-text text-secondary">
					{props.description}
				</p>
				<a href="#" className="btn btn-outline-success">Go Anywhere</a>
			</div>
		</div>
	);
}
 
export default Card;

5. Card Container (Cards.jsx)

// Cards.jsx
 
import React, { Component } from 'react'; // Import React and Component
import Card from './CardUI'; // Import the Card component
 
// Import images
import img1 from '../assets/img1.jpg';
import img2 from '../assets/img2.jpg';
import img3 from '../assets/img3.jpg';
 
class Cards extends Component {
	render() {
		return (
			<div className="container-fluid d-flex justify-content-center">
				<div className="row">
					<div className="col-md-4">
						<Card imgsrc={img1} title="Project 1" description="This is project 1 description" />
					</div>
					
					<div className="col-md-4">
						<Card imgsrc={img2} title="Project 2" description="This is project 2 description" />
					</div>
					
					<div className="col-md-4">
						<Card imgsrc={img3} title="Project 3" description="This is project 3 description" />
					</div>
				</div>
			</div>
		);
	}
}
 
export default Cards;

6. Custom Styles (card-style.css)

/* card-style.css */
 
/* Background gradient for the entire page */
body {
	background: radial-gradient(#e5e5e5, #ffff, #e5e5e5);	
}
 
/* Card width and hover effect */
.card {
	width: 20rem;
}
.card:hover {
	box-shadow: 5px 10px 20px 1px rgba(0, 0, 0, 0.253) !important;
}
 
/* Card body padding and text styling */
.card-body {
	padding: 3rem 0 !important;
}
.card-text {
	font-size: 0.9rem;
	padding: 0.4rem 1.9rem;
}
 
/* Spacing above the card container */
.container-fluid .row {
	padding-top: 6rem;
}
 
/* Image scale and hover effect */
.card-img-top {
	transform: scale(1);
	transition: transform 0.5s ease;
}
.card-img-top:hover {
	transform: scale(1.5);
}
 
/* Hide overflow of the image */
.overflow {
	overflow: hidden;
}

7. Entry Point (index.js)

import React from 'react';
import ReactDOM from 'react-dom/client'; // Correct import for React 18+
import './index.css'; // Import global styles
import * as serviceWorker from './serviceWorker'; // Import service worker setup
 
import "bootstrap/dist/css/bootstrap.min.css"; // Import Bootstrap CSS
import Cards from './Cards/Cards'; // Import the Cards component
 
// Render the Cards component into the root element
const root = ReactDOM.createRoot(document.getElementById('root')); // Updated for React 18+
root.render(<Cards />);
 
// Service worker registration (optional)
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();