T 流年印迹
流年印迹
首页 Python 正文

Python 100 天 · 第 41 天:HTML 入门:个人主页三件套

这是「Python 100 天」系列第 41 天的学习笔记:HTML 入门:个人主页三件套。正文包含知识点拆解、可直接运行的代码,以及当天仓库源文件的逐文件讲解。

今天要解决的问题

从 Python 转向前端:用纯 HTML 写一个个人主页 index.html,外加 hobbies.html、contact.html 两个子页,互相用链接串起来。README 知识点:标签的解剖、HTML 骨架、文本结构、列表、图片、链接。

一、标签的解剖

1
<a href="https://example.com" target="_blank">点我</a>
  • <a> 是起始标签,</a> 是结束标签,中间是内容;
  • href="..." 是属性,属性名="值";
  • 有些标签没有内容,叫空元素:<img src="x.png" alt="图">、<br>、<hr>。

标签必须正确嵌套:<p><em>文字</em></p> 可以,<p><em>文字</p></em> 不行(浏览器会"猜"你的意思,但结果不可控)。

二、HTML 骨架

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>页面标题</title>
</head>
<body>
<!-- 用户能看到的内容 -->
</body>
</html>
  • <!DOCTYPE html> 声明这是 HTML5;
  • <head> 放元信息(编码、标题、引入的 CSS),用户看不到;
  • <body> 放可见内容;
  • <meta charset="utf-8"> 必须有,否则中文乱码。

三、常用文本标签

标签 含义 说明
<h1>~<h6>标题数字越小越大,h1 一页只该有一个
<p>段落自带上下间距
<em>强调斜体,语义是"重读"
<strong>重要粗体,语义比 <b> 强
<ul> / <ol> / <li>无序/有序列表li 必须在 ul/ol 里
<hr>分隔线空元素
<br>换行空元素,不要用它排版

<b> 和 <strong> 长得一样但语义不同:屏幕阅读器和搜索引擎看的是语义,不是样式。

四、图片与链接

1
2
3
<img src="profile pic.png" alt="我的照片">
<a href="hobbies.html">My Hobbies</a>
<a href="https://github.com/phillipai" target="_blank">GitHub</a>
  • src 是路径,alt 是图片加载失败时的替代文字(也是无障碍的必需项);
  • href 可以是相对路径(同目录下另一个文件)或绝对 URL;
  • target="_blank" 在新标签页打开。

代码全文

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title>Phillip's Personal Site</title>
</head>

<body>
<img src="profile pic.png" alt="Phillip's profile picture">
<h1>Phillip</h1>
<p><em>Aspiring Software Developer</em></p>
<p>I love to code in <a href="https://github.com/phillipai">Python</a>.</p>
<hr>
<h3>Education</h3>
<ul>
<li><a href="https://github.com/phillipai/udacity-cs101">
Udacity's CS101</a></li>
<li><a href="https://github.com/phillipai/100-days-of-code-python">
100 Days of Code: The Complete Python Pro Bootcamp</a></li>
</ul>
<a href="hobbies.html">My Hobbies</a>
<br>
<a href="contact.html">Contact Me</a>
</body>

</html>

hobbies.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>My Hobbies</title>
</head>
<body>
<h3>My Hobbies</h3>
<ol>
<li>Reading</li>
<li>Working out</li>
<li><a href="https://www.youtube.com/c/TheStradman">Cars</a></li>
</ol>
</body>
</html>

contact.html

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Contact Me</title>
</head>
<body>
<h1>Not my Contact Details</h1>
<p>Not my address: <a href="https://www.mariowiki.com/Rainbow_Road">
111 Rainbow Road</a></p>
<p>Not my e-mail: [email protected]</p>
</body>
</html>

代码走读

  1. <img> → <h1>Phillip</h1> → <p><em>...</em></p>:先图后名再一句话介绍,是最简单的"英雄区"。
  2. <hr> 把页面切成几块。
  3. <h3>Education</h3> + <ul> 列表,每个 <li> 里套一个 <a>——列表项里放链接是最常见的导航写法。
  4. 底部两个链接用 <br> 换行,分别指向子页面。
  5. 文件名 profile pic.png 带空格:能显示,但路径里有空格容易出问题(URL 要转义成 %20),工程上一律用 profile-pic.png。

怎么打开

直接双击 index.html,或 python3 -m http.server 8000 后访问 http://localhost:8000(后者更接近真实环境,路径行为一致)。

易错点

  1. 中文乱码:漏了 <meta charset="utf-8">。
  2. 图片不显示:路径错、文件名大小写不符(Linux 区分大小写)。
  3. href 写成 herf:不报错,就是不跳转。
  4. 标签没闭合:浏览器容错,但布局可能整体错位。

