Float和Flex布局

[复制链接]
发表于 2025-9-29 22:30:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Flex布局

01-标准流

标准流也叫文档流,指的是标签在页面中默认的排布规则,比方:块元素独占一行,行内元素可以一行表现多个。

02-浮动Float(相识)

根本利用

浮动就是让元素脱离标准流,简称脱标
作用:让块元素水中分列。
属性名:float
属性值


  • left:左对齐
  • right:右对齐
浮动 利用left:左对齐 .one和.two全部脱标
  1. <style>
  2.   /* 特点:顶对齐;具备行内块显示模式特点;浮动的盒子会脱标 */
  3.   .one {
  4.     width: 100px;
  5.     height: 100px;
  6.     background-color: brown;
  7.     float: left;
  8.   }
  9.   .two {
  10.     width: 200px;
  11.     height: 200px;
  12.     background-color: orange;
  13.     float: left;
  14.   }
  15. </style>
  16. <div class="one">one</div>
  17. <div class="two">two</div>
复制代码

浮动 利用right:右对齐 .one和.two全部脱标
  1. <style>
  2.   /* 特点:顶对齐;具备行内块显示模式特点;浮动的盒子会脱标 */
  3.   .one {
  4.     width: 100px;
  5.     height: 100px;
  6.     background-color: brown;
  7.     float: left;
  8.   }
  9.   .two {
  10.     width: 200px;
  11.     height: 200px;
  12.     background-color: orange;
  13.    
  14.     float: right;
  15.   }
  16. </style>
  17. <div class="one">one</div>
  18. <div class="two">two</div>
复制代码

**脱标 ** 下图.one浮动,不在占用标准流的位置(像油飘在水上一样),这就是脱标了。.two没有利用浮动,即没有脱标,以是左上角开始标准流排布。
  1. <style>
  2.   /* 特点:顶对齐;具备行内块显示模式特点;浮动的盒子会脱标 */
  3.   .one {
  4.     width: 100px;
  5.     height: 100px;
  6.     background-color: brown;
  7.     float: left;
  8.   }
  9.   .two {
  10.     width: 200px;
  11.     height: 200px;
  12.     background-color: orange;
  13.   }
  14. </style>
  15. <div class="one">one</div>
  16. <div class="two">two</div>
复制代码

特点:


  • 浮动后的盒子顶对齐
  • 浮动后的盒子具备行内块特点
  • 浮动后的盒子脱标,不占用标准流的位置
脱标的特点

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>Document</title>
  7.   <style>
  8.     *{
  9.       margin: 0;
  10.       padding: 0;
  11.     }
  12.     .father{
  13.       margin: 100px auto;
  14.       height: 300px;
  15.       width: 300px;
  16.       background-color: pink;
  17.     }
  18.     .son1{
  19.       height: 100px;
  20.       width: 300px;
  21.       background-color: skyblue;
  22.     }
  23.     .son2{
  24.       /* float: left; */
  25.       height: 100px;
  26.       font-weight: 700;
  27.       font-size: 20px;
  28.       color: red;
  29.     }
  30.     .son3{
  31.       background-color: #efefef;
  32.     }
  33.   </style>
  34. </head>
  35. <body>
  36.   <div class="father">
  37.     <div class="son1">我是第一个</div>
  38.     <span class="son2">我是第二个</span>
  39.     <div class="son3">我是第三个</div>
  40.   </div>
  41. </body>
  42. </html>
复制代码

参加浮动后的特点
1.相对于现在的位置浮动,不占位 (绝对定位会用这个特点)
2.图文混排(笔墨在图片四周)
  1. .son2{
  2.       /* 将浮动注释取消 */
  3.       float: left;
  4.       height: 100px;
  5.       font-weight: 700;
  6.       font-size: 20px;
  7.       color: red;
  8.     }
复制代码

产物地区布局


HTML标签

  1. <!-- 版心:左右,右面:8个产品 → 8个 li -->
  2. <div class="product">
  3.   <div class="left"></div>
  4.   <div class="right">
  5.     <ul>
  6.       <li></li>
  7.       <li></li>
  8.       <li></li>
  9.       <li></li>
  10.       <li></li>
  11.       <li></li>
  12.       <li></li>
  13.       <li></li>
  14.     </ul>
  15.   </div>
  16. </div>
