css中的clear的作用是什么_css中class的用法

css中的clear的作用是什么_css中class的用法cssclear属性深入了解:一、什么是CSSclear清除浮动?元素浮动之后,周围的元素会重新排列,为了避免这种情况,使用clear属性。clear属性指定元素两侧不能出现浮动元素。使用clear属性往文本中添加图片廊:clear属性值:lef…


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

css  clear属性深入了解:

 

 

一、什么是 CSS  clear清除浮动?

       

            元素浮动之后,周围的元素会重新排列,为了避免这种情况,使用 clear 属性。

            clear 属性指定元素两侧不能出现浮动元素。

            使用 clear 属性往文本中添加图片廊:

  

clear属性值:

left 清除该元素 左边的浮动元素。俗一点就是说谁设置了clear:left属性,谁的左边就不允许存在浮动的元素

right 清除该元素 右边的浮动元素。俗一点就是说谁设置了clear:right属性,谁的右边就不允许存在浮动的元素

both清除两边的浮动 ,清除该元素 左右边的浮动元素。俗一点就是说谁设置了clear:both属性,谁的左右边 都不允许存在浮动的元素       

none,就是不做任何处理,不清除任意一边的浮动元素

inherit,就是让它跟随父元素的属性值,父元素如何设置清除,它就如何设置清除。

 

 

 

属性 描述 值 CSS
clear 指定不允许元素周围有浮动元素。 left
right
both
none
inherit
1

二、clear应用场景

        ①Float  往往是用于图像,就像是办公软件word中的文字坏绕图片的方式设置,

                那么clear就是让元素不要坏绕图片,而是自成一行

        ②float  布局时一样非常有用(让元素脱离正常的文档流)。

                那么clear就是让脱离的元素回归到正常的文档流中。

 

     清除图片浮动应用实例:

    实例:图片浮动在左边,后面的元素清除左边的浮动,就是不要让p的左边存在  带有 浮动属性  的  元素。

               那么p元素就会自己再去成一行了。

 
p {
      clear: left;
    }
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    img {
      float: left;
    }

    p {
      clear: left;
    }
  </style>
</head>

<body>

  <div style="width: 300px;border: 1px solid black;">
    <img src="http://www.runoob.com/images/klematis_small.jpg" alt="tu1" />
    <p>这里是一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵 这里是一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵 这里是一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵花一朵
    </p>
  </div>
</body>

</html>

结果展示:

 

    没有清除浮动                                                                                 设置清除浮动

 

css中的clear的作用是什么_css中class的用法                        css中的clear的作用是什么_css中class的用法
布局中清除浮动实例: 

float通过浮动设置,让文档脱离正常的标准流。设置了浮动的元素可以紧跟上上一个设置了浮动的元素后面,不再自成一行。

clear让这些浮动的元素  后面的元素回归正常的文档流,拥有自己的空间

实例:

设计一个空div,让其两边清除浮动,那么后面的元素就不会被覆盖,拥有自己的空间。

 .clearfloat{
    clear: both;
  }
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<style>
  body,
  p {
    padding: 0;
    margin: 0;
  }

  nav {
    width: 100%;
    height: 30px;
    background-color: gray;
  }

  .menu {
    float: left;
    width: 20%;
    height: 300px;
    background-color: blueviolet;
  }

  .banner {
    float: left;
    width: 60%;
    height: 300px;
    background-color: rosybrown;
  }

  .selector {
    float: left;
    width: 20%;
    height: 300px;
    background-color: yellowgreen;
  }

  .clearfloat {
    clear: both;
  }

  .normal {
    width: 50%;
    height: 320px;
    border: 1px dashed green;
    background: red;
  }
</style>

<body>
  <nav>导航导航区域</nav>
  <div class="menu">菜单菜单区域</div>
  <div class="banner">轮播轮播区域</div>
  <div class="selector">查询查询区域</div>
  <div class="clearfloat"></div>
  <div class="normal">这里是正常的文档,没有设置浮动,没有浮动,没有浮动!</div>
</body>

</html>

结果展示:

清除浮动之后,normal div就有自己的正常空间了。

css中的clear的作用是什么_css中class的用法

 

 

 

三、clear 属性各个属性值。

left 清除该元素 左边的浮动元素。俗一点就是说谁设置了clear:left属性,谁的左边就不允许存在浮动的元素

right 清除该元素 右边的浮动元素。俗一点就是说谁设置了clear:right属性,谁的右边就不允许存在浮动的元素

