css制作商品块
商品块布局设计
使用Flexbox或Grid布局可以快速实现商品块的排列。Flexbox适合单行或单列布局,Grid适合复杂的多行多列布局。
/* Flexbox示例 */
.product-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
}
/* Grid示例 */
.product-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
}
商品卡片样式
商品卡片需要包含图片、标题、价格和按钮等元素。设置统一的宽度、圆角和阴影效果增强视觉吸引力。

.product-card {
width: 250px;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
transition: transform 0.3s ease;
}
.product-card:hover {
transform: translateY(-5px);
}
.product-image {
width: 100%;
height: 200px;
object-fit: cover;
}
.product-info {
padding: 15px;
}
.product-title {
font-size: 16px;
margin-bottom: 8px;
}
.product-price {
font-weight: bold;
color: #e63946;
margin-bottom: 12px;
}
.add-to-cart {
background-color: #2a9d8f;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
}
响应式设计
通过媒体查询确保商品块在不同设备上都能正常显示。

@media (max-width: 768px) {
.product-container {
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
}
.product-card {
width: 180px;
}
}
@media (max-width: 480px) {
.product-container {
grid-template-columns: 1fr;
}
.product-card {
width: 100%;
}
}
交互效果增强
添加悬停动画和按钮反馈效果提升用户体验。
.add-to-cart:hover {
background-color: #21867a;
transform: scale(1.05);
}
.add-to-cart:active {
transform: scale(0.98);
}
.product-image {
transition: opacity 0.3s;
}
.product-card:hover .product-image {
opacity: 0.9;
}
价格标签样式
突出显示价格信息,可以添加原价对比或折扣标签。
.price-container {
display: flex;
align-items: center;
}
.original-price {
text-decoration: line-through;
color: #999;
margin-right: 8px;
}
.discount-badge {
background-color: #ff6b6b;
color: white;
padding: 2px 6px;
border-radius: 3px;
font-size: 12px;
}






