Media components help show pictures, videos, or sounds alongside text. This makes content easier to understand and more interesting.
Why media components enhance content in Bootsrap
<div class="media"> <img src="image.jpg" class="align-self-start mr-3" alt="..."> <div class="media-body"> <h5 class="mt-0">Media heading</h5> Content text goes here. </div> </div>
The media class creates a container for media and text.
Use media-body for the text part next to the media.
<div class="media"> <img src="avatar.png" class="align-self-center mr-3" alt="User avatar"> <div class="media-body"> <h5>User Name</h5> <p>This is a short user bio.</p> </div> </div>
<div class="media"> <video class="mr-3" controls width="150"> <source src="video.mp4" type="video/mp4"> Your browser does not support the video tag. </video> <div class="media-body"> <h5>Video Title</h5> <p>Description of the video content.</p> </div> </div>
This complete example uses Bootstrap's media component to show an image next to text inside a bordered box. It is responsive and looks neat on all screen sizes.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap Media Component Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <main class="container mt-4"> <h2>Media Component Example</h2> <div class="media border p-3"> <img src="https://via.placeholder.com/64" class="align-self-start mr-3" alt="Placeholder image"> <div class="media-body"> <h5 class="mt-0">Sample Media Heading</h5> <p>This is some example text next to the image. It shows how media components keep content organized and visually appealing.</p> </div> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Bootstrap 5 replaced the .media component with utilities like Flexbox, but you can still create similar layouts using d-flex and spacing classes.
Always add alt text to images for accessibility.
Use margin classes like mr-3 (margin-right) to add space between media and text.
Media components combine images, videos, or sounds with text to make content clearer and more engaging.
They help organize content so it looks neat and works well on different devices.
Bootstrap provides easy classes to create media layouts quickly and accessibly.
Practice
Solution
Step 1: Understand the purpose of media components
Media components are designed to combine images, videos, or sounds with text to improve clarity and engagement.Step 2: Compare options with this purpose
Only They combine images or videos with text to make content clearer and more engaging. correctly describes this purpose; others are incorrect or misleading.Final Answer:
They combine images or videos with text to make content clearer and more engaging. -> Option AQuick Check:
Media components enhance content = They combine images or videos with text to make content clearer and more engaging. [OK]
- Thinking media components only add decoration
- Believing media components remove text
- Assuming media components slow down pages
Solution
Step 1: Recall Bootstrap media component classes
Bootstrap uses the classmediato create a media object container.Step 2: Verify other options
Classes likemedia-container,media-object, andmedia-boxare not standard Bootstrap classes.Final Answer:
media -> Option AQuick Check:
Bootstrap media container class = media [OK]
- Using non-existent classes like media-object
- Confusing media with media-box
- Adding extra words to class names
<div class="media">
<img src="img.jpg" class="mr-3" alt="Image">
<div class="media-body">
<h5>Title</h5>
<p>Description text here.</p>
</div>
</div>Solution
Step 1: Understand Bootstrap media layout
Themediaclass arranges the image and text side by side, with the image on the left by default.Step 2: Analyze the code structure
Theimgwith classmr-3adds margin to the right, spacing it from the text inmedia-bodywhich contains the title and description.Final Answer:
An image on the left with a title and description text to the right. -> Option CQuick Check:
Bootstrap media layout = image left, text right [OK]
- Assuming text appears above or below image
- Ignoring margin class effects
- Thinking image hides text
<div class="media">
<img src="photo.png" alt="Photo">
<div class="media-body">
<h4>Heading</h4>
<p>Some text here.</p>
</div>
</div>Solution
Step 1: Check Bootstrap media spacing rules
Bootstrap recommends adding margin classes likemr-3to the image for spacing between image and text.Step 2: Verify other parts of the code
Themedia-bodyis correctly insidemedia, the image tag is valid without self-closing slash in HTML5, andaltshould describe the image, not be empty.Final Answer:
Missing classmr-3on the image for spacing. -> Option DQuick Check:
Image spacing class needed = Missing classmr-3on the image for spacing. [OK]
- Thinking image tag must self-close
- Placing media-body outside media
- Leaving alt attribute empty
Solution
Step 1: Understand default media layout
By default, Bootstrap media components place the image on the left and text on the right.Step 2: Reverse order with flex utilities
Adding the classflex-row-reverseto the media container reverses the order, placing the image on the right and text on the left.Step 3: Check other options
Simply placing image first won't reverse order;media-bodyis for text container, andcontainerandroware unrelated to media component layout.Final Answer:
Usemediaclass and addflex-row-reverseto reverse the order. -> Option BQuick Check:
Reverse media order with flex-row-reverse [OK]
- Ignoring flex utilities for order change
- Misusing media-body on images
- Confusing container and row with media layout
Ready to prove it?
Pass the test and this pattern moves unknown
