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

Python 100 天 · 第 58 天:Bootstrap:栅格、组件与响应式

这是「Python 100 天」系列第 58 天的学习笔记:Bootstrap:栅格、组件与响应式。正文包含知识点拆解、可直接运行的代码,以及当天仓库源文件的逐文件讲解。

今天要解决的问题

用 Bootstrap 4 做一个"TinDog"落地页:导航栏、英雄区、功能卡片、轮播、定价表、页脚,并且在手机上也好看。README 知识点:安装 Bootstrap、导航栏、栅格系统、容器、按钮与 Font Awesome、轮播、卡片、z-index、媒体查询断点、代码重构、选择器与优先级。

一、引入 Bootstrap

1
2
3
4
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>

Bootstrap 4 的 JS 组件依赖 jQuery + Popper,所以三个都要引,且顺序不能乱(jQuery → Popper → Bootstrap)。Bootstrap 5 已经去掉了 jQuery 依赖。

integrity 是 SRI 校验,防止 CDN 被篡改——生产环境应该保留。

二、栅格系统

1
2
3
4
5
<div class="row">
<div class="col-lg-4 col-md-6">卡片 1</div>
<div class="col-lg-4 col-md-6">卡片 2</div>
<div class="col-lg-4 col-md-6">卡片 3</div>
</div>
  • 一行 12 列,col-4 占 4 列(三个正好一行);
  • col-lg-4 col-md-6:大屏 3 列、中屏 2 列、小屏自动堆叠;
  • 断点:sm ≥576px、md ≥768px、lg ≥992px、xl ≥1200px。

这是响应式布局的核心:不用写一行媒体查询,靠类名就能适配。

三、容器

1
2
<div class="container">        <!-- 固定宽度、居中 -->
<div class="container-fluid"> <!-- 占满整宽 -->

四、常用组件

组件 关键类
导航栏navbar navbar-expand-lg navbar-dark
按钮btn btn-dark btn-lg
卡片card + card-body + card-title
轮播carousel slide + carousel-inner + carousel-item
图标<i class="fas fa-check-circle"></i>(Font Awesome)

五、z-index 与层叠

1
2
.carousel { z-index: 0; }
.navbar { z-index: 10; }

z-index 只对定位元素(relative/absolute/fixed)生效,数值大的在上面。轮播图经常盖住导航栏,就是 z-index 没设对。

代码全文

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
58
59
60
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>TinDog</title>

<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Montserrat|Ubuntu" rel="stylesheet">

<!-- CSS Stylesheets -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="css/styles.css">

<!-- Font Awesome -->
<script defer src="https://use.fontawesome.com/releases/v5.0.7/js/all.js"></script>

<!-- Bootstrap Scripts -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>

<body>

<section class="colored-section" id="title">

<div class="container-fluid">

<!-- Nav Bar -->

<nav class="navbar navbar-expand-lg navbar-dark">

<a class="navbar-brand" href="">tindog</a>

<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02">
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarTogglerDemo02">

<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#footer">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#pricing">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#cta">Download</a>
</li>
</ul>

</div>
</nav>


<!-- Title -->

<div class="row">

代码走读

  1. head 里引 Google Fonts(Montserrat + Ubuntu)、Bootstrap CSS、自己的 styles.css、Font Awesome、三个 JS。
  2. <section class="colored-section" id="title">:每个区块用 section + 语义化 id。
  3. 导航栏 navbar-expand-lg:大屏展开、小屏折叠成汉堡菜单;data-toggle="collapse" + data-target="#navbarTogglerDemo02" 控制折叠区域。
  4. container-fluid 让内容占满宽度。
  5. 后续区块用 row / col-* 组织,配合 carousel、card 组件。

自定义 CSS 怎么和 Bootstrap 共存