复制代码
CSS样式

  1. <style>
  2.   * {
  3.     margin: 0;
  4.     padding: 0;
  5.   }
  6.   li {
  7.     list-style: none;
  8.   }
  9.   .product {
  10.     margin: 50px auto;
  11.     width: 1226px;
  12.     height: 628px;
  13.     background-color: pink;
  14.   }
  15.   .left {
  16.     float: left;
  17.     width: 234px;
  18.     height: 628px;
  19.     background-color: skyblue;
  20.   }
  21.   .right {
  22.     float: right;
  23.     width: 978px;
  24.     height: 628px;
  25.     background-color: brown;
  26.   }
  27.   .right li {
  28.     float: left;
  29.     margin-right: 14px;
  30.     margin-bottom: 14px;
  31.     width: 234px;
  32.     height: 300px;
  33.     background-color: orange;
  34.   }
  35.   /* 第四个li和第八个li 去掉右侧的margin */
  36.   .right li:nth-child(4n) {
  37.     margin-right: 0;
  38.   }
  39.   /* 细节:如果父级宽度不够,浮动的盒子会掉下来 */
  40. </style>
复制代码
扫除浮动

场景:浮动元素会脱标,假如父级没有高度,子级无法撑开父级高度(大概导致页面布局庞杂)
办理方法:扫除浮动(扫除浮动带来的影响)
场景搭建


  1. <style>
  2.   .father {
  3.     margin: 10px auto;
  4.     width: 1200px;
  5.     /*  这个父级盒子.father高度取消 那么子元素 .left .right盒子无法撑起 这个父盒子 */
  6.     /* height: 300px; */
  7.     background-color: pink;
  8.   }
  9.   .left {
  10.     float: left;
  11.     width: 200px;
  12.     height: 300px;
  13.     background-color: skyblue;
  14.   }
  15.   .right {
  16.     float: right;
  17.     width: 950px;
  18.     height: 300px;
  19.     background-color: orange;
  20.   }
  21.   .bottom {
  22.     height: 100px;
  23.     background-color: brown;
  24.   }
  25. </style>
  26. <div class="father">
  27.   <div class="left"></div>
  28.   <div class="right"></div>
  29. </div>
  30. <div class="bottom"></div>
复制代码
当.father的高度取消掉如下图。通过查抄元素发现不存在高度,阐明白实浮动的盒子没有撑起父盒子 ,父盒子.father "脱标’'了

扫除浮动(办理上面脱标)的方法

  • 额外标签法
  • 单伪元素法
  • 双伪元素法
  • overfow法
法一:额外标签法

在父元素内容的末了添加一个块级元素,设置 CSS 属性 clear: both
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>css-day06-练习</title>
  5.   <style>
  6.     .father {
  7.     margin: 10px auto;
  8.     width: 1200px;
  9.     background-color: pink;
  10.   }
  11.   .left {
  12.     float: left;
  13.     width: 200px;
  14.     height: 300px;
  15.     background-color: skyblue;
  16.   }
  17.   .right {
  18.     float: right;
  19.     width: 950px;
  20.     height: 300px;
  21.     background-color: orange;
  22.   }
  23.   .bottom {
  24.     height: 100px;
  25.     background-color: brown;
  26.   }
  27.   /* 这里 */
  28.   .clearfix{
  29.     clear: both;
  30.   }
  31.   </style>
  32. </head>
  33. <body>
  34.   <div class="father">
  35.     <div class="left"></div>
  36.     <div class="right"></div>
  37.     <!-- 这里面加个clearfix -->
  38.     <div class="clearfix"></div>
  39.   </div>
  40.   <div class="bottom"></div>
  41.   
  42. </body>
  43. </html>
复制代码

法二:单伪元素法

