zoukankan      html  css  js  c++  java
  • 清除float的方法

    清除浮动有两种方法:

    一种是加入clear:both:

    一种是BFC申明:

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>float的清除</title>
    </head>
    <style>
    .clearfix:after { content: ''; display: table; clear: both; }
    .clearfix { *zoom: 1; }
    </style>
    <body>
    1.
    <div style="background-color: #f5f5f5;" class="clearfix">
    <img src="http://img.mukewang.com/53d60af3000171a002560191.jpg" style="float:left;margin-bottom:100px;" >
    </div>
    <div style="margin-top: 100px;">本文字离图片的距离是?</div>
    2.
    <div style="background-color: #f5f5f5; overflow: hidden;">
    <img src="http://img.mukewang.com/53d60af3000171a002560191.jpg" style="float:left; margin-bottom: 100px;">
    </div>
    <div style="margin-top: 100px;">本文字离图片的距离是?</div><br><br><br>
    3.
    <div style="background-color: #f5f5f5;">
    <img src="http://img.mukewang.com/53d60af3000171a002560191.jpg" style="float:left;margin-bottom:100px;" >
    <div style="clear:both"></div>
    </div>
    <div style="margin-top: 100px;">本文字离图片的距离是?</div>

    4.
    <div style="background-color: #f5f5f5;" >
    <img src="http://img.mukewang.com/53d60af3000171a002560191.jpg" style="float:left;" >
    </div>
    <div style="margin-top: 100px;">本文字离图片的距离是?</div>
    <br>
    </body>
    </html>

    第一个div块是通过在css中增加删除float的类,注意的是应该在有浮动float的父元素加入。其中的.clearfix { *zoom: 1; }是为了兼容低级浏览器

    第二个div块是通过在加入float的DOM元素下面的加入style=“clear:both;”的同级元素来清除浮动

    第三个div块在浮动元素的父元素上加入overflow: hidden;样式也可清除浮动(overflow: hidden;使得包含图片的div自成BFC)

    第四个div没有清除浮动

  • 相关阅读:
    Netty应用
    原生JDK网络编程- NIO之Reactor模式
    Kafka入门教程
    Java CAS
    Java读写锁
    遍历map的四种方法
    java selector
    Range Sum Query
    Increasing Triplet Subsequence
    Count Numbers with Unique Digits
  • 原文地址:https://www.cnblogs.com/wxw1314/p/5210420.html
Copyright © 2011-2022 走看看