CSS清除浮动

作者: 唐卡豆子 | 来源:发表于2018-04-11 18:19 被阅读0次

1.父级手动给高度
缺点:不推荐使用。只适合高度固定的布局,要给出精确的高度,如果高度和父级div不一样时,会产生问题

.con{border:1px solid red;height:300px;}
.left{width:100px;height:100px;float:left;background: green;}
.right{width:200px;height:150px;float:left;background: blue;}

<div class="con clearfloat">
    <div class="left">left</div>
    <div class="right">right</div>
 </div>

2.在末尾添加一个空div或者br标签
缺点:不推荐使用。如果页面浮动布局多,就要增加很多空div,让人感觉很不好

.con{border:1px solid red;}
.left{width:100px;height:100px;float:left;background: green;}
.right{width:200px;height:150px;float:left;background: blue;}
.clearfloat{clear:both;}

<div class="con clearfloat">
      <div class="left">left</div>
      <div class="right">right</div>
      <br class="clearfloat">
      /*<div class="clearfloat"></div>*/
</div>

3.父级添加overflow:hidden或者overflow:auto
缺点:不推荐使用。不能和position配合使用,因为超出的尺寸的会被隐藏。

.con{border:1px solid red;overflow:hidden;}
.left{width:100px;height:100px;float:left;background: green;}
.right{width:200px;height:150px;float:left;background: blue;}

<div class="con clearfloat">
    <div class="left">left</div>
    <div class="right">right</div>
 </div>

4.父级添加伪元素::after和zoom
推荐使用:两句代码结合使用才能让主流浏览器都支持

.con{border:1px solid red;}
.left{width:100px;height:100px;float:left;background: green;}
.right{width:200px;height:150px;float:left;background: blue;}
.clearfloat::after{
  content:",
  display:block;
  clear:both;
  visibility:hidden,
  height:0;
}
.clearfloat{
  zoom:1;
}

<div class="con clearfloat">
    <div class="left">left</div>
    <div class="right">right</div>
 </div>

相关文章

  • CSS浮动续

    CSS清除浮动案例 CSS版心居中显示案例 清除浮动的四种用法: 1. 使用空标记清除浮动,隔墙法,增加标签 2....

  • CSS clear both清除浮动

    原文地址:CSS clear both清除浮动 DIV+CSS clear both清除产生浮动我们知道有时使用了...

  • 清除浮动

    一、清除浮动 or 闭合浮动 ? 清除浮动:清除对应的单词是 clear,对应CSS中的属性是 clear:lef...

  • 一篇文章带你了解CSS clear both清除浮动

    一、前言 CSS clear both清除产生浮动 ,使用了css float浮动会产生css浮动,这个时候就需要...

  • CSS 中的浮动

    浮动的定义: 元素脱离文档流 举栗子: 修改 CSS 代码,清除浮动: 浮动的影响: 父元素高度塌陷 清除浮动: ...

  • CSS浮动.清除浮动

    给父级元素设置高度 在底部添加一个空元素,清除浮动 父级div定义 overflow:hidden或者auto 为...

  • css浮动 清除浮动

    float : left | right | none 设计之初的作用是做文字环绕 p标签段落双标签块级 i...

  • Test10

    引用文章: 那些年我们一起清除过的浮动 CSS浮动float详解 Clear Float CSS float浮动的...

  • 经常写却记不住的前端代码

    CSS透明 清除浮动影响 响应式 css 文字处理

  • css3复习

    清除浮动: 方法:clear清除浮动(添加空div法)在浮动元素下方添加空div,并给该元素写css样式: ...

网友评论

    本文标题:CSS清除浮动

    本文链接:https://www.haomeiwen.com/subject/ycpzhftx.html