both清除两边的浮动 ,清除该元素 左右边的浮动元素。俗一点就是说谁设置了clear:both属性,谁的左右边 都不允许存在浮动的元素       

none,就是不做任何处理,不清除任意一边的浮动元素

inherit,就是让它跟随父元素的属性值,父元素如何设置清除,它就如何设置清除。

 

 

如下代码,1,2,3,4   div元素设置了做浮动,5,6,7 div元素设置了右浮动。

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .box1,
    .box2,
    .box3,
    .box4,
    .box5,
    .box6,
    .box7 {
      width: 100px;
      height: 100px;
    }

    .box1,
    .box2,
    .box3,
    .box4 {
      float: left;
    }

    .box5,
    .box6,
    .box7 {
      float: right;
    }

    .clear1 {
      clear: left;
    }
  </style>
</head>

<body>

  <div class="box1" style="background-color:red;">111</div>
  <div class="box2" style="background-color:rgb(115, 255, 0);">222</div>
  <div class="box3" style="background-color:rgb(187, 255, 0);">3333</div>
  <div class="box4" style="background-color:rgb(255, 0, 191);">444</div>
  <div class="box5" style="background-color:rgb(4, 0, 255);">555</div>
  <div class="box6" style="border:1px dashed balck;background-color:rgb(0, 255, 0);">666</div>
  <div class="box7" style="background-color:rgb(255, 0, 191);">777</div>



</body>

</html>

 

结果展示:

 

css中的clear的作用是什么_css中class的用法
 

下面是给box3,333这个div设置清除左边浮动,(那么它的左边就不会存在有浮动的元素),

 

代码:

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .box1,
    .box2,
    .box3,
    .box4,
    .box5,
    .box6,
    .box7 {
      width: 100px;
      height: 100px;
    }

    .box1,
    .box2,
    .box3,
    .box4 {
      float: left;
    }

    .box5,
    .box6,
    .box7 {
      float: right;
    }

    .clearfloatleft {
      clear: left;
    }

    .clearfloatright {
      clear: right;
    }

    .clearfloatboth {
      clear: both;
    }
  </style>
</head>

<body>

  <div class="box1" style="background-color:red;">111</div>
  <div class="box2" style="background-color:rgb(115, 255, 0);">222</div>
  <div class="box3 clearfloatleft" style="background-color:rgb(187, 255, 0);">3333</div>
  <div class="box4" style="background-color:rgb(255, 0, 191);">444</div>
  <div class="box5" style="background-color:rgb(4, 0, 255);">555</div>
  <div class="box6" style="border:1px dashed balck;background-color:rgb(0, 255, 0);">666</div>
  <div class="box7" style="background-color:rgb(255, 0, 191);">777</div>



</body>

</html>

结果展示:

给3设置清除左边浮动,因为1与2都是浮动的,为了达到3清除浮动的效果,3就自成一行了,紧接着3后面的元素,4,5,6,7还是按照自己的属性该干嘛干嘛,如何浮动就如何紧接着3的后面浮动。

css中的clear的作用是什么_css中class的用法

 

 

下面是给box7,777这个div设置清除右边浮动,(那么它的右边就不会存在有浮动的元素),

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .box1,
    .box2,
    .box3,
    .box4,
    .box5,
    .box6,
    .box7 {
      width: 100px;
      height: 100px;
    }

    .box1,
    .box2,
    .box3,
    .box4 {
      float: left;
    }

    .box5,
    .box6,
    .box7 {
      float: right;
    }

    .clearfloatleft {
      clear: left;
    }

    .clearfloatright {
      clear: right;
    }

    .clearfloatboth {
      clear: both;
    }
  </style>
</head>

<body>

  <div class="box1" style="background-color:red;">111</div>
  <div class="box2" style="background-color:rgb(115, 255, 0);">222</div>
  <div class="box3 clearfloatleft" style="background-color:rgb(187, 255, 0);">3333</div>
  <div class="box4" style="background-color:rgb(255, 0, 191);">444</div>
  <div class="box5" style="background-color:rgb(4, 0, 255);">555</div>
  <div class="box6" style="border:1px dashed balck;background-color:rgb(0, 255, 0);">666</div>
  <div class="box7 clearfloatright" style="background-color:rgb(255, 0, 191);">777</div>



</body>

</html>

结果展示:

设置了box7 ,清除右边浮动,它右边的666,555都是浮动的,为了达到box7 右边没有浮动,box7就只好自己自成一行了。

 

css中的clear的作用是什么_css中class的用法

 

