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>

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

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

 



Weiter lernen
||
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!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">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
einreichenCode zurücksetzen
图片放大关闭