css商城制作
布局设计
使用Flexbox或Grid布局实现响应式设计。Flexbox适合一维布局,Grid适合二维复杂布局。Flexbox示例:
.product-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: space-between;
}
Grid示例:
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
}
商品卡片样式
设置统一商品卡片样式,包含图片、标题、价格和按钮:
.product-card {
border: 1px solid #ddd;
border-radius: 8px;
overflow: hidden;
transition: transform 0.3s ease;
}
.product-card:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.product-image {
width: 100%;
height: 200px;
object-fit: cover;
}
.product-info {
padding: 15px;
}
.price {
color: #e63946;
font-weight: bold;
font-size: 1.2rem;
}
.add-to-cart {
background: #457b9d;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
}
导航栏设计
创建固定顶部导航栏,包含logo、搜索框和购物车图标:
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 5%;
background: white;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
position: sticky;
top: 0;
z-index: 100;
}
.search-bar {
flex-grow: 1;
margin: 0 20px;
}
.search-bar input {
width: 70%;
padding: 8px 15px;
border-radius: 20px;
border: 1px solid #ddd;
}
.cart-icon {
position: relative;
}
.cart-count {
position: absolute;
top: -8px;
right: -8px;
background: red;
color: white;
border-radius: 50%;
width: 20px;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
font-size: 12px;
}
购物车侧边栏
实现可滑动的购物车侧边栏:
.cart-sidebar {
position: fixed;
top: 0;
right: -400px;
width: 400px;
height: 100vh;
background: white;
box-shadow: -2px 0 10px rgba(0,0,0,0.1);
transition: right 0.3s ease;
z-index: 1000;
padding: 20px;
overflow-y: auto;
}
.cart-sidebar.active {
right: 0;
}
.cart-item {
display: flex;
margin-bottom: 15px;
border-bottom: 1px solid #eee;
padding-bottom: 15px;
}
.cart-item-image {
width: 80px;
height: 80px;
object-fit: cover;
margin-right: 15px;
}
响应式调整
添加媒体查询确保移动端友好:
@media (max-width: 768px) {
.product-grid {
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.navbar {
padding: 10px;
flex-wrap: wrap;
}
.search-bar {
order: 3;
width: 100%;
margin: 10px 0;
}
.search-bar input {
width: 100%;
}
.cart-sidebar {
width: 90%;
right: -90%;
}
}
动画效果
添加微交互提升用户体验:

@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.product-card {
animation: fadeIn 0.5s ease forwards;
}
.add-to-cart {
transition: background 0.2s ease;
}
.add-to-cart:hover {
background: #1d3557;
}
.add-to-cart:active {
transform: scale(0.95);
}






