CSS基础教程之浮动

CSS浮动

  •  float:让元素浮动,取值:left(左浮动)、right(右浮动)

  • 浮动的元素,将向左或向右浮动,浮动到包围元素的边上,或上一个浮动元素的边上为止。

  • 浮动的元素,不再占空间了,并且,浮动元素的层级要高于普通元素。

  • 浮动的元素,一定是“块元素”。不管它原来是什么元素。

  • 如果浮动的元素,没有指定宽度的话,浮动后它将尽可能的变窄。因此,浮动元素一般要定宽和高。

  • 一行的多个元素,要浮动一起浮动。

浮动的功能:可以实现将多个块元素并列排版。

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
    <style type="text/css">
        .box{
            width:350px;
            height:400px;
            background-color:#f0f0f0;
        }
        .box1{
             width:50px;
            height:50px;
            background-color:#ff0000;
            float:left;
        }
        .box2{
             width:50px;
            height:50px;
            background-color:#00ff00;
            float:left;
        }
        .box3{
             width:50px;
            height:50px;
            background-color:#0000ff;
            float:left;
        }
    </style>
    </head>
    <body>
        <div class="box">
            <div class="box1">php.cn</div>
            <div class="box2">php.cn</div>
            <div class="box3">php.cn</div>
        </div>
    </body>
</html>

问:如何让包围元素,包住浮动元素?

这时我们就需要下一节的知识了,在浮动元素的下边,使用清除浮动操作。

 



继续学习
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <style type="text/css"> .box{ width:350px; height:400px; background-color:#f0f0f0; } .box1{ width:50px; height:50px; background-color:#ff0000; float:left; } .box2{ width:50px; height:50px; background-color:#00ff00; float:left; } .box3{ width:50px; height:50px; background-color:#0000ff; float:left; } </style> </head> <body> <div class="box"> <div class="box1">php.cn</div> <div class="box2">php.cn</div> <div class="box3">php.cn</div> </div> </body> </html>
提交重置代码
章节
笔记
提问
课件
反馈
捐赠

HTML+CSS基础入门教程

  • 推荐课程
  • 评论
  • 问答
  • 笔记
  • 课件下载

我是灰太狼

这个在以后页面布局的时候能用到

8年前    添加回复 0

看山看水看代码

浮动的元素,不再占空间了,并且,浮动元素的层级要高于普通元素。 浮动的元素,一定是“块元素”。不管它原来是什么元素。 记住了

8年前    添加回复 0

看透不说透

浮动用的还是很多的,div 布局的时候我经常用到浮动

8年前    添加回复 0

baby不要哭泣

可以都靠一边对齐

8年前    添加回复 0

尼古拉阿列克塞耶维奇奥斯特洛夫斯基

对网页的排版很有用

8年前    添加回复 0

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