BFC详解

x33g5p2x  于2022-03-21 转载在 其他  
字(4.8k)|赞(0)|评价(0)|浏览(416)

一、写在前面
今天看到一道面试题,主要是关于BFC的理解,下面将针对BFC的相关特性做出总结。
二、何为BFC

  1. BFC(Block Formatting Context)格式化上下文,是
  2. web页面中盒子模型布局的CSS渲染模式,指的是一个独立
  3. 的渲染区域或者是一个隔离的独立容器。

三、形成BFC的条件

  1. 1、浮动元素,floatnone以外。
  2. 2、定位元素,position(absolute, fixed)
  3. 3display为以下其中之一的值:inline-block,table-cell,table-caption.
  4. 4overflow除了visible以外的值(hidden,auto, scroll)

四、实践
4.1、BFC中的盒子对齐

  1. 内部的Box会在垂直方向上一个接着一个的放置。

4.2、外边距重叠
在正常的文档流中,两个兄弟盒子之间的具有由其外边距决定的,但是不是他们的外边距之和决定的,而是以较大的为准。

  1. <!DOCTYPE html>
  2. <html lang="cn">
  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>Document</title>
  8. <style>
  9. * {
  10. margin: 0px;
  11. padding:0px
  12. }
  13. .container {
  14. overflow: hidden;
  15. width: 100px;
  16. height: 100px;
  17. background-color: red;
  18. }
  19. .box1 {
  20. height: 20px;
  21. margin: 10px 0;
  22. background-color: green;
  23. }
  24. .box2 {
  25. height: 20px;
  26. margin: 20px 0;
  27. background-color: green;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="container">
  33. <div class="box1"></div>
  34. <div class="box2"></div>
  35. </div>
  36. </body>
  37. </html>

这里我们可以看到,第一个盒子存在上边距(因为使用BFC不会造成margin穿透问题);但是两个盒子之间的距离是20px,而不是30px,因为垂直外边距会存在折叠问题,间距以较大的为准。
此时我们就可以使用另一个特性,bfc就是页面上的一个独立容器,容器里面的子元素不会影响外部元素,此时我们就可以在其中一个子元素的外层嵌套一层div,然后设置BFC

  1. <!DOCTYPE html>
  2. <html lang="cn">
  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>Document</title>
  8. <style>
  9. * {
  10. margin: 0px;
  11. padding:0px
  12. }
  13. .container {
  14. overflow: hidden;
  15. width: 100px;
  16. height: 100px;
  17. background-color: red;
  18. }
  19. .box1 {
  20. height: 20px;
  21. margin: 10px 0;
  22. background-color: green;
  23. }
  24. .box2 {
  25. height: 20px;
  26. margin: 20px 0;
  27. background-color: green;
  28. }
  29. .wapper {
  30. overflow: hidden;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="container">
  36. <div class="wapper">
  37. <div class="box1"></div>
  38. </div>
  39. <div class="box2"></div>
  40. </div>
  41. </body>
  42. </html>

4.3、不被浮动元素覆盖
如果我们想要实现两栏布局,比如说左边固定宽度,右边不设置宽度,此时右边自适应宽度,随着浏览器窗口的大小而改变。在没有设置BFC的时候,此时就会发现浮动元素就会覆盖right的元素,但是如果我们想要实现两栏布局,此时我们可以给right的元素设置BFC,因为设置BFC会不影响外部。

  1. <!DOCTYPE html>
  2. <html lang="cn">
  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>Document</title>
  8. <style>
  9. .column:nth-of-type(1) {
  10. float: left;
  11. width: 200px;
  12. height: 300px;
  13. margin-right: 10px;
  14. background-color: red;
  15. }
  16. .column:nth-of-type(2) {
  17. overflow: hidden;
  18. height: 300px;
  19. background-color: purple;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="column"></div>
  25. <div class="column"></div>
  26. </body>
  27. </html>

4.4、三栏布局
左右两边固定,中间不设置宽度,因此中间的宽度自适应,随着浏览器的大小变化而变化。

  1. <!DOCTYPE html>
  2. <html lang="cn">
  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>Document</title>
  8. <style>
  9. .column:nth-of-type(1),
  10. .column:nth-of-type(2) {
  11. float: left;
  12. width: 100px;
  13. height: 300px;
  14. background-color: green;
  15. }
  16. .column:nth-of-type(2) {
  17. float: right;
  18. }
  19. .column:nth-of-type(3) {
  20. overflow: hidden;
  21. /*创建bfc*/
  22. height: 300px;
  23. background-color: red;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="contain">
  29. <div class="column"></div>
  30. <div class="column"></div>
  31. <div class="column"></div>
  32. </div>
  33. </body>
  34. </html>

4.5、可以防止字体环绕
众所周知,浮动的盒子会遮盖下面的盒子,但是下面盒子里的文字是不会被遮盖的,文字反而还会环绕浮动的盒子。这也是一个比较有趣的特性。

但是我们如果不想让字体环绕,此时可以使用BFC,进行设置。

  1. <!DOCTYPE html>
  2. <html lang="cn">
  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>Document</title>
  8. <style>
  9. .left {
  10. width: 200px;
  11. height: 200px;
  12. background-color: green;
  13. float: left;
  14. }
  15. .right {
  16. background-color: red;
  17. height: 300px;
  18. overflow: hidden;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div>
  24. <div class="left"></div>
  25. <div class="right">你好你好你好你好你好你好你好你好你好你好你好
  26. 你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好
  27. 你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好
  28. 你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好
  29. 你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好
  30. 你好你好你好你好你好你好你好你好你好你好你好你好你好你好</div>
  31. </div>
  32. </body>
  33. </html>

此时展现的效果就为:

4.6、BFC包含浮动的块
如果我们使用浮动,则该元素就会脱离正常的文档流,则会造成父元素高度的坍塌,此时我们可以对其使用overflow:hidden来对其设置。

相关文章