塊級元素
和行內元素
,若是想讓一些元素既要有塊級元素的特色也同時保留行內元素特色,只能讓這些元素脫離標準文檔流便可。float
屬性來實現的。float
屬性值說明表:屬性值 | 描述 |
---|---|
left | 設置元素向左浮動。 |
right | 設置元素向右浮動。 |
class
屬性值爲.box1
元素設置爲右浮動。代碼塊html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮動</title> <style> .box{ width: 600px; border: 1px solid #000; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
結果圖ui
div
標籤中尚未內容呢,如今咱們將子div
標籤設置寬高度爲100px
像素而且添加背景顏色。代碼塊spa
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮動</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
結果圖3d
div
標籤都是塊級元素。class
屬性值爲.box1
的元素設置爲右浮動。代碼塊code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮動</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:right; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
結果圖htm
注意:如今咱們發現
calss
屬性值爲.box
元素高度變矮了,這就說明了(浮動元素它已經脫離了標準文檔流,再也不佔用空間了)、而且向右浮動,浮動到自身的父元素的邊緣位置就中止了浮動。blog
class
屬性值爲.box1
元素設置爲左浮動。代碼塊文檔
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮動</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
結果圖it
class
屬性值爲.box2
元素看不見的緣由。結果圖Atable
結果圖B
class
屬性值爲.box
是一個池塘,3
個子元素都是可以漂浮在池塘水面上的東西,如今咱們將calss
屬性值爲.box1
元素浮動起來,漂在池塘水面上,是否是就再也不佔用池塘內的空間了。class
屬性值爲.box2
元素看不見,並不表明它不存在只是被class
屬性值爲.box1
元素給遮擋住了,如今咱們將class
屬性值爲.box2
元素寬度設置爲150px
像素。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮動</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 150px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
結果圖
注意:事實證實
class
屬性值爲.box2
元素是存在的。
calss
屬性值爲.box2
元素設置爲左浮動看看有什麼不同的效果代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮動</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 150px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
結果圖
class
屬性值爲.box2
的元素左浮動並無左浮動到自己父元素的邊緣位置,爲何在class
屬性值爲.box1
後面呢?由於父元素已經有了浮動的子元素後面的子元素在浮動就浮動到前面浮動的元素以後。class
屬性值爲.box3
的元素設置爲左浮動,看看有什麼不同的效果。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮動</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 150px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
結果圖
注意:浮動元素浮動之後,其父元素再也不將浮動的子元素包裹在父元素以內,因此結果圖出現一條黑色的邊框線,如有不明白的看第一個實踐內容。
span
標籤設置爲左浮動。span
標籤設置寬高度和背景顏色有什麼效果。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮動</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <span class="box1">微笑是最初的信仰1</span> <span class="box2">微笑是最初的信仰2</span> <span class="box3">微笑是最初的信仰3</span> </div> </body> </html>
結果圖
span
標籤設置了寬高度爲100px
像素並無生效,由於如今span
標籤仍是行內元素。span
標籤設置左浮動,而後咱們在看看效果如何。代碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮動</title> <style> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float: left; } .box2{ width: 100px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <span class="box1">微笑是最初的信仰1</span> <span class="box2">微笑是最初的信仰2</span> <span class="box3">微笑是最初的信仰</span> </div> </body> </html>
結果圖
注意:行內元素設置爲浮動以後就擁有了塊級元素的特色。