这是「Python 100 天」系列第 44 天的学习笔记:CSS 进阶:定位、浮动与居中 。正文包含知识点拆解、可直接运行的代码,以及当天仓库源文件的逐文件讲解。
今天要解决的问题
把个人主页做成一个完整的单页:顶部"云朵 + 山"的英雄区、中间头像与技能(左右图文环绕)、底部彩色页脚。README 知识点:div、静态/相对定位、绝对定位、居中、字体、尺寸单位、浮动与清除。
一、div:无语义的盒子
1 2 3 4 <div class="top-container"> <img class="top-cloud" src="images/cloud.png"> <h1>I'm Phillip.</h1> </div>
<div> 本身没有任何样式,就是一个"块级容器",用来分组 。页面布局的本质就是"一堆盒子怎么摆"。
二、定位
1 2 3 4 5 6 7 .top-container { position: relative; } /* 建立定位参照系 */ .top-cloud { position: absolute; /* 相对最近的定位祖先 */ left: 250px; bottom: 300px; }
static:默认,跟着文档流走;
relative:相对自己原来的位置 偏移,原位置仍占空间;
absolute:脱离文档流,相对最近的非 static 祖先 定位;
fixed:相对浏览器窗口,滚动不动(导航栏常用);
sticky:滚动到阈值后变固定。
关键点 :absolute 的子元素要有 relative 的父元素,否则会一路往上找,最终相对 <body> 定位——这是"绝对定位乱飞"的根本原因。
三、居中
1 2 3 h1 { margin: 50px auto 0 auto; } /* 块级元素水平居中 */ body { text-align: center; } /* 行内内容水平居中 */ .intro { width: 30%; margin: auto; }
margin: auto 只在元素有明确宽度 时才生效(否则它本来就占满一行,居中没有意义)。
垂直居中:现代用 display: flex; align-items: center; justify-content: center;。
四、浮动
1 2 .code-img { width: 25%; float: left; margin-right: 60px; } .chillies-img { width: 25%; float: right; }
float 让元素"浮"到一侧,文字环绕它——这是"图文混排"的经典做法(报纸排版)。副作用是父元素高度塌陷 (浮动元素不撑开父级),要用 clear: both 或 overflow: hidden 修复。
现代做法是 Flexbox / Grid ,float 现在只用于"文字环绕图片"这一种场景。
五、尺寸单位
单位
含义
px绝对像素
%相对父元素
em相对当前元素字号(层层相乘,易失控)
rem相对根元素字号(推荐)
vw/vh相对视口宽/高
font-size: 5.625em 在 h1 上意味着"父级字号的 5.625 倍",叠几层就会失控;rem 更可预测。
代码全文
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Phillip</title> <link rel="stylesheet" href="css/styles.css"> <link rel="icon" href="favicon.ico"> <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=Merriweather&family=Montserrat&family=Sacramento&display=swap" rel="stylesheet"> </head> <body> <div class="top-container"> <img class="top-cloud" src="images/cloud.png" alt="cloud-img"> <h1>I'm Phillip.</h1> <h2>a programmer</h2> <img class="bottom-cloud" src="images/cloud.png" alt="cloud-img"> <img src="images/mountain.png" alt="mountain-img"> </div> <div class="middle-container"> <div class="profile"> <img src="images/profile pic.png" alt="profile-pic"> <h2>Hello.</h2> <p class="intro">An aspiring Software Engineer who is passionate about producing efficient, reliable and readable code.</p> </div> <hr> <div class="skills"> <h2>My Skills.</h2> <div class="skill-row"> <img class="code-img" src="https://media1.giphy.com/media/QssGEmpkyEOhBCb7e1/giphy.gif?cid=ecf05e473j7uwuxasvxi7q2onqxqrgn6wc140azeqyez7mzg&rid=giphy.gif&ct=s" alt="code-gif"> <h3>Development</h3> <p>Python, HTML, CSS, Bootstrap.</p> </div> <div class="skill-row"> <img class="chillies-img" src="images/chillies.png" alt="chillies-img"> <h3>Hot Wings Challenge</h3> <p>But my best skill is actually in eating hot wings. I am the undisputed king of hot wing challenges. Ghost Peppers and Carolina Reapers are my favourite snacks.</p> </div> </div> <hr> <div class="contact-me"> <h2>Get In Touch</h2> <h3>If you love hot wings as much as I do.</h3> <p class="contact-message">But my best skill is actually in eating hot wings. I am the undisputed of hot wing challenges. Ghost Peppers and Carolina Reapers are my favourite snacks.</p> <a class="btn" href="mailto:[email protected] ">CONTACT ME</a> </div> </div> <div class="bottom-container"> <a class="footer-link" href="https://www.linkedin.com/">LinkedIn</a> <a class="footer-link" href="https://twitter.com/">Twitter</a> <a class="footer-link" href="https://github.com/phillipai">Github</a> <p class="copyright">© 2022 Phillip</p> </div> </body> </html>
css/styles.css(节选:定位与浮动部分)
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 28 29 30 31 32 33 34 } .chillies-img { width: 25%; float: right; margin-right: 60px; } .btn { background: #11CDD4; background-image: -webkit-linear-gradient(top, #11CDD4, #11999E); background-image: -moz-linear-gradient(top, #11CDD4, #11999E); background-image: -ms-linear-gradient(top, #11CDD4, #11999E); background-image: -o-linear-gradient(top, #11CDD4, #11999E); background-image: linear-gradient(to bottom, #11CDD4, #11999E); -webkit-border-radius: 8; -moz-border-radius: 8; border-radius: 8px; font-family: 'Montserrat', sans-serif; color: #ffffff; font-size: 20px; padding: 10px 20px 10px 20px; text-decoration: none; } .btn:hover { background: #30E3CB; background-image: -webkit-linear-gradient(top, #30E3CB, #2BC4AD); background-image: -moz-linear-gradient(top, #30E3CB, #2BC4AD); background-image: -ms-linear-gradient(top, #30E3CB, #2BC4AD); background-image: -o-linear-gradient(top, #30E3CB, #2BC4AD); background-image: linear-gradient(to bottom, #30E3CB, #2BC4AD); text-decoration: none; }
代码走读
三个大 div:top-container(背景 + 云 + 山)、middle-container(简介 + 技能 + 联系)、bottom-container(页脚链接)。
top-container 设 position: relative,两朵云 position: absolute 分别贴左上和右下,靠 left/right/top/bottom 微调。
.skill-row { width: 50%; margin: 100px auto; text-align: left; }:技能行整体居中、内部左对齐。
.code-img { float: left } / .chillies-img { float: right }:两张图分别浮在两侧,文字环绕。
.btn 用线性渐变做按钮,border-radius: 8px 圆角,padding 撑开点击区域。
a:hover 改颜色,给出悬停反馈。
页面结构图
1 2 3 4 5 6 7 8 9 10 top-container 浅绿背景,标题 + 两朵云 + 山 middle-container profile 头像 + Hello + 一句介绍 hr skills skill-row [代码图] Development skill-row Hot Wings [辣椒图] hr contact-me Get In Touch + 按钮 bottom-container 青色背景 + 三个链接 + 版权
易错点
absolute 找不到 relative 父级 :元素跑到页面角落。
float 后父元素高度变 0 :背景色不显示,需要清除浮动。
margin: auto 没生效 :元素没设宽度,或不是块级元素。
字体没加载 :Google Fonts 的 <link> 被墙或路径错,会退化成默认字体(不报错,只是"看着不对")。
z-index 不生效 :只对定位元素(relative/absolute/fixed)有效。
动手改
用 Flexbox 重写 .skill-row,去掉 float。
把 em 全部换成 rem。
加一个 @media (max-width: 600px),手机上改成单列。
源文件逐个讲解
7 个文件 + 4 张图 + 1 个 ico ,这是全书最完整的「静态网页」:
文件
行数
职责
index.html63 单页式个人站:顶部云山、中间技能、底部社交
css/styles.css130 全部样式:字体、定位、浮动、按钮渐变
images/cloud.png— 云(用了两次)
images/mountain.png— 山(顶部大图)
images/profile pic.png— 头像
images/chillies.png— 辣椒(第二个技能配图)
favicon.ico— 浏览器标签页图标
README.md8 说明页
index.html(63 行)
(1)引入三套 Google 字体
1 2 3 <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=Merriweather&family=Montserrat&family=Sacramento&display=swap" rel="stylesheet">
preconnect 是性能优化 :提前和字体服务器建立 TCP/TLS 连接,字体加载快一点。
display=swap :字体没加载完时先用系统字体显示 ,加载完再替换——避免「白屏等字体」 。
三套字体分工 :
字体
用在哪
风格
Sacramentoh1「I'm Phillip.」手写体
Montserrath2、h3、链接、按钮无衬线
Merriweatherbody 正文衬线
这就是「字体层级」 :一个手写体做标题、一个无衬线做副标题、一个衬线做正文。
(2)三段式结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <div class="top-container"> <img class="top-cloud" src="images/cloud.png" alt="cloud-img"> <h1>I'm Phillip.</h1> <h2>a programmer</h2> <img class="bottom-cloud" src="images/cloud.png" alt="cloud-img"> <img src="images/mountain.png" alt="mountain-img"> </div> <div class="middle-container"> <div class="profile">...</div> <hr> <div class="skills">...</div> <hr> <div class="contact-me">...</div> </div> <div class="bottom-container">...</div>
top / middle / bottom 三个大 div ——这是最朴素的「分区」 。Day 58 的 Bootstrap 会用 <section> 和栅格重做。
「云 + 山」的经典首屏 :两朵云绝对定位在天空里,山在底部。这是 2022 年很流行的一种个人站风格 。
(3)技能区:左右浮动的图文
1 2 3 4 5 6 7 8 9 10 <div class="skill-row"> <img class="code-img" src="https://media1.giphy.com/media/.../giphy.gif" alt="code-gif"> <h3>Development</h3> <p>Python, HTML, CSS, Bootstrap.</p> </div> <div class="skill-row"> <img class="chillies-img" src="images/chillies.png" alt="chillies-img"> <h3>Hot Wings Challenge</h3> <p>But my best skill is actually in eating hot wings. ...</p> </div>
第一个图 float: left(代码 GIF 在左),第二个 float: right(辣椒在右) ——文字自动绕图 。
第一个 src 是外链 GIF(Giphy) ,第二个是本地 PNG。外链图片的风险 :对方删了图就变成裂图,而且跨域、加载慢 。真实项目应该下载到本地。
css/styles.css(130 行)——最重要的 8 段
(1)body 重置 + 全局字体
1 2 3 4 5 6 body { margin: 0px; text-align: center; font-family: 'Merriweather', serif; color: #40514E; }
margin: 0 是关键 :浏览器给 body 默认 8px 外边距,不重置的话顶部会有白边,背景色铺不满 。
text-align: center 让所有文字居中 ——这是「整页居中」最省事的写法,代价是每个要左对齐的地方都得单独覆盖 (下面的 .skill-row 就这么干的)。
(2)h1 的字号与间距
1 2 3 4 5 6 h1 { font-size: 5.625em; margin: 50px auto 0 auto; font-family: 'Sacramento', cursive; color: #66BFBF; }
5.625em 是个奇怪的数字 ——它等于 90px(假设父级 16px)。课程里就是从设计稿量出来的像素值换算的 。
em 和 rem 的区别 :
em 相对于父元素 字号 → 层层嵌套会累积;
rem 相对于根元素(html) → 稳定。
这里 h1 用 em、h2 用 rem ,混着来不严谨,但结果能看。
margin: 50px auto 0 auto 四值:上 50、右 auto、下 0、左 auto——左右 auto = 水平居中 。
(3)点线分割线(和 Day 43 同一个技巧)
1 2 3 4 5 6 hr { border: dotted #EAF6f6 6px; border-top: none; width: 4%; margin: 100px auto; }
border: dotted #EAF6f6 6px 一次设三个属性(样式/颜色/宽度),然后 border-top: none 去掉上边 ——剩下的左、右、下三条边宽度是 0,只有上边……等等,去掉的是上边 。
这条规则的实际效果 :左右下都是 6px 点线,上边没有——但因为 width: 4%,盒子很窄,三边只显示成一条虚线。这种写法和 Day 43 的 border-style: dotted none none 等价,只是反过来写 。
(4)绝对定位的两朵云
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 .top-container { background-color: #E4F9F5; position: relative; padding-top: 100px; } .top-cloud { position: absolute; left: 250px; bottom: 300px; } .bottom-cloud { position: absolute; right: 300px; top: 40px; }
position: relative 的父 + position: absolute 的子 ——这是绝对定位的固定搭配 :子元素相对于最近的「已定位祖先」定位,而不是相对于窗口。
left: 250px / right: 300px 是从父容器边缘量的 ,不是从中心。所以窗口一窄,云会跑到内容外面 ——没有响应式。
(5)浮动图文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 .skill-row { width: 50%; margin: 100px auto 100px auto; text-align: left; } .code-img { width: 25%; float: left; margin-right: 60px; } .chillies-img { width: 25%; float: right; margin-right: 60px; }
.skill-row 用 text-align: left 覆盖了 body 的居中 ——因为整段文字要左对齐,只有图片浮动。
float: left + 后续文字 :文字会环绕 图片。margin-right: 60px 给图和字之间留白 。
.chillies-img 的 margin-right: 60px 是个 bug :图在右边浮动,应该是 margin-left 才对——现在的效果是「图右浮动,但右边还有 60px 空白」,文字离图太近 。
浮动最大的问题 :父容器高度塌陷 (浮动元素不占父元素高度)。这里因为 .skill-row 里有文字撑着,没暴露;如果只有图,父 div 会高度为 0 。现代做法是 display: flex 或 clearfix。
(6)渐变按钮
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 .btn { background: #11CDD4; background-image: -webkit-linear-gradient(top, #11CDD4, #11999E); background-image: -moz-linear-gradient(top, #11CDD4, #11999E); background-image: -ms-linear-gradient(top, #11CDD4, #11999E); background-image: -o-linear-gradient(top, #11CDD4, #11999E); background-image: linear-gradient(to bottom, #11CDD4, #11999E); -webkit-border-radius: 8; -moz-border-radius: 8; border-radius: 8px; font-family: 'Montserrat', sans-serif; color: #ffffff; font-size: 20px; padding: 10px 20px 10px 20px; text-decoration: none; }
五个 background-image 是「CSS3 时代的兼容写法」 :-webkit-、-moz-、-ms-、-o- 各一份,最后一份标准写法。2022 年之后这些前缀基本不需要了 ,现代浏览器都支持 linear-gradient。
这是从 CSS 渐变生成器(如 cssgradient.io)直接复制过来的代码 ——能跑,但臃肿 。
border-radius: 8 少了单位 (应该是 8px)——-webkit-border-radius: 8 是无效值,标准写法的 8px 生效了 ,所以看起来正常。
text-decoration: none 去掉 <a> 默认的下划线,让它看起来像按钮。
这份代码的问题
.chillies-img 用 margin-right 而不是 margin-left :右浮动的图外侧留白,文字贴太近。
绝对定位的云用固定像素 :窗口变窄就出界。
border-radius: 8 缺单位 。
五套渐变前缀冗余 。
float 布局没有 clearfix ,只是侥幸没塌。
5.625em 这种「设计稿像素换 em」的写法 ,改基准字号就全乱。
外链 Giphy GIF ,随时可能失效。
没有 <meta name="viewport"> ,手机上排版错乱。
README.md(8 行)
标题 Day 44 - Intermediate CSS,知识点:CSS Static and Relative Positioning、Absolute positioning、Centering Elements、Font Styling、CSS Sizing、Font Properties、The CSS Box Model。
七条里覆盖了 6 条 :相对/绝对定位(云)、居中(margin: auto)、字体、尺寸(em/%)、盒模型(padding/border)。
Font Properties 这一天大量使用 (font-family、font-size、color)。
文件关系
1 2 3 4 5 6 7 8 index.html ├── css/styles.css (全部样式) ├── favicon.ico (标签页图标) ├── images/cloud.png (用了两次:top-cloud / bottom-cloud) ├── images/mountain.png ├── images/profile pic.png ├── images/chillies.png └── https://fonts.googleapis.com/... (三套字体,外链)
「一个 HTML + 一个 CSS + 一个 images 目录」 是静态站的标准结构。Day 45 之后这个站会被 Flask 接管 (templates/ + static/)。