How to create a product card with CSS?


On an E-commerce website, you must have seen product cards for specific products. It includes the product name, image, price, any discount, etc. On a web page, we can easily create a product card with CSS. With that, the Buy Now or Add to Cart button is also placed on this card so it’s easier for users to directly buy.

Set the div for the card

Under the div for the product card, set the product image and the product name as a heading. Rest, place the product details and price in the <p>. Also, the buy button is set using the <button> element −

<div class="productCard">
   <img src="https://images.pexels.com/photos/1152077/pexels-photo-1152077.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" style="width:100%"/>
   <h1>Leather Bag</h1>
   <p class="productDetails">Exotic Quality</p>
   <p>Price 50$</p>
   <p><button>Buy Now</button></p>
</div>

Style the card

To style the product card, set the maximum width using the max-width property. Align the text of the card in the center using the text-align property. Also, set the box shadow −

.productCard {
   box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
   max-width: 300px;
   margin: auto;
   text-align: center;
   font-family: arial;
   background-color: rgb(190, 224, 224);
}

Product Card Details

Give a different look to the details to make it look more appealing. Font style can be set with the font-weight property −

.productDetails {
   color: rgb(26, 0, 51);
   font-weight: bold;
   font-size: 18px;
}

Display the button

The button on the product card is displayed using the display property with the value inline-block. Set the cursor property to pointer to make the cursor look clickable −

button {
   border: none;
   outline: 0;
   display: inline-block;
   padding: 8px;
   color: white;
   background-color: rgb(23, 31, 102);
   text-align: center;
   cursor: pointer;
   width: 100%;
   font-size: 18px;
}

Example

To create a product card with CSS, the code is as follows −

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      }
      .productCard {
         box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
         max-width: 300px;
         margin: auto;
         text-align: center;
         font-family: arial;
         background-color: rgb(190, 224, 224);
      }
      .productDetails {
         color: rgb(26, 0, 51);
         font-weight: bold;
         font-size: 18px;
      }
      button {
         border: none;
         outline: 0;
         display: inline-block;
         padding: 8px;
         color: white;
         background-color: rgb(23, 31, 102);
         text-align: center;
         cursor: pointer;
         width: 100%;
         font-size: 18px;
      }
      button:hover, a:hover {
         opacity: 0.7;
      }
   </style>
</head>
<body>
   <h2 style="text-align:center">Product Card Example</h2>
   <div class="productCard">
      <img src="https://images.pexels.com/photos/1152077/pexels-photo-1152077.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" style="width:100%"/>
      <h1>Leather Bag</h1>
      <p class="productDetails">Exotic Quality</p>
      <p>Price 50$</p>
      <p><button>Buy Now</button></p>
   </div>
</body>
</html>

Updated on: 08-Dec-2023

826 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements