前面的話
在Web頁面或者說移動頁面制作中,常常看到圖文混排效果,圖片居左(或居右),內容居右(或居左)排列。常常把這樣的效果稱為媒體對象。可以說它是一種抽象的樣式,可以用來構建不同類型的組件。本文將詳細介紹Bootstrap媒體對象
默認樣式
媒體對象一般是成組出現,而一組媒體對象常常包括以下幾個部分:
☑ 媒體對像的容器:常使用“media”類名表示,用來容納媒體對象的所有內容
☑ 媒體對像的對象:常使用“media-object”表示,就是媒體對象中的對象,常常是圖片
☑ 媒體對象的主體:常使用“media-body”表示,就是媒體對像中的主體內容,可以是任何元素,常常是圖片側邊內容
☑ 媒體對象的標題:常使用“media-heading”表示,就是用來描述對象的一個標題,此部分可選
除了上面四個部分之外,在Bootstrap框架中還常常使用“media-left”或者“media-right”來控制媒體對象中的對象浮動方式
[注意]在 html 結構中, .media-right 應當放在 .media-body 的后面
媒體對象樣式相對來說比較簡單,只是設置他們之間的間距
.media,.media-body { overflow: hidden; zoom: 1;}.media,.media .media { margin-top: 15px;}.media:first-child { margin-top: 0;}.media-object { display: block;}.media-heading { margin: 0 0 5px;}.media-left { margin-right: 10px;}.media-right { margin-left: 10px;}<div class="media"> <a class="media-left" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" width=100 src="http://files.VeVB.COm/file_images/article/201707/huochai.jpg" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">小火柴的藍色理想</h4> <div>好的代碼像粥一樣,都是用時間熬出來的</div> </div></div>

嵌套
在評論系統中,經常會有媒體對象嵌套的需求。在Bootstrap框架中的媒體對象也具備這樣的功能,只需要將另一個媒體對象結構放置在媒體對象的主體內“media-body”
<div class="media"> <a class="media-left" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" width=100 src="http://files.VeVB.COm/file_images/article/201707/huochai.jpg" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">小火柴的藍色理想</h4> <div>好的代碼像粥一樣,都是用時間熬出來的</div> <div class="media"> <a class="pull-left" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" src="http://via.placeholder.com/100x100" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">我是小火柴</h4> <div>好巧啊,我也叫小火柴</div> <div class="media"> <a class="pull-left" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" width=100 src="http://files.VeVB.COm/file_images/article/201707/huochai.jpg" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">小火柴的藍色理想</h4> <div>是的</div> </div> </div> </div> </div> </div></div><div class="media"> <a class="media-left" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" width=100 src="http://files.VeVB.COm/file_images/article/201707/huochai.jpg" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">小火柴的藍色理想</h4> <div>藍色理想衰落了,前端卻欣欣向榮起來</div> </div></div>

對齊方式
圖片或其他媒體類型可以頂部、中部或底部對齊。默認頂部對齊。通過.media-middle或.media-bottom來設置
.media-middle { vertical-align: middle;}.media-bottom { vertical-align: bottom;}<div class="media"> <a class="media-left" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" width=30 src="http://files.VeVB.COm/file_images/article/201707/huochai.jpg" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">小火柴的藍色理想</h4> <div>藍色理想衰落了,前端卻欣欣向榮起來</div> </div></div><div class="media"> <a class="media-left media-middle" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" width=30 src="http://files.VeVB.COm/file_images/article/201707/huochai.jpg" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">小火柴的藍色理想</h4> <div>藍色理想衰落了,前端卻欣欣向榮起來</div> </div></div><div class="media"> <a class="media-left media-bottom" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" width=30 src="http://files.VeVB.COm/file_images/article/201707/huochai.jpg" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">小火柴的藍色理想</h4> <div>藍色理想衰落了,前端卻欣欣向榮起來</div> </div></div>

媒體對象列表
媒體對象的嵌套僅是媒體對象中一個簡單應用效果之一,在很多時候,我們還會碰到一個列表,每個列表項都和媒體對象長得差不多。Bootstrap框架提供了一個列表展示的效果,在寫結構的時候可以使用ul,并且在ul上添加類名“media-list”,而在li上使用“media”
媒體對象列表,在樣式上也并沒有做過多的特殊處理,只是把列表的左間距置0以及去掉了項目列表符號
.media-list { padding-left: 0; list-style: none;}<ul class="media-list"> <li class="media"> <a class="media-left" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" width=30 src="http://files.VeVB.COm/file_images/article/201707/huochai.jpg" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">小火柴的藍色理想</h4> <div>藍色理想衰落了,前端卻欣欣向榮起來</div> </div> </li> <li class="media"> <a class="media-left" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <img class="media-object" width=30 src="http://files.VeVB.COm/file_images/article/201707/huochai.jpg" alt="..."> </a> <div class="media-body"> <h4 class="media-heading">小火柴的藍色理想</h4> <div>好的代碼像粥一樣,都是用時間熬出來的</div> </div> </li> </ul>

以上這篇老生常談Bootstrap媒體對象就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答