1.伸缩菜单项
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
div{
width: 500px;
height: 400px;
border: 1px solid #cccccc;
margin: 0 auto;
}
ul
{
list-style: none;
width: 100%;
display: flex;
}
div>ul>li
{
flex: 1;
height: 36px;
line-height: 36px;
text-align: center;
background-color: chartreuse;
border-right: 1px solid #CCC;
}
</style>
</head>
<body>
<div>
<ul>
<li>首页</li>
<li>商品分类</li>
<li>我的订单</li>
<li>最新商品</li>
</ul>
</div>
</body>
</html>
2.宽高自适应
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
.box{
width: 500px;
height: 600px;
display: flex;
background-color: #cccccc;
margin: 10px auto;
flex-flow: column;
}
main{
width: 100%;
display: flex;
/* 当前元素占据父容器剩余空间
因为默认主轴是水平排列。header和footer占据完了水平空间,水平没空间让main放了
main就放不下,所以应该让主轴column排列
*/
flex: 1;
background-color: blue;
}
main>article{
height: 100%;
flex: 1;
background-color: burlywood;
}
main>aside{
height: 100%;
flex: 3;
background-color: chartreuse;
}
header
{
width: 100%;
height: 60px;
background-color: red;
}
footer{
width: 100%;
height: 80px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="box">
<header></header>
<main>
<article></article>
<aside></aside>
</main>
<footer></footer>
</div>
</body>
</html>
3.携程网案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
header
{
width: 100%;
display: flex;
}
header>a{
flex: 1;
}
header>a>div
{
width: 100%;
height: 90px;
background-color: chartreuse;
}
main
{
width: 100%;
padding: 0 10px;
/* 因为加了padding之后盒子的宽度会发生变换,所欲哦要设置盒模型的大小为加了边框的大小 */
box-sizing: border-box;
}
main>.item
{
width: 100%;
height: 100px;
background-color: aqua;
border-radius: 5px;
margin-top: 10px;
display: flex;
}
main>.item:nth-last-of-type(2)
{
background-color: bisque;
}
main>.item:nth-last-of-type(3)
{
background-color: teal;
}
main>.item:nth-last-of-type(4)
{
background-color: forestgreen;
}
main>.item>.left
{
flex: 1;
}
main>.item>.right
{
flex: 3;
/* 设置换行显示,子元素宽度和必须大于父元素 */
flex-flow: wrap;
display: flex;
}
main>.item>.right>a
{
text-align: center;
width: 50%;
line-height:50px;
/* 加了边框会放不下去 */
border-left: 1px solid #ffffff;
border-bottom: 1px solid #ffffff;
box-sizing: border-box;
text-decoration: none;
display: block;
}
.extra
{
display: flex;
justify-content: space-around;
}
.extra>a
{
flex: 1;
height: 50px;
}
.extra>a:nth-last-of-type(1)>div{
background-color: blue;
width: 100%;
height: 50px;
}
.extra>a:nth-last-of-type(2)>div{
background-color: red;
width: 100%;
height: 50px;
}
footer
{
width: 100%;
}
footer>nav{
width: 100%;
display: flex;
border-top: 1px solid green;
border-top: 1px solid green;
}
footer>nav>a
{
flex: 1;
text-align: center;
line-height: 30px;
color: hotpink;
font-size: 13px;
text-decoration: noneb;
}
footer>.link{
text-align: center;
line-height:25px ;
}
footer>.copyright
{
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<header>
<a href="" style="text-align: center;vertical-align: middle;font-size:50px;line-height: 50px;">
<div>携程</div>
</a>
</header>
<main>
<section class="item">
<div class="left"></div>
<div class="right">
<a href="">海外酒店</a>
<a href="">团购</a>
<a href="">特惠酒店</a>
<a href="">客栈公寓</a>
</div>
</section>
<section class="item">
<div class="left"></div>
<div class="right">
<a href="">海外酒店</a>
<a href="">团购</a>
<a href="">特惠酒店</a>
<a href="">客栈公寓</a>
</div>
</section>
<section class="item">
<div class="left"></div>
<div class="right">
<a href="">海外酒店</a>
<a href="">团购</a>
<a href="">特惠酒店</a>
<a href="">客栈公寓</a>
</div>
</section>
<section class="item">
<div class="left"></div>
<div class="right">
<a href="">海外酒店</a>
<a href="">团购</a>
<a href="">特惠酒店</a>
<a href="">客栈公寓</a>
</div>
</section>
<section class="extra">
<a href="">
<div></div>
</a>
<a href="">
<div></div>
</a>
</section>
</main>
<footer>
<nav>
<a href="">电话预定</a>
<a href="">下载客户端</a>
<a href="">我的订单</a>
</nav>
<p class="link">
<a href="">网站地图</a>
<a href="">ENGLISH</a>
<a href="">电脑版</a>
</p>
<p class="copyright">
©2015携程旅行
</p>
</footer>
</div>
</body>
</html>
5.综合
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
.box
{
width: 800px;
height: 700px;
border: 1px solid rebeccapurple;
box-sizing: border-box;
display: flex;/*设置为flex布局之后,子元素的float clear vertical-align都失效*/
justify-content: space-around;
align-items: stretch;
flex-wrap: nowrap;/*是否换行*/
/* flex-flow: flex-dirction flex-wrap; */
flex-flow: row nowrap;
}
.first
{
width: 200px;
/* height: 200px; */
background-color: springgreen;
order: 4;/*改变元素的顺序*/
flex-grow: 0;/*//定义项目的放大比列,默认为0*/
flex-shrink: 1;/*如果所有项目的flex-shrink都为1,空间不够则等比例缩小*/
}
.second
{
width: 230px;
height: 200px;
background-color: skyblue;
order: 3;
flex-shrink: 1;
}
.third{
width: 200px;
height: 200px;
background-color: salmon;
order: 2;
flex-shrink: 1;
}
.fourth{
width: 200px;
height: 200px;
background-color: red;
order: 1;
flex-shrink: 1;
}
</style>
</head>
<body>
<div class="box">
<div class="first">1</div>
<div class="second">2</div>
<div class="third">3</div>
<div class="fourth">4</div>
</div>
</body>
</html>