利用伪元素,相当于方法一 在父元素内容的末了添加一个块级元素 注意content: “”;必须有

  • 准备 after 伪元素
    1. .clearfix::after {
    2.   content: "";
    3.   display: block;
    4.   clear: both;
    5. }
    复制代码
  • 父级利用 clearfix 类
    1. <div class="father clearfix"></div>
    复制代码
  • 整合一下
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.   <meta charset="UTF-8">
    5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.   <title>css-day06-练习</title>
    7.   <style>
    8.     .father {
    9.     margin: 10px auto;
    10.     width: 1200px;
    11.     background-color: pink;
    12.   }
    13.   .left {
    14.     float: left;
    15.     width: 200px;
    16.     height: 300px;
    17.     background-color: skyblue;
    18.   }
    19.   .right {
    20.     float: right;
    21.     width: 950px;
    22.     height: 300px;
    23.     background-color: orange;
    24.   }
    25.   .bottom {
    26.     height: 100px;
    27.     background-color: brown;
    28.   }
    29.   /* 放在这里,谁用给谁就行 */
    30.   .clearfix::after {
    31.     content: "";
    32.     display: block;
    33.     clear: both;
    34.   }
    35.   </style>
    36. </head>
    37. <body>
    38.   <!-- 请看day03的类选择器的部分 -->
    39.   <div class="father clearfix">
    40.     <div class="left"></div>
    41.     <div class="right"></div>
    42.   </div>
    43.   <div class="bottom"></div>
    44.   
    45. </body>
    46. </html>
    复制代码
法三:双伪元素法(保举)


  • 准备 after 和 before 伪元素
    1. /* before 解决外边距塌陷问题 */
    2. /* 双伪元素法 */
    3. .clearfix::before,
    4. .clearfix::after {
    5.   content: "";
    6.   display: table;
    7. }
    8. /* after 清除浮动 */
    9. .clearfix::after {
    10.   clear: both;
    11. }
    复制代码
  • 父级利用 clearfix 类
    1. <div class="father clearfix"></div>
    复制代码
  • 归并
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.   <meta charset="UTF-8">
    5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.   <title>css-day06-练习</title>
    7.   <style>
    8.     .father {
    9.     margin: 10px auto;
    10.     width: 1200px;
    11.     background-color: pink;
    12.   }
    13.   .left {
    14.     float: left;
    15.     width: 200px;
    16.     height: 300px;
    17.     background-color: skyblue;
    18.   }
    19.   .right {
    20.     float: right;
    21.     width: 950px;
    22.     height: 300px;
    23.     background-color: orange;
    24.   }
    25.   .bottom {
    26.     height: 100px;
    27.     background-color: brown;
    28.   }
    29.   
    30.   /* before 解决外边距塌陷问题 */
    31.   /* 双伪元素法 */
    32.   .clearfix::before,
    33.   .clearfix::after {
    34.     content: "";
    35.     display: table;
    36.   }
    37.   
    38.   /* after 清除浮动 */
    39.   .clearfix::after {
    40.     clear: both;
    41.   }
    42.   </style>
    43. </head>
    44. <body>
    45.   <!-- 请看我发表的博客的类选择器 -->
    46.   <div class="father clearfix">
    47.     <div class="left"></div>
    48.     <div class="right"></div>
    49.   </div>
    50.   <div class="bottom"></div>
    51.   
    52. </body>
    53. </html>
    复制代码
法四:overfow法

  1. .father {
  2.   margin: 10px auto;
  3.   width: 1200px;
  4.   background-color: pink;
  5.   /* 浮动问题是父级无法被子级撑开
  6.   使用overflow: hidden 属性 浏览器就会检查父级范围*/
  7.   overflow: hidden;
  8. }
复制代码
浮动总结


浮动本质是用于图文混排,给图片加浮动,笔墨会在旁边表现。其时是由于没有更好的技能。以是盒子浮动布局很繁琐,以是就引出了接下来要学的Flex布局
03-Flex布局

由上一节的浮动总结可知为什么浮动被镌汰了。但是我们为什么要学Float布局呢,这是由于浮动为css的定位学习做准备。
Flex 布局也叫弹性布局,是欣赏器提倡的布局模子,非常恰当布局化布局,提供了强大的空间分布和对齐本领。
Flex 模子不会产生浮动布局中脱标征象,布局网页更简朴、更机动。
float与Flex对比

利用float :会脱标而且不好控制间距
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       /* height: 300px; */
  10.       border: 1px solid #000;
  11.     }
  12.     .box div{
  13.       float: left;
  14.       /*控制间距*/
  15.       margin: 50px;
  16.       width: 200px;
  17.       height: 100px;
  18.       background-color: pink;
  19.       
  20.     }
  21.     .bottom{
  22.       height: 100px;
  23.       background-color: skyblue;
  24.     }
  25.   </style>
  26. </head>
  27. <body>
  28.   <div class="box">
  29.     <div></div>
  30.     <div></div>
  31.     <div></div>
  32.   </div>
  33.   <div class="bottom"></div>
  34.   
  35. </body>
  36. </html>
