Media Object Pattern
You'll learn how to build the classic media object—a flexible layout combining a fixed-size image with flowing text content.
The media object is one of the most common design patterns on the web. Think of a social media post: there's a profile picture on the left (fixed size) and the post content on the right (takes up remaining space). Or a product listing with a thumbnail and description. This pattern appears everywhere because it elegantly handles an image beside content that can be any length.
With Flexbox, creating this pattern becomes remarkably simple. You create a flex container with two children: one for the fixed-width media (usually an image) and one for the flexible content area. The content area uses flex-grow: 1 to claim all remaining space, while the image stays at its natural size.
Here's the basic structure:
.media-object {
display: flex;
align-items: flex-start; /* or center, depending on design */
gap: 1rem;
}
.media-object__image {
flex-shrink: 0; /* prevent image from shrinking */
width: 80px; /* or whatever fixed size you need */
}
.media-object__content {
flex-grow: 1; /* take up remaining space */
}
<div class="media-object">
<img src="avatar.jpg" alt="User" class="media-object__image">
<div class="media-object__content">
<h3>Article Title</h3>
<p>This content can be any length and will wrap naturally...</p>
</div>
</div>
The flex-shrink: 0 on the image is crucial—it prevents the image from being squished if content is tight. The gap property provides clean spacing without needing margins. You can easily reverse the layout using flex-direction: row-reverse.
Key Takeaway: The media object pattern uses a flex container with a fixed-size media element and a flexible content area (flex-grow: 1) to create the ubiquitous image-beside-text layout efficiently.