- 响应式设计:在手机、平板和电脑上都能完美显示。
- 现代美观的 UI:使用了流行的布局和配色。
- 核心功能模块:导航栏、轮播图、商品展示、购物车、页脚等。
- 交互功能:使用原生 JavaScript 实现了“添加到购物车”和“数量增减”功能。
- 清晰的代码结构:使用 HTML5 语义化标签,并附有详细注释,方便您理解和修改。
如何使用
- 复制代码:将下面的全部代码复制到一个新的文本文件中。
- 保存文件:将文件另存为
index.html。
- 在浏览器中打开:直接用 Chrome、Firefox 等现代浏览器打开这个
index.html 文件,即可看到效果。
电子商务网站模板 HTML 代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">优品商城 - 您的在线购物首选</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<style>
/* --- 全局样式和变量 --- */
:root {
--primary-color: #ff6b6b;
--secondary-color: #4ecdc4;
--dark-color: #2c3e50;
--light-color: #f4f4f4;
--text-color: #333;
--box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background-color: #f9f9f9;
color: var(--text-color);
line-height: 1.6;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
h1, h2, h3 {
color: var(--dark-color);
margin-bottom: 15px;
}
a {
text-decoration: none;
color: var(--dark-color);
}
ul {
list-style: none;
}
.btn {
display: inline-block;
background: var(--primary-color);
color: #fff;
padding: 12px 20px;
border: none;
cursor: pointer;
border-radius: 5px;
transition: background 0.3s ease;
}
.btn:hover {
background: #ff5252;
}
.btn-secondary {
background: var(--secondary-color);
}
.btn-secondary:hover {
background: #3dbcb4;
}
.btn-outline {
background: transparent;
border: 2px solid var(--primary-color);
color: var(--primary-color);
}
.btn-outline:hover {
background: var(--primary-color);
color: #fff;
}
/* --- 头部导航 --- */
.header {
background: #fff;
box-shadow: var(--box-shadow);
position: sticky;
top: 0;
z-index: 1000;
}
.header-main {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 0;
}
.logo {
font-size: 2rem;
font-weight: bold;
color: var(--primary-color);
}
.search-bar {
flex: 1;
max-width: 500px;
margin: 0 30px;
position: relative;
}
.search-bar input {
width: 100%;
padding: 10px 40px 10px 15px;
border: 1px solid #ddd;
border-radius: 25px;
outline: none;
}
.search-bar i {
position: absolute;
right: 15px;
top: 50%;
transform: translateY(-50%);
color: #999;
}
.header-actions {
display: flex;
align-items: center;
gap: 20px;
}
.header-actions a {
color: var(--dark-color);
font-size: 1.2rem;
position: relative;
}
.cart-count {
position: absolute;
top: -8px;
right: -8px;
background: var(--primary-color);
color: white;
font-size: 0.7rem;
width: 20px;
height: 20px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
/* --- 轮播图 --- */
.hero {
height: 400px;
background: url('https://images.unsplash.com/photo-1557821552-4b87b5e36e44?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80') no-repeat center center/cover;
position: relative;
margin-bottom: 40px;
}
.hero-content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
color: white;
}
.hero-content h1 {
font-size: 3rem;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
/* --- 商品展示区 --- */
.section {
padding: 60px 0;
}
.section-title {
text-align: center;
margin-bottom: 40px;
font-size: 2.5rem;
}
.products-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 30px;
}
.product-card {
background: #fff;
border-radius: 10px;
overflow: hidden;
box-shadow: var(--box-shadow);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.product-card:hover {
transform: translateY(-10px);
box-shadow: 0 8px 20px rgba(0,0,0,0.15);
}
.product-img {
height: 200px;
background-size: cover;
background-position: center;
}
.product-info {
padding: 20px;
}
.product-name {
font-size: 1.2rem;
margin-bottom: 10px;
}
.product-price {
font-size: 1.5rem;
font-weight: bold;
color: var(--primary-color);
margin-bottom: 15px;
}
.add-to-cart-btn {
width: 100%;
}
/* --- 购物车侧边栏 --- */
.cart-sidebar {
position: fixed;
top: 0;
right: -400px;
width: 400px;
height: 100vh;
background: #fff;
box-shadow: -5px 0 15px rgba(0,0,0,0.1);
transition: right 0.3s ease;
z-index: 1001;
display: flex;
flex-direction: column;
}
.cart-sidebar.active {
right: 0;
}
.cart-header {
padding: 20px;
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-between;
align-items: center;
}
.cart-header h2 {
font-size: 1.5rem;
}
.close-cart {
font-size: 1.5rem;
cursor: pointer;
color: #999;
}
.cart-items {
flex: 1;
overflow-y: auto;
padding: 20px;
}
.cart-item {
display: flex;
gap: 15px;
margin-bottom: 20px;
padding-bottom: 20px;
border-bottom: 1px solid #eee;
}
.cart-item-img {
width: 80px;
height: 80px;
background-size: cover;
background-position: center;
border-radius: 5