零基础HTML5前端开发课程完整入门指南:从零开始学习网页制作,掌握HTML5和CSS基础,跟着实战项目一步步做出自己的第一个网站
嘿,朋友!你好呀~
我是Agnes,今天我们要一起踏上这段有趣的旅程:从零开始,做出属于自己的第一个网站。无论你是学生、转行者、还是单纯对网页世界充满好奇,这篇指南都会像一位耐心陪伴你的老朋友,带你一步步走进HTML5和CSS的奇妙世界。
别担心”零基础”这个词让你退缩——相信我,前端开发其实比你想象的更亲切、更有趣,而且一旦入门,你会发现自己打开了一扇新世界的大门。
第一章:什么是网页?它是怎么被”制造”出来的?
在动手之前,我们先聊点基础的,帮你建立一个清晰的认知框架。
想象一下:网页就像一栋房子,而你即将成为这栋房子的建筑师和装修师。HTML是房子的骨架——它定义了哪里是房间、哪里是门、哪里是窗户。CSS则是装修风格——决定了墙壁的颜色、家具的摆放、灯光的氛围。而浏览器,就是那个最终”欣赏”这栋房子的住客。
网页的三大基石
前端开发的世界主要由三个核心语言构成,它们的关系就像是:
| 语言 | 作用 | 类比 |
|---|---|---|
| HTML | 网页的结构和内容 | 房子的钢筋水泥框架 |
| CSS | 网页的外观和样式 | 装修、配色、家具布局 |
| JavaScript | 网页的交互和动态效果 | 智能家居系统,让房子”活”起来 |
本章我们专注于前两者——HTML5和CSS。JavaScript会作为补充偶尔出现,让你在了解全貌的同时,保持对未来的期待。
浏览器是怎么工作的?
当你输入一个网址并按回车时,实际上发生了一系列有趣的事情:
- 你的浏览器向服务器发送请求,说:”嘿,给我这个网页!”
- 服务器把HTML文件、CSS文件、图片等一堆数据打包发送回来
- 浏览器拿到这些文件,开始”阅读”HTML,理解结构;然后”穿上”CSS,让网页变漂亮;最后渲染出你看到的一切
理解这个过程很重要,因为它让你明白:你写的每一行代码,最终都会在浏览器里变成可视化的东西。这种”所见即所得”的反馈,正是前端开发最迷人的地方。
第二章:工欲善其事,必先利其器
开始之前,你需要准备几样工具。好消息是,它们都是免费的,而且非常友好。
2.1 选择一个代码编辑器
代码编辑器是你写代码的地方,选一个顺手的非常重要。对于新手,我强烈推荐以下几个选择:
Visual Studio Code(简称VS Code) —— 这是目前最受欢迎的代码编辑器,由微软开发,完全免费。
为什么我首推它?
- 界面简洁,对新手非常友好
- 有丰富的插件生态,安装几个插件后体验直接起飞
- 自动补全功能帮你少写很多错别字
- 内置终端,不用切换窗口
下载方式:访问 https://code.visualstudio.com/,根据你的操作系统下载安装即可。
安装完成后,打开VS Code,你会看到一个这样的界面:
┌─────────────────────────────────────────────────┐
│ 文件(F) 编辑(E) 视图(V) 跳转(J) ... 🔍 │
├──────────────────┬──────────────────────────────┤
│ 📁 项目文件夹 │ │
│ 📄 index.html │ // 这里是代码编辑区 │
│ 📄 style.css │ 你可以开始写代码了! │
│ 📁 images/ │ │
│ 📁 js/ │ │
├──────────────────┴──────────────────────────────┤
│ 行号 状态栏显示当前文件编码、语言模式等 │
└─────────────────────────────────────────────────┘
2.2 安装必要的插件
在VS Code中,点击左侧活动栏的扩展图标(看起来像四个小方块),搜索并安装以下插件:
| 插件名称 | 作用 | 推荐理由 |
|---|---|---|
| Live Server | 实时预览网页 | 保存文件后自动刷新浏览器,超实用! |
| HTML CSS Support | HTML智能提示 | 让写HTML和CSS更顺畅 |
| Auto Rename Tag | 自动重命名标签 | 改开始标签时自动改结束标签 |
| Prettier | 代码格式化 | 自动整理代码格式,告别混乱 |
| Error Lens | 错误提示增强 | 直接在代码行上显示错误信息 |
安装完Live Server后,右键点击HTML文件,选择”Open with Live Server”,浏览器会自动打开并实时显示你的网页。修改代码保存后,浏览器会自动刷新,不需要手动按F5。这个体验一旦习惯,你就再也回不去了。
2.3 认识浏览器开发者工具
Chrome、Edge、Firefox等主流浏览器都内置了强大的开发者工具,它是你学习过程中最重要的”伙伴”之一。
打开方式:在网页上右键点击任意元素,选择”检查”(Inspect),或者按快捷键 F12 或 Ctrl+Shift+I(Mac是 Cmd+Option+I)。
开发者工具主要有几个面板:
- Elements:查看和修改页面的HTML结构和CSS样式
- Console:查看JavaScript错误和输出信息
- Network:查看页面加载了哪些资源,请求花了多少时间
- Sources:查看和调试源代码
- Performance:性能分析
学会使用开发者工具,意味着你可以”逆向工程”任何你喜欢的网页——查看别人是怎么写代码的,这是进步最快的学习方式之一。
第三章:HTML5入门——搭建网页的骨架
好,工具准备就绪,让我们开始写代码了!
3.1 HTML是什么?
HTML的全称是HyperText Markup Language(超文本标记语言)。它不是编程语言,而是一种标记语言——用标签来告诉浏览器”这段内容是标题”、”这段内容是段落”、”这是一张图片”等等。
HTML文件以.html为扩展名,你可以用任何文本编辑器创建它,但VS Code会让整个过程轻松很多。
3.2 第一个HTML文件
在VS Code中,新建一个文件,命名为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>我的第一个网站</title>
</head>
<body>
<h1>你好,世界!</h1>
<p>这是我的第一个网页,我做到了!</p>
</body>
</html>
保存后,用Live Server打开,你会在浏览器中看到:
╔══════════════════════════════════╗
║ 浏览器窗口 ║
║ ║
║ 你好,世界! ← 大标题 ║
║ 这是我的第一个网页,我做到了! ← 段落文字 ║
║ ║
╚══════════════════════════════════╝
太棒了!你已经创建了第一个网页。现在让我带你逐行拆解,看看每一部分是什么意思。
逐行解析
<!DOCTYPE html>
这行告诉浏览器:”我是一个HTML5文档”。虽然看起来简单,但它是必须的——没有它,浏览器可能进入”怪异模式”,导致页面渲染出现问题。
<html lang="zh-CN">
<html>标签是整个文档的根元素,所有HTML内容都包含在它里面。lang="zh-CN"表示页面语言是中文,这对搜索引擎和屏幕阅读器都很重要。
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的第一个网站</title>
</head>
<head>部分包含的是元数据——网页的”幕后信息”,不会直接显示在页面上。
<meta charset="UTF-8">:设置字符编码为UTF-8,确保中文能正确显示<meta name="viewport" ...>:设置视口,让网页在手机上也显示正常(响应式设计的起点)<title>:网页标题,会显示在浏览器标签页上
<body>
<h1>你好,世界!</h1>
<p>这是我的第一个网页,我做到了!</p>
</body>
<body>部分包含的是实际内容——用户在浏览器中看到的一切。
<h1>:一级标题,页面中最重要的标题<p>:段落标签,用于包裹文本内容
3.3 HTML标签的语法
HTML由标签(tag)构成。大多数标签成对出现:
<标签名 属性="值">内容</标签名>
- 开始标签:
<标签名>,告诉浏览器”从这里开始” - 结束标签:
</标签名>,斜杠表示”结束” - 内容:标签之间的部分
- 属性:提供额外的信息,如
class、id、href等
有一些标签是自闭合的,不需要结束标签,例如:
<img src="photo.jpg" alt="一张照片">
<br> <!-- 换行 -->
<hr> <!-- 水平线 -->
<input type="text">
3.4 常用的HTML标签
掌握这些标签,你就能构建绝大多数网页的基本结构:
标题标签(h1~h6)
<h1>一级标题(最大、最重要)</h1>
<h2>二级标题</h2>
<h3>三级标题</h3>
<h4>四级标题</h4>
<h5>五级标题</h5>
<h6>六级标题(最小)</h6>
<h1>应该在整个页面中只出现一次,作为页面主题。标题的语义化对SEO(搜索引擎优化)和可访问性都很重要。
段落和文本标签
<p>这是一个段落。段落之间会自动有空隙。</p>
<p>这是第一段。</p>
<p>这是第二段。</p>
<strong>这段文字会被加粗,且强调语义</strong>
<em>这段文字会被斜体,且强调语义</em>
<span>行内容器,用于包裹小段内容</span>
<br> <!-- 换行,但不产生段落间距 -->
<a href="https://www.google.com">点击我去Google</a>
列表标签
<!-- 无序列表(项目符号) -->
<ul>
<li>苹果</li>
<li>香蕉</li>
<li>橙子</li>
</ul>
<!-- 有序列表(数字编号) -->
<ol>
<li>第一步:打开VS Code</li>
<li>第二步:新建文件</li>
<li>第三步:开始写代码</li>
</ol>
<!-- 定义列表 -->
<dl>
<dt>HTML</dt>
<dd>超文本标记语言,用于构建网页结构</dd>
<dt>CSS</dt>
<dd>层叠样式表,用于美化网页外观</dd>
</dl>
图片标签
<img src="images/cat.jpg" alt="一只可爱的猫咪" width="300" height="200">
src:图片路径,可以是本地路径或网络URLalt:替代文本,图片加载失败时显示,对SEO和屏幕阅读器至关重要width/height:图片尺寸(建议同时设置,避免页面布局抖动)
链接标签
<!-- 链接到另一个网页 -->
<a href="https://www.baidu.com" target="_blank">在新标签页打开百度</a>
<!-- 链接到页面内的某个位置 -->
<a href="#section2">跳转到第二部分</a>
<!-- 链接到邮件 -->
<a href="mailto:example@email.com">发邮件给我</a>
<!-- 链接到电话 -->
<a href="tel:+8613800138000">拨打电话</a>
表格标签
<table>
<caption>学生成绩表</caption>
<thead>
<tr>
<th>姓名</th>
<th>数学</th>
<th>英语</th>
<th>总分</th>
</tr>
</thead>
<tbody>
<tr>
<td>小明</td>
<td>95</td>
<td>88</td>
<td>183</td>
</tr>
<tr>
<td>小红</td>
<td>92</td>
<td>96</td>
<td>188</td>
</tr>
</tbody>
</table>
表单标签(前端开发的重要部分)
<form action="/submit" method="POST">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" placeholder="请输入用户名">
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" placeholder="example@email.com">
<label for="password">密码:</label>
<input type="password" id="password" name="password">
<label for="gender">性别:</label>
<select id="gender" name="gender">
<option value="male">男</option>
<option value="female">女</option>
<option value="other">其他</option>
</select>
<label>
<input type="checkbox" name="agree" value="yes"> 我同意服务条款
</label>
<label>
<input type="radio" name="plan" value="free"> 免费套餐
</label>
<label>
<input type="radio" name="plan" value="premium"> 高级套餐
</label>
<label for="bio">个人简介:</label>
<textarea id="bio" name="bio" rows="4" placeholder="介绍一下你自己..."></textarea>
<button type="submit">提交</button>
<button type="reset">重置</button>
</form>
语义化标签(HTML5的新特性)
HTML5引入了很多语义化标签,让代码更有可读性,也对SEO更友好:
<header>
<nav>
<ul>
<li><a href="/">首页</a></li>
<li><a href="/about">关于</a></li>
<li><a href="/contact">联系</a></li>
</ul>
</nav>
</header>
<main>
<article>
<section>
<h2>文章标题</h2>
<p>文章内容...</p>
</section>
<section id="comments">
<h3>评论</h3>
<p>评论区内容...</p>
</section>
</article>
<aside>
<h3>相关阅读</h3>
<ul>
<li><a href="#">相关教程1</a></li>
<li><a href="#">相关教程2</a></li>
</ul>
</aside>
</main>
<footer>
<p>© 2024 我的网站. 保留所有权利.</p>
</footer>
这些语义化标签不只是”好看”,它们告诉浏览器和搜索引擎”这里是页头”、”这里是主要内容”、”这里是侧边栏”、”这里是页脚”,有助于构建更结构化的网页。
3.5 HTML注释
注释不会在页面中显示,但对你和其他开发者很有用:
<!-- 这是一个单行注释 -->
<!--
这是一个多行注释
可以用来写大段的说明
或者临时屏蔽某段代码
-->
第四章:CSS入门——让网页变漂亮的魔法
HTML搭建好了骨架,现在轮到CSS来赋予它生命和美感了。
4.1 CSS是什么?
CSS的全称是Cascading Style Sheets(层叠样式表)。它的作用是控制HTML元素的外观表现——颜色、字体、大小、间距、布局、动画等。
CSS有三种引入方式:
<!-- 方式1:行内样式(不推荐,优先级最高但最难维护) -->
<p style="color: red; font-size: 18px;">红色文字</p>
<!-- 方式2:内部样式表(写在<head>的<style>标签内) -->
<head>
<style>
p { color: red; font-size: 18px; }
</style>
</head>
<!-- 方式3:外部样式表(推荐!写在独立的.css文件中) -->
<head>
<link rel="stylesheet" href="style.css">
</head>
我强烈建议使用方式3——外部样式表。它让结构和样式分离,代码更整洁,也更容易维护。
4.2 CSS选择器
选择器决定了”哪些元素需要被样式化”。
基础选择器
/* 元素选择器:选中所有p标签 */
p {
color: #333;
font-size: 16px;
}
/* 类选择器:选中所有class="highlight"的元素 */
.highlight {
background-color: #fff3cd;
padding: 10px;
}
/* id选择器:选中id="header"的元素 */
#header {
background-color: #2c3e50;
color: white;
}
/* 通配符选择器:选中所有元素(慎用,性能影响) */
* {
margin: 0;
padding: 0;
}
组合选择器
/* 后代选择器:选中div内的所有p */
div p {
color: blue;
}
/* 子元素选择器:只选中div的直接子元素p */
div > p {
color: green;
}
/* 相邻兄弟选择器:选中h1后面紧邻的p */
h1 + p {
font-weight: bold;
}
/* 通用兄弟选择器:选中h1后面所有的p */
h1 ~ p {
color: gray;
}
属性选择器
/* 选中所有有href属性的a标签 */
a[href] {
text-decoration: none;
}
/* 选中href以"https://"开头的链接 */
a[href^="https://"] {
color: green;
}
/* 选中href以".jpg"结尾的图片链接 */
a[href$=".jpg"] {
border: 2px solid blue;
}
/* 选中title属性包含"warning"的元素 */
[title*="warning"] {
color: orange;
}
伪类和伪元素
/* 伪类:元素的不同状态 */
a:hover { /* 鼠标悬停时 */
color: red;
}
li:first-child { /* 第一个列表项 */
font-weight: bold;
}
li:last-child { /* 最后一个列表项 */
border-bottom: none;
}
input:focus { /* 输入框获得焦点时 */
border-color: blue;
outline: none;
}
/* 伪元素:创建虚拟元素 */
p::first-line { /* 段落的第一行 */
font-weight: bold;
font-size: 1.2em;
}
p::before { /* 在每个p前面插入内容 */
content: "📌 ";
}
p::after { /* 在每个p后面插入内容 */
content: " 🔗";
}
/* 常见的伪元素 */
::selection { /* 用户选中的文字 */
background: #4a90d9;
color: white;
}
4.3 常用的CSS属性
文本样式
.text-example {
/* 颜色 */
color: #333333;
color: rgb(51, 51, 51);
color: rgba(51, 51, 51, 0.8); /* 带透明度 */
color: hsl(0, 0%, 20%);
/* 字体 */
font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
font-size: 16px;
font-size: 1rem; /* 1rem = 父元素的font-size */
font-weight: normal; /* normal | bold | 100~900 */
font-style: italic; /* italic | normal */
/* 文本对齐 */
text-align: left; /* left | center | right | justify */
/* 行高 */
line-height: 1.6; /* 推荐用倍数,自适应字体大小 */
/* 字间距 */
letter-spacing: 1px;
/* 文本装饰 */
text-decoration: none; /* none | underline | line-through */
/* 文本转换 */
text-transform: uppercase; /* uppercase | lowercase | capitalize */
}
盒模型(Box Model)—— 最重要的概念之一
每个HTML元素都是一个”盒子”,理解盒模型是掌握CSS布局的基础:
┌─────────────────────────────────────┐
│ margin (外边距) │
│ ┌───────────────────────────────┐ │
│ │ border (边框) │ │
│ │ ┌─────────────────────────┐ │ │
│ │ │ padding (内边距) │ │ │
│ │ │ ┌───────────────────┐ │ │ │
│ │ │ │ content (内容) │ │ │ │
│ │ │ │ 这里是内容 │ │ │ │
│ │ │ │ 这里是内容 │ │ │ │
│ │ │ └───────────────────┘ │ │ │
│ │ └─────────────────────────┘ │ │
│ └───────────────────────────────┘ │
└─────────────────────────────────────┘
.box {
/* 宽度 */
width: 300px;
/* 高度 */
height: 200px;
/* 内边距:上 右 下 左 */
padding: 20px 30px 20px 30px;
/* 也可以分别设置 */
padding-top: 20px;
padding-right: 30px;
padding-bottom: 20px;
padding-left: 30px;
/* 外边距:上 右 下 左 */
margin: 10px 20px 10px 20px;
/* 边框 */
border: 1px solid #ddd;
border-width: 2px;
border-style: solid;
border-color: #333;
/* 单边边框 */
border-top: 2px dashed red;
/* 圆角 */
border-radius: 8px;
border-radius: 50%; /* 圆形 */
/* 盒模型尺寸计算方式 */
box-sizing: content-box; /* 默认:width不包含padding和border */
box-sizing: border-box; /* 推荐:width包含padding和border */
}
💡 重要提示:强烈建议在项目开始时加上这个全局重置:
> *, *::before, *::after { > box-sizing: border-box; > } > ``` > > 这样你在设置宽度时就不用操心padding和border会不会撑破容器了。 #### 背景样式 ```css .background { /* 背景颜色 */ background-color: #f5f5f5; /* 背景图片 */ background-image: url('images/background.jpg'); background-size: cover; /* 覆盖整个容器,保持比例 */ background-position: center; /* 图片位置 */ background-repeat: no-repeat; /* 不重复 */ background-attachment: fixed; /* 固定背景,滚动时不动 */ /* 简写方式 */ background: #f5f5f5 url('images/bg.jpg') center / cover no-repeat fixed; /* 渐变背景 */ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); background: radial-gradient(circle, #fff 0%, #eee 100%); }
4.4 布局系统
这是CSS中最核心、也是最有趣的部分。现代CSS提供了多种布局方式:
4.4.1 display属性
.block-element {
display: block; /* 块级元素:独占一行,可设置宽高 */
/* 例如:div, p, h1~h6, section, article 等 */
}
.inline-element {
display: inline; /* 行内元素:不独占一行,不能设置宽高 */
/* 例如:span, a, strong, em 等 */
}
.inline-block-element {
display: inline-block; /* 行内块:不独占一行,但可以设置宽高 */
}
.flex-element {
display: flex; /* 弹性布局 */
}
.grid-element {
display: grid; /* 网格布局 */
}
/* 隐藏元素 */
.hidden {
display: none; /* 完全隐藏,不占空间 */
}
.visible-hidden {
visibility: hidden; /* 隐藏,但仍占空间 */
}
4.4.2 Flexbox布局(弹性布局)
Flexbox是处理一维布局(行或列)的最佳方案。
/* 父容器设置 */
.flex-container {
display: flex;
flex-direction: row; /* 主轴方向:row(水平) | column(垂直) */
flex-wrap: wrap; /* 允许换行 */
justify-content: center; /* 主轴对齐:flex-start | flex-end | center | space-between | space-around | space-evenly */
align-items: center; /* 交叉轴对齐:flex-start | flex-end | center | stretch | baseline */
gap: 20px; /* 元素之间的间距 */
}
/* 子元素设置 */
.flex-item {
flex: 1; /* 等分剩余空间 */
flex: 0 0 200px; /* 不放大、不缩小、固定宽度200px */
flex: 1 1 300px; /* 可以放大缩小,基准宽度300px */
order: 2; /* 改变排列顺序(默认是1) */
align-self: flex-end; /* 单独控制此元素的对齐方式 */
}
Flexbox实战示例——居中一个元素(经典需求)
.center-everything {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
min-height: 100vh; /* 占满整屏高度 */
}
这就做到了传说中的”垂直水平居中”,在Flexbox出现之前,这可是前端开发者的噩梦。
导航栏实战
<nav class="navbar">
<div class="logo">我的网站</div>
<ul class="nav-links">
<li><a href="#">首页</a></li>
<li><a href="#">关于</a></li>
<li><a href="#">作品</a></li>
<li><a href="#">联系</a></li>
</ul>
</nav>
.navbar {
display: flex;
justify-content: space-between; /* logo在左,链接在右 */
align-items: center;
padding: 15px 30px;
background-color: #2c3e50;
}
.nav-links {
display: flex;
list-style: none;
gap: 30px;
margin: 0;
padding: 0;
}
.nav-links a {
color: white;
text-decoration: none;
font-size: 16px;
transition: color 0.3s ease;
}
.nav-links a:hover {
color: #3498db;
}
4.4.3 Grid布局(网格布局)
Grid是处理二维布局(行和列)的终极武器。
.grid-container {
display: grid;
/* 定义列:3列,每列最小200px,最大1fr(等分剩余空间) */
grid-template-columns: 1fr 1fr 1fr;
/* 定义行:每行高度200px */
grid-template-rows: 200px 200px;
/* 元素间距 */
gap: 20px;
/* 网格区域的命名(可选,用于精确定位) */
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
}
/* 使用命名区域放置元素 */
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
响应式网格
/* 自动适应:每列最小300px,最多能放几列放几列 */
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
/* 配合媒体查询,实现更精细的控制 */
.card-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 24px;
}
@media (max-width: 992px) {
.card-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: 576px) {
.card-grid {
grid-template-columns: 1fr;
}
}
4.4.4 定位(Positioning)
.relative {
position: relative; /* 相对定位:相对于自己原来的位置偏移 */
top: 10px;
left: 20px;
}
.absolute {
position: absolute; /* 绝对定位:相对于最近的定位祖先元素 */
top: 0;
right: 0;
}
.fixed {
position: fixed; /* 固定定位:相对于浏览器窗口 */
bottom: 20px;
right: 20px;
}
.sticky {
position: sticky; /* 粘性定位:滚动到指定位置时"粘"住 */
top: 0;
}
经典场景:图片右上角的角标
<div class="badge-container">
<img src="photo.jpg" alt="照片">
<span class="badge">NEW</span>
</div>
.badge-container {
position: relative; /* 作为绝对定位的参照 */
display: inline-block;
}
.badge {
position: absolute;
top: -8px;
right: -8px;
background: #e74c3c;
color: white;
padding: 4px 8px;
border-radius: 12px;
font-size: 12px;
font-weight: bold;
}
4.5 CSS过渡和动画
让你的网页动起来,增加交互的趣味性。
过渡(Transition)
.button {
background-color: #3498db;
color: white;
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
/* 过渡:属性变化时,花0.3秒平滑过渡 */
transition: all 0.3s ease;
}
.button:hover {
background-color: #2980b9;
transform: translateY(-2px); /* 向上移动2px */
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
关键帧动画(@keyframes)
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.animated-card {
animation: fadeIn 0.6s ease-out forwards;
}
.spinner {
animation: spin 1s linear infinite;
}
.pulse-dot {
animation: pulse 1.5s ease-in-out infinite;
}
第五章:实战项目——打造你的第一个网站
光说不练假把式。现在,让我们把学到的知识串起来,做一个真正的项目。
项目目标
我们要制作一个个人简介网站,包含以下页面和元素:
- 导航栏
- 英雄区域(Hero Section)—— 首页的大图+自我介绍
- 关于我部分
- 技能展示
- 作品/项目展示
- 联系表单
- 页脚
5.1 项目结构
先在电脑上创建一个文件夹,比如my-website,然后在里面创建:
my-website/
├── index.html ← 主页面
├── style.css ← 样式文件
└── images/ ← 图片文件夹
├── hero.jpg
├── avatar.jpg
└── project1.jpg
5.2 完整HTML代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>张三的个人网站</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- 导航栏 -->
<header class="header">
<nav class="navbar">
<a href="#" 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>
<button class="menu-toggle" aria-label="菜单">
<span></span>
<span></span>
<span></span>
</button>
</nav>
</header>
<!-- 英雄区域 -->
<section id="home" class="hero">
<div class="hero-content">
<h1>你好,我是<span class="highlight">张三</span></h1>
<p class="hero-subtitle">前端开发者 / UI设计爱好者 / 终身学习者</p>
<p class="hero-desc">
我热爱用代码创造美好的用户体验。
从一行HTML开始,我将带领你走进前端开发的世界。
</p>
<div class="hero-buttons">
<a href="#projects" class="btn btn-primary">查看作品</a>
<a href="#contact" class="btn btn-secondary">联系我</a>
</div>
</div>
<div class="hero-image">
<img src="images/avatar.jpg" alt="张三的头像">
</div>
</section>
<!-- 关于我 -->
<section id="about" class="about">
<div class="container">
<h2 class="section-title">关于我</h2>
<div class="about-content">
<div class="about-text">
<p>
你好!我是一名刚刚入门的前端开发者。虽然我的经验还在积累中,
但我对学习的渴望和对细节的追求从未改变。
</p>
<p>
我喜欢研究新的技术,也喜欢把复杂的问题用简单的方式表达出来。
我相信好的代码不仅要能工作,还要让别人也能读懂。
</p>
<p>
工作之余,我喜欢摄影、阅读和探索新的咖啡店的秘密。
如果你对这些也有兴趣,欢迎随时联系我聊聊!
</p>
</div>
<div class="about-stats">
<div class="stat-item">
<span class="stat-number">10+</span>
<span class="stat-label">学习项目</span>
</div>
<div class="stat-item">
<span class="stat-number">500+</span>
<span class="stat-label">小时学习</span>
</div>
<div class="stat-item">
<span class="stat-number">3</span>
<span class="stat-label">完成证书</span>
</div>
</div>
</div>
</div>
</section>
<!-- 技能 -->
<section id="skills" class="skills">
<div class="container">
<h2 class="section-title">我的技能</h2>
<div class="skills-grid">
<div class="skill-card">
<div class="skill-icon">🌐</div>
<h3>HTML5</h3>
<p>语义化标签、表单、无障碍访问、SEO优化</p>
<div class="skill-bar">
<div class="skill-progress" style="width: 85%"></div>
</div>
</div>
<div class="skill-card">
<div class="skill-icon">🎨</div>
<h3>CSS3</h3>
<p>Flexbox、Grid、动画、响应式设计、预处理</p>
<div class="skill-bar">
<div class="skill-progress" style="width: 75%"></div>
</div>
</div>
<div class="skill-card">
<div class="skill-icon">⚡</div>
<h3>JavaScript</h3>
<p>ES6+、DOM操作、事件处理、基础算法</p>
<div class="skill-bar">
<div class="skill-progress" style="width: 60%"></div>
</div>
</div>
<div class="skill-card">
<div class="skill-icon">🛠️</div>
<h3>工具</h3>
<p>Git、VS Code、Chrome DevTools、Figma</p>
<div class="skill-bar">
<div class="skill-progress" style="width: 70%"></div>
</div>
</div>
</div>
</div>
</section>
<!-- 作品 -->
<section id="projects" class="projects">
<div class="container">
<h2 class="section-title">我的作品</h2>
<div class="projects-grid">
<div class="project-card">
<img src="images/project1.jpg" alt="项目一">
<div class="project-info">
<h3>响应式博客模板</h3>
<p>使用纯HTML+CSS制作的个人博客页面,支持深色模式切换。</p>
<div class="project-tags">
<span class="tag">HTML</span>
<span class="tag">CSS</span>
<span class="tag">响应式</span>
</div>
<div class="project-links">
<a href="#" class="btn btn-small">预览</a>
<a href="#" class="btn btn-small btn-outline">源码</a>
</div>
</div>
</div>
<div class="project-card">
<img src="images/project2.jpg" alt="项目二">
<div class="project-info">
<h3>天气查询应用</h3>
<p>调用天气API,根据城市名称显示实时天气信息。</p>
<div class="project-tags">
<span class="tag">JavaScript</span>
<span class="tag">API</span>
<span class="tag">CSS动画</span>
</div>
<div class="project-links">
<a href="#" class="btn btn-small">预览</a>
<a href="#" class="btn btn-small btn-outline">源码</a>
</div>
</div>
</div>
<div class="project-card">
<img src="images/project3.jpg" alt="项目三">
<div class="project-info">
<h3>Todo清单应用</h3>
<p>功能完整的待办事项管理工具,支持增删改查和本地存储。</p>
<div class="project-tags">
<span class="tag">JavaScript</span>
<span class="tag">localStorage</span>
<span class="tag">DOM</span>
</div>
<div class="project-links">
<a href="#" class="btn btn-small">预览</a>
<a href="#" class="btn btn-small btn-outline">源码</a>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- 联系 -->
<section id="contact" class="contact">
<div class="container">
<h2 class="section-title">联系我</h2>
<div class="contact-wrapper">
<div class="contact-info">
<h3>让我们开始对话</h3>
<p>无论是项目合作、技术交流还是 просто 打个招呼,都欢迎联系我。</p>
<div class="contact-details">
<div class="contact-item">
<span class="contact-icon">📧</span>
<span>zhangsan@example.com</span>
</div>
<div class="contact-item">
<span class="contact-icon">📍</span>
<span>中国·北京</span>
</div>
<div class="contact-item">
<span class="contact-icon">🌐</span>
<span>github.com/zhangsan</span>
</div>
</div>
</div>
<form class="contact-form" action="#" method="POST">
<div class="form-group">
<label for="name">姓名</label>
<input type="text" id="name" name="name" placeholder="你的名字" required>
</div>
<div class="form-group">
<label for="email">邮箱</label>
<input type="email" id="email" name="email" placeholder="your@email.com" required>
</div>
<div class="form-group">
<label for="subject">主题</label>
<input type="text" id="subject" name="subject" placeholder="消息主题">
</div>
<div class="form-group">
<label for="message">消息</label>
<textarea id="message" name="message" rows="5" placeholder="在这里写下你的消息..." required></textarea>
</div>
<button type="submit" class="btn btn-primary btn-full">发送消息</button>
</form>
</div>
</div>
</section>
<!-- 页脚 -->
<footer class="footer">
<div class="container">
<p>© 2024 张三的个人网站. 使用HTML5和CSS3制作.</p>
<div class="social-links">
<a href="#" aria-label="GitHub">🐙 GitHub</a>
<a href="#" aria-label="Twitter">🐦 Twitter</a>
<a href="#" aria-label="LinkedIn">💼 LinkedIn</a>
</div>
</div>
</footer>
</body>
</html>
5.3 完整CSS代码
/* ============================================
CSS重置和基础样式
============================================ */
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
scroll-behavior: smooth; /* 平滑滚动 */
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",
"PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
sans-serif;
font-size: 16px;
line-height: 1.6;
color: #333;
background-color: #fafafa;
}
img {
max-width: 100%;
height: auto;
display: block;
}
a {
text-decoration: none;
color: inherit;
}
ul {
list-style: none;
}
/* ============================================
工具类
============================================ */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 20px;
}
.section-title {
font-size: 2.5rem;
text-align: center;
margin-bottom: 3rem;
color: #2c3e50;
position: relative;
}
.section-title::after {
content: "";
display: block;
width: 60px;
height: 4px;
background: linear-gradient(135deg, #667eea, #764ba2);
margin: 12px auto 0;
border-radius: 2px;
}
.btn {
display: inline-block;
padding: 12px 28px;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
border: 2px solid transparent;
}
.btn-primary {
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
}
.btn-secondary {
background: transparent;
color: white;
border-color: white;
}
.btn-secondary:hover {
background: white;
color: #667eea;
}
.btn-outline {
background: transparent;
color: #667eea;
border-color: #667eea;
}
.btn-outline:hover {
background: #667eea;
color: white;
}
.btn-small {
padding: 8px 16px;
font-size: 14px;
}
.btn-full {
width: 100%;
}
/* ============================================
导航栏
============================================ */
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1000;
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
box-shadow: 0 2px 20px rgba(0, 0, 0, 0.08);
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
max-width: 1200px;
margin: 0 auto;
}
.logo {
font-size: 1.5rem;
font-weight: 700;
color: #667eea;
}
.nav-links {
display: flex;
gap: 30px;
}
.nav-links a {
color: #555;
font-weight: 500;
transition: color 0.3s ease;
position: relative;
}
.nav-links a::after {
content: "";
position: absolute;
bottom: -4px;
left: 0;
width: 0;
height: 2px;
background: #667eea;
transition: width 0.3s ease;
}
.nav-links a:hover {
color: #667eea;
}
.nav-links a:hover::after {
width: 100%;
}
.menu-toggle {
display: none;
flex-direction: column;
gap: 5px;
background: none;
border: none;
cursor: pointer;
padding: 5px;
}
.menu-toggle span {
width: 25px;
height: 3px;
background: #333;
border-radius: 2px;
transition: all 0.3s ease;
}
/* ============================================
英雄区域
============================================ */
.hero {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: space-between;
padding: 100px 5% 60px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
overflow: hidden;
}
.hero-content {
flex: 1;
max-width: 600px;
padding-right: 40px;
}
.hero h1 {
font-size: 3.5rem;
line-height: 1.2;
margin-bottom: 20px;
animation: fadeInUp 0.8s ease-out;
}
.highlight {
background: linear-gradient(135deg, #ffd700, #ffaa00);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.hero-subtitle {
font-size: 1.3rem;
opacity: 0.9;
margin-bottom: 16px;
animation: fadeInUp 0.8s ease-out 0.2s both;
}
.hero-desc {
font-size: 1.1rem;
opacity: 0.85;
margin-bottom: 32px;
animation: fadeInUp 0.8s ease-out 0.4s both;
}
.hero-buttons {
display: flex;
gap: 16px;
animation: fadeInUp 0.8s ease-out 0.6s both;
}
.hero-image {
flex: 0 0 400px;
animation: fadeIn 1s ease-out 0.5s both;
}
.hero-image img {
width: 100%;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
/* ============================================
关于我
============================================ */
.about {
padding: 100px 0;
background: white;
}
.about-content {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 60px;
align-items: center;
}
.about-text p {
margin-bottom: 16px;
color: #555;
font-size: 1.05rem;
}
.about-stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.stat-item {
text-align: center;
padding: 24px 16px;
background: #f8f9fa;
border-radius: 12px;
transition: transform 0.3s ease;
}
.stat-item:hover {
transform: translateY(-5px);
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
}
.stat-number {
display: block;
font-size: 2rem;
font-weight: 700;
color: #667eea;
}
.stat-label {
font-size: 0.9rem;
color: #888;
}
.stat-item:hover .stat-label {
color: rgba(255, 255, 255, 0.8);
}
/* ============================================
技能
============================================ */
.skills {
padding: 100px 0;
background: #f8f9fa;
}
.skills-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 24px;
}
.skill-card {
background: white;
padding: 32px;
border-radius: 16px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
transition: all 0.3s ease;
}
.skill-card:hover {
transform: translateY(-8px);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
}
.skill-icon {
font-size: 3rem;
margin-bottom: 16px;
}
.skill-card h3 {
font-size: 1.3rem;
margin-bottom: 8px;
color: #2c3e50;
}
.skill-card p {
color: #888;
font-size: 0.95rem;
margin-bottom: 16px;
}
.skill-bar {
height: 8px;
background: #e9ecef;
border-radius: 4px;
overflow: hidden;
}
.skill-progress {
height: 100%;
background: linear-gradient(135deg, #667eea, #764ba2);
border-radius: 4px;
transition: width 1s ease;
}
/* ============================================
作品
============================================ */
.projects {
padding: 100px 0;
background: white;
}
.projects-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
gap: 32px;
}
.project-card {
background: white;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
transition: all 0.3s ease;
}
.project-card:hover {
transform: translateY(-8px);
box-shadow: 0 16px 40px rgba(0, 0, 0, 0.15);
}
.project-card img {
width: 100%;
height: 200px;
object-fit: cover;
}
.project-info {
padding: 24px;
}
.project-info h3 {
font-size: 1.3rem;
margin-bottom: 8px;
color: #2c3e50;
}
.project-info p {
color: #666;
font-size: 0.95rem;
margin-bottom: 16px;
}
.project-tags {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 16px;
}
.tag {
padding: 4px 12px;
background: #f0f4ff;
color: #667eea;
border-radius: 20px;
font-size: 0.85rem;
}
.project-links {
display: flex;
gap: 12px;
}
/* ============================================
联系
============================================ */
.contact {
padding: 100px 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.contact .section-title {
color: white;
}
.contact .section-title::after {
background: rgba(255, 255, 255, 0.6);
}
.contact-wrapper {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 60px;
align-items: start;
}
.contact-info h3 {
font-size: 1.8rem;
margin-bottom: 16px;
}
.contact-info p {
opacity: 0.9;
margin-bottom: 32px;
}
.contact-details {
display: flex;
flex-direction: column;
gap: 16px;
}
.contact-item {
display: flex;
align-items: center;
gap: 12px;
font-size: 1.05rem;
}
.contact-icon {
font-size: 1.5rem;
}
.contact-form {
background: white;
padding: 36px;
border-radius: 16px;
color: #333;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 6px;
font-weight: 600;
color: #555;
}
.form-group input,
.form-group textarea {
width: 100%;
padding: 12px 16px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 16px;
font-family: inherit;
transition: border-color 0.3s ease;
}
.form-group input:focus,
.form-group textarea:focus {
outline: none;
border-color: #667eea;
}
.form-group textarea {
resize: vertical;
min-height: 120px;
}
/* ============================================
页脚
============================================ */
.footer {
padding: 40px 0;
background: #2c3e50;
color: rgba(255, 255, 255, 0.7);
}
.footer .container {
display: flex;
justify-content: space-between;
align-items: center;
}
.social-links {
display: flex;
gap: 20px;
}
.social-links a {
transition: color 0.3s ease;
}
.social-links a:hover {
color: white;
}
/* ============================================
响应式设计
============================================ */
@media (max-width: 992px) {
.hero {
flex-direction: column;
text-align: center;
padding-top: 120px;
}
.hero-content {
padding-right: 0;
margin-bottom: 40px;
}
.hero h1 {
font-size: 2.5rem;
}
.hero-buttons {
justify-content: center;
}
.hero-image {
flex: none;
max-width: 300px;
}
.about-content,
.contact-wrapper {
grid-template-columns: 1fr;
}
.about-stats {
max-width: 400px;
margin: 0 auto;
}
}
@media (max-width: 768px) {
.nav-links {
display: none; /* 移动端隐藏导航链接,后续可用JS实现展开 */
}
.menu-toggle {
display: flex;
}
.section-title {
font-size: 2rem;
}
.hero h1 {
font-size: 2rem;
}
.footer .container {
flex-direction: column;
gap: 16px;
text-align: center;
}
}
5.4 运行和调试
- 保存所有文件后,在VS Code中右键点击
index.html - 选择”Open with Live Server”
- 浏览器会自动打开,显示你的网站
- 修改代码保存后,浏览器自动刷新
使用开发者工具调试:
- 右键元素 → 检查,查看HTML结构和CSS样式
- 尝试在右侧面板直接修改CSS值,看看效果如何
- 切换到移动端视图(设备工具栏图标),检查响应式效果
第六章:学习路径和建议
学到这里,你已经掌握了前端开发的核心基础。接下来该怎么做?
推荐的下一步
| 阶段 | 学习内容 | 建议方式 |
|---|---|---|
| 巩固 | 再做一个完整的静态网站 | 模仿一个你喜欢的网站重新做一遍 |
| 进阶CSS | 学习CSS预处理器(Sass/Less) | 尝试把上面的CSS改成SCSS |
| 入门JS | 变量、函数、DOM操作、事件 | 给网站加一个暗色模式切换按钮 |
| 工具链 | Git、npm、Vite/webpack基础 | 了解版本管理和包管理 |
| 框架 | React / Vue / Svelte | 选择一个深入学习 |
| 进阶 | TypeScript、测试、性能优化 | 在实际项目中实践 |
学习建议
- 不要只看,要动手——复制粘贴别人的代码不如自己从头打一遍
- 善用开发者工具——它是你最好的老师,没有之一
- 模仿是最好的学习——看到一个喜欢的网站,试着用HTML+CSS还原它
- 遇到问题先自己搜索——99%的问题别人都已经遇到过并解答过
- 保持耐心和好奇心——前端世界日新月异,保持学习的心态比掌握具体技术更重要
推荐资源
- MDN Web Docs(
developer.mozilla.org)——最权威的Web技术文档 - CSS-Tricks(
css-tricks.com)——高质量的CSS教程 - freeCodeCamp——免费的交互式编程课程
- 前端早读课(公众号)——国内优质前端资讯
- 掘金(
juejin.cn)——中文技术社区
写在最后
亲爱的读者,感谢你读完这篇指南。
写到这里,我想起自己刚开始学前端时的样子——对着空白的编辑器发呆,不知道从何下手,看到满屏的代码感到既兴奋又害怕。如果你现在也有同样的感受,我想告诉你:这完全正常。
每一个你现在 admired 的网站,它的作者也曾从零开始,也曾面对空白文档不知所措。前端开发最迷人的地方在于,你写的每一行代码,都能在浏览器里立即看到结果。这种即时反馈是学习过程中最大的动力。
你现在已经知道了:
- 如何用HTML搭建网页的骨架
- 如何用CSS为网页穿上漂亮的衣服
- 如何搭建一个完整的个人网站
- 如何调试和优化你的作品
接下来的路,就靠你自己走了。但请相信,你已经迈出了最重要的一步。
如果这篇指南对你有帮助,不妨动手改几个参数,换个颜色,加几张图,让它真正成为”你的”网站。前端开发的乐趣,就藏在这个过程中。
加油,未来的网页设计师!🚀