复制代码

利用flex :不脱标而且好控制间距
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       /* height: 300px; */
  10.       /* 使用flex布局 */
  11.       display: flex;
  12.       justify-content: space-between;
  13.       border: 1px solid #000;
  14.     }
  15.     .box div{
  16.       /* float: left;
  17.       margin: 50px; */
  18.       width: 200px;
  19.       height: 100px;
  20.       background-color: pink;
  21.       
  22.     }
  23.     .bottom{
  24.       height: 100px;
  25.       background-color: skyblue;
  26.     }
  27.   </style>
  28. </head>
  29. <body>
  30.   <div class="box">
  31.     <div></div>
  32.     <div></div>
  33.     <div></div>
  34.   </div>
  35.   <div class="bottom"></div>
  36.   
  37. </body>
  38. </html>
复制代码

Flex构成

设置方式:给父元素设置 display: flex(df即可输出),子元素可以主动挤压或拉伸
纵然子盒子设置了宽度。当元素过多也会被挤压(以是说是弹性盒子),反面会学不被积存的属性

当子盒子不设置高度 ,子元素可以主动拉伸

构成部门:


  • 弹性容器(父盒子)
  • 弹性盒子(子盒子)
  • 主轴: 默认在水平方向。弹性盒子(子盒子)沿着主轴方向分列
  • 侧轴 / 交织轴:默认在垂直方向

相干属性名

形貌属性创建flex容器display:flex主轴对齐方式justify-content侧轴对齐方式align-items某个弹性盒子侧轴对齐方式align-self修改主轴方向flex-direction弹性伸缩比flex弹性盒子换行flex-wrap行对齐方式(行与行之间)当flex-wrap:wrap见效align-content  具体内容请看图解CSS3弹性盒子属性_css弹性盒子图文-CSDN博客
  创建flex容器 display

属性值阐明flex块级的弹性盒子inline-flex行级的弹性盒子-webkit-flex苹果和谷歌的兼容性主轴对齐方式 justify-content(弹性容器)

属性名:justify-content
属性值效果flex-start默认值, 弹性盒子从出发点开始依次分列flex-end弹性盒子从止境开始依次分列center弹性盒子沿主轴居中分列space-between弹性盒子沿主轴匀称分列,空缺间距匀称分在弹性盒子之间, 两侧盒子贴着双方space-around弹性盒子沿主轴匀称分列,空缺间距匀称分在弹性盒子两侧,两侧盒子与双方有间隔space-evenly弹性盒子沿主轴匀称分列,弹性盒子与容器之间间距相称
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       display: flex;
  10.       justify-content: flex-start;
  11.       height: 300px;
  12.       border: 1px solid #000;
  13.     }
  14.     .box div{
  15.       width: 200px;
  16.       height: 100px;
  17.       background-color: pink;
  18.     }
  19.   </style>
  20. </head>
  21. <body>
  22.   <div class="box">
  23.     <div>1</div>
  24.     <div>2</div>
  25.     <div>3</div>
  26.   </div>
  27. </body>
  28. </html>
复制代码
根据以上代码 变更justify-content:的值 举行展示

  • justify-content: flex-start;

  • justify-content: flex-end;

  • justify-content: center;

  • justify-content: space-between; 父级剩余的尺寸分配间距,间距相称。

  • justify-content: space-around; 间距出现在弹性盒子两侧,弹性盒子之间的间距是两侧间距的二倍。

  • space-evenly; evenly是匀称地意思,弹性盒子之间的间距 == 两侧间距。

侧轴对齐方式 align-items(弹性容器)和align-self(弹性盒子)

align-items父盒子



  • align-items:当前弹性容器内全部弹性盒子的侧轴对齐方式(给弹性容器设置,即给父盒子设置属性)
  • align-self:单独控制某个弹性盒子的侧轴对齐方式(给弹性盒子设置,即给子盒子设置属性)
