百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>html5教程> 几种对于html革除浮动的办法
分享文章到:

几种对于html革除浮动的办法

发布时间:09/01 来源:未知 浏览: 关键词:
本文讲了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>
p,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>
<p class="p1">p1</p>
<p class="p2">p2</p>
<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>
<p class="box1"></p>
<p class="box2"></p>
</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;}
.p{ width:200px;height:200px;background:red;float:left;}
/*
    清浮动
    1.给父级也加浮动(不居中了)
*/
</style>
</head>
<body>
<p class="box">
    <p class="p"></p>
</p>
</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;}
.p{ width:200px;height:200px;background:red;float:left;}
/*
    清浮动
    1.给父级也加浮动
    2.给父级加display:inline-block
*/
</style>
</head>
<body>
<p class="box">
    <p class="p"></p>
</p>
</body>
</html>

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

.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;}
.p{ width:200px;height:200px;background:red;float:left;}
.clear{ height:0px;font-size:0;clear:both;}
/*
    清浮动
    1.给父级也加浮动
    2.给父级加display:inline-block
    3.在浮动元素下加<p class="clear"></p>
    .clear{ height:0px;font-size:0;clear:both;}
*/
</style>
</head>
<body>
<p class="box">
    <p class="p"></p>
        <p class="clear"></p>
</p>
</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;}
.p{ width:200px;height:200px;background:red;float:left;}
/*
    清浮动
    1.给父级也加浮动
    2.给父级加display:inline-block
    3.在浮动元素下加<p class="clear"></p>
    .clear{ height:0px;font-size:0;clear:both;}
    4.在浮动元素下加<br clear="all"/>
*/
</style>
</head>
<body>
<p class="box">
    <p class="p"></p>
    <br clear="all"/>
</p>
</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;}
.p{ width:200px;height:200px;background:red;float:left;}
.clear{zoom:1;}
.clear:after{content:""; display:block;clear:both;}
/*
    清浮动
    1.给父级也加浮动
    2.给父级加display:inline-block
    3.在浮动元素下加<p class="clear"></p>
    .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>
<p class="box clear">
    <p class="p"></p>
</p>
</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;}
.p1{ width:260px;height:400px;background:Red;float:left;}
</style>
</head>
<body>
<p class="box">
    <p class="p1"></p>
</p>
</body>
</html>

以上就是本文的全部内容,但愿对大家的学习有所帮忙,更多相关内容请关注PHP中文网!

相关引荐:

怎样使用Html屏蔽右键菜单和左键划字功效

关于HTML 文本格局化的代码

以上就是几种关于html清除浮动的办法的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有152人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板