一、 flex布局体验
1. 传统布局与flex布局
2.初体验
⑴. 搭建HTML结构
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
里面的3个span是行内元素
⑵. CSS样式
① span 直接给宽度和高度,背景颜色,还有蓝色边框
② 给 div 只需要添加 “display:flex” 即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
display: flex;
width: 80%;
height: 300px;
background-color: pink;
justify-content: space-around;
}
div span {
/* width: 150px; */
height: 100px;
background-color: purple;
margin-right: 5px;
flex: 1;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
二、 flex布局原理
flex 是 flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性,任何一个容器都可以 指定为 flex 布局。
当我们为父盒子设为 flex 布局以后,子元素的 float、clear 和 vertical-align 属性将失效。
伸缩布局 = 弹性布局 = 伸缩盒布局 = 弹性盒布局 =flex布局
1. 布局原理
采用 Flex 布局的元素,称为 Flex 容器(flex container),简称"容器"。它的所有子元素自动成为容器成 员,称为 Flex 项目(flex item),简称"项目"。
体验中 div 就是 flex父容器。
体验中 span 就是 子容器 flex项目
子容器可以横向排列也可以纵向排列
总结flex布局原理:
就是通过给父盒子添加flex属性,来控制子盒 子的位置和排列方式
三、flex布局父项常见属性
1. 常见父项属性
flex-direction:设置主轴的方向
justify-content:设置主轴上的子元素排列方式
flex-wrap:设置子元素是否换行
align-content:设置侧轴上的子元素的排列方式(多行)
align-items:设置侧轴上的子元素排列方式(单行)
flex-flow:复合属性,相当于同时设置了 flex-direction 和 flex-wrap
2. flex-direction 设置主轴的方向
⑴ 主轴与侧轴
在 flex 布局中,是分为主轴和侧轴两个方向,同样的叫法有 : 行和列、x 轴和y 轴
默认主轴方向就是 x 轴方向,水平向右
默认侧轴方向就是 y 轴方向,水平向下 主轴,水平向右
⑵. 属性值
flex-direction 属性决定主轴的方向(即项目的排列方向)
注意: 主轴和侧轴是会变化的,就看 flex-direction 设置谁为主轴,剩下的就是侧轴。而我们的子元素是跟着主轴来排列的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
/* 给父级添加flex属性 */
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x 轴 行 row 那么y轴就是侧轴喽 */
/* 我们的元素是跟着主轴来排列的 */
/* flex-direction: row; */
/* 简单了解 翻转 */
/* flex-direction: row-reverse; */
/* 我们可以把我们的主轴设置为 y轴 那么 x 轴就成了侧轴 */
flex-direction: column;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
3. justify-content 设置主轴上的子元素排列方式
justify-content 属性定义了项目在主轴上的对齐方式
注意: 使用这个属性之前一定要确定好主轴是哪个
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x 轴 row */
flex-direction: row;
/* justify-content: 是设置主轴上子元素的排列方式 */
/* justify-content: flex-start; */
/* justify-content: flex-end; */
/* 让我们子元素居中对齐 */
/* justify-content: center; */
/* 平分剩余空间 */
/* justify-content: space-around; */
/* 先两边贴边, 在分配剩余的空间 */
justify-content: space-between;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
⑴flex-satrt
⑵flex-end
⑶center
⑷space-around
⑸space-between
4. flex-wrap 设置子元素是否换行
默认情况下,项目都排在一条线(又称”轴线”)上。flex-wrap属性定义,flex布局中默认是不换行的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
display: flex;
width: 600px;
height: 400px;
background-color: pink;
/* flex布局中,默认的子元素是不换行的, 如果装不开,会缩小子元素的宽度,放到父元素里面 */
flex-wrap: nowrap;
/* flex-wrap: wrap; */
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
</body>
</html>
⑴nowrap:flex布局中,默认的子元素是不换行的, 如果装不开,会缩小子元素的宽度,放到父元素里面
⑵wrap
5. align-items 设置侧轴上的子元素排列方式(单行 )
该属性是控制子项在侧轴(默认是y轴)上的排列方式 在子项为单项(单行)的时候使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 默认的主轴是 x 轴 row */
flex-direction: column;
justify-content: center;
/* 我们需要一个侧轴居中 */
/* 拉伸,但是子盒子不要给高度 */
/* align-items: stretch; */
align-items: center;
/* align-content: center; */
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
⑴flex-start
⑵flex-end
⑶center
⑷stretch
6. align-content 设置侧轴上的子元素的排列方式(多行)
设置子项在侧轴上的排列方式 并且只能用于子项出现 换行 的情况(多行),在单行下是没有效果的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 换行 */
flex-wrap: wrap;
/* 因为有了换行,此时我们侧轴上控制子元素的对齐方式我们用 align-content */
align-content: flex-start;
/* align-content: center; */
/* align-content: space-between; */
/* align-content: space-around; */
}
div span {
width: 150px;
height: 100px;
background-color: purple;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
</div>
</body>
</html>
⑴flex-start
⑵center
⑶space-around
⑷space-between
align-content 和 align-items 区别
align-items 适用于单行情况下, 只有上对齐、下对齐、居中和 拉伸
align-content 适应于换行(多行)的情况下(单行情况下无效), 可以设置 上对齐、 下对齐、居中、拉伸以及平均分 配剩余空间等属性值。
总结就是单行找 align-items 多行找 align-content 单行align-items 单行align-items 多行align-content
7.flex-flow
flex-flow 属性是 flex-direction 和 flex-wrap 属性的复合属性
flex-flow:row wrap;
flex-direction:设置主轴的方向
justify-content:设置主轴上的子元素排列方式
flex-wrap:设置子元素是否换行
align-content:设置侧轴上的子元素的排列方式(多行)
align-items:设置侧轴上的子元素排列方式(单行)
flex-flow:复合属性,相当于同时设置了 flex-direction 和 flex-wrap
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
display: flex;
width: 600px;
height: 300px;
background-color: pink;
/* flex-direction: column;
flex-wrap: wrap; */
/* 把设置主轴方向和是否换行(换列)简写 */
flex-flow: column wrap;
}
div span {
width: 150px;
height: 100px;
color: aliceblue;
background-color: purple;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
</body>
</html>
四、flex布局子项常见属性
flex 子项目占的份数
align-self 控制子项自己在侧轴的排列方式
order属性定义子项的排列顺序(前后顺序)
1. flex 属性
flex 属性定义子项目分配剩余空间,用flex来表示占多少份数。
.item {
flex: <number>; /* default 0 */
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
section {
display: flex;
width: 60%;
height: 150px;
background-color: pink;
margin: 0 auto;
}
section div:nth-child(1) {
width: 100px;
height: 150px;
background-color: red;
}
section div:nth-child(2) {
flex: 1;
background-color: green;
}
section div:nth-child(3) {
width: 100px;
height: 150px;
background-color: blue;
}
p {
display: flex;
width: 60%;
height: 150px;
background-color: pink;
margin: 100px auto;
}
p span {
flex: 1;
}
p span:nth-child(2) {
flex: 2;
background-color: purple;
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
</section>
<p>
<span>1</span>
<span>2</span>
<span>3</span>
</p>
</body>
</html>
2. align-self 控制子项自己在侧轴上的排列方式
align-self 属性允许单个项目有与其他项目不一样的对齐方式,可覆盖 align-items 属性。 默认值为 auto,表示继承父元素的 align-items 属性,如果没有父元素,则等同于 stretch。
span:nth-child(2) {
/* 设置自己在侧轴上的排列方式 */
align-self: flex-end;
3. order 属性定义项目的排列顺序
数值越小,排列越靠前,默认为0。
注意:和 z-index 不一样。
.item {
order: <number>;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
display: flex;
width: 80%;
height: 300px;
background-color: pink;
/* 让三个子盒子沿着侧轴底侧对齐 */
/* align-items: flex-end; */
/* 我们想只让3号盒子下来底侧 */
}
div span {
width: 150px;
height: 100px;
background-color: purple;
margin-right: 5px;
}
div span:nth-child(2) {
/* 默认是0 -1比0小所以在前面 */
order: -1;
}
div span:nth-child(3) {
align-self: flex-end;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
五、补充渐变色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
width: 600px;
height: 200px;
/* 背景渐变必须添加浏览器私有前缀 */
/* background: -webkit-linear-gradient(left, red, blue); */
/* background: -webkit-linear-gradient(red, blue); */
background: -webkit-linear-gradient(top left, red, blue);
}
</style>
</head>
<body>
<div></div>
</body>
</html>
让我们逐步解析这段代码 background: -webkit-linear-gradient(top left, red, blue); 及其相关知识点:
1. 私有前缀的作用
-webkit- 是 WebKit 内核浏览器(如 Chrome、Safari)的私有前缀,用于兼容旧版本浏览器(如 Chrome 26 之前或 Safari 9 之前)。
为什么需要添加前缀?
早期浏览器对 CSS3 标准支持不一致,需通过私有前缀确保兼容性。现代浏览器已支持标准语法 linear-gradient(),但为兼容旧版本,仍需保留前缀。
2. 线性渐变语法
标准语法 vs 私有前缀语法
标准语法(W3C):
background: linear-gradient(方向, 颜色1, 颜色2);
方向:可以是角度(如 45deg)或方向关键字(如 to bottom right)。
示例:linear-gradient(to bottom right, red, blue) 表示从左上到右下渐变。
WebKit 私有语法:
background: -webkit-linear-gradient(起始点, 颜色1, 颜色2);
起始点:直接指定渐变的起始方向(如 top left),无需加 to。
示例:-webkit-linear-gradient(top left, red, blue) 表示从左上角开始向右下角渐变。
3. 代码解析
background: -webkit-linear-gradient(top left, red, blue);
参数说明:
top left:
指定渐变的起始点为 左上角,渐变方向为 向右下角(即从左上到右下)。
对应标准语法的方向是 to bottom right。
red, blue:
渐变从红色(起始点)平滑过渡到蓝色(终点)。
4. 方向参数的差异
语法类型 | 方向写法 | 示例 | 效果 |
---|---|---|---|
标准语法 | to bottom right | linear-gradient(to bottom right, red, blue) | 从左上到右下渐变 |
WebKit 私有语法 | top left (起始点) | -webkit-linear-gradient(top left, red, blue) | 同上效果,但语法不同 |
5. 其他方向参数的写法
⑴.方向关键字
标准语法:
to top /* 从下到上 */
to bottom /* 从上到下(默认) */
to left /* 从右到左 */
to right /* 从左到右 */
to bottom right /* 从左上到右下 */
WebKit 私有语法:
top left /* 从左上到右下 */
bottom right /* 从右下到左上 */
left /* 从左到右(等同于 to right) */
⑵.角度参数
标准语法:
45deg
表示从 45度方向 开始渐变(以水平向右为 0度,顺时针旋转)。- 示例:
linear-gradient(45deg, red, blue)
。WebKit 私有语法:
- 支持角度参数,但需注意角度规则与标准语法一致。
6. 兼容性写法
为兼容不同浏览器,建议同时添加多个前缀和标准语法:
.div {
/* 旧版浏览器 */
background: -webkit-linear-gradient(top left, red, blue); /* Chrome/Safari */
background: -moz-linear-gradient(top left, red, blue); /* Firefox */
background: -o-linear-gradient(top left, red, blue); /* Opera */
/* 现代浏览器(标准语法) */
background: linear-gradient(to bottom right, red, blue);
}
六、 携程网首页案例制作
案例:携程网移动端首页
1. 技术选型
方案:我们采取单独制作移动页面方案
技术:布局采取flex布局
2. 搭建相关文件夹结构
3. 设置视口标签以及引入初始化样式
<meta name="viewport" content="width=device-width, user-scalable=no,
initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/index.css">
4. 常用初始化样式
body {
/* 设置页面主体最大宽度为 540px */
max-width: 540px;
/* 设置页面主体最小宽度为 320px */
min-width: 320px;
/* 使页面主体在水平方向上居中 */
margin: 0 auto;
/* 设置字体样式,正常字体,字号 14px,行高 1.5,字体族依次为 Tahoma 等 */
font: normal 14px/1.5 Tahoma, "Lucida Grande", Verdana, "Microsoft Yahei", STXihei, hei;
/* 设置文字颜色为黑色 */
color: #000;
/* 设置页面主体背景颜色为浅灰色 */
background: #f2f2f2;
/* 隐藏水平滚动条 */
overflow-x: hidden;
/* 去除移动端点击链接或按钮时出现的高亮效果 */
-webkit-tap-highlight-color: transparent;
}
5. 常见模块命名
6. 常见flex布局思路
7. 背景线性渐变
语法1:
background: linear-gradient(起始方向, 颜色1, 颜色2, ...);
background: -webkit-linear-gradient(left, red , blue);
background: -webkit-linear-gradient(left top, red , blue);
背景渐变必须添加浏览器私有前缀
起始方向可以是: 方位名词 或者 度数 , 如果省略默认就是 top
七、练习
1.顶部搜索模块
html:
<!-- 顶部搜索 -->
<div class="search-index">
<div class="search">搜索:目的地/酒店/景点/航班号</div>
<a href="#" class="user">我 的</a>
</div>
css:
/* 搜索模块 */
.search-index {
display: flex;
/* 固定定位跟父级没有关系 它以屏幕为准 */
position: fixed;
top: 0;
left: 50%;
/* 固定的盒子应该有宽度 */
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
width: 100%;
min-width: 320px;
max-width: 540px;
height: 44px;
/* background-color: pink; */
background-color: #F6F6F6;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
.search {
position: relative;
height: 26px;
line-height: 24px;
border: 1px solid #ccc;
flex: 1;
font-size: 12px;
color: #666;
margin: 7px 10px;
padding-left: 25px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
}
.search::before {
content: "";
position: absolute;
top: 5px;
left: 5px;
width: 15px;
height: 15px;
background: url(../images/sprite.png) no-repeat -59px -279px;
background-size: 104px auto;
}
.user {
width: 44px;
height: 44px;
/* background-color: purple; */
font-size: 12px;
text-align: center;
color: #2eaae0;
}
.user::before {
content: "";
display: block;
width: 23px;
height: 23px;
background: url(../images/sprite.png) no-repeat -59px -194px;
background-size: 104px auto;
margin: 4px auto -2px;
}
⑴.position: fixed;
用途
- 创建固定导航栏:你可以让导航栏在页面滚动时始终出现在页面顶部或者底部,这样用户就能随时访问导航链接。
- 实现悬浮广告:使广告一直显示在页面的某个位置,不管用户如何滚动页面都能看到。
- 固定返回顶部按钮:把返回顶部按钮固定在页面右下角,方便用户随时返回页面顶部。
特点
- 脱离文档流:设置了
position: fixed;
的元素会脱离正常的文档流,不会对其他元素的布局产生影响,其他元素会把它当作不存在一样进行布局。 - 相对于浏览器窗口定位:元素的位置是相对于浏览器窗口来确定的,不会随着页面的滚动而移动。
- 需要使用偏移属性:通常要配合
top
、right
、bottom
、left
这些偏移属性来确定元素在浏览器窗口中的具体位置。
这个效果是搜索框一直在顶部
⑵display:flex; 和flex:1;
html:
<div class="search-index">
<div class="search"></div>
<a href="#" class="user">我的/a>
</div>
css:
.search-index {
display: flex;
position: fixed;
top: 0;
background-color: pink;
/* 将元素的左侧边缘放置在浏览器窗口水平方向的中间位置 */
left: 50%;
/* 使用 -webkit- 前缀的变换属性,将元素在水平方向上向左移动自身宽度的 50%,以实现元素在水平方向上的居中 */
-webkit-transform: translateX(-50%);
/* 标准的变换属性,将元素在水平方向上向左移动自身宽度的 50%,以实现元素在水平方向上的居中 */
transform: translateX(-50%);
/* 设置元素的宽度为浏览器窗口的 100% */
width: 100%;
/* 设置元素的最小宽度为 320px,当窗口宽度小于 320px 时,元素宽度固定为 320px */
min-width: 320px;
/* 设置元素的最大宽度为 540px,当窗口宽度大于 540px 时,元素宽度固定为 540px */
max-width: 540px;
/* 设置元素的高度为 44px */
height: 44px;
}
.search {
flex: 1;
}
.user {
width: 44px;
height: 44px;
background-color: purple;
}
display: flex;
在 CSS 中,display: flex;
是用来开启弹性布局(Flexbox)的属性值。当你对一个元素设置 display: flex;
时,这个元素就会变成一个弹性容器(flex container),而它的直接子元素则会变成弹性项目(flex items)。弹性布局提供了一种高效的方式来分配容器内子元素的空间、对齐和排列它们。
在代码里,.search-index
类的元素被设置了 display: flex;
,这意味着 .search-index
成为了一个弹性容器,它里面的直接子元素(也就是 .search
和 .user
)会按照弹性布局的规则进行排列。
开启弹性布局后,弹性容器有很多与之相关的属性可以用来控制子元素的布局,比如 flex-direction
(决定子元素的排列方向)、justify-content
(控制子元素在主轴上的对齐方式)、align-items
(控制子元素在交叉轴上的对齐方式)等。
flex: 1;
flex
是一个复合属性,它是 flex-grow
、flex-shrink
和 flex-basis
这三个属性的缩写。当你使用 flex: 1;
时,它实际上等价于 flex-grow: 1; flex-shrink: 1; flex-basis: 0;
。下面分别解释这三个属性的含义:
flex-grow
:定义弹性项目的放大比例。如果所有弹性项目的flex-grow
属性都为 1,则它们会等分剩余空间。在你的代码中,.search
元素的flex-grow
为 1,这意味着它会尽可能地占据弹性容器中剩余的空间。flex-shrink
:定义弹性项目的缩小比例。当弹性容器空间不足时,flex-shrink
值越大的元素会被优先缩小。这里.search
元素的flex-shrink
为 1,表示它会参与缩小。flex-basis
:定义弹性项目在分配剩余空间之前的初始大小。flex-basis: 0;
表示.search
元素在分配空间之前的初始大小为 0。
结合 HTML 和 CSS 代码,.search-index
是一个弹性容器,它被固定在页面顶部且水平居中。.search
元素使用 flex: 1;
会占据 .search-index
容器内除了 .user
元素所占据的 44px 宽度之外的所有剩余空间。而 .user
元素有固定的宽度和高度,并且背景颜色为紫色。这样就实现了一个布局,左边是一个可伸缩的搜索区域,右边是一个固定大小的用户区域。
待续未完。。。