属性值效果stretch弹性盒子沿着侧轴线被拉伸至铺满容器(弹性盒子没有设置侧轴方向尺寸则默认拉伸)center弹性盒子沿侧轴居中分列flex-start弹性盒子从出发点开始依次分列flex-end弹性盒子从止境开始依次分列这里是给弹性容器添加

  • align-items: stretch;
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.   <meta charset="UTF-8">
    5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.   <title>css-day06-练习</title>
    7.   <style>
    8.     .box{
    9.       display: flex;
    10.       /* 弹性盒子在侧轴(即垂直方向)没有尺寸才能拉伸  
    11.       比如下面的.box div注释掉高度 才会拉伸。
    12.       浏览器默认是justify-items的值是 stretch;*/
    13.       align-items: stretch;
    14.       height: 300px;
    15.       border: 1px solid #000;
    16.     }
    17.     .box div{
    18.       width: 200px;
    19.       /* height: 100px; */
    20.       background-color: pink;
    21.     }
    22.   </style>
    23. </head>
    24. <body>
    25.   <div class="box">
    26.     <div>1</div>
    27.     <div>2</div>
    28.     <div>3</div>
    29.   </div>
    30. </body>
    31. </html>
    复制代码

  • align-items: center;
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.   <meta charset="UTF-8">
    5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.   <title>css-day06-练习</title>
    7.   <style>
    8.     .box{
    9.       display: flex;
    10.       /* 弹性盒子在侧轴(即垂直方向) 垂直居中
    11.         当弹性盒子没有高度按照弹性盒子内容填充的高度*/
    12.       align-items: center;
    13.       height: 300px;
    14.       border: 1px solid #000;
    15.     }
    16.     .box div{
    17.       width: 200px;
    18.       height: 100px;
    19.       background-color: pink;
    20.     }
    21.   </style>
    22. </head>
    23. <body>
    24.   <div class="box">
    25.     <div>1</div>
    26.     <div>2</div>
    27.     <div>3</div>
    28.   </div>
    29. </body>
    30. </html>
    复制代码

  • align-items: self-start;
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.   <meta charset="UTF-8">
    5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.   <title>css-day06-练习</title>
    7.   <style>
    8.     .box{
    9.       display: flex;
    10.       /* 弹性盒子在侧轴(即垂直方向)从起点开始依次排列
    11.       当弹性盒子没有高度,按照弹性盒子内容填充高度*/
    12.       align-items: self-start;
    13.       height: 300px;
    14.       border: 1px solid #000;
    15.     }
    16.     .box div{
    17.       width: 200px;
    18.       height: 100px;
    19.       background-color: pink;
    20.     }
    21.   </style>
    22. </head>
    23. <body>
    24.   <div class="box">
    25.     <div>1</div>
    26.     <div>2</div>
    27.     <div>3</div>
    28.   </div>
    29. </body>
    30. </html>
    复制代码

  • align-items: self-end;
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.   <meta charset="UTF-8">
    5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.   <title>css-day06-练习</title>
    7.   <style>
    8.     .box{
    9.       display: flex;
    10.       /* 弹性盒子在侧轴(即垂直方向)从终点开始依次排列
    11.       当弹性盒子没有高度按照内容填充*/
    12.       align-items: self-end;
    13.       height: 300px;
    14.       border: 1px solid #000;
    15.     }
    16.     .box div{
    17.       width: 200px;
    18.       height: 100px;
    19.       background-color: pink;
    20.     }
    21.   </style>
    22. </head>
    23. <body>
    24.   <div class="box">
    25.     <div>1</div>
    26.     <div>2</div>
    27.     <div>3</div>
    28.   </div>
    29. </body>
    30. </html>
    复制代码

align-self子元素(弹性盒子)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       display: flex;
  10.       align-items: self-start;
  11.       height: 300px;
  12.       border: 1px solid #000;
  13.     }
  14.     .box div{
  15.       width: 200px;
  16.       height: 100px;
  17.       background-color: pink;
  18.     }
  19.     /* 不仅仅是ul下的li可用 */
  20.     .box div:nth-child(1){
  21.       align-self: center;
  22.     }
  23.   </style>
  24. </head>
  25. <body>
  26.   <div class="box">
  27.     <!-- 给第一个盒子单独加 -->
  28.     <div>1</div>
  29.     <div>2</div>
  30.     <div>3</div>
  31.   </div>
  32. </body>
  33. </html>
复制代码

修改主轴方向flex-direction(弹性容器)