下面是给box4,444这个div设置清除两边浮动,(那么它的两边都不会存在有浮动的元素),

代码:

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .box1,
    .box2,
    .box3,
    .box4,
    .box5,
    .box6,
    .box7 {
      width: 100px;
      height: 100px;
    }

    .box1,
    .box2,
    .box3,
    .box4 {
      float: left;
    }

    .box5,
    .box6,
    .box7 {
      float: right;
    }

    .clearfloatleft {
      clear: left;
    }

    .clearfloatright {
      clear: right;
    }

    .clearfloatboth {
      clear: both;
    }
  </style>
</head>

<body>

  <div class="box1" style="background-color:red;">111</div>
  <div class="box2" style="background-color:rgb(115, 255, 0);">222</div>
  <div class="box3 clearfloatleft" style="background-color:rgb(187, 255, 0);">3333</div>
  <div class="box4 clearfloatboth" style="background-color:rgb(255, 0, 191);">444</div>
  <div class="box5" style="background-color:rgb(4, 0, 255);">555</div>
  <div class="box6" style="border:1px dashed balck;background-color:rgb(0, 255, 0);">666</div>
  <div class="box7 clearfloatright" style="background-color:rgb(255, 0, 191);">777</div>



</body>

</html>

结果展示:

好像box4右边的浮动没有被清除啊?这个怎么就不正确了呢?没有按照理解中的来了呢?

css中的clear的作用是什么_css中class的用法

难道是因为555,666是离开太远了,影响不到?下面来一个box4-5是进跟在box4后面的,

代码:

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .box1,
    .box2,
    .box3,
    .box4,
    .box4-5,
    .box5,
    .box6,
    .box7 {
      width: 100px;
      height: 100px;
    }

    .box1,
    .box2,
    .box3,
    .box4,
    .box4-5 {
      float: left;
    }

    .box5,
    .box6,
    .box7 {
      float: right;
    }

    .clearfloatleft {
      clear: left;
    }

    .clearfloatright {
      clear: right;
    }

    .clearfloatboth {
      clear: both;
    }
  </style>
</head>

<body>

  <div class="box1" style="background-color:red;">111</div>
  <div class="box2" style="background-color:rgb(115, 255, 0);">222</div>
  <div class="box3 clearfloatleft" style="background-color:rgb(187, 255, 0);">3333</div>
  <div class="box4 clearfloatboth" style="background-color:rgb(255, 0, 191);">444</div>
  <div class="box4-5 clearfloatboth" style="background-color:gold;">444-555</div>
  <div class="box5" style="background-color:rgb(4, 0, 255);">555</div>
  <div class="box6" style="border:1px dashed balck;background-color:rgb(0, 255, 0);">666</div>
  <div class="box7 clearfloatright" style="background-color:rgb(255, 0, 191);">777</div>



</body>

</html>

 

结果展示:

 

还是不行啊?! box4右边还是存在浮动的元素,那么到底是因为什么呢?

css中的clear的作用是什么_css中class的用法

 

四、出现清除没有效果的原因???

原因: 代码是顺序执行的,设置清除的没法影响后面加载的元素的,它只能影响前面的元素!

屁列,说法一样不对,下面的一个例子中box3出现可以清除是可以理解的,box4的加载在最后面怎么也可以清除了?

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

  如下,box1 与box3设置为做浮动,box设置为右浮动,下载来给box3设置清除两边浮动,

 它们的加载顺序是box1,然后是box2,最终是box3,

 代码:

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .box1,
    .box2,
    .box3,
    .box4,
    .box5,
    .box6,
    .box7 {
      width: 100px;
      height: 100px;
    }

    .box1,
    .box3 {
      float: left;
    }

    .box2 {
      float: right;
    }

    .clearboth {
      clear: both;
    }
  </style>
</head>

<body>
  <div class="box1" style="background-color:red;">111</div>
  <div class="box2 " style="background-color:rgb(115, 255, 0);">222</div>
  <div class="box3 clearboth" style="background-color:rgb(187, 255, 0);">3333</div>
</body>

</html>

 

  结果展示:

 

 给box3设置,3个属性值的效果都是一致的。

 

box3设置  clear:both                                                                                   box3  设置clear:left                                               

css中的clear的作用是什么_css中class的用法css中的clear的作用是什么_css中class的用法

box3设置 clear:right  

css中的clear的作用是什么_css中class的用法

 

 

下面同样的box4设置清除效果。

下载顺序box1,box2,box3,box4,

