CSS Float(浮动)

float是什么?

float即为浮动,在CSS中的作用是使元素脱离正常的文档流并使其移动到其父元素的“最左边”或“最右边”。下面解释下这个定义中的几个名词的概念:

文档流:在html中文档流即为元素从上至下排列的顺序。

脱离文档流:元素从正常的排列顺序被抽离。

最左边/最右边:上述的移动到父元素最左和最右是指元素往左或往右移动直到碰到另一个浮动元素或父元素内容区的边界(不包括padding)。  

float属性:

 ① left :元素向左浮动。

 ② right :元素向右浮动。

 ③ none :默认值。

 ④ inherit :从父元素继承float属性。

实例:左浮动

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>实例</title> 
  <title>float属性</title>
    <style type="text/css">
        #a
        {
            background-color:Red;
            height:50px;
            width:100px;
        }
        #b
        {
            background-color:Yellow;    
            height:50px;
            width:200px;
            float:left;
        }
        #c
        {
            background-color:Blue;   
            height:50px;
            width:300px;
         }
         #d
         {
            background-color:Gray;
            height:50px;
            width:400px;
          }
    </style>
</head>
<body>
<div id="a">div-a</div>
<div id="b">div-b</div>
<div id="c">div-c</div>
<div id="d">div-d</div>
</body>
</html>

实例:右浮动

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>实例</title> 
  <title>float属性</title>
    <style type="text/css">
        #a
        {
            background-color:Red;
            height:50px;
            width:100px;
        }
        #b
        {
            background-color:Yellow;    
            height:50px;
            width:200px;
                  float:right;
        }
        #c
        {
            background-color:Blue;   
            height:50px;
            width:300px;
         }
         #d
         {
            background-color:Gray;
            height:50px;
            width:400px;
                  float:right;
         }
    </style>
</head>
<body>
<div id="a">div-a</div>
<div id="b">div-b</div>
<div id="c">div-c</div>
<div id="d">div-d</div>
</body>
</html>

彼此相邻的浮动元素

如果你把几个浮动的元素放到一起,如果有空间的话,它们将彼此相邻。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>实例</title> 
  <title>float属性</title>
    <style type="text/css">
        #a
        {
            background-color:Red;
            height:50px;
            width:100px;
        }
        #b
        {
            background-color:Yellow;    
            height:50px;
            width:200px;
        }
        #c
        {
            background-color:Blue;   
            height:50px;
            width:300px;
         }
         #d
         {
            background-color:Gray;
            height:50px;
            width:400px;
         }
         div
              {
             float:left;
              }
    </style>
</head>
<body>
<div id="a">div-a</div>
<div id="b">div-b</div>
<div id="c">div-c</div>
<div id="d">div-d</div>
</body>
</html>

清除浮动 - 使用 clear

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

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


继续学习
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>实例</title> <title>float属性</title> <style type="text/css"> #a { background-color:Red; height:50px; width:100px; } #b { background-color:Yellow; height:50px; width:200px; } #c { background-color:Blue; height:50px; width:300px; } #d { background-color:Gray; height:50px; width:400px; } div { float:right; } </style> </head> <body> <div id="a">div-a</div> <div id="b">div-b</div> <div id="c">div-c</div> <div id="d">div-d</div> </body> </html>
提交重置代码
  • 推荐课程
  • 评论
  • 问答
  • 笔记
  • 课件下载
哈哈哈

哈哈哈

讲的不是很清楚啊 看例子看了半天 应该多做点文字注释的

4年前    添加回复 0

回复
学习ing

学习ing

float即为浮动,在CSS中的作用是使元素脱离正常的文档流并使其移动到其父元素的“最左边”或“最右边”。

4年前    添加回复 0

回复
末日的春天

末日的春天

把几个浮动的元素放到一起,如果有空间的话,它们将彼此相邻。

4年前    添加回复 0

回复
橱窗的光

橱窗的光

浮动在布局的过程中用的是很多的,这个必须掌握

4年前    添加回复 0

回复

这个id选择器上面没有用#div1设置css样式,这个是怎么回事啊,两个div都可以用?

[最新 数据分析师 的回答]这个id选择器上面没有用#div1设置css样式,这个是怎么回事啊,两个div都可以用?-PHP中文网问答-这个id选择器上面没有用#div1设置css样式,这个是怎么回事啊,两个div都可以用?-PHP中文网问答围观一下哦,学习一下。

时间:4年前

男神

男神

left :元素向左浮动。 right :元素向右浮动。 none :默认值。 inherit :从父元素继承float属性。

4年前 0

课件暂不提供下载,工作人员正在整理中,后期请多关注该课程~