动手改

  • 把 profile pic.png 改名去掉空格,同步修改 src。
  • 加一个 <table> 展示教育经历(Day 42 的内容,可提前试)。
  • 用 <strong> 替换 <em>,观察视觉效果与语义差别。

源文件逐个讲解

4 个文件:三个页面 + 一张头像图。这一天没有一行 Python。

文件 行数 职责
index.html30主页:头像、姓名、简介、教育经历、跳转链接
hobbies.html18爱好页:有序列表
contact.html12联系页:地址、邮箱(都是假的)
profile pic.png—头像

index.html(30 行)

1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title>Phillip's Personal Site</title>
</head>

<body>

五个必备骨架:

  • <!DOCTYPE html>:告诉浏览器「这是 HTML5」,不写会进「怪异模式」,CSS 表现会不一样;
  • lang="en":语言,影响朗读器和搜索引擎;
  • dir="ltr":从左到右排版(阿拉伯语、希伯来语才用 rtl);
  • <meta charset="utf-8">:必须有,否则中文乱码;
  • <head> / <body>:元信息 vs 可见内容。
1
2
3
4
5
    <img src="profile pic.png" alt="Phillip's profile picture">
<h1>Phillip</h1>
<p><em>Aspiring Software Developer</em></p>
<p>I love to code in <a href="https://github.com/phillipai">Python</a>.</p>
<hr>

四个标签的语义:

  • <img src=... alt=...>:alt 是必须的(图加载失败/盲人读屏时显示);
  • <h1>:一级标题,一个页面只应有一个;
  • <em>:语义上的「强调」,浏览器默认渲染成斜体(<i> 只是斜体,无语义);
  • <a href>:超链接;
  • <hr>:水平分割线,自闭合标签(HTML5 里不写 / 也行)。

src="profile pic.png" 里有空格:浏览器能处理,但在服务器上必须 URL 编码成 profile%20pic.png。文件名带空格是坏习惯。

1
2
3
4
5
6
7
    <h3>Education</h3>
<ul>
<li><a href="https://github.com/phillipai/udacity-cs101">
Udacity's CS101</a></li>
<li><a href="https://github.com/phillipai/100-days-of-code-python">
100 Days of Code: The Complete Python Pro Bootcamp</a></li>
</ul>

<ul> 无序列表 + <li> 列表项。链接文字跨行写——HTML 会把换行当空格,显示出来是「Udacity's CS101」,看不出换行。

1
2
3
    <a href="hobbies.html">My Hobbies</a>
<br>
<a href="contact.html">Contact Me</a>

href="hobbies.html" 是相对路径——同目录下的文件。<br> 强制换行(<br> 只用于「文本内的换行」,不该用来做布局间距)。

hobbies.html(18 行)

1
2
3
4
5
6
    <h3>My Hobbies</h3>
<ol>
<li>Reading</li>
<li>Working out</li>
<li><a href="https://www.youtube.com/c/TheStradman">Cars</a></li>
</ol>

<ol> 是有序列表(1. 2. 3.),对比主页的 <ul>(圆点)。同一个「列表」语义,两种呈现。

这一页没有 <hr>、没有返回链接——用户点进来就回不去了(只能按浏览器后退)。这是一个真实的可用性缺陷。

contact.html(12 行)

1
2
3
4
    <h1>Not my Contact Details</h1>
<p>Not my address: <a href="https://www.mariowiki.com/Rainbow_Road">
111 Rainbow Road</a></p>
<p>Not my e-mail: [email protected]</p>

故意写成「不是我的联系方式」(彩虹路是马里奥的地图)——作者在保护隐私,这点值得学:公开网页上不要放真实地址和邮箱。

注意 <h1> 用了两次(主页和这一页各一个)——不同页面各有一个 <h1> 是正确的,同一页面才只能有一个。

三个页面的关系

1
2
index.html ──link──▶ hobbies.html
└─link──▶ contact.html

单向链接:主页能进两个子页,子页回不去。真实网站会有导航栏(<nav>),Day 44 之后也没加——因为课程重点是 CSS 布局,不是信息架构。

这一天的问题

  1. hobbies.html、contact.html 没有返回链接。
  2. 文件名带空格(profile pic.png)。
  3. <br> 用来做间距(应该用 CSS margin)。
  4. 没有 <meta name="viewport">:手机上会按桌面宽度缩放,字很小。Day 58 学 Bootstrap 才补上。

README.md(8 行)

标题 Day 41 - Intermediate HTML,知识点:HTML Structure、HTML Lists、HTML Image Elements、HTML Links and Anchor Tags。

四条正好对应四个标签族:结构、列表、图片、链接。这一天只学「有什么标签」,不管好不好看——样式是 Day 43 的事。

微信收款码 微信
支付宝收款码 支付宝
评论