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>

示例:

相关推荐
林深现海7 小时前
Jetson Orin nano/nx刷机后无法打开chrome/firefox浏览器
前端·chrome·firefox
黄诂多7 小时前
APP原生与H5互调Bridge技术原理及基础使用
前端
前端市界7 小时前
用 React 手搓一个 3D 翻页书籍组件,呼吸海浪式翻页,交互体验带感!
前端·架构·github
文艺理科生7 小时前
Nginx 路径映射深度解析:从本地开发到生产交付的底层哲学
前端·后端·架构
千寻girling7 小时前
主管:”人家 Node 框架都用 Nest.js 了 , 你怎么还在用 Express ?“
前端·后端·面试
C澒8 小时前
Vue 项目渐进式迁移 React:组件库接入与跨框架协同技术方案
前端·vue.js·react.js·架构·系统架构
xiaoxue..8 小时前
合并两个升序链表 与 合并k个升序链表
java·javascript·数据结构·链表·面试
清山博客8 小时前
OpenCV 人脸识别和比对工具
前端·webpack·node.js
要加油哦~8 小时前
AI | 实践教程 - ScreenCoder | 多agents前端代码生成
前端·javascript·人工智能
程序员Sunday8 小时前
说点不一样的。GPT-5.3 与 Claude Opus 4.6 同时炸场,前端变天了?
前端·gpt·状态模式