CSS BFC

news/2024/5/19 0:40:50 标签: css, BFC

文章目录

  • BFC
    • 什么是BFC
    • BFC解决问题
    • 开启BFC
    • Demo
      • BFC解决margin塌陷问题
      • BFC解决浮动元素覆盖问题
      • BFC解决元素高度塌陷问题
    • 代码下载

BFC_2">BFC

BFC_4">什么是BFC

在CSS中,BFC(Block Formatting Context,即块级格式化上下文)。它是一个独立的渲染区域,块级盒子按照移动的规则进行布局,并与外部的元素相互隔离。

BFC_10">BFC解决问题

  • 元素开启BFC后,子元素不会产生margin塌陷问题。
  • 元素开启BFC后,元素本身不会被其他浮动元素覆盖。
  • 元素开启BFC后,即使子元素浮动,元素自身高度也不会塌陷。

BFC_18">开启BFC

  • 根元素,即HTML元素。
  • 浮动元素。
  • 绝对定位、固定定位的元素。
  • 行内块元素。
  • 表格元素,如table、thead、tbody、tfoot、th、td、tr、caption。
  • overflow不为visible的块元素。
  • 弹性盒子。
  • 多列容器。

Demo

BFCmargin_33">BFC解决margin塌陷问题

问题

<!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>BFC解决margin塌陷问题</title>
    <style>css">
      * {
        padding: 0;
        margin: 0;
      }

      .outer {
        width: 400px;
        background-color: grey;
      }

      .inner {
        width: 100px;
        height: 100px;
        margin: 20px;
      }

      .inner1 {
        background-color: red;
      }
      .inner2 {
        background-color: green;
      }
      .inner3 {
        background-color: blue;
      }
    </style>
  </head>
  <body>
    <div class="outer">
      <div class="inner inner1"></div>
      <div class="inner inner2"></div>
      <div class="inner inner3"></div>
    </div>
  </body>
</html>

在这里插入图片描述

开启BFC解决问题

<!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>BFC解决margin塌陷问题</title>
    <style>css">
      * {
        padding: 0;
        margin: 0;
      }
      body {
        /* display: flex; */
      }
      .outer {
        width: 400px;
        background-color: grey;
        /* float: left; */
        /* position: absolute; */
        /* display: inline-block; */
        /* display: table; */
        /* overflow: hidden; */
        /* column-count: 1; */
      }

      .inner {
        width: 100px;
        height: 100px;
        margin: 20px;
      }

      .inner1 {
        background-color: red;
      }
      .inner2 {
        background-color: green;
      }
      .inner3 {
        background-color: blue;
      }
    </style>
  </head>
  <body>
    <div class="outer">
      <div class="inner inner1"></div>
      <div class="inner inner2"></div>
      <div class="inner inner3"></div>
    </div>
  </body>
</html>

BFC_141">BFC解决浮动元素覆盖问题

问题

<!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>BFC解决浮动元素覆盖问题</title>
    <style>css">
      * {
        padding: 0;
        margin: 0;
      }
      .box {
        width: 100px;
        height: 100px;
      }
      .box1 {
        background-color: red;
        float: left;
      }
      .box2 {
        background-color: green;
      }
    </style>
  </head>
  <body>
    <div class="box box1"></div>
    <div class="box box2"></div>
  </body>
</html>

在这里插入图片描述

开启BFC解决问题

<!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>BFC解决浮动元素覆盖问题</title>
    <style>css">
      * {
        padding: 0;
        margin: 0;
      }
      body {
        /* display: flex; */
      }
      .box {
        width: 100px;
        height: 100px;
      }
      .box1 {
        background-color: red;
        float: left;
      }
      .box2 {
        background-color: green;
        /* float: left; */
        /* position: absolute; */
        /* display: inline-block; */
        /* display: table; */
        /* overflow: hidden; */
        /* column-count: 1; */
      }
    </style>
  </head>
  <body>
    <div class="box box1"></div>
    <div class="box box2"></div>
  </body>
</html>

BFC_224">BFC解决元素高度塌陷问题

问题

<!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>BGC解决高度塌陷问题</title>
    <style>css">
      * {
        padding: 0;
        margin: 0;
      }
      .outer {
        width: 400px;
        background-color: grey;
      }
      .inner {
        width: 100px;
        height: 100px;
        float: left;
      }
      .inner1 {
        background-color: red;
      }
      .inner2 {
        background-color: green;
      }
    </style>
  </head>
  <body>
    <div class="outer">
      <div class="inner inner1"></div>
      <div class="inner inner2"></div>
    </div>
  </body>
</html>

开启BFC解决问题

<!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>BGC解决高度塌陷问题</title>
    <style>css">
      * {
        padding: 0;
        margin: 0;
      }
      body {
        /* display: flex; */
      }
      .outer {
        width: 400px;
        background-color: grey;
        /* float: left; */
        /* position: absolute; */
        /* display: inline-block; */
        /* display: table; */
        /* overflow: hidden; */
        /* column-count: 1; */
      }
      .inner {
        width: 100px;
        height: 100px;
        float: left;
      }
      .inner1 {
        background-color: red;
      }
      .inner2 {
        background-color: green;
      }
    </style>
  </head>
  <body>
    <div class="outer">
      <div class="inner inner1"></div>
      <div class="inner inner2"></div>
    </div>
  </body>
</html>

代码下载


http://www.niftyadmin.cn/n/151551.html

相关文章

3、re库报错、切换JDK版本

一、re库报错&#xff1a;error: nothing to repeat at position 0 回顾有关re.match的知识&#xff1a; &#xff08;1&#xff09;作用&#xff1a;从字符串的开始位置进行匹配&#xff0c;匹配成功则返回match对象&#xff1b;否则&#xff0c;返回None. &#xff08;2&…

数据库判断分解的无损连接性

判断无损连接 常用的还是表格法&#xff0c;但我看网上的正规方法有点繁琐。所以我自己琢磨了一下我自己的做法。 例题&#xff1a;关系模式R(ABCDE),F{A->C, C->D, B->C, DE->C, CE->A}。分解成R1&#xff08;AD&#xff09;,R2(AB)&#xff0c;R3(BC)&#…

Cartographer源码阅读---开始轨迹的相关处理

上一节我们看了一下node_main.cc的内容,发现其中最重要的部分就是Node类,这个类吃下了map_builder类,且里面实现了传感器数据的处理与分发(sensor_bridge),还有整个Cartographer算法的调用(map_builder与map_builder_bridge). 这一节我们重点看一下Node类到底做了些什么.Node.h…

腾讯安全被列为全球大型威胁情报厂商

近日&#xff0c;国际权威研究机构Forrester发布《2023年威胁情报服务厂商评估报告》&#xff08;《External Threat Intelligence Services provider,2023》&#xff09;&#xff08;以下简称《报告》&#xff09;。腾讯安全强势入围全球「Large」梯队代表性厂商&#xff0c;成…

数据挖掘实验:关联规则分析之Apriori算法的实现

一、实验原理 Apriori算法是第一个关联规则挖掘算法&#xff0c;也是最经典的算法。它利用逐层搜索的迭代方法找出数据库中项集的关系&#xff0c;以形成规则&#xff0c;其过程由连接&#xff08;类矩阵运算&#xff09;与剪枝&#xff08;去掉那些没必要的中间结果&#xff0…

一文彻底搞懂前端缓存机制

浏览器缓存步骤 1&#xff09;浏览器在加载资源时&#xff0c;先根据这个资源的一些http header判断它是否命中强缓存&#xff0c;强缓存如果命中&#xff0c;浏览器直接从自己的缓存中读取资源&#xff0c;不会发请求到服务器。比如某个css文件&#xff0c;如果浏览器在加载它…

Day917.契约测试 -SpringBoot与K8s云原生微服务实践

契约测试 Hi&#xff0c;我是阿昌&#xff0c;今天学习记录的是关于契约测试的内容。 一、什么是契约测试 契约测试 是一种软件测试方法&#xff0c;其主要目标是测试不同系统或组件之间的接口&#xff0c;以确保它们在协调运作时的正确性。 该方法基于契约理论&#xff0c…

JavaEE面向对象编程【持续补充完善】

Java 面向对象学习验证回文串ThreadLocal验证回文串 如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后&#xff0c;短语正着读和反着读都一样。则可以认为该短语是一个 回文串 。 代码解析&#xff1a;将获取到的字符串中的非数字和非字母的字符替换为&quo…