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>

示例:

相关推荐
老前端的功夫2 小时前
Vue 3 性能深度解析:从架构革新到运行时的全面优化
javascript·vue.js·架构
天天扭码2 小时前
如何实现流式输出?一篇文章手把手教你!
前端·aigc·ai编程
前端 贾公子3 小时前
vue移动端适配方案 === postcss-px-to-viewport
前端·javascript·html
GISer_Jing4 小时前
AI营销增长:4大核心能力+前端落地指南
前端·javascript·人工智能
明远湖之鱼4 小时前
一种基于 Service Worker 的渐进式渲染方案的基本原理
前端
前端小端长5 小时前
Vue 中 keep-alive 组件的原理与实践详解
前端·vue.js·spring
FeelTouch Labs5 小时前
Nginx核心架构设计
运维·前端·nginx
雪球工程师团队5 小时前
别再“苦力”写后台,Spec Coding “跑” 起来
前端·ai编程
m0_471199635 小时前
【场景】前端怎么解决离线收银、数据同步异常等场景问题
前端·javascript