Bootstrap Media Objects
Bootstrap Media Objects are used to display content where an image or icon is placed beside related text. They are useful for comments, messages, profiles, notifications and other list-based content.
What is a Media Object?
A Media Object is a common web design pattern used to arrange an image, icon or other media beside descriptive content.
The media element is normally placed on one side, while the text content is displayed next to it.
1. Basic Media Object
A basic media object contains a media element and a content section placed beside it.
Example
<div class="d-flex">
<div class="flex-shrink-0">
<img src="/Images/Boostrap/Image4.png"
width="80"
height="80"
class="rounded-circle"
alt="User">
</div>
<div class="flex-grow-1 ms-3">
<h5>
Rahul Sharma
</h5>
<p>
Bootstrap is easy to learn.
</p>
</div>
</div>
Output:
Rahul Sharma
Bootstrap is easy to learn and very useful for responsive web development.
2. Media Object with Heading
A media object can contain a heading, paragraph, date and other related information.
Example
<div class="d-flex">
<img src="/Images/Boostrap/Image4.png"
width="70"
height="70"
class="rounded-circle"
alt="Student">
<div class="ms-3">
<h5>
Student Feedback
</h5>
<p>
The Bootstrap course was very useful.
</p>
<small>
Posted Today
</small>
</div>
</div>
Student Feedback
The Bootstrap course was very useful.
Posted Today3. Media Object Alignment
Bootstrap utility classes can be used to control the alignment of media content.
For example, align-self-center can be
used to vertically center the media element.
Example
<div class="d-flex">
<img src="/Images/Boostrap/Image4.png"
width="80"
height="80"
class="rounded-circle align-self-center"
alt="Student">
<div class="ms-3">
<h5>
Bootstrap Student
</h5>
<p>
This image is vertically centered
beside the content.
</p>
</div>
</div>
Bootstrap Student
This image is vertically centered beside the content.
4. Nested Media Object
A media object can contain another media object inside its content area.
This is useful for comments and replies where one comment can contain another reply.
Example
<div class="d-flex">
<img src="/Images/Boostrap/Image4.png"
width="70"
height="70"
class="rounded-circle"
alt="User">
<div class="ms-3">
<h5>
Rahul
</h5>
<p>
Bootstrap is easy to understand.
</p>
<div class="d-flex">
<img src="/Images/Boostrap/Image4.png"
width="55"
height="55"
class="rounded-circle"
alt="Reply">
<div class="ms-3">
<h6>
Amit
</h6>
<p>
Yes, I agree.
</p>
</div>
</div>
</div>
</div>
Rahul
Bootstrap is easy to understand.
Amit
Yes, I agree.
5. Media Object with Button
A media object can also contain buttons along with the heading and description.
Example
<div class="d-flex">
<img src="/Images/Boostrap/Image4.png"
width="80"
height="80"
class="rounded-circle"
alt="Trainer">
<div class="ms-3">
<h5>
Bootstrap Trainer
</h5>
<p>
Learn Bootstrap with practical examples.
</p>
<button class="btn btn-primary">
View Profile
</button>
</div>
</div>
Bootstrap Trainer
Learn Bootstrap with practical examples.
6. Media Object for Comments
Media Objects are commonly used to create comment sections on websites.
Example
<div class="d-flex">
<img src="/Images/Boostrap/Image4.png"
width="65"
height="65"
class="rounded-circle"
alt="User">
<div class="ms-3">
<h6>
Priya
</h6>
<p>
This Bootstrap topic is very easy to understand.
</p>
<small>
10 minutes ago
</small>
</div>
</div>
Priya
This Bootstrap topic is very easy to understand.
10 minutes ago7. Media Object for Notifications
Media Objects can also be used to create notification layouts.
Example
<div class="d-flex align-items-center">
<img src="/Images/Boostrap/Image4.png"
width="60"
height="60"
class="rounded-circle"
alt="Notification">
<div class="ms-3">
<h6>
New Course Available
</h6>
<p class="mb-0">
Bootstrap Advanced course is now available.
</p>
</div>
</div>
New Course Available
Bootstrap Advanced course is now available.
8. Media Object for Profile
Media Objects are useful for displaying user or trainer profile information.
Example
<div class="d-flex align-items-center">
<img src="/Images/Boostrap/Image4.png"
width="90"
height="90"
class="rounded-circle"
alt="Trainer">
<div class="ms-3">
<h4>
CIIT Trainer
</h4>
<p>
Full Stack Development Trainer
</p>
</div>
</div>
CIIT Trainer
Full Stack Development Trainer
Important Classes Used
| Class | Purpose |
|---|---|
.d-flex |
Creates a flexible horizontal layout. |
.flex-shrink-0 |
Prevents the media element from shrinking. |
.flex-grow-1 |
Allows the content area to use available space. |
.ms-3 |
Adds margin on the start side. |
.align-self-center |
Vertically centers an individual element. |
.align-items-center |
Vertically aligns flex items in the center. |
.rounded-circle |
Makes a suitable image circular. |
.img-fluid |
Makes an image responsive. |
Common Mistakes
-
Forgetting to use a flex container such as
d-flex. - Not adding spacing between the media and content.
- Using very large fixed image dimensions.
-
Forgetting the
altattribute. - Not using responsive classes for mobile layouts.
- Using unnecessary nested structures when a simple flex layout is enough.
Summary
- Media Objects are used to place media beside related content.
-
d-flexcreates the basic flexible layout. -
ms-3provides spacing between the image and content. -
align-items-centeraligns items vertically. - Media Objects are useful for comments, profiles, notifications and messages.
- Media Objects can also be nested to create replies and discussion threads.
- Responsive image and spacing utilities can be combined with Media Object layouts.