html清除浮动的6种方法示例
使用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>
html清除浮动的6种方法示例的更多相关文章
- CSS读书笔记(3)---清除浮动的几种方法
浮动元素容易造成页面错位现象.下面说说关于清除浮动的几种方法. 首先.先创建一个浮动导致错位的页面. <!DOCTYPE html> <html lang="en" ...
- [Web 前端] 018 css 清除浮动的四种方法
清除浮动的四种方法 加 clear: ...(见例1) 父级上增加属性 overflow:hidden(见例2.1) 在最后一个子元素的后面加一个空的 div,给它一个样式属性 clear: both ...
- CSS 清除浮动的4种方法
此为未清除浮动源代码,运行代码无法查看到父级元素浅黄色背景.<style type=”text/css”> <!– *{margin:0;padding:0;} body{font: ...
- CSS 清除浮动的四种方法
在实际项目中,我们经常会用到float属性来对页面进行布局.当使用float时,意味着该元素已经脱离了文档流,相当于浮于文档之上,不占据空间.但是针对兄弟元素为文字内容时,会占据一定空间,从而产生文字 ...
- css清除浮动的几种方法整理
四种清除浮动方法如下: 1.使用空标签清除浮动.空标签可以是div标签,也可以是P 标签.这种方式是在需要清除浮动的父级元素内部的所有浮动元素后添加这样一个标签 清除浮动,并为其定义CSS代码:cle ...
- 前端布局常见IE6 bug的解决方法,清除浮动的几种方法以及各自的优缺点
相信有很多前端的朋友再布局的时候经常面对IE6咬牙切齿,尤其是刚刚入行的朋友,在这里给大家一点常见问题的解决方案,希望对大家有所帮助 1)png24位的图片在iE6浏览器上出现背景,解决方案是做成PN ...
- 活学活用,CSS清除浮动的4种方法
清除浮动这个问题,做前端的应该再熟悉不过了,咱是个新人,所以还是记个笔记,做个积累,努力学习向大神靠近. CSS清除浮动的方法网上一搜,大概有N多种,用过几种,说下个人感受. 1.结尾处加空div标签 ...
- css清除浮动的8种方法以及优缺点
浮动会使当前标签产生上浮的效果,同时会影响到前后的标签.父级标签的位置及width height 属性.而且同样的代码,在各种浏览器中效果可能不同,这样让清除浮动更难了.清除浮动引起的问题有很多的方法 ...
- 转载 | float 清除浮动的7种方法
什么叫浮动:浮动会使当前标签脱离文档流,产生上浮的效果,同时还会影响周边元素(前后标签)及父级元素的位置和width,height属性.下面用一个小例子来看一看浮动的全过程:1.首先我们新建一个网页, ...
随机推荐
- 127.0.0.1和localhost不能正确映射的问题
可能引起的问题: 检查:分别ping一下127.0.0.1和localhost 以上是正确演示,不匹配的话,ping localhost会返回: 解决方案: 可能1: 在浏览器中打开http://12 ...
- NX二次开发-Block UI C++界面Enumeration(枚举)控件的获取(持续补充)
NX9+VS2012 public: void SetBlockUIShow(); void EnumInt::SetBlockUIShow() { //获取枚举控件 PropertyList* En ...
- EXCEL2016 OLE/COM开发-常用功能封装代码
hpp #pragma once #include "stdafx.h" #include "CApplication.h" #include "CW ...
- docker镜像下载加速(5)
镜像下载加速 由于 Docker Hub 的服务器在国外,下载镜像会比较慢.幸好 DaoCloud 为我们提供了免费的国内镜像服务. 下面介绍如果使用镜像. 在 daocloud.io 免费注册一个用 ...
- 微信小程序学习笔记(一)--创建微信小程序
一.创建小程序 1.申请帐号.安装及创建小程序,请参照官方文档里面的操作 https://developers.weixin.qq.com/miniprogram/dev/. 小程序在创建的时候会要求 ...
- WinDbg中Check for invalid symbols or bad syntax(断点设置)解决办法
基础知识 bp 程序运行过程中下断点 bu 程序未加载之前下断点 bl 列出所有断点 bc 清除断点 今天在调试驱动的时候 发现下好断点后 无法调试 WinDbg显示 kd> g Break ...
- mvn eclipse:eclipse
pom.xml 在哪个文件夹, 你就在哪里按shift 右键,,[在此处打开命令窗口] 执行那个命令. mvn eclipse:eclipse
- 力扣算法题—111.Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the sh ...
- Java缓冲区读写
缓冲区读 有两种方法从缓冲区读取数据: 绝对位置 相对位置 使用四个版本重载的get()方法用于从缓冲区读取数据. get(int index)返回给定索引处的数据. get()从缓冲区中的当前位置返 ...
- JUC源码分析-线程池篇(三)ScheduledThreadPoolExecutor
JUC源码分析-线程池篇(三)ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor 继承自 ThreadPoolExecutor.它主要用来在 ...