扫码一下
查看教程更方便
如果要创建一个布局,其中包含左对齐或右对齐的媒体对象(如图像或视频)以及文本内容(如博客评论、推文等),我们可以使用新引入的 Bootstrap 媒体组件轻松完成此操作,如下所示:
<div class="media"> <img src="/demo_source/avatar.svg" class="mr-3" alt="Sample Image"> <div class="media-body"> <h5 class="mt-0">Jhon Carter <small><i>Posted on January 10, 2019</i></small></h5> <p>This is really an excellent feature! I love it. One day I'm definitely going to use this Bootstrap media object component into my application.</p> </div> </div>
上面示例如下所示
我们还可以创建媒体对象的其他变体。 将 .rounded
或 .rounded-circle
等图像修饰符类应用于图像来创建圆角或圆形图像。
<div class="media"> <img src="/demo_source/avatar.svg" class="rounded-circle mr-3" alt="Sample Image"> <div class="media-body"> <h5 class="mt-0">Jhon Carter <small><i>Posted on January 10, 2019</i></small></h5> <p>This is really an excellent feature! I love it. One day I'm definitely going to use this Bootstrap media object component into my application.</p> </div> </div>
上面示例如下所示
媒体对象也可以嵌套在其他媒体对象中。 让我们看一个例子:
<div class="media"> <img src="/demo_source/avatar.svg" class="rounded-circle mr-3" alt="Sample Image"> <div class="media-body"> <h5>jiyik <small class="text-muted"><i>Posted on January 10, 2021</i></small></h5> <p>This is really an excellent feature! I love it. One day I'm definitely going to use this Bootstrap media object component into my application.</p> <!-- Nested media object --> <div class="media mt-2"> <img src="/demo_source/avatar.svg" class="rounded-circle mr-3" alt="Sample Image"> <div class="media-body"> <h5>onmpw <small class="text-muted"><i>Posted on January 12, 2021</i></small></h5> <p>Thanks, you found this component useful. Don't forget to check out other Bootstrap components as well. They're also very useful.</p> </div> </div> </div> </div>
上面示例显示的嵌套媒体对象如下所示:
我们还可以通过简单地调整 HTML 代码本身来更改内容以及媒体对象的水平对齐方式,如以下示例所示:
<div class="media"> <div class="media-body"> <h5>Jiyik <small class="text-muted"><i>Posted on January 10, 2021</i></small></h5> <p>Excellent feature! I love it. One day I'm definitely going to put this Bootstrap component into use and I'll let you know once I do.</p> </div> <img src="/demo_source/avatar.svg" class="ml-3" alt="Sample Image"> </div>
上面示例显示的结果如下
除此之外,我们还可以使用 flexbox 实用程序类在内容块的中间或底部对齐图像或其他媒体对象,例如,可以使用 .align-self-center
类进行垂直居中对齐,使用 .align-self-end
类用于底部对齐。
默认情况下,媒体对象内的媒体是顶部对齐的。 看下面的例子:
<!--Top aligned media--> <div class="media"> <img src="images/avatar.svg" class="mr-3" alt="Sample Image"> <div class="media-body"> <h5 class="mt-0">Top aligned media <small><i>This is Default</i></small></h5> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p> </div> </div> <hr> <!--Middle aligned media--> <div class="media"> <img src="images/avatar.svg" class="align-self-center mr-3" alt="Sample Image"> <div class="media-body"> <h5 class="mt-0">Middle Aligned Media</h5> <p>Vestibulum quis quam ut magna consequat faucibus aleo...</p> </div> </div> <hr> <!--Bottom aligned media--> <div class="media"> <img src="images/avatar.svg" class="align-self-end mr-3" alt="Sample Image"> <div class="media-body"> <h5 class="mt-0">Bottom Aligned Media</h5> <p>Amet nibh libero, in gravida nulla. Nulla vel metus...</p> </div> </div>