How to create product cards with HTML and CSS
Tags: html, beginners, css
Published: 2023-04-26 11:59:41

Nowadays, most modern websites and web applications have product cards. Product cards enable developers to put some related informations neatly in sections that are appealing to users. You can add some cool animations for great user experience. 

Today we will be create such cards with HTML and CSS, here is what we will create 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uzwzjg77czue77i395p0.jpg).

First of all, we will create a parent container (appropriately, a div 😊) that will nest all of our three product cards, so that we can use flex box and place our product cards  side by side. Remember that you can name your div any name you want but always give it a meaningful name, at least a name you can understand.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnsn1urn62yr3j8eikre.jpg)
Now we will have to create the cards itself, we will start with the first one.
Let's create another div for the cards itself, this div will carry another div that will enable us to apply padding easily, don't worry we are not creating a div soup 😊. It is usually nice to put our image inside a div or container, so that we can give our image a width of 100%.

After that, we also need another div for our text contents for clarity. Inside this div, we will have an h4 that will contain a text "Fragment" `<h4>Fragment</h4>`.
After that, we will create a p tag and give it a class name, it will contain a text "0.080 ETH 1 of 56". Here is a trick, if you look at our design you will notice that our 0.080 ETH has a different color from our 1 of 56 right? For us to achieve this easily, we have to put our 0.080 ETH in a span tag so that we can give it a different color and give the other text, that is not inside the span tag a different color, which is white. 
We will use the same trick on the second p tag and give it a different class name for easy styling. Here is what we have done so far 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfpogyko2628ko1r0chz.jpg)

THE STYLING (although we will still add some HTML later)

We have to remove the default styling that comes with HTML for smooth experience. Here is the code for it `*{
padding:0;
margin:0;
box-sizing:border-box;
}`
Now we will have to give our card itself a width, you can choose any width you like, but I will go for 30 rem. Don't worry you can still use your regular px, you just have to divide this 30 rem by 16 for you to get the value in pixels. I love using rem for some good reasons(don't worry I will write about it later). I will give this cards a background color of red so that I can easily see what I am doing (I will remove it later).
Will give our image a width of 100% .
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v4nbwfiarodan55oiat4.jpg)
Let's preview our work on the browser 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6iwmt9gsfk9rxuynawm.jpg) Wow 😊, we are getting there.
Now will add some padding to the top and the bottom of our h4 tag, we will also increase the font-size to 25px and change it's color to white. We will then add some padding bottom of 1rem, font size of 20px and a color of white to our first p tag. Now we will have to the give the span element inside our first p tag a different color (the color code is in the code) and give it a margin-left of 0.5rem. Let's give our second p tag a font size of 20px and a different color (check the code for the color). Now we will have to give the span tag inside our second p tag a white color and a margin-left of 0.5rem 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/czmiphditga1upuhq83c.jpg). If you check very well, you can see that I have changed the background color of our card to something else. Now let's preview our work 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnx3fiu3aoeff51iymq6.jpg), Wow, we are getting there.
Let's create an HR tag under our second p tag, we will give it a border, color and a margin top of 1rem. This will create that white horizontal line you are seeing on the design. Here is the HTML code 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47a7eyarqa6eko9b1s5a.jpg) Here is the CSS as well 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfaj53dmmyc49q4o1t8l.jpg), now let's preview our work 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wnj7shtka7t3m43vqjxw.jpg) it's nice right 😀?

Now let's create the last part of our card. We will create a parent container or div that will nest two other divs. The first div will have a p tag that will have a span tag with a clock like icon (we will get it from fontawesome.com),the 6 hours. We will then put our ago text inside a span so that we can give it some margin left.We will also give our clock like icon some margin right.

We will create a p tag in our other div as well, we will put the heart icon in a span tag and give it a margin right, then our 674. Here is the code 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfkmkavojxipnne0ldbt.jpg) And here is the result 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ngtpyp1qm1brxbm83cj.jpg)
Now let's style this last part. We will give some padding top to that our parent container that nests our two other div, so that we can create some space between our horizontal line and the div itself. We will also make it a flex container, space the other two divs inside and align them at the center,so that they can be on the same line.
We will then give our p tags that are inside two separate divs a color of white, font size of 20px. You remember the clock icon we put inside a span tag? We will give it some margin right and also give our ago that is inside another span tag some margin left.
You remember that heart icon we put in a span tag in the other p tag? We will give it some margin right and a pink color. This should be your result 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/siueafeu4xl72yww5jyv.jpg) this should be your css as well 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zg06dmr3dmycrf40a8iq.jpg).
Now let's copy our card div, yes the one we nested all of our card informations in. Paste it two times within that same products-cards-con because we will make them a flex items. Change the src of the omg of the two cards you copied and paste so that they can have different images. Here is how your HTML should look like 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3e5y3fhu6xd3e98wxrm.jpg) And this should be your current result 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x23pnwtcjes40g7zizeg.jpg)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/un6zv3fbv9lszwiutw00.jpg)
We will make our product-cards-con a flex, give it a column gap, some padding and a background color of black. This is the CSS code 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4m4prlyw5xd2t5bw98mx.jpg) And this should be your result 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nx7d43l124hd4ntxdl9.jpg). If you can notice, I changed the last image because it's not of the same size as others, so you images that have the same size to avoid issues.

And that's it guys, I hope you learnt something from this. Please leave a comment if you have any questions I will be glad to answer you.

Happy coding 😀👨‍💻!