<!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>
*{
margin: 0;
padding: 0;
list-style: none;
}
ul{
width: 1000px;
border: 10px red solid;
/* 将ul设置为弹性容器 */
display: flex;
/*
flex-direction 指定容器中弹性元素的排列方式
可选值:
row 默认值,弹性元素在容器中水平排列(左向右)
- 主轴 自左向右
row-reverse 弹性元素在容器中反向水平排列(右向左)
- 主轴 自右向左
column 弹性元素纵向排列(自上向下)
column-reverse 弹性元素方向纵向排列(自下向上)
主轴:
弹性元素的排列方向称为主轴
侧轴:
与主轴垂直方向的称为侧轴
*/
flex-direction: row;
}
li{
width: 200px;
height: 100px;
background-color: #bfa;
font-size: 50px;
text-align: center;
line-height: 100px;
/*
弹性元素的属性:
flex-grow 指定弹性元素的伸展的系数
- 当父元素有多余空间的时,子元素如何伸展
- 父元素的剩余空间,会按照比例进行分配
flex-shrink 指定弹性元素的收缩系数
- 当父元素中的空间不足以容纳所有的子元素时,如果对子元素进行收缩
*/
flex-grow: 1;
/*
*/
flex-shrink: 1;
}
li:nth-child(1){
flex-grow: 0;
flex-shrink: 1;
}
li:nth-child(2){
background-color: pink;
flex-grow: 2;
flex-shrink: 2;
}
li:nth-child(3){
background-color: orange;
flex-grow: 3;
flex-shrink: 3;
}
</style>
</head>
<body>
<!--
flex(弹性盒、伸缩盒)
- 是CSS中的又一种布局手段,它主要用来代替浮动来完成页面的布局
- flex可以使元素具有弹性,让元素可以跟随页面的大小的改变而改变
- 弹性容器
- 要使用弹性盒,必须先将一个元素设置为弹性容器
- 我们通过 display 来设置弹性容器
display:flex 设置为块级弹性容器
display:inline-flex 设置为行内的弹性容器
- 弹性元素
- 弹性容器的子元素是弹性元素(弹性项)
- 弹性元素可以同时是弹性容器
-->
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</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>
<link rel="stylesheet" href="css/reset.css">
<style>
.nav{
width: 1210px;
height: 48px;
line-height: 48px;
margin: 50px auto;
background-color: #E8E7E3;
/* 设置为弹性容器 */
display: flex;
}
.nav li{
/* 设置增长系数 */
flex-grow: 1;
}
.nav a{
display: block;
color: #808080;
text-decoration: none;
font-size: 16px;
text-align: center;
}
.nav a:hover{
background-color: #636363;
color: #fff;
}
</style>
</head>
<body>
<ul class="nav">
<li><a href="#">HTML/CSS</a></li>
<li><a href="#">Browser Side</a></li>
<li><a href="#">Server Side</a></li>
<li><a href="#">Programming</a></li>
<li><a href="#">XML</a></li>
<li><a href="#">Web Building</a></li>
<li><a href="#">Reference</a></li>
</ul>
</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>
*{
margin: 0;
padding: 0;
list-style: none;
}
ul{
width: 100px;
border: 10px red solid;
/* 设置ul为弹性容器 */
display: flex;
/* flex-direction: column; */
/* flex-wrap:
设置弹性元素是否在弹性容器中自动换行
可选值:
nowrap 默认值,元素不会自动换行
wrap 元素沿着辅轴方向自动换行
wrap-reverse 元素沿着辅轴反方向换行
*/
/* flex-wrap: wrap-reverse; */
/* flex-flow: wrap 和 direction 的简写属性 */
/* flex-flow: row wrap; */
/*
justify-content
- 如何分配主轴上的空白空间(主轴上的元素如何排列)
- 可选值:
flex-start 元素沿着主轴起边排列
flex-end 元素沿着主轴终边排列
center 元素居中排列
space-around 空白分布到元素两侧
space-between 空白均匀分布到元素间
space-evenly 空白分布到元素的单侧
*/
/* justify-content: center; */
}
li{
width: 200px;
height: 100px;
background-color: #bfa;
font-size: 50px;
text-align: center;
line-height: 100px;
flex-shrink: 0;
}
/* li:nth-child(1){
} */
li:nth-child(2){
background-color: pink;
}
li:nth-child(3){
background-color: orange;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</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>
*{
margin: 0;
padding: 0;
list-style: none;
}
ul{
width: 600px;
height: 800px;
border: 10px red solid;
/* 设置ul为弹性容器 */
display: flex;
/* flex-direction: column; */
/* flex-wrap:
设置弹性元素是否在弹性容器中自动换行
可选值:
nowrap 默认值,元素不会自动换行
wrap 元素沿着辅轴方向自动换行
wrap-reverse 元素沿着辅轴反方向换行
*/
/* flex-wrap: wrap-reverse; */
/* flex-flow: wrap 和 direction 的简写属性 */
flex-flow: row wrap;
/*
justify-content
- 如何分配主轴上的空白空间(主轴上的元素如何排列)
- 可选值:
flex-start 元素沿着主轴起边排列
flex-end 元素沿着主轴终边排列
center 元素居中排列
space-around 空白分布到元素两侧
space-between 空白均匀分布到元素间
space-evenly 空白分布到元素的单侧
*/
/* justify-content: center; */
/*
align-items:
- 元素在辅轴上如何对齐
- 元素间的关系
- 可选值:
stretch 默认值,将元素的长度设置为相同的值
flex-start 元素不会拉伸,沿着辅轴起边对齐
flex-end 沿着辅轴的终边对齐
center 居中对齐
baseline 基线对齐
*/
/* justify-content: center; */
align-items: flex-start;
/* align-content: 辅轴空白空间的分布 */
align-content: space-between;
}
li{
width: 200px;
background-color: #bfa;
font-size: 50px;
text-align: center;
line-height: 100px;
flex-shrink: 0;
}
li:nth-child(1){
/* align-self: 用来覆盖当前弹性元素上的align-items */
align-self: stretch;
}
li:nth-child(2){
background-color: pink;
}
li:nth-child(3){
background-color: orange;
}
li:nth-child(4){
background-color: yellow;
}
li:nth-child(5){
background-color: chocolate;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>
2
<div>2</div>
</li>
<li>
3
<div>3</div>
<div>3</div>
</li>
<li>1</li>
<li>
2
<div>2</div>
</li>
</ul>
</body>
</html>
练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
/* 设置外层的容器 */
.nav {
width: 100%;
}
/* 设置每一行的容器 */
.nav-inner {
/* 设置为弹性容器 */
display: flex;
/* 设置主轴上空白分布 */
justify-content: space-around;
}
.item {
width: 20%;
text-align: center;
}
.item img {
/* 设置图片宽度 */
width: 100%;
}
.item a {
color: #333;
text-decoration: none;
font-size: 16px;
}
</style>
</head>
<body>
<!-- 创建一个外部容器 -->
<nav class="nav">
<div class="nav-inner">
<div class="item">
<a href="#">
<img src="img/16/1.png" alt="">
<span>天猫</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="img/16/2.png" alt="">
<span>聚划算</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="img/16/3.png" alt="">
<span>天猫国际</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="img/16/4.png" alt="">
<span>外卖</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="img/16/5.png" alt="">
<span>天猫超市</span>
</a>
</div>
</div>
<div class="nav-inner">
<div class="item">
<a href="#">
<img src="img/16/6.png" alt="">
<span>充值中心</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="img/16/7.png" alt="">
<span>飞猪旅行</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="img/16/8.png" alt="">
<span>领金币</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="img/16/9.png" alt="">
<span>拍卖</span>
</a>
</div>
<div class="item">
<a href="#">
<img src="img/16/10.png" alt="">
<span>分类</span>
</a>
</div>
</div>
</nav>
</body>
</html>
VW适配
<!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>
*{
margin: 0;
padding: 0;
}
html{
/*
网页中字体大小最小是12px,不能设置一个比12像素还小的字体
如果我们设置了一个小于12px的字体,则字体自动设置为12
0.1333333vw = 1px
5.3333vw = 40px
*/
font-size: 5.3333vw;
}
.box1{
/*
rem
- 1 rem = 1 html的字体大小
- 1 rem = 40 px(设计图)
*/
width: 18.75rem;
width: 8.75rem;
height: 0.875rem;
background-color: #bfa;
}
</style>
</head>
<body>
<!--
48 x 35
-->
<div class="box1"></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>
/*
使用媒体查询
语法: @media 查询规则{}
媒体类型:
all 所有设备
print 打印设备
screen 带屏幕的设备
speech 屏幕阅读器
- 可以使用,连接多个媒体类型,这样它们之间就是一个或的关系
可以在媒体类型前添加一个only,表示只有。
only的使用主要是为了兼容一些老版本浏览器
*/
/* @media print,screen{
body{
background-color: #bfa;
}
} */
@media only screen {
body{
background-color: #bfa;
}
}
</style>
</head>
<body>
<!--
响应式布局
- 网页可以根据不通的设备或窗口大小呈现出不同的效果
- 使用响应式布局,可以使一个网页适用于所有设备
- 响应布局的关键就是 媒体查询
- 通过媒体查询,可以为不通的设备,或设备不同状态来分别设置样式
-->
</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>
/*
媒体特性:
width 视口的宽度
height 视口的高度
min-width 视口的最小宽度(视口大于指定宽度时生效)
max-width 视口的最大宽度(视口小于指定宽度时生效)
*/
/* @media (max-width: 500px){
body{
background-color: #bfa;
}
} */
/*
样式切换的分界点,我们称其为断点,也就是网页的样式会在这个点时发生变化
一般比较常用的断点
小于768 超小屏幕 max-width=768px
大于768 小屏幕 min-width=768px
大于992 中型屏幕 min-width=992px
大于1200 大屏幕 min-width=1200px
*/
@media only screen and (min-width: 500px) and (max-width:700px){
body{
background-color: #bfa;
}
}
</style>
</head>
<body>
</body>
</html>