Media object

Documentation and examples for Bootstrap’s media object to construct highly repetitive components like blog comments, tweets, and the like.

Example

The media object helps build complex and repetitive components where some media is positioned alongside content that doesn’t wrap around said media. Plus, it does this with only two required classes thanks to flexbox.

Below is an example of a single media object. Only two classes are required—the wrapping .media and the .media-body around your content. Optional padding and margin can be controlled through spacing utilities.

Media object - 图1

  1. <div class="media">
  2. <img src="..." class="mr-3" alt="...">
  3. <div class="media-body">
  4. <h5 class="mt-0">Media heading</h5>
  5. Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
  6. </div>
  7. </div>
Flexbug #12: Inline elements aren’t treated as flex items

Internet Explorer 10-11 do not render inline elements like links or images (or ::before and ::after pseudo-elements) as flex items. The only workaround is to set a non-inline display value (e.g., block, inline-block, or flex). We suggest using .d-flex, one of our display utilities, as an easy fix.

Source:Flexbugs on GitHub

Nesting

Media objects can be infinitely nested, though we suggest you stop at some point. Place nested .media within the .media-body of a parent media object.

Media object - 图2

  1. <div class="media">
  2. <img src="..." class="mr-3" alt="...">
  3. <div class="media-body">
  4. <h5 class="mt-0">Media heading</h5>
  5. Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
  6. <div class="media mt-3">
  7. <a class="mr-3" href="#">
  8. <img src="..." class="mr-3" alt="...">
  9. </a>
  10. <div class="media-body">
  11. <h5 class="mt-0">Media heading</h5>
  12. Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
  13. </div>
  14. </div>
  15. </div>
  16. </div>

Alignment

Media in a media object can be aligned with flexbox utilities to the top (default), middle, or end of your .media-body content.

Media object - 图3

  1. <div class="media">
  2. <img src="..." class="align-self-start mr-3" alt="...">
  3. <div class="media-body">
  4. <h5 class="mt-0">Top-aligned media</h5>
  5. <p>Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.</p>
  6. <p>Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  7. </div>
  8. </div>

Media object - 图4

  1. <div class="media">
  2. <img src="..." class="align-self-center mr-3" alt="...">
  3. <div class="media-body">
  4. <h5 class="mt-0">Center-aligned media</h5>
  5. <p>Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.</p>
  6. <p class="mb-0">Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  7. </div>
  8. </div>

Media object - 图5

  1. <div class="media">
  2. <img src="..." class="align-self-end mr-3" alt="...">
  3. <div class="media-body">
  4. <h5 class="mt-0">Bottom-aligned media</h5>
  5. <p>Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.</p>
  6. <p class="mb-0">Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  7. </div>
  8. </div>

Order

Change the order of content in media objects by modifying the HTML itself, or by adding some custom flexbox CSS to set the order property (to an integer of your choosing).

Media object - 图6

  1. <div class="media">
  2. <div class="media-body">
  3. <h5 class="mt-0 mb-1">Media object</h5>
  4. Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
  5. </div>
  6. <img src="..." class="ml-3" alt="...">
  7. </div>

Media list

Because the media object has so few structural requirements, you can also use these classes on list HTML elements. On your <ul> or <ol>, add the .list-unstyled to remove any browser default list styles, and then apply .media to your <li>s. As always, use spacing utilities wherever needed to fine tune.

Media object - 图7

  1. <ul class="list-unstyled">
  2. <li class="media">
  3. <img src="..." class="mr-3" alt="...">
  4. <div class="media-body">
  5. <h5 class="mt-0 mb-1">List-based media object</h5>
  6. Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
  7. </div>
  8. </li>
  9. <li class="media my-4">
  10. <img src="..." class="mr-3" alt="...">
  11. <div class="media-body">
  12. <h5 class="mt-0 mb-1">List-based media object</h5>
  13. Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
  14. </div>
  15. </li>
  16. <li class="media">
  17. <img src="..." class="mr-3" alt="...">
  18. <div class="media-body">
  19. <h5 class="mt-0 mb-1">List-based media object</h5>
  20. Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
  21. </div>
  22. </li>
  23. </ul>