网页设计三合一案例教程:打造你的个人作品集网站
第一部分:项目概述与准备
项目目标 我们将创建一个单页个人作品集网站,包含以下部分:

(图片来源网络,侵删)
- 导航栏:固定在顶部,点击可平滑滚动到对应区域。
- 首屏英雄区:展示个人简介、头像和“了解更多”的号召性用语。
- 关于我:详细介绍个人背景和技能。
- 我的作品:以卡片形式展示项目,点击可弹出模态框查看详情。
- 联系方式:包含一个简单的联系表单和社交媒体链接。
技术栈
- HTML5: 定义网页的结构和内容。
- CSS3: 使用现代CSS特性(如 Flexbox, Grid, 变量)进行样式设计和美化。
- 原生JavaScript: 实现交互功能,无需依赖外部库。
开发环境
- 代码编辑器:推荐使用 Visual Studio Code (免费且强大)。
- 浏览器:推荐使用 Google Chrome 或 Microsoft Edge,它们有出色的开发者工具。
项目文件结构
在电脑上创建一个新文件夹,命名为 portfolio,并在其中创建以下文件和文件夹:
portfolio/
├── index.html # 网页的主结构
├── css/
│ └── style.css # 所有样式写在这里
├── js/
│ └── script.js # 所有交互逻辑写在这里
└── images/
├── profile.jpg # 你的头像
└── project-1.jpg # 项目图片
第二部分:HTML (骨架) - 搭建网页结构
打开 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>
<!-- 引入外部 CSS 文件 -->
<link rel="stylesheet" href="css/style.css">
<!-- 引入 Google Fonts (可选) -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
</head>
<body>
<!-- 导航栏 -->
<header class="navbar">
<div class="container">
<a href="#home" class="logo">张三</a>
<nav>
<a href="#home">首页</a>
<a href="#about">关于我</a>
<a href="#portfolio">作品</a>
<a href="#contact">联系</a>
</nav>
</div>
</header>
<main>
<!-- 首屏英雄区 -->
<section id="home" class="hero">
<div class="container">
<img src="images/profile.jpg" alt="我的头像" class="profile-img">
<h1>你好,我是 <span class="highlight">张三</span></h1>
<p class="subtitle">一名充满激情的前端开发工程师</p>
<a href="#about" class="cta-button">了解更多</a>
</div>
</section>
<!-- 关于我 -->
<section id="about" class="about">
<div class="container">
<h2>关于我</h2>
<p>我是一名专注于创建优雅、高效、用户友好界面的前端开发者,拥有5年的行业经验,熟练掌握 HTML, CSS, JavaScript 及其现代框架...</p>
</div>
</section>
<!-- 我的作品 -->
<section id="portfolio" class="portfolio">
<div class="container">
<h2>我的作品</h2>
<div class="portfolio-grid">
<div class="portfolio-item" data-title="项目一:电商平台" data-description="这是一个使用 React 和 Node.js 构建的现代化电商平台。">
<img src="images/project-1.jpg" alt="项目一">
<h3>电商平台</h3>
</div>
<div class="portfolio-item" data-title="项目二:任务管理应用" data-description="一个基于 Vue.js 的任务管理应用,支持拖拽排序和团队协作。">
<img src="images/project-2.jpg" alt="项目二">
<h3>任务管理应用</h3>
</div>
<div class="portfolio-item" data-title="项目三:个人博客" data-description="使用 Next.js 和 Tailwind CSS 搭建的个人博客系统,具有优秀的SEO性能。">
<img src="images/project-3.jpg" alt="项目三">
<h3>个人博客</h3>
</div>
</div>
</div>
</section>
<!-- 联系方式 -->
<section id="contact" class="contact">
<div class="container">
<h2>联系我</h2>
<form id="contact-form">
<input type="text" placeholder="你的名字" required>
<input type="email" placeholder="你的邮箱" required>
<textarea placeholder="你的留言" rows="5" required></textarea>
<button type="submit">发送消息</button>
</form>
</div>
</section>
</main>
<!-- 页脚 -->
<footer>
<p>© 2025 张三. All rights reserved.</p>
</footer>
<!-- 模态框 (默认隐藏) -->
<div id="modal" class="modal">
<div class="modal-content">
<span class="close-btn">×</span>
<h2 id="modal-title"></h2>
<p id="modal-description"></p>
</div>
</div>
<!-- 引入外部 JavaScript 文件 -->
<script src="js/script.js"></script>
</body>
</html>
代码解释:
<!DOCTYPE html>:声明文档类型。<head>:包含页面的元数据,如标题、字符集、引入的CSS等。<body>:包含所有可见的页面内容。- 语义化标签:我们使用了
<header>,<nav>,<main>,<section>,<footer>等语义化标签,这有助于SEO和代码可读性。 - 类名:如
navbar,hero,container等,这些是CSS用来选择和样式化元素的“钩子”。 id:如#home,#about,用于导航链接的锚点定位。- 模态框:一个
div,默认隐藏,当点击作品时会显示。
第三部分:CSS (外观) - 美化网页
打开 css/style.css 文件,让我们为这个骨架穿上漂亮的衣服。
/* --- 全局样式和变量 --- */
:root {
--primary-color: #3498db; /* 主色调 */
--secondary-color: #2c3e50; /* 次要色 */
--background-color: #f4f4f4;
--text-color: #333;
--white: #ffffff;
--font-family: 'Poppins', sans-serif;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
scroll-behavior: smooth; /* 平滑滚动 */
}
body {
font-family: var(--font-family);
line-height: 1.6;
color: var(--text-color);
background-color: var(--background-color);
}
.container {
max-width: 1100px;
margin: auto;
padding: 0 2rem;
}
h1, h2, h3 {
font-weight: 600;
line-height: 1.2;
}
section {
padding: 4rem 0;
}
/* --- 导航栏 --- */
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: var(--white);
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
z-index: 1000;
}
.navbar .container {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
}
.navbar .logo {
font-size: 1.5rem;
font-weight: bold;
color: var(--primary-color);
text-decoration: none;
}
.navbar nav a {
color: var(--text-color);
text-decoration: none;
margin-left: 2rem;
transition: color 0.3s ease;
}
.navbar nav a:hover {
color: var(--primary-color);
}
/* --- 首屏英雄区 --- */
.hero {
background: linear-gradient(rgba(0,0,0,0.7), rgba(0,0,0,0.7)), url('https://images.unsplash.com/photo-1557682257-2f9c37a3a5f3?ixlib=rb-4.0.3') no-repeat center center/cover;
color: var(--white);
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
.hero .profile-img {
width: 150px;
height: 150px;
border-radius: 50%;
border: 5px solid var(--primary-color);
margin-bottom: 1rem;
}
.hero h1 {
font-size: 3rem;
margin-bottom: 0.5rem;
}
.hero .highlight {
color: var(--primary-color);
}
.cta-button {
display: inline-block;
background: var(--primary-color);
color: var(--white);
padding: 0.8rem 2rem;
border-radius: 5px;
text-decoration: none;
margin-top: 1rem;
transition: background 0.3s ease;
}
.cta-button:hover {
background: #2980b9;
}
/* --- 关于我 --- */
.about {
background: var(--white);
text-align: center;
}
.about h2 {
font-size: 2.5rem;
margin-bottom: 1rem;
}
/* --- 我的作品 --- */
.portfolio {
text-align: center;
}
.portfolio h2 {
font-size: 2.5rem;
margin-bottom: 2rem;
}
.portfolio-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
}
.portfolio-item {
background: var(--white);
border: 1px solid #ddd;
border-radius: 5px;
overflow: hidden;
cursor: pointer;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.portfolio-item:hover {
transform: translateY(-10px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
.portfolio-item img {
width: 100%;
height: 200px;
object-fit: cover;
}
.portfolio-item h3 {
padding: 1rem;
}
/* --- 联系方式 --- */
.contact {
background: var(--white);
}
.contact h2 {
text-align: center;
font-size: 2.5rem;
margin-bottom: 2rem;
}
#contact-form {
max-width: 600px;
margin: 0 auto;
}
#contact-form input,
#contact-form textarea {
width: 100%;
padding: 1rem;
margin-bottom: 1rem;
border: 1px solid #ddd;
border-radius: 5px;
font-family: var(--font-family);
}
#contact-form button {
display: block;
width: 100%;
padding: 1rem;
background: var(--primary-color);
color: var(--white);
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1rem;
transition: background 0.3s ease;
}
#contact-form button:hover {
background: #2980b9;
}
/* --- 页脚 --- */
footer {
text-align: center;
padding: 1rem;
background: var(--secondary-color);
color: var(--white);
}
/* --- 模态框 --- */
.modal {
display: none; /* 默认隐藏 */
position: fixed;
z-index: 2000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.7);
justify-content: center;
align-items: center;
}
.modal-content {
background-color: var(--white);
padding: 2rem;
border-radius: 10px;
position: relative;
max-width: 500px;
width: 90%;
}
.close-btn {
position: absolute;
top: 10px;
right: 20px;
font-size: 2rem;
cursor: pointer;
color: var(--text-color);
}
CSS 核心技术点:
- CSS变量 (
root):定义全局颜色和字体,方便统一管理和修改。 - Flexbox:用于导航栏、英雄区等一维布局,实现元素的对齐和分布。
- CSS Grid:用于作品集网格 (
portfolio-grid),实现响应式的二维布局。 - 选择器:类选择器 (
.navbar)、ID选择器 (#home)、属性选择器 ([data-title]) 等。 - 伪类:
hover用于实现鼠标悬停效果。 - 响应式设计:使用
max-width和auto-fit等单位,让网站在不同屏幕尺寸下都能良好显示。 - 视觉层次:通过
padding,margin,font-size,box-shadow等创建清晰的视觉层次和空间感。
第四部分:JavaScript (交互) - 赋予网页生命
让我们打开 js/script.js,为网站添加动态行为。

(图片来源网络,侵删)
// 等待整个 HTML 文档加载完成后再执行脚本
document.addEventListener('DOMContentLoaded', function() {
// --- 1. 平滑滚动导航 ---
// 获取所有导航链接
const navLinks = document.querySelectorAll('nav a');
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
// 阻止默认的跳转行为(页面瞬间跳转)
e.preventDefault();
// 获取目标元素的 ID ( #home)
const targetId = this.getAttribute('href');
const targetSection = document.querySelector(targetId);
// 使用 scrollIntoView 方法进行平滑滚动
targetSection.scrollIntoView({
behavior: 'smooth'
});
});
});
// --- 2. 作品集模态框功能 ---
const portfolioItems = document.querySelectorAll('.portfolio-item');
const modal = document.getElementById('modal');
const modalTitle = document.getElementById('modal-title');
const modalDescription = document.getElementById('modal-description');
const closeBtn = document.querySelector('.close-btn');
// 为每个作品项添加点击事件
portfolioItems.forEach(item => {
item.addEventListener('click', function() {
// 从 data-* 属性中获取信息
const title = this.getAttribute('data-title');
const description = this.getAttribute('data-description');
// 将信息填充到模态框中
modalTitle.textContent = title;
modalDescription.textContent = description;
// 显示模态框
modal.style.display = 'flex';
});
});
// 点击关闭按钮隐藏模态框
closeBtn.addEventListener('click', function() {
modal.style.display = 'none';
});
// 点击模态框背景 (modal-content以外的区域) 也能关闭模态框
window.addEventListener('click', function(event) {
if (event.target == modal) {
modal.style.display = 'none';
}
});
// --- 3. 联系表单提交 (模拟) ---
const contactForm = document.getElementById('contact-form');
contactForm.addEventListener('submit', function(e) {
// 阻止表单默认的提交行为(页面刷新)
e.preventDefault();
// 这里可以添加实际的表单提交逻辑,例如发送到服务器
// 为了演示,我们只弹出一个提示框
alert('感谢你的留言!我会尽快回复你。');
// 清空表单
contactForm.reset();
});
});
JavaScript 核心技术点:
- 事件监听 (
addEventListener):监听用户的点击、滚动等行为。 - DOM操作:
querySelector()/querySelectorAll():选择页面上的元素。getAttribute()/setAttribute():获取和设置元素的属性。textContent:修改元素的文本内容。style.display:修改元素的CSS样式(如显示/隐藏)。
- 事件对象 (
event):e.preventDefault():阻止事件的默认行为(如链接跳转、表单提交)。e.target:获取触发事件的元素。
scrollIntoView():实现平滑滚动到指定元素。- 表单处理:获取表单数据、验证、提交(模拟)。
第五部分:整合与优化
- 添加图片:从网上下载一些合适的图片(从 Unsplash 找一些高质量的免费图片),或者用你自己的照片,放入
images文件夹,并确保index.html中的src路径正确。 - 预览网站:用浏览器打开
index.html文件,你应该能看到一个功能完整的作品集网站了。 - 测试:
- 点击导航栏,看是否能平滑滚动。
- 点击作品卡片,看模态框是否弹出并显示正确信息。
- 点击模态框的关闭按钮或背景,看是否能关闭。
- 尝试填写并提交联系表单。
- 响应式测试:调整浏览器窗口的大小,观察网站布局是否随之变化(CSS Grid 和 Flexbox 会自动处理)。
总结与进阶
恭喜!你已经通过这个案例,亲手实践了网页设计的三大核心技术:
- HTML 提供了结构和内容。
- CSS 负责美化、布局和呈现。
- JavaScript 增加了交互和动态行为。
进阶方向:
- CSS动画:为元素添加更复杂的进入和退出动画。
- 表单验证:在提交前用JS检查用户输入是否合法。
- API集成:使用
fetchAPI 从服务器获取真实数据来展示作品。 - 构建工具:学习使用 Webpack 或 Vite 等工具来优化和打包你的项目。
- 前端框架:尝试使用 React, Vue, 或 Angular 来构建更复杂的应用。
这个三合一案例为你打下了坚实的基础,接下来就是不断练习和探索,成为一名优秀的前端开发者!
