前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CSS清除浮动的几种方式

CSS清除浮动的几种方式

原创
作者头像
程序员海军
发布2022-02-15 13:12:22
7750
发布2022-02-15 13:12:22
举报
文章被收录于专栏:前端笔记ing

1.在知道孩子的高度时,给父亲加上高度,就会清除浮动

HTML
代码语言:html
复制
  <div class="father">
        <div class="son-left">
            
        </div>
        <div class="son-right">

        </div>
    </div>
CSS
代码语言:css
复制
    .father{
        width: 500px;
        border: 1px solid  red;
        background-color: orange;
        height: 300px;
    }
    .son-left .son-right{
        width: 200px;
        height: 300px;
        background: blueviolet;
        float: left;
    }
    .son-left{
    	float:left
    }
    .son-right{
        float: right;
    }

2.定义一个空的div在父亲下,css 中定义 clear: both;

HTML
代码语言:html
复制
    <div class="father">
        <div class="son-left">
            
        </div>
        <div class="son-right">

        </div>
        <div class="clean">

        </div>
    </div>
CSS
代码语言:css
复制
    .father{
        width: 500px;
        border: 1px solid  red;
        background-color: orange;
    }
    .son-left .son-right{
        width: 200px;
        height: 300px;
        background: blueviolet;
        float: left;
    }
    .son-left{
    	float:left
    }
    .son-right{
        float: right;
    }
    .clean{
        clear: both;
    }
    </style>

3. 在父亲身上定义overflow:hidden,来清除浮动

HTML
代码语言:html
复制
    <div class="father">
        <div class="son-left">
            
        </div>
        <div class="son-right">

        </div>
        <div class="clean">

        </div>
    </div>
CSS
代码语言:css
复制
 .father{
        width: 500px;
        border: 1px solid  red;
        background-color: orange;
        overflow: hidden;
    }
    .son-left .son-right{
        width: 200px;
        height: 300px;
        background: blueviolet;
        float: left;
    }
    .son-left{
    	float:left
    }
    .son-right{
        float: right;
    }

为什么加入overflow:hidden即可清除浮动呢?

因为overflow:hidden属性相当于是让父级紧贴内容,这样即可紧贴其对象内内容(包括使用float的div盒子),从而实现了清除浮动

清除浮动常用:overflow:hidden

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.在知道孩子的高度时,给父亲加上高度,就会清除浮动
  • 2.定义一个空的div在父亲下,css 中定义 clear: both;
  • 3. 在父亲身上定义overflow:hidden,来清除浮动
    • 为什么加入overflow:hidden即可清除浮动呢?
    • 清除浮动常用:overflow:hidden
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档