flex布局
弹性练习链接
Flexbox Froggy - A game for learning CSS flexbox
flex布局display:flex;
- flex布局找父元素设置样式display:flex;
- 子元素布局
- 可以设置宽高和margin
属性 | 说明 |
flex-direction |
内部元素是如何在 flex 容器中布局 row(默认)横向从左到右 | row-reverse横向从右到左 | column纵向从上到下 | column-reverse纵向从下到上 |
justify-content |
水平对齐弹性容器的方式 flex-start向左排列、flex-end向右排列、center居中、space-between两边排列,左右两边没有间隔、space-around均匀排列,左右两边有间隔、space-evenly每个元素之间和元素距离边距的距离都相等 |
align-items |
垂直对齐弹性容器的方式 flex-start从上到下排列、flex-end从下到上排列、center居中 |
flex-wrap | 自动换行 wrap换行 nowrap不换行(默认) |
flex | 自适应 flex:1 1 15px; 语法:flex:flex-grow flex-shrink flex-basis;(flex:剩余空间相对比例 收缩规则 主轴方向上的初始大小) flex:1; 一个值,无单位数字就是设置flex-grow(常用) flex:15px; 一个值,有单位数字就是设置flex-basis flex:1 1; 两个值,都无单位就是设置flex-grow和flex-shrink flex:1 15px; 两个值,就是设置flex-grow和flex-basis flex: auto; |
代码:
<!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>
.box {
width: 100%;
height: 150px;
border: 1px solid red;
}
.child {
width: 50px;
height: 20px;
background-color: rgb(49, 73, 125);
}
</style>
</head>
<body>
<!-- 水平:居中 垂直:居中 -->
<div class="box" style="display: flex; justify-content: center; align-items: center;">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
</div>
<!-- 水平:向左布局 垂直:居中 -->
<div class="box" style="display: flex; justify-content: left; align-items: center;">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
</div>
<!-- 水平:向右布局 垂直方向:居中 -->
<div class="box" style="display: flex; justify-content: right; align-items: center;">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
</div>
<!-- 水平:左右布局 垂直:靠上 -->
<div class="box" style="display: flex; justify-content: space-between; align-items: flex-start;">
<div class="child">1</div>
<div class="child">2</div>
</div>
<!-- 水平:左右布局,两边有空 垂直:靠上 -->
<div class="box" style="display: flex; justify-content: space-around; align-items: flex-start;">
<div class="child">1</div>
<div class="child">2</div>
</div>
<!-- 水平:向下 垂直:居中 -->
<div class="box" style="display: flex; flex-direction: column; justify-content: center; align-items: center; ">
<div class="child">1</div>
<h1>img</h1>
<button>安装</button>
</div>
</body>
</html>
效果:
flex-wrap:wrap;演示:
代码:
<!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>
.box{
width: 500px;
height: 500px;
border: 5px solid rgb(102, 192, 132);
box-sizing: border-box;
display: flex;
/* 自动换行 */
flex-wrap: wrap;
}
.item{
width: 100px;
height: 100px;
border: 5px solid rgb(219, 144, 144);
}
</style>
</head>
<body>
<div class="box">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</body>
</html>
效果:
flex属性练习:
代码:
<!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;
}
.box {
/* 总的宽度 */
width: 500px;
height: 100px;
border: rgb(102, 192, 132);
box-sizing: border-box;
display: flex;
/* 自动换行 */
flex-wrap: wrap;
}
.item1 {
/* 已知宽度 */
width: 100px;
height: 100px;
background: rgb(219, 144, 144);
}
.item2 {
background: rgb(95, 182, 86);
/* 自适应 */
/*
(总的宽度-已知固定的宽度) / 总的份数 * 你的占的份数
(500 - 100)/ 5 * 3 = 240px
*/
flex: 3;
}
.item3 {
background: rgb(145, 182, 86);
/* 自适应 */
/*
(总的宽度-已知固定的宽度) / 总的份数 * 你的占的份数
(500 - 100)/ 5 * 2 = 160px
*/
flex: 2;
}
</style>
</head>
<body>
<div class="box">
<div class="item1">固定宽度100px</div>
<div class="item2">自适应</div>
<div class="item3">自适应</div>
</div>
</body>
</html>
效果:
仿写Sass网页
Sass世界上最成熟、稳定和强大的CSS扩展语言 | Sass中文网
代码:
html.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>
<link rel="stylesheet" href="./css/css.css">
<style>
/* 移除标签的默认属性 */
*{
padding: 0;
margin: 0;
}
body{
/* 确定网页最小尺寸,防止网页被压缩 */
min-width: 1200px;
}
</style>
</head>
<body>
<div>
<!-- 头部 -->
<div class="header">
<div class="sass">Sass中文网</div>
<div class="nav">
<div>首页</div>
<div>安装</div>
<div>快速入门</div>
<div>中文文档</div>
<div>常用问题</div>
<div>使用技巧</div>
<div>Sass在线转换</div>
<div>关于</div>
<div>
<select name="" id="">
<option value="">Language</option>
</select>
</div>
</div>