1. 傳統(tǒng)網(wǎng)頁的三種布局方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!--
網(wǎng)頁布局的本質(zhì): 就是用CSS來擺放盒子, 把盒子擺放到相應(yīng)的位置;
三種布局方式:
1. 普通流;
2. 浮動;
3. 定位;
-->
<!--
標(biāo)準(zhǔn)流(普通流/文檔流): 就是標(biāo)簽按照規(guī)定好的默認(rèn)方式進(jìn)行排列;
1. 塊級元素: 獨(dú)占一行, 從上往下順序排列; (div, hr, p, h1~h6, ul, ol, dl, form, table...)
2. 行內(nèi)元素: 從左至右順序排列, 碰到父親邊緣則自動換行; (span, a, i, em...)
===> 以上都是標(biāo)準(zhǔn)流布局, 我們前面學(xué)習(xí)的就是標(biāo)準(zhǔn)流, 標(biāo)準(zhǔn)流是最基本的布局方式;
-->
</body>
</html>
2. 為什么需要浮動
2.1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
height: 200px;
width: 200px;
/* display: inline-block; */
float: left;
}
</style>
</head>
<body>
<!--
為什么需要浮動?
有很多的布局效果, 標(biāo)準(zhǔn)流沒有辦法完成, 此時就可以利用浮動完成布局; 因?yàn)楦涌梢愿淖冊貥?biāo)簽的默認(rèn)排列方式;
浮動最典型應(yīng)用:可以讓多個塊級元素在一行內(nèi)排列顯示;
網(wǎng)頁布局第一準(zhǔn)則:多個塊級元素縱向排列找標(biāo)準(zhǔn)流, 多個塊級元素橫向排列找浮動;
-->
<!--
例子:比如有3個div, 默認(rèn)是縱向排列, 如何使其橫向排列?
法一:轉(zhuǎn)換成行內(nèi)塊元素; 此時一行顯示, 但是此時3個div之間存在間隔, 并且間隔是幾像素我們并不知道;
法二:浮動; 此時一行顯示, 并且之間沒有間隔;
-->
<div style="background-color: green;">1</div>
<div style="background-color: red">2</div>
<div style="background-color: hotpink;">3</div>
</body>
</html>
2.2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#div1 {
width: 200px;
height: 200px;
background-color: red;
float: left;
}
#div2 {
width: 200px;
height: 200px;
background-color: green;
float: left;
float: right;
}
/*
1. div1, div2 都是left, 那么兩者將靠左顯示, 并且div2緊挨著div1;
2. div1->left div2->right, 那么將是左青龍, 右白虎;
*/
</style>
</head>
<body>
<div id="div1">111</div>
<div id="div2">222</div>
</body>
</html>
3. 浮動特性
3.1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!--
1. 浮動元素會脫離標(biāo)準(zhǔn)流(脫標(biāo));
2. 浮動元素會一行內(nèi)顯示并且頂部對齊;
3. 浮動元素會具有行內(nèi)塊元素的特點(diǎn); (任何元素都可以進(jìn)行浮動, 具有浮動的元素可以直接指定高度和寬度)
-->
</body>
</html>
3.2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#left {
width: 100px;
height: 100px;
background-color: red;
float: left;
}
#right {
width: 200px;
height: 200px;
background-color: green;
}
</style>
</head>
<body>
<!--
脫標(biāo)特性:
1. 脫離標(biāo)流的控制,移動到指定位置(left, right);
2. 浮動的盒子不再保留原先的位置;
-->
<div id="left">div1</div>
<div id="right">div2</div>
<!--
由于div1進(jìn)行了左浮,div2沒有浮動,
所以div1脫標(biāo),不再保留原來的位置,原來的位置被2號占據(jù),
出現(xiàn)了堆疊性。
-->
</body>
</html>
3.3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
height: 200px;
width: 200px;
float: left;
}
span {
width: 200px;
height: 200px;
background-color: blue;
float: left; /* 行內(nèi)元素加了浮動特性, 不需要再轉(zhuǎn)換為塊級元素, 可以直接指定高度和寬度 */
}
p {
float: right;
/* float: left; */
height: 400px;
background-color: purple;
}
</style>
</head>
<body>
<!--
浮動第二特性:浮動元素會一行內(nèi)顯示并且頂部對齊;
(1)如果多個盒子都設(shè)置了浮動,則它們會按照屬性值 => 一行內(nèi)顯示并且頂端對齊排列。
(2)浮動的盒子是相互貼在一起的,相互之間沒有縫隙,如果父級寬度裝不下這些浮動的盒子,
那么多出的盒子將會自動另起一行顯示。(可以縮小窗口寬度觀察)
(3)任何元素都可以進(jìn)行浮動,浮動元素具有行內(nèi)塊元素的特性。
如果行內(nèi)元素有了浮動特性, 則不需要轉(zhuǎn)換成塊級/行內(nèi)元元素, 直接就可以指定高度和寬度;
(4)如果塊級盒子沒有設(shè)置寬度,默認(rèn)寬度和父級一樣寬; 但是添加浮動后,它的寬度根據(jù)內(nèi)容來決定。
-->
<!-- 驗(yàn)證第(1), (2): -->
<div style="background-color: red;">1</div>
<div style="background-color: green; height: 250px;">2</div>
<div style="background-color: blue;">3</div>
<div style="background-color: yellow;">4</div>
<!-- 驗(yàn)證第(3): -->
<span>文本1</span>
<span>文本2</span>
<!-- 驗(yàn)證第(4): -->
<p>HelloWorld</p>
</body>
</html>
4. 浮動布局練習(xí)
5.1 案例1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 1200px;
height: 460px;
background-color: green;
margin: 0 auto;
}
.left {
width: 230px;
height: 460px;
background-color: purple;
float: left;
}
.right {
width: 970px;
height: 460px;
background-color: skyblue;
float: left;
}
</style>
</head>
<body>
<!-- *** 浮動布局練習(xí)1 *** -->
<!--
浮動元素經(jīng)常和標(biāo)準(zhǔn)流父級盒子搭配使用:
為了約束浮動元素位置, 我們網(wǎng)頁布局一般采取的策略是:
先用標(biāo)準(zhǔn)流的父元素排列上下位置, 之后父元素的內(nèi)部子元素采取浮動來排列左右位置;
-->
<div class="box">
<div class="left">左盒子</div>
<div class="right">右盒子</div>
</div>
</body>
</html>
5.2 案例2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
.box {
background-color: red;
width: 1226px;
height: 285px;
margin: 0 auto;
}
.box li {
float: left;
width: 296px;
height: 285px;
background-color: purple;
margin-right: 14px;
}
.box .last {
margin-right: 0;
}
</style>
</head>
<body>
<!-- *** 浮動布局練習(xí)2 *** -->
<ul class="box">
<li>1</li>
<li>2</li>
<li>3</li>
<li class="last">4</li>
</ul>
</body>
</html>
5.3 案例3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 1226px;
height: 615px;
background-color: red;
}
.left {
float: left;
width: 234px;
height: 615px;
background-color: green;
}
.right {
float: left;
width: 992px;
height: 615px;
background-color:blue;;
}
.right > div {
float: left;
width: 234px;
height: 300px;
background-color: purple;
margin-left: 14px;
margin-bottom: 14px;
}
</style>
</head>
<body>
<!-- *** 浮動布局練習(xí)3 *** -->
<!--
1. 網(wǎng)頁布局第二準(zhǔn)則:
先設(shè)置盒子大小, 之后設(shè)置盒子的位置;
2. 浮動布局的注意事項(xiàng):
1. 先用標(biāo)準(zhǔn)流的父元素排列上下位置,之后內(nèi)部子元素采取浮動排列左右位置!
2. 一個元素浮動,理論上其余的兄弟元素也要浮動,否則將會引發(fā)一些排列問題?。ㄒ桓∪。? 3. 浮動的盒子只會影響浮動盒子后面的標(biāo)準(zhǔn)流,不會影響前面的標(biāo)準(zhǔn)流!
-->
<div class="box">
<!-- 以下兩個div(left, right)可以使用.left+.right快速生成 -->
<!-- 下面8個div可以用 div*8{$}快速生成 -->
<!-- -->
<div class="left"></div>
<div class="right">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</div>
</body>
</html>
5. 常見網(wǎng)頁布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
.top {
height: 50px;
background-color: gray;
}
.banner {
width: 980px;
height: 150px;
background-color: gray;
margin: 10px auto;
}
.box {
width: 978px;
height: 300px;
margin: 10px auto;
background-color: red;
}
.box li {
float: left;
width: 237px;
height: 300px;
background-color: gray;
margin-right: 10px;
}
.box .last {
margin-right: 0px;
}
.footer {
height: 200px;
background-color: gray;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="top">top</div>
<div class="banner">banner</div>
<div class="box"> <!-- ul>li*4{$} -->
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li class="last">4</li>
</ul>
</div>
<div class="footer"></div>
</body>
</html>
6. 浮動注意點(diǎn)
6.1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 800px;
height: 300px;
margin: 0 auto;
background-color: red;
}
.damao {
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: green;
}
.sanmao {
width: 300px;
height: 300px;
background-color: blue;
}
</style>
</head>
<body>
<!--
1. 浮動和標(biāo)準(zhǔn)流的父盒子搭配。
先用標(biāo)準(zhǔn)流的父元素排列上下位置,之后父元素內(nèi)部子元素采取浮動排列左右位置。
2. 一個元素浮動了,理論上其余的兄弟元素也要浮動。
一個盒子里面有多個子盒子,如果其中一個盒子浮動了,那么其他兄弟也應(yīng)該浮動,以防引起問題。
3. 浮動的盒子不會影響其前面的標(biāo)準(zhǔn)流, 只會影響其后面的標(biāo)準(zhǔn)流。(只影響后面)
===> 具體看運(yùn)行結(jié)果便知?。。? damao是標(biāo)準(zhǔn)流, 獨(dú)占一行顯示;
ermao給添加了float-left,它將脫標(biāo), 此時它不會影響其前面的標(biāo)準(zhǔn)流;
sammao是標(biāo)準(zhǔn)流, 由于ermao脫標(biāo)了, 會影響其后面的標(biāo)準(zhǔn)流, 導(dǎo)致其壓住了sanmao;
****** 浮動的盒子不再保留原先的位置;
-->
<div class="box">
<div class="damao">大毛</div> <!-- 標(biāo)準(zhǔn)流 -->
<div class="ermao">二毛</div> <!-- 左浮動 -->
<div class="sanmao">三毛</div> <!-- 標(biāo)準(zhǔn)流 -->
</div>
</body>
</html>
6.2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 800px;
height: 300px;
margin: 0 auto;
background-color: gray;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
width: 400px;
height: 150px;
background-color: green;
}
.sanmao {
float: left;
width: 300px;
height: 300px;
background-color: blue;
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div> <!-- 浮動 -->
<div class="ermao">二毛</div> <!-- 標(biāo)準(zhǔn)流 -->
<div class="sanmao">三毛</div> <!-- 浮動 -->
</div>
<!--
首先:damao 進(jìn)行了脫標(biāo)操作, 導(dǎo)致 damao 浮在了 ermao 上面;
而 sanmao又是浮動, 但是它不會影響前面的標(biāo)準(zhǔn)流(ermao), 它和 damao 都是浮動, 自然要緊挨著 damao 了,
但是由于 ermao 的存在, 所以不會和 damao頂端對齊, 所以最終它左側(cè)緊挨著 damao , 上側(cè)緊挨著ermao;
-->
</body>
</html>
7. 清除浮動
7.1 為什么清除浮動
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 800px;
margin: 0 auto;
border: 1px solid blue;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: green;
}
.footer {
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<!--
[1]. 為什么要清清除浮動?
=>由于父級盒子在很多情況之下,不方便給高度,但是子盒子浮動又不占有位置,
最后父級盒子高度為0時,就會影響下面的標(biāo)準(zhǔn)流盒子。
(理想的情況是,不指定父盒子高度,使父盒子的高度隨子盒子的加入自動增長,自動被子盒子撐開)
2. 清除浮動的本質(zhì):清除浮動元素所造成的影響;
[3]. 如果父盒子本身有高度,則不需要清除浮動;
[4]. 清除浮動之后,父級就會根據(jù)子盒子的高度自動檢測高度,父級有了高度,就不會影響下面的標(biāo)準(zhǔn)流了;
5. clear: left(不允許左側(cè)有浮動元素)|right|both;
6. 策略:閉合浮動。
7. 何時需要清除浮動?
(1) 父級沒有高度;
(2) 子盒子浮動了;
(3) 影響下面布局了, 我們就應(yīng)該清除浮動了;
8. 清除浮動的方法:
(1)額外標(biāo)簽法(隔墻法);[新增的標(biāo)簽必須是塊級元素,不能是行內(nèi)元素]
(2)父級添加overflow屬性;
(3)父級添加::after偽元素;
(4)父級添加雙偽元素(::before, ::after)。
-->
</body>
<!--
本例子展示了清除浮動的背景:
(1) 父盒子box并沒有像之前那樣指定高度;
(2) 此時子盒子浮動不占有位置, 將會導(dǎo)致父級盒子高度為0, 此時將會影響下面的標(biāo)準(zhǔn)流盒子;
=> 見運(yùn)行結(jié)果即知!
-->
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer">footer</div>
</html>
7.2 簡單補(bǔ)充
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.com {
float: left;
width: 100px;
height: 100px;
}
.clear {
clear: left;
}
span {
display: block;
width: 300px;
height: 300px;
background-color: yellow;
}
</style>
</head>
<body>
<!--
本來, 兩個div都是左浮動的, 按道理是緊挨在一起的;
但是我們?yōu)榈诙€div清除了左浮動, 此時第二個div就不會左靠著div1了, 而是另起一行顯示;
-->
<div class="com" style="background-color: red;">123</div>
<div class="com clear" style="background-color: green;">456</div>
<span>好吧</span>
</body>
</html>
7.3 隔墻法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!--
1. 額外標(biāo)簽法(隔墻法)清除浮動:
(1)是W3C的做法;
(2)在浮動元素末尾添加一個空的標(biāo)簽, 然后指定clear屬性: both。例如 <div style="clear: both"></div>, 或者是其他標(biāo)簽(如<br/>等)
2. 缺點(diǎn):添加無意義標(biāo)簽, 結(jié)構(gòu)混亂;
3. Notice: 新添加的標(biāo)簽要是必須是塊級元素, 不能是行內(nèi)元素;
-->
<style>
.box {
width: 800px;
margin: 0 auto;
border: 1px solid blue;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: green;
}
.footer {
height: 200px;
background-color: yellow;
}
.clear {
clear: both;
}
</style>
</head>
<body>
</body>
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
<div class="ermao">二毛</div>
<div class="ermao">二毛</div>
<div class="ermao">二毛</div>
<div class="clear"></div> <!-- 額外標(biāo)簽法 -->
<!--
<span class="clear"></span>
===> ERROR,span是行內(nèi)元素,不能夠清除浮動!
-->
</div>
<div class="footer">footer</div>
</html>
7.4 overflow屬性清除浮動
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!--
給父盒子添加overflow屬性來清除浮動;
優(yōu)點(diǎn) :代碼簡潔
缺點(diǎn):無法顯示溢出部分
-->
<style>
.box {
width: 800px;
border: 1px solid red;
margin: 0 auto;
overflow: hidden;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: green;
}
.footer {
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer"></div>
</body>
</html>
7.5 after偽元素清除浮動
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.clearfix::after {
content: ""; /* 偽元素必須寫此屬性 */
display: block; /* 插入的元素必須是塊級 */
height: 0; /* 高度為0, 看不見該元素 */
clear: both; /* 清除浮動的核心代碼 */
visibility: hidden; /*不要看見該元素*/
}
.clearfix {
*zoom: 1; /* IE6,7專有 */
}
.box {
width: 800px;
border: 1px solid red;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: green;
}
.footer {
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<!--
父盒子添加after元素來清除浮動;
-->
<div class="box clearfix">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer">標(biāo)準(zhǔn)流盒子</div>
</body>
</html>
7.6 雙偽元素清除浮動
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.clearfix::before, .clearfix::after {
content: "";
display: table;
}
.clearfix::after {
clear: both;
}
.clearfix {
*zoom: 1;
}
.box {
width: 800px;
border: 1px solid red;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: green;
}
.footer {
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<!--
給父盒子添加雙偽元素來清除浮動;
-->
<div class="box clearfix">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer"></div>
</body>
</html>
7.7 學(xué)成在線案例
git地址:https://gitee.com/xingweicoding/studys.git
文章來源:http://www.zghlxwxcb.cn/news/detail-464561.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-464561.html
到了這里,關(guān)于css浮動特性的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!