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>

示例:

相关推荐
问心无愧051317 分钟前
ctf show web入门111
android·前端·笔记
唐某人丶26 分钟前
模型越来越强,我们还需要 Agent 工程吗?—— 从价值重估到 Harness 实践
前端·agent·ai编程
智码看视界38 分钟前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
JS菌1 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
excel2 小时前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3112 小时前
https连接传输流程
前端·面试
徐小夕2 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
threelab2 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
武器大师723 小时前
lv_binding_js 代码解读
开发语言·javascript·ecmascript