如何使用 CSS 创建产品卡片?
在电子商务网站上,您一定见过针对特定产品的卡片。其中包括产品名称、图片、价格、任何折扣等等。在网页上,我们可以轻松地使用 CSS 创建产品卡片。在此基础上,购买或添加至购物车按钮可置于此卡中,以便用户直接购买产品。
设置卡片的 div
在产品卡片的 div 下,将产品图片和产品名称设置为标题。另外,将产品详细信息和价格置于 <p> 元素中。此外,使用 <button> 元素来设置购买按钮:
<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>
设置卡片样式
若要设置产品卡片的样式,请使用 max-width 属性设置最大宽度。使用 text-align 属性将卡片的文字对齐到中心。另外,设置 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);
}
产品卡片详细信息
赋予详细信息不同的外观,使其看起来更具吸引力。可以使用 font-weight 属性设置字体样式:
.productDetails {
color: rgb(26, 0, 51);
font-weight: bold;
font-size: 18px;
}
显示按钮
产品卡片上的按钮使用 display 属性进行显示,值设置为 inline-block。将 cursor 属性设置为 pointer,以便使光标看起来可点击:
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;
}
示例
若要使用 CSS 创建产品卡片,代码如下:
<!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>
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP