css 清除浮动方案

清除浮动的三种方案

  • [1. 第一种方案(overflow: hidden)](#1. 第一种方案(overflow: hidden))
  • [2. 第二种方案(clear:both)](#2. 第二种方案(clear:both))
  • [3. 第三种方案(为元素)](#3. 第三种方案(为元素))

1. 第一种方案(overflow: hidden)

html 复制代码
  <style>
    .container {
      background-color: bisque;
      /* 清除方案 */
      overflow: hidden;
    }

    .inner {
      width: 50px;
      height: 50px;
      background-color: cadetblue;
      float: left;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="inner"></div>
  </div>
</body>

2. 第二种方案(clear:both)

html 复制代码
  <style>
    .container {
      background-color: bisque;
    }

    .inner {
      width: 50px;
      height: 50px;
      background-color: cadetblue;
      float: left;
    }
    // 清除方案
    .side {
      clear: both;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="inner"></div>
    <div class="side"></div>
  </div>
</body>

3. 第三种方案(为元素)

html 复制代码
  <style>
    .container {
      background-color: bisque;
    }
    // 清除方案
    .container::after {
      content: "";
      display: table;
      clear: both;
    }

    .inner {
      width: 50px;
      height: 50px;
      background-color: cadetblue;
      float: left;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="inner"></div>
  </div>
</body>

示例:

相关推荐
寒寒_17 小时前
使用Vue与Fabric.js创建图片标注工具
javascript·vue.js·fabric
mCell1 天前
如何零成本搭建个人站点
前端·程序员·github
mCell1 天前
为什么 Memo Code 先做 CLI:以及终端输入框到底有多难搞
前端·设计模式·agent
恋猫de小郭1 天前
AI 在提高你工作效率的同时,也一直在增加你的疲惫和焦虑
前端·人工智能·ai编程
少云清1 天前
【安全测试】2_客户端脚本安全测试 _XSS和CSRF
前端·xss·csrf
萧曵 丶1 天前
Vue 中父子组件之间最常用的业务交互场景
javascript·vue.js·交互
银烛木1 天前
黑马程序员前端h5+css3
前端·css·css3
m0_607076601 天前
CSS3 转换,快手前端面试经验,隔壁都馋哭了
前端·面试·css3
听海边涛声1 天前
CSS3 图片模糊处理
前端·css·css3
IT、木易1 天前
css3 backdrop-filter 在移动端 Safari 上导致渲染性能急剧下降的优化方案有哪些?
前端·css3·safari