How to create a "card" with CSS?


On a web page, we can easily create a card like you must have seen on a Team’s page. The employee name, and designation is visible with a thumbnail image. With CSS, we can easily a card and also set the hover style using the :hover selector.

Set the Card Image

The <img> element is used to set the image for the card. Place it inside a div −

<div class="card">
   <img src="https://www.tutorialspoint.com/assets/profiles/123055/profile/200_187394-1565938756.jpg">
   <div class="designation">
      <h3>Amit Diwan</h3>
      <p>Founder</p>
   </div>
</div>

Set the Card Header and Text

Under the above <div>, create another <div> to set the card header and text −

<div class="Designation">
   <h3>Amit Diwan</h3>
   <p>Founder</p>
</div>

Style the Card

The div we have set above for the card will now be styled to make it look professional. For that, we have used the box-shadow property. The max-width is also set to set the exact width −

.card {
	box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
	width: 40%;
	max-width: 300px;
	background-color: rgb(56, 185, 185);
}

On Hover

A slightly different style for the shadow is set on hovering over the card −

.card:hover {
   box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}

Style the Card Header and Text

We have also set styles for the card header and text to make it look professional −

.designation {
	padding: 2px 16px;
	background-color: rgb(107, 43, 167);
	color:white;
}

Example

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

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <style>
      body{
         font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
         margin:20px;
      }
      .card {
         box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
         width: 40%;
         max-width: 300px;
         background-color: rgb(56, 185, 185);
      }
      .card:hover {
         box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
      }
      .designation {
         padding: 2px 16px;
         background-color: rgb(107, 43, 167);
         color:white;
      }
      .card img{
         width: 288px;
         height: 250px;
      }
   </style>
</head>
<body>
   <h1>Card Example</h1>
   <div class="card">
      <img src="https://www.tutorialspoint.com/assets/profiles/123055/profile/200_187394-1565938756.jpg">
      <div class="designation">
         <h3>Amit Diwan</h3>
         <p>Founder</p>
      </div>
   </div>
</body>
</html>

Updated on: 17-Nov-2023

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements