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>
預覽
Clear
- 課程推薦
- 課件下載
課件暫不提供下載,工作人員正在整理中,後期請多關注該課程~ 