1
2
/* 自己的 styles.css 在 bootstrap 之后引入,才能覆盖 */
.colored-section { background-color: #ff4c68; color: #fff; }

引入顺序决定优先级:后引入的同权重规则胜出。要覆盖 Bootstrap,自己的 CSS 必须放在它后面;要更强势就用更具体的选择器(#title .navbar)或 !important(尽量别用)。

运行结果

一个完整的营销落地页:粉色英雄区 + 手机图、三个功能图标、三张价格卡、底部社交链接;窗口缩小到手机尺寸时,导航折叠、卡片竖排。

易错点

  1. 忘了引 jQuery:轮播、折叠菜单全部失灵。
  2. col-4 放在没有 row 的父级里:布局错乱。
  3. Bootstrap 版本混用:4 的类名和 5 不兼容(ml- vs ms-)。
  4. 自定义样式被覆盖:CSS 引入顺序错,或选择器权重不够。

动手改

  • 把 col-lg-4 改成 col-lg-3,看四个卡片怎么排。
  • 用 Bootstrap 5 重写一遍,体会去掉 jQuery 的差别。
  • 给自己的 Day 41 个人主页套上 Bootstrap 的 container + row。

源文件逐个讲解

1 个 HTML + 1 个 CSS + 7 张图 + README,没有 Python:

文件 行数 职责
index.html约 240TinDog 单页:导航、首屏、特性、评价轮播、媒体、定价、CTA、页脚
css/styles.css179全部自定义样式 + 一个媒体查询
images/iphone6.png—首屏手机样机
images/dog-img.jpg / images/lady-img.jpg—两条评价配图
images/TechCrunch.png / tnw.png / bizinsider.png / mashable.png—四家媒体 logo

index.html——Bootstrap 4 的「标准骨架」

(1)head 里五条引入,顺序有讲究

1
2
3
4
5
6
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" ...>
<link rel="stylesheet" href="css/styles.css">
<script defer src="https://use.fontawesome.com/releases/v5.0.7/js/all.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" ...></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" ...></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" ...></script>

Bootstrap 4 的三个 JS 依赖,顺序不能乱:

1
jQuery  →  Popper.js  →  bootstrap.js

因为:Bootstrap 的 JS 用 jQuery 语法;下拉菜单/提示框依赖 Popper 定位。顺序反了会 $(...).dropdown is not a function。

integrity="sha384-..." 是 SRI(子资源完整性):浏览器校验文件哈希,CDN 被篡改就拒绝加载。这是安全好习惯,很多教程都不写。

自定义 css/styles.css 放在 bootstrap 之后——因为 CSS 是「后写的覆盖先写的」,放前面就被框架覆盖了。

defer:字体图标脚本延后到 DOM 解析完再执行,不阻塞渲染。

(2)colored-section / white-section 交替

1
2
3
4
5
6
  <section class="colored-section" id="title"> ... </section>
<section class="white-section" id="features"> ... </section>
<section class="colored-section" id="testimonials"> ... </section>
<section class="colored-section" id="press"> ... </section>
<section class="white-section" id="pricing"> ... </section>
<section class="colored-section" id="cta"> ... </section>

六个 <section>,颜色交替——这是「视觉节奏」:粉底白字 → 白底黑字 → 粉底白字。

id 同时被导航栏用作锚点(href="#footer"、#pricing、#cta)——一物两用:既是 CSS 钩子又是跳转目标。

(3)栅格:row + col-lg-6 / col-lg-4 / col-md-6

1
2
3
4
      <div class="row">
<div class="col-lg-6"> ... 文字 + 两个按钮 ... </div>
<div class="col-lg-6"> ... <img class="title-image" ...> </div>
</div>

Bootstrap 的 12 列栅格:

  • col-lg-6 = 在大屏(≥992px)占 6/12 = 一半;
  • col-lg-4 = 三列并排(特性区、定价区);
  • col-md-6 = 在中屏(≥768px)占一半、小屏自动堆叠。

这就是「响应式」不用写媒体查询的原因——栅格类名自己带断点。

(4)导航栏:navbar-expand-lg + 汉堡按钮

1
2
3
4
5
6
7
8
9
      <nav class="navbar navbar-expand-lg navbar-dark">
<a class="navbar-brand" href="">tindog</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav ml-auto"> ... </ul>
</div>
</nav>

四个类的分工:

类 作用
navbar-expand-lg≥lg 时展开成横排,<lg 时收成汉堡
navbar-dark白字(配深色背景)
collapse + id小屏时可折叠区域
ml-automargin-left: auto → 菜单靠右

data-toggle="collapse" data-target="#...":点击按钮 → jQuery 插件把 #... 展开/收起。这就是那三个 JS 文件的用处。

(5)轮播:carousel

1
2
3
4
5
6
7
8
    <div id="testimonial-carousel" class="carousel slide" data-ride="false">
<div class="carousel-inner">
<div class="carousel-item active container-fluid"> ... </div>
<div class="carousel-item container-fluid"> ... </div>
</div>
<a class="carousel-control-prev" href="#testimonial-carousel" role="button" data-slide="prev">
<a class="carousel-control-next" href="#testimonial-carousel" role="button" data-slide="next">
</div>

五个要素:

  1. 外层 .carousel + id;
  2. .carousel-inner 装所有页;
  3. .carousel-item,第一页必须加 .active(否则什么都不显示);
  4. 左右箭头 data-slide="prev/next";
  5. data-ride="false":不自动播放(课程为了演示手动切换)。

(6)定价卡:card 组件

1
2
3
4
5
6
7
8
9
        <div class="card">
<div class="card-header"><h3>Chihuahua</h3></div>
<div class="card-body">
<h2 class="price-text">Free</h2>
<p>5 Matches Per Day</p>
...
<button class="btn btn-lg btn-block btn-outline-dark" type="button">Sign Up</button>
</div>
</div>

card / card-header / card-body 是 Bootstrap 的卡片组件——自带边框、圆角、内边距。

btn-block 让按钮占满整行。

三档套餐的名字是狗的品种:Chihuahua(免费)/ Labrador($49)/ Mastiff($99)——用品种大小暗示价格梯度,这是设计巧思。

(7)三处真实的笔误

1
            <p>Pirority Listing</p>

Pirority 应该是 Priority——拼错了。

1
      <button class="download-button btn btn-lg brn-light" type="button">

brn-light 应该是 btn-outline-light——brn 拼错 + 类名不存在,所以这个按钮没有描边样式(只剩 btn-lg)。

1
        <div class="pricing-column col-lg-4">

第三张卡只有 col-lg-4,前两张是 col-lg-4 col-md-6——中屏时第三张卡会独占一行(前两张各占一半,第三张换行占满)。这是遗漏,不是设计。

css/styles.css(179 行)——六个关键段

(1)字体族名是「假字体」

1
2
3
4
5
6
7
8
9
10
11
12
body {
font-family: "Montserrat";
text-align: center;
}

h1, h2, h3, h4, h5, h6 {
font-family: "Montserrat-Bold";
}

.big-heading {
font-family: "Montserrat-Black";
}

HTML 里只引入了 Montserrat|Ubuntu 两个字体——但 CSS 里写了 Montserrat-Bold、Montserrat-Black、Montserrat-Light。

这些「带后缀的名字」不是真实字体——它们是课程作者在本地装了「Montserrat Bold/Black/Light」的独立字体文件后的名字。在别人电脑上,font-family: "Montserrat-Black" 匹配不到,会回退到默认字体。

正确写法是用 font-weight:

1
h1 { font-family: "Montserrat", sans-serif; font-weight: 900; }

这是这份代码最影响「换台电脑就变样」的问题。

(2)transform: rotate(25deg) + position: absolute

1
2
3
4
5
6
.title-image {
width: 60%;
transform: rotate(25deg);
position: absolute;
right: 30%;
}

手机样机斜着放——这是 TinDog 的视觉标志。

position: absolute 让它脱离文档流,所以后面的 #features 需要 position: relative 才不会被盖住:

1
2
3
#features {
position: relative;
}

这是一个「补救式」的写法:因为绝对定位的图片会溢出到下一个 section,靠给下一个 section 加 position: relative + 背景色,把它盖住。这是很取巧的做法(现代会用 overflow: hidden 或 z-index)。

(3)border-radius: 100% 做圆形头像

1
2
3
4
5
.testimonial-image {
width: 10%;
border-radius: 100%;
margin: 20px;
}

100% 比 50% 更「保险」:50% 在非正方形上会变椭圆,100% 也是——但视觉上都够圆。严格说 50% 才是标准圆。

(4)媒体查询只写了一个

1
2
3
4
5
6
7
8
9
10
@media (max-width: 1028px) {
#title {
text-align: center;
}

.title-image {
position: static;
transform: rotate(0);
}
}

窄屏时:标题居中、手机图取消绝对定位、取消旋转——回到「图在文字下面」的正常流。

position: static 是「取消绝对定位」的正确写法(不是 none)。

注意断点写的是 1028px——Bootstrap 的 lg 断点是 992px。这里和栅格的断点不一致,导致 992~1028 之间会出现「两栏布局但图还在旋转」的错位。这是个细节 bug。

(5).container-fluid { padding: 7% 15%; }

% 内边距是相对「父容器的宽度」算的——上下 7%、左右 15% 都按宽度算,这是 CSS 的规则(padding 的百分比一律基于宽度)。很多人以为是基于高度,这是常见误解。

(6)两个空的注释段落

1
2
3
4
/* CTA Section */


/* Footer Section */

注释在,样式没有——说明这两块样式还没写(#cta 和页脚只靠 Bootstrap 默认样式)。

这份代码的问题

  1. Montserrat-Bold 等是假字体名,换电脑就失效。
  2. 三处拼写错误:Pirority、brn-light、第三张卡缺 col-md-6。
  3. 绝对定位的图片靠 position: relative 硬盖。
  4. 媒体查询断点 1028 与栅格 992 不一致。
  5. #cta 和页脚没有自定义样式(注释空着)。
  6. 没有 <meta name="viewport">:手机上 Bootstrap 的响应式失效!这是最严重的一处——<head> 里只有 charset 和 title,少了 viewport,所以 col-lg/col-md 的判断基准是「桌面宽度」,手机上看到的是缩小版桌面。

第 6 点是真 bug,可以直接对照 Day 56 的模板(那里有 <meta name="viewport" content="width=device-width, initial-scale=1" />)。

README.md(8 行)

标题 Day 58 - Bootstrap,知识点:What is Bootstrap、Installing Bootstrap、Bootstrap Components、Bootstrap Grid Layout System。

四条:是什么、怎么装、组件、栅格。这一天不是写代码,是学「用别人的组件」——导航栏、轮播、卡片、按钮全是 Bootstrap 现成的,自己只写了颜色和间距。

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