Set an Image Beside Text

The events page now has a spread header and a sidebar beside Coming Up. It still lacks a photo beside a block of text, a pattern you’ve seen on almost every site. The volunteers page in the project has six.

Rosa wants a photo of the swap table on the seed swap card, beside its heading and date. In this lesson, you’ll build that pattern with flexbox, then choose a tool for every layout in this section. Start with the markup, because a flex container arranges only its own children.

Put the Photo Beside the Text

An image that keeps its own size, with text filling the rest of the row, is called a media object. The card will be the flex container, so the text has to be a single child of it.

In events/upcoming.html, replace the Spring Seed Swap card with this version:

Language: HTML
<article class="event-card featured media">
  <img src="../images/seed-swap.jpg"
    alt="Seed packets in wooden trays and jars of saved seeds on a table by the garden gate"
    width="1200" height="896">
  <div class="media-text">
    <h3>Spring Seed Swap</h3>
    <p>
      <time datetime="2027-03-21T14:00">Sunday,
        March 21, from 2 p.m.</time> to 4 p.m.
      <a href="seed-swap.html">How the swap works</a>
    </p>
  </div>
</article>

The class media names the pattern, so you can reuse it. The <div class="media-text"> makes the heading and the paragraph one item, instead of two. The width and height attributes give the file’s real size.

Save, and the photo sits above the text, as wide as the card’s content:

The seed swap card with a wide photo of a seed table filling the card's width, and the card's heading and date below it.
The Photo Above the Text in Normal Flow

The reset in style.css gives every <img> max-width: 100%, so the photo is 426 pixels wide. The card has grown to 508 pixels tall. To put the photo beside the text, add these two rules at the end of events/layout.css:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.