温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CSS清除浮动第一些方法

发布时间:2020-07-04 17:02:03 阅读:323 作者:lvning296 栏目:开发技术
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

清除浮动的方法:常用第5种

第1种 :给父级也加浮动(这种情况当父级margin:0 auto;时不居中

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{ width:300px;margin:0 auto;border:10px solid #000; float:left;}

.div{ width:200px;height:200px;background:red;float:left;}

/*

    清浮动

    1.给父级也加浮动(不居中了)

*/

</style>

</head>

<body>

<div class="box">

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

</div>

</body>

</html>

第2种  :给父级加display:inline-block;(同方法1,不居中。只有IE6,7居中)

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{ width:300px;margin:0 auto;border:10px solid #000; display:inline-block;}

.div{ width:200px;height:200px;background:red;float:left;}

/*

    清浮动

    1.给父级也加浮动

    2.给父级加display:inline-block

*/

</style>

</head>

<body>

<div class="box">

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

</div>

</body>

</html>

第3种 在浮动元素下加<div class="clear"></div>

   .clear{ height:0px;font-size:0;clear:both;}但是在ie6下,块元素有最小高度,即当height<19px时,默认为19px,解决方法:font-size:0;或overflow:hidden;

  <!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{ width:300px;margin:0 auto;border:10px solid #000;}

.div{ width:200px;height:200px;background:red;float:left;}

.clear{ height:0px;font-size:0;clear:both;}

/*

    清浮动

    1.给父级也加浮动

    2.给父级加display:inline-block

    3.在浮动元素下加<div class="clear"></div>

    .clear{ height:0px;font-size:0;clear:both;}

*/

</style>

</head>

<body>

<div class="box">

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

        <div class="clear"></div>

</div>

</body>

</html>

第4种  在浮动元素下加<br clear="all">

  <!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{ width:300px;margin:0 auto;border:10px solid #000;}

.div{ width:200px;height:200px;background:red;float:left;}

/*

    清浮动

    1.给父级也加浮动

    2.给父级加display:inline-block

    3.在浮动元素下加<div class="clear"></div>

    .clear{ height:0px;font-size:0;clear:both;}

    4.在浮动元素下加<br clear="all"/>

*/

</style>

</head>

<body>

<div class="box">

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

    <br clear="all"/>

</div>

</body>

</html>

第5种.给浮动元素父级加{zoom:1;}

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{margin:0 auto;border:10px solid #000;}

.div{ width:200px;height:200px;background:red;float:left;}

.clear{zoom:1;}

.clear:after{content:""; display:block;clear:both;}

/*

    清浮动

    1.给父级也加浮动

    2.给父级加display:inline-block

    3.在浮动元素下加<div class="clear"></div>

    .clear{ height:0px;font-size:0;clear:both;}

    4.在浮动元素下加<br clear="all"/>

    5. 给浮动元素的父级加{zoom:1;}

    :after{content:""; display:block;clear:both;}

    **在IE6,7下浮动元素的父级有宽度就不用清浮动

    haslayout 根据元素内容的大小 或者父级的父级的大小来重新的计算元素的宽高

  display: inline-block

  height: (任何值除了auto)

  float: (left 或 right)

  width: (任何值除了auto)

  zoom: (除 normal 外任意值) 

*/

</style>

</head>

<body>

<div class="box clear">

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

</div>

</body>

</html>

.html清除浮动的6种方法示例

作者: 字体:[增加 减小] 类型:转载 时间:2013-11-15 我要评论

本文讲了6种清除HTML元素浮动的方法供大家参考使用,具体下看下文

..使用display:inline-block会出现的情况:

1.使块元素在一行显示

2.使内嵌支持宽高

3.换行被解析了

4.不设置的时候宽度由内容撑开

5.在IE6,7下步支持块标签

由于inline-block属性换行的时候被解析(有间隙)故解决方法使用浮动float:left/right

使用浮动时出现的情况:

1.使块元素在一行显示

2.使内嵌元素支持宽高

3.不设置不宽高的时候宽度由内容撑开

4.换行不被解析(故使用行内元素的时候清除间隙的方法可以使用浮动)

5.元素添加浮动,会脱离文档流,按照指定的一个方向移动,直到碰到父级的边界或者另一个浮动元素停止(文档流是文档中可显示对象在排列时所占用的位置)

复制代码 代码如下:

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

div,span{height:100px;background:red;border:1px solid #000; float:left;}

/*

inline-block

1.使块元素在一行显示

2.使内嵌支持宽高

3.换行被解析了

4.不设置宽度的时候宽度由内容撑开

5.在IE6,7下不支持块标签

浮动:

1.使块元素在一行显示

2.使内嵌支持宽高

3.不设置宽度的时候宽度由内容撑开

*/

</style>

</head>

<body>

<div class="div1">div1</div>

<div class="div2">div2</div>

<span class="span1">span1</span>

<span class="span2">span2</span>

</body>

</html>

下面的代码只有box1浮动,则box1,box2重叠一起。两者都浮动就不会重叠

复制代码 代码如下:

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box1{ width:100px;height:100px;background:red; float:left;}

.box2{ width:200px;height:200px;background:blue; /* float:left;*/}

</style>

</head>

<body>

<div class="box1"></div>

<div class="box2"></div>

</body>

</html>

清浮动的方法:

1.给父级也加浮动(这种情况当父级margin:0 auto;时不居中)

复制代码 代码如下:

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{ width:300px;margin:0 auto;border:10px solid #000; float:left;}

.div{ width:200px;height:200px;background:red;float:left;}

/*

    清浮动

    1.给父级也加浮动(不居中了)

*/

</style>

</head>

<body>

<div class="box">

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

</div>

</body>

</html>

2.给父级加display:inline-block;(同方法1,不居中。只有IE6,7居中)

复制代码 代码如下:

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{ width:300px;margin:0 auto;border:10px solid #000; display:inline-block;}

.div{ width:200px;height:200px;background:red;float:left;}

/*

    清浮动

    1.给父级也加浮动

    2.给父级加display:inline-block

*/

</style>

</head>

<body>

<div class="box">

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

</div>

</body>

</html>

3.在浮动元素下加<div class="clear"></div>

  .clear{ height:0px;font-size:0;clear:both;}但是在ie6下,块元素有最小高度,即当height<19px时,默认为19px,解决方法:font-size:0;或overflow:hidden;

复制代码 代码如下:

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{ width:300px;margin:0 auto;border:10px solid #000;}

.div{ width:200px;height:200px;background:red;float:left;}

.clear{ height:0px;font-size:0;clear:both;}

/*

    清浮动

    1.给父级也加浮动

    2.给父级加display:inline-block

    3.在浮动元素下加<div class="clear"></div>

    .clear{ height:0px;font-size:0;clear:both;}

*/

</style>

</head>

<body>

<div class="box">

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

        <div class="clear"></div>

</div>

</body>

</html>

4.在浮动元素下加<br clear="all">

复制代码 代码如下:

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{ width:300px;margin:0 auto;border:10px solid #000;}

.div{ width:200px;height:200px;background:red;float:left;}

/*

    清浮动

    1.给父级也加浮动

    2.给父级加display:inline-block

    3.在浮动元素下加<div class="clear"></div>

    .clear{ height:0px;font-size:0;clear:both;}

    4.在浮动元素下加<br clear="all"/>

*/

</style>

</head>

<body>

<div class="box">

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

    <br clear="all"/>

</div>

</body>

</html>

5.给浮动元素父级加{zoom:1;}

:after{content:""; display:block;clear:both;}

复制代码 代码如下:

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{margin:0 auto;border:10px solid #000;}

.div{ width:200px;height:200px;background:red;float:left;}

.clear{zoom:1;}

.clear:after{content:""; display:block;clear:both;}

/*

    清浮动

    1.给父级也加浮动

    2.给父级加display:inline-block

    3.在浮动元素下加<div class="clear"></div>

    .clear{ height:0px;font-size:0;clear:both;}

    4.在浮动元素下加<br clear="all"/>

    5. 给浮动元素的父级加{zoom:1;}

    :after{content:""; display:block;clear:both;}

    **在IE6,7下浮动元素的父级有宽度就不用清浮动

    haslayout 根据元素内容的大小 或者父级的父级的大小来重新的计算元素的宽高

  display: inline-block

  height: (任何值除了auto)

  float: (left 或 right)

  width: (任何值除了auto)

  zoom: (除 normal 外任意值) 

*/

</style>

</head>

<body>

<div class="box clear">

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

</div>

</body>

</html>

第6种.给浮动元素父级加overflow:auto;

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>无标题文档</title>

<style>

.box{ width:300px;border:1px solid #000;overflow:auto;}

.div1{ width:260px;height:400px;background:Red;float:left;}

</style>

</head>

<body>

<div class="box">

    <div class="div1"></div>

</div>

</body>

</html>

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×