主轴默认在水平方向,侧轴默认在垂直方向
属性名:flex-direction
属性值效果row水平方向,从左向右(默认)column垂直方向,从上向下(注意侧轴主轴互换)row-reverse水平方向,从右向左column-reverse垂直方向,从下向上
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       display: flex;
  10.       /*  这主轴变侧轴,侧轴变主轴 */
  11.       flex-direction: column;
  12.       /* 主轴在垂直,视觉效果:垂直居中 */
  13.       justify-content: center;
  14.       /* 侧轴在水平,视觉效果:水平居中 */
  15.       align-items: center;
  16.       height: 300px;
  17.       border: 1px solid #000;
  18.     }
  19.     .box div{
  20.       width: 200px;
  21.       height: 100px;
  22.     }
  23.     /* 不仅仅是ul下的li可用 */
  24.     .box div:nth-child(1){
  25.       background-color: skyblue;
  26.     }
  27.     .box div:nth-child(2){
  28.       background-color: gold;
  29.     }
  30.   </style>
  31. </head>
  32. <body>
  33.   <div class="box">
  34.     <div>1</div>
  35.     <div>2</div>
  36.   </div>
  37. </body>
  38. </html>
复制代码

弹性盒子换行flex-wrap(弹性容器)

弹性盒子可以主动挤压或拉伸,默认情况下,全部弹性盒子都在一行表现。
属性名:flex-wrap
属性值


  • wrap:换行 (弹性盒子不会被挤压)
  • nowrap:不换行(默认) 会被挤压(纵然弹性盒子设置了宽度也会被挤压)
弹性伸缩比flex(弹性盒子属性重点)

作用:控制弹性盒子的主轴方向的尺寸。
属性名:flex
属性值:整数数字,表现占用父级剩余尺寸的份数。
1 不加flex布局如下
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       height: 300px;
  10.       border: 1px solid #000;
  11.     }
  12.     .box div{
  13.       /* height: 100px; */
  14.       background-color: pink;
  15.     }
  16.     .box div:nth-child(1){
  17.       width: 200px;
  18.     }
  19.   </style>
  20. </head>
  21. <body>
  22.   <div class="box">
  23.     <div>1</div>
  24.     <div>2</div>
  25.     <div>3</div>
  26.   </div>
  27. </body>
  28. </html>
复制代码

2 参加flex布局
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       /* 默认情况下,当弹性盒子不设置宽高,主轴方向尺寸靠内容撑开,侧轴默认拉伸 */
  10.       display: flex;
  11.       height: 300px;
  12.       border: 1px solid #000;
  13.     }
  14.     .box div{
  15.       /* height: 100px; */
  16.       background-color: pink;
  17.     }
  18.     .box div:nth-child(1){
  19.       width: 200px;
  20.     }
  21.   </style>
  22. </head>
  23. <body>
  24.   <div class="box">
  25.     <div>1</div>
  26.     <div>2</div>
  27.     <div>3</div>
  28.   </div>
  29. </body>
  30. </html>
复制代码

3 当第二个弹性盒子参加flex
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       /* 默认情况下,当弹性盒子不设置宽高,主轴方向尺寸靠内容撑开,侧轴默认拉伸 */
  10.       display: flex;
  11.       height: 300px;
  12.       border: 1px solid #000;
  13.     }
  14.     .box div{
  15.       background-color: pink;
  16.     }
  17.     .box div:nth-child(1){
  18.       width: 200px;
  19.     }
  20.     /* 水平方向剩余尺寸=
  21.                 1920px-
  22.                 第一个弹性盒子宽200px-
  23.                 第二个弹性盒子内容所占宽度-
  24.                 第三个弹性盒子内容所占宽度
  25.         
  26.         第二个弹性盒子宽度 =  水平方向剩余尺寸/1
  27.     */
  28.     .box div:nth-child(2){
  29.       flex:1
  30.     }
  31.   </style>
  32. </head>
  33. <body>
  34.   <div class="box">
  35.     <div>1</div>
  36.     <div>2</div>
  37.     <div>3</div>
  38.   </div>
  39. </body>
  40. </html>
复制代码