代码:

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .box1,
    .box2,
    .box3,
    .box4,
    .box5,
    .box6,
    .box7 {
      width: 100px;
      height: 100px;
    }

    .box1,
    .box3 {
      float: left;
    }

    .box2,
    .box4 {
      float: right;
    }

    .clearboth {
      clear: both;
    }

    .clearleft {
      clear: left;
    }

    .clearright {
      clear: right;
    }
  </style>
</head>

<body>

  <div class="box1" style="background-color:red;">111</div>
  <div class="box2 " style="background-color:rgb(115, 255, 0);">222</div>
  <div class="box3 " style="background-color:rgb(187, 255, 0);">3333</div>
  <div class="box4 clearboth " style="background-color:gray;">44444</div>



</body>

</html>

box4设置 clear:both                                                                                                box4设置 clear:left

css中的clear的作用是什么_css中class的用法             css中的clear的作用是什么_css中class的用法

 

 

box4设置 clear:right  

box4是在box2后面

css中的clear的作用是什么_css中class的用法

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194832.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Windows安装pip方法「建议收藏」

    pip是一款非常方便的python包管理工具,本文主要介绍在windows10下安装pip方法。1.下载pip地址:https://pypi.python.org/pypi/pip#downloads注意选择tar.gz压缩包,目前最新版本为9.0.1,这里选择的版本是:pip-9.0.1.tar.gz(md5,pgp)点击:下载2.解压安装解压下载的压缩包至…

    2022年4月13日
  • java的pdf转永中_永中pdf转word下载|

    java的pdf转永中_永中pdf转word下载|永中pdf转word是永中软件推出的一款网页版在线pdf转word转换器工具,这款软件之所以能在众多同类型软件中脱颖而出,是因为有这几个亮点,一个是免费且无需下载,二是不限使用次数,再就是转换后无乱码、排版整齐,有需要的朋友不要错过哦!永中pdf转word转换器介绍PDF意为”便携式文档格式”,以易于传输与储存、方便阅读、高质感等优点越来越多被使用于办公、学习和科研中,PDF文件一般需要安装阅读器…

    2022年5月18日
  • [原创] 仿滴滴出行打车APP定位导航,增加RFID认证、海外版、司机证件号码识别功能

    [原创] 仿滴滴出行打车APP定位导航,增加RFID认证、海外版、司机证件号码识别功能这是我自己做的一个仿滴滴打车的Android出行项目,主要针对滴滴等出行平台一直饱受质疑的“人车不符”问题,以及当前越发火热的或计划和出海战略,给出行项目增加了下面几个功能:1.RFID识别验证功能:在司机证件或者车内识别硬件嵌入RFID识别芯片,乘客使用手机读取到芯片信息,并且通过网络发送到出行平台数据库进行验证(我用JNI加了一个C语言的MD5加密算法对识别到的信息进行了加密)。如果不是合规的“人”或“车”,则不能完成订单并向平台或监管单位汇报当前位置。

    2022年7月24日
  • 网上英语学习资源大整理

    网上英语学习资源大整理 翻译http://www.bilinguist.com/汉英论坛,高手云集[url]http://www.chinatranslate.net/[/url]中国翻译网,号称全国最大的翻译专业网站[url]http://gb.transea.com/[/url]机器即时翻译,适合整体翻译网站及段落,但不够准确。适合一般人[url]http://www.si-chi…

    2022年10月26日
  • 拉格朗日插值公式详解[通俗易懂]

    拉格朗日插值公式详解[通俗易懂]一.线性插值(一次插值)   已知函数f(x)在区间[xk ,xk+1 ]的端点上的函数值yk =f(xk ),yk+1 =f(xk+1 ),求一个一次函数y=P1 (x)使得yk =f(xk ),yk+1 =f(xk+1 ),其几何意义是已知平面上两点(xk ,yk ),(xk+1 ,yk+1 ),求一条直线过该已知两点。   1.插值函数和插值基函数由直线的

    2022年10月22日
  • 主板上的minipcie_移远4g模组

    主板上的minipcie_移远4g模组MiniPCIE封装4G模组价值仍在2019年,中国正式进入5G商用元年。4G网络不管是速度、还是信号上都再无优势,那么4G网络会被淘汰吗?现在物联网应用最大的承载部分是在2G/3G网络,而现在的NB-IOT网络,不适用在高速率,低延时通信场景中。以前对速率,时延有一定要求的物联网设备将会向4G的LTE网络迁移,4G网络已经足够承载相关设备。也就是说,很长一段时间内就是作为物联网的承载网络…

    2022年9月8日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号