css 制作购物网站
CSS 制作购物网站的关键技术
布局与响应式设计
使用 Flexbox 或 Grid 布局实现商品列表的排列,确保在不同屏幕尺寸下适配。媒体查询(@media)针对移动端优化布局,例如调整商品卡片的宽度和间距。
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
}
@media (max-width: 768px) {
.product-grid {
grid-template-columns: 1fr;
}
}
商品卡片样式
为商品卡片添加阴影、圆角边框和悬停效果,增强视觉交互。通过 transition 实现平滑动画。
.product-card {
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
transition: transform 0.3s ease;
}
.product-card:hover {
transform: translateY(-5px);
}
导航与按钮设计
导航栏使用固定定位(position: fixed)确保滚动时始终可见。按钮采用渐变背景和按压效果,提升点击感。
.navbar {
position: fixed;
width: 100%;
background: white;
z-index: 1000;
}
.primary-btn {
background: linear-gradient(to right, #ff7e5f, #feb47b);
padding: 10px 20px;
border: none;
color: white;
}
购物车交互样式
通过 CSS 实现购物车侧边栏滑入效果(transform: translateX)。商品数量标签使用绝对定位(position: absolute)显示在图标右上角。
.cart-sidebar {
position: fixed;
right: 0;
top: 0;
width: 300px;
height: 100%;
transform: translateX(100%);
transition: transform 0.3s ease;
}
.cart-open {
transform: translateX(0);
}
表单与反馈样式
输入框添加聚焦状态的高亮边框,错误提示用红色文本和图标辅助显示。加载动画通过 @keyframes 实现旋转效果。
input:focus {
border-color: #4a90e2;
outline: none;
}
.error-message {
color: #e74c3c;
font-size: 12px;
}
.loading-spinner {
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
注意事项
- 颜色方案遵循品牌一致性,主色和辅助色不超过 3 种。
- 字体选择确保可读性,正文优先使用无衬线字体(如
system-ui)。 - 性能优化:压缩 CSS 文件,避免过多复杂选择器嵌套。