4 当第二,三个弹性盒子参加flex
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       /* 默认情况下,当弹性盒子不设置宽高,主轴方向尺寸靠内容撑开,侧轴默认拉伸 */
  10.       display: flex;
  11.       height: 300px;
  12.       border: 1px solid #000;
  13.     }
  14.     .box div{
  15.       background-color: pink;
  16.     }
  17.     .box div:nth-child(1){
  18.       width: 200px;
  19.     }
  20.     /* 水平方向剩余尺寸=
  21.                 1920px-
  22.                 第一个弹性盒子宽200px-
  23.                 第二个弹性盒子内容所占宽度-
  24.                 第三个弹性盒子内容所占宽度
  25.         
  26.         第二个弹性盒子宽度 =  1*水平方向剩余尺寸/(1+2)
  27.         第三个弹性盒子宽度 =  2*水平方向剩余尺寸/(1+2)
  28.     */
  29.     .box div:nth-child(2){
  30.       flex:1
  31.     }
  32.     .box div:nth-child(3){
  33.       flex:2
  34.     }
  35.   </style>
  36. </head>
  37. <body>
  38.   <div class="box">
  39.     <div>1</div>
  40.     <div>2</div>
  41.     <div>3</div>
  42.   </div>
  43. </body>
  44. </html>
复制代码

5 参加flex-direction: column; 主轴侧轴反过来
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.   <title>flex布局-弹性伸缩比</title>
  8.   <style>
  9.     /* 默认情况下,主轴方向尺寸是靠内容撑开;侧轴默认拉伸 */
  10.     .box {
  11.       display: flex;
  12.       flex-direction: column;
  13.       height: 300px;
  14.       border: 1px solid #000;
  15.     }
  16.     .box div {
  17.       /* height: 100px; */
  18.       background-color: pink;
  19.     }
  20.     .box div:nth-child(1) {
  21.       width: 200px;
  22.     }
  23.     .box div:nth-child(2) {
  24.       flex: 1;
  25.     }
  26.     .box div:nth-child(3) {
  27.       flex: 2;
  28.     }
  29.   </style>
  30. </head>
  31. <body>
  32.   <div class="box">
  33.     <div>1</div>
  34.     <div>2</div>
  35.     <div>3</div>
  36.   </div>
  37. </body>
  38. </html>
复制代码

行内对齐方式align-content(弹性容器)

属性名:align-content (必要设置父元素的高才见效)
属性值效果flex-start默认值,弹性盒子从出发点开始依次分列flex-end弹性盒子从止境开始依次分列center弹性盒子沿主轴居中分列space-between弹性盒子沿主轴匀称分列,,空缺间距匀称分在弹性盒子之间, 两侧盒子贴着双方space-aroud弹性盒子沿主轴匀称分列,空缺间距匀称分在弹性盒子两侧,两侧盒子与双方有间隔space-evenly弹性盒子沿主轴匀称分列,弹性盒子与容器之间间距相称  注意:该属性对单行弹性盒子模子无效。
  默认布局
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   <title>css-day06-练习</title>
  7.   <style>
  8.     .box{
  9.       display: flex;
  10.       flex-wrap: wrap;
  11.       justify-content: space-between;
  12.       
  13.       /* 这里使用align-content:value */
  14.       height: 400px;
  15.       border: 1px solid #000;
  16.     }
  17.     .box div{
  18.       width: 200px;
  19.       height: 100px;
  20.       background-color: pink;
  21.     }
  22.   </style>
  23. </head>
  24. <body>
  25.   <div class="box">
  26.     <div>1</div>
  27.     <div>2</div>
  28.     <div>3</div>
  29.     <div>1</div>
  30.     <div>2</div>
  31.     <div>3</div>
  32.     <div>1</div>
  33.     <div>2</div>
  34.   </div>
  35. </body>
  36. </html>
复制代码


  • align-content: flex-start;

  • align-content: flex-end;

  • align-content: center;

  • align-content: space-between;

  • align-content: space-space-around;

  • align-content: space-evenly;

flex-flow复合属性:

这个属性是flex-direction 和 flex-wrap复合写法
  1. flex-flow:row nowrap;
  2. flex-flow:column wrap-reverse;
复制代码
order(默认0)

实用于:flex子项和flex容器中的绝对定位子元素
用整数值来界说分列次序,数值小的排在前面。可以为负值
flex拓展

flex-grow:拉伸(默认0)

自身紧缩数/紧缩总和*剩余空间
flex-shrink:紧缩(默认1)

(自身宽度自身紧缩数)/(sum每个(宽度紧缩数))*超出的空间
flex-basis:宽度(默认0)

px:像素值
%:百分值
auto(默认):无特定宽度值,取决于别的属性值
content:基于内容主动盘算宽度
flex:复合写法

flex-grow|flex-shrink|flex-basis
flex:1 1 300px;
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表