我将为你提供一个从简单到进阶的完整指南,包含 HTML 结构、CSS 样式 和 JavaScript 交互,并附上完整的代码和详细的解释。

(图片来源网络,侵删)
我们将创建一个现代、简洁且响应式的个人介绍网页,这个网页将包含以下部分:
- 导航栏: 方便访问页面内的不同部分。
- 主页/欢迎区: 包含你的姓名、照片和一句简短的个人介绍。
- 关于我: 更详细的自我介绍,包括你的性格、兴趣等。
- 技能/专长: 以进度条或图标形式展示你的技能。
- 项目经历: 展示你参与过的项目,包含项目名称、描述和链接。
- 联系方式: 提供你的邮箱、社交媒体链接等。
第一步:创建基本结构
我们需要一个 HTML 文件来定义网页的整体结构,我们将使用 HTML5 的语义化标签,这有助于搜索引擎更好地理解你的页面内容。
创建一个名为 index.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="style.css">
<!-- 引入字体图标库 (Font Awesome) -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
<!-- 导航栏 -->
<header id="navbar">
<nav class="container">
<a href="#home" class="logo">张三</a>
<ul class="nav-links">
<li><a href="#home">首页</a></li>
<li><a href="#about">关于我</a></li>
<li><a href="#skills">技能</a></li>
<li><a href="#projects">项目</a></li>
<li><a href="#contact">联系</a></li>
</ul>
</nav>
</header>
<!-- 主页/欢迎区 -->
<section id="home">
<div class="container">
<div class="home-content">
<img src="profile.jpg" alt="我的照片" class="profile-img">
<h1>你好,我是 <span class="highlight">张三</span></h1>
<p>一名热爱编程与设计的大学生</p>
<a href="#contact" class="btn">联系我</a>
</div>
</div>
</section>
<!-- 关于我 -->
<section id="about">
<div class="container">
<h2 class="section-title">关于我</h2>
<p>这里是关于我的详细介绍,我是一名计算机科学专业的学生,对前端开发和用户体验设计充满热情,我善于学习新知识,喜欢解决复杂的问题,并乐于与团队成员协作,在课余时间,我喜欢阅读技术博客、打篮球和探索新的美食。</p>
</div>
</section>
<!-- 技能 -->
<section id="skills">
<div class="container">
<h2 class="section-title">我的技能</h2>
<div class="skills-container">
<div class="skill">
<h3>HTML & CSS</h3>
<div class="progress-bar">
<div class="progress" style="width: 90%;"></div>
</div>
</div>
<div class="skill">
<h3>JavaScript</h3>
<div class="progress-bar">
<div class="progress" style="width: 75%;"></div>
</div>
</div>
<div class="skill">
<h3>React</h3>
<div class="progress-bar">
<div class="progress" style="width: 60%;"></div>
</div>
</div>
<div class="skill">
<h3>Python</h3>
<div class="progress-bar">
<div class="progress" style="width: 70%;"></div>
</div>
</div>
</div>
</div>
</section>
<!-- 项目经历 -->
<section id="projects">
<div class="container">
<h2 class="section-title">我的项目</h2>
<div class="projects-container">
<div class="project-card">
<h3>个人博客网站</h3>
<p>使用 React 和 Node.js 构建的个人博客,支持文章发布、评论和标签分类功能。</p>
<a href="#" class="btn btn-small">查看详情</a>
</div>
<div class="project-card">
<h3>在线待办事项应用</h3>
<p>一个基于 Vue.js 的响应式待办事项应用,支持任务的增删改查和数据持久化。</p>
<a href="#" class="btn btn-small">查看详情</a>
</div>
</div>
</div>
</section>
<!-- 联系方式 -->
<section id="contact">
<div class="container">
<h2 class="section-title">联系我</h2>
<div class="contact-container">
<p>如果你想和我交流或有任何合作意向,欢迎通过以下方式联系我:</p>
<div class="social-links">
<a href="mailto:your.email@example.com"><i class="fas fa-envelope"></i></a>
<a href="https://github.com/yourusername" target="_blank"><i class="fab fa-github"></i></a>
<a href="https://linkedin.com/in/yourusername" target="_blank"><i class="fab fa-linkedin"></i></a>
<a href="https://twitter.com/yourusername" target="_blank"><i class="fab fa-twitter"></i></a>
</div>
</div>
</div>
</section>
<!-- 页脚 -->
<footer>
<p>© 2025 张三. All rights reserved.</p>
</footer>
<!-- 引入 JavaScript 文件 -->
<script src="script.js"></script>
</body>
</html>
代码解释:

(图片来源网络,侵删)
<!DOCTYPE html>: 声明这是一个 HTML5 文档。<head>: 包含页面的元数据,如标题、字符编码、视口设置(用于响应式设计)以及引入外部 CSS 和 JavaScript 文件。<body>: 包含页面的所有可见内容。<header>: 定义网页或节的页眉,这里我们放导航栏。<nav>: 定义导航链接的部分。<section>: 定义文档中的节,如#home,#about等,每个section都有一个唯一的id,方便导航和定位。<div>: 一个通用的容器,用于组合其他 HTML 元素,我们用class来为它添加样式。<a>: 定义超链接,href属性指向目标地址。 代表页面内的锚点链接。<img>: 定义图片,src是图片路径,alt是替代文本,对 SEO 和无障碍访问很重要。<h1>到<h6>: 定义标题,<h1>是最重要的标题。<p>: 定义段落。<i>: 用于放置图标,我们通过 Font Awesome 类来指定图标。<link>和<script>: 分别用于引入外部样式表和脚本。
第二步:添加样式
我们的网页看起来还很朴素,我们用 CSS 来美化它。
创建一个名为 style.css 的文件,并将其放在与 index.html 相同的目录下,然后复制以下代码:
/* --- 全局样式和变量 --- */
:root {
--primary-color: #3498db;
--secondary-color: #2c3e50;
--text-color: #333;
--light-bg: #f4f4f4;
--white: #ffffff;
--font-family: 'Arial', sans-serif;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-family);
line-height: 1.6;
color: var(--text-color);
background-color: var(--white);
}
.container {
max-width: 1100px;
margin: 0 auto;
padding: 0 20px;
}
h2.section-title {
text-align: center;
font-size: 2.5rem;
margin-bottom: 2rem;
color: var(--secondary-color);
}
/* --- 导航栏 --- */
header {
background: var(--white);
position: fixed;
top: 0;
left: 0;
width: 100%;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
z-index: 1000;
}
header nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 0;
}
.logo {
font-size: 1.5rem;
font-weight: bold;
color: var(--primary-color);
text-decoration: none;
}
.nav-links {
display: flex;
list-style: none;
}
.nav-links li a {
color: var(--text-color);
text-decoration: none;
padding: 0.5rem 1rem;
transition: color 0.3s ease;
}
.nav-links li a:hover {
color: var(--primary-color);
}
/* --- 主页/欢迎区 --- */
#home {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url('hero-bg.jpg') no-repeat center center/cover;
color: var(--white);
padding-top: 60px; /* 为固定导航栏留出空间 */
}
.home-content {
max-width: 800px;
}
.profile-img {
width: 200px;
height: 200px;
border-radius: 50%;
border: 5px solid var(--white);
margin-bottom: 1rem;
}
.home-content h1 {
font-size: 3rem;
margin-bottom: 0.5rem;
}
.highlight {
color: var(--primary-color);
}
.btn {
display: inline-block;
background: var(--primary-color);
color: var(--white);
padding: 0.8rem 1.5rem;
border-radius: 5px;
text-decoration: none;
margin-top: 1rem;
transition: background 0.3s ease;
}
.btn:hover {
background: #2980b9;
}
.btn-small {
padding: 0.5rem 1rem;
font-size: 0.9rem;
}
/* --- 关于我 --- */
#about {
padding: 4rem 0;
background-color: var(--light-bg);
text-align: center;
}
#about p {
max-width: 800px;
margin: 0 auto;
font-size: 1.1rem;
}
/* --- 技能 --- */
#skills {
padding: 4rem 0;
text-align: center;
}
.skills-container {
max-width: 800px;
margin: 0 auto;
}
.skill {
margin-bottom: 1.5rem;
text-align: left;
}
.skill h3 {
margin-bottom: 0.5rem;
}
.progress-bar {
background: #ddd;
height: 10px;
border-radius: 5px;
overflow: hidden;
}
.progress {
height: 100%;
background: var(--primary-color);
width: 0; /* 初始宽度为0,用于JS动画 */
transition: width 1.5s ease-in-out;
}
/* --- 项目 --- */
#projects {
padding: 4rem 0;
background-color: var(--light-bg);
}
.projects-container {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
gap: 2rem;
}
.project-card {
background: var(--white);
padding: 1.5rem;
border-radius: 8px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
flex: 1;
min-width: 300px;
max-width: 400px;
transition: transform 0.3s ease;
}
.project-card:hover {
transform: translateY(-10px);
}
/* --- 联系方式 --- */
#contact {
padding: 4rem 0;
text-align: center;
}
.social-links {
margin-top: 2rem;
}
.social-links a {
color: var(--text-color);
font-size: 1.5rem;
margin: 0 1rem;
transition: color 0.3s ease;
}
.social-links a:hover {
color: var(--primary-color);
}
/* --- 页脚 --- */
footer {
background: var(--secondary-color);
color: var(--white);
text-align: center;
padding: 1rem 0;
}
/* --- 响应式设计 --- */
@media (max-width: 768px) {
header nav {
flex-direction: column;
}
.nav-links {
margin-top: 1rem;
}
.home-content h1 {
font-size: 2rem;
}
.projects-container {
flex-direction: column;
align-items: center;
}
}
代码解释:
root和var(): 定义 CSS 变量,方便统一管理颜色、字体等主题。box-sizing: border-box: 让padding和border不会增加元素的宽高,是现代布局的基石。display: flex: 强大的布局工具,用于在一维(行或列)上排列元素,我们用它来创建导航栏、项目卡片布局等。position: fixed: 让导航栏固定在页面顶部,滚动时不会消失。background: linear-gradient(...): 创建渐变背景,并叠加一张背景图片,使欢迎区更具视觉冲击力。border-radius: 用于创建圆角,如头像和按钮。transition: 为元素的状态变化(如颜色、宽度、transform)添加平滑的动画效果。@media (max-width: 768px): 媒体查询,当屏幕宽度小于 768px 时,应用内部的 CSS 规则,实现移动端适配。
第三步:添加交互效果
为了让网页更生动,我们可以添加一些 JavaScript 交互,比如平滑滚动和技能进度条动画。
创建一个名为 script.js 的文件,并复制以下代码:
// 等待整个文档加载完毕后执行
document.addEventListener('DOMContentLoaded', () => {
// --- 平滑滚动 ---
// 获取所有导航链接
const navLinks = document.querySelectorAll('.nav-links a[href^="#"]');
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault(); // 阻止默认的跳转行为
const targetId = this.getAttribute('href').substring(1); // 获取目标id (去掉#)
const targetSection = document.getElementById(targetId);
if (targetSection) {
// 计算目标位置,考虑固定导航栏的高度
const navHeight = document.getElementById('navbar').offsetHeight;
const targetPosition = targetSection.offsetTop - navHeight;
// 使用 scrollIntoView 实现平滑滚动
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
}
});
});
// --- 技能进度条动画 ---
const skillsSection = document.getElementById('skills');
const progressBars = document.querySelectorAll('.progress');
// 创建一个观察器,用于检测元素是否进入视口
const observerOptions = {
root: null, // 相对于视口
rootMargin: '0px',
threshold: 0.5 // 当50%的元素可见时触发
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// 当技能区域进入视口时,为每个进度条设置其CSS中定义的宽度
progressBars.forEach(bar => {
const width = bar.style.width;
bar.style.width = '0%'; // 先重置为0
// 使用 setTimeout 确保重置后动画能再次触发
setTimeout(() => {
bar.style.width = width;
}, 100);
});
// 动画播放一次后,停止观察该元素,避免重复触发
observer.unobserve(entry.target);
}
});
}, observerOptions);
// 开始观察技能区域
if (skillsSection) {
observer.observe(skillsSection);
}
});
代码解释:
DOMContentLoaded: 事件在初始 HTML 文档被完全加载和解析完成后触发,确保我们的脚本在页面元素准备好之后才运行。- 平滑滚动:
- 我们监听所有导航链接的点击事件。
e.preventDefault()阻止了<a href="#">默认的瞬间跳转行为。window.scrollTo()配合behavior: 'smooth'实现平滑滚动效果。
- 技能进度条动画:
- 我们使用 Intersection Observer API,这是一个现代的、高性能的 API,用于检测一个元素是否进入或离开另一个元素(或视口)的可见区域。
- 当用户滚动到“技能”部分时,观察器会触发。
- 我们将每个进度条的宽度从
0%动画到在 CSS 中预设的宽度,创造出一种填充的视觉效果。 - 动画只播放一次,然后停止观察,以优化性能。
第四步:准备素材和最终部署
-
个人照片:
- 在你的
index.html文件中,找到<img src="profile.jpg" ...>这一行。 - 准备一张你自己的照片(最好是正方形,500x500 像素),并将其命名为
profile.jpg。 - 将这张
profile.jpg文件和你的index.html,style.css,script.js文件放在同一个文件夹里。
- 在你的
-
背景图片 (可选):
- 如果你想要欢迎区的背景图,可以准备一张高质量的图片,命名为
hero-bg.jpg,并和网页文件放在一起。
- 如果你想要欢迎区的背景图,可以准备一张高质量的图片,命名为
-
如何查看你的网页:
- 最简单的方法是直接用浏览器打开
index.html文件,你可以直接在文件管理器中找到它,然后用 Chrome、Firefox 或 Edge 等浏览器打开。 - 注意:直接打开时,某些功能(如涉及
file://协议的)可能会受限,但对于这个静态网页来说完全足够。
- 最简单的方法是直接用浏览器打开
进阶建议
当你熟悉了以上步骤后,可以尝试以下进阶功能:
- 更复杂的项目展示: 使用轮播图来展示多个项目。
- 表单联系: 在“联系我”部分添加一个简单的表单,让访客可以直接给你发消息(这需要后端支持,如 PHP, Node.js 等)。
- 暗黑模式: 添加一个切换按钮,让用户可以在亮色和暗色主题之间切换。
- 动态加载: 使用 JavaScript 动态加载项目数据,方便你未来更新内容而无需修改 HTML。
这个项目是学习前端开发的绝佳起点,祝你制作顺利!
