Post Card

@use 'sprucecss/scss/spruce' as *;

.post-card {
  $this: &;

  @include a11y-card-link('.post-card__link', true);
  display: flex;
  flex-direction: column;
  gap: spacer('m');

  &--horizontal {
    align-items: center;

    @include breakpoint('xs') {
      flex-direction: row;
    }

    #{$this}__thumbnail {
      @include breakpoint('xs') {
        aspect-ratio: 1;
        inline-size: 10rem;
      }
    }
  }

  &__thumbnail {
    aspect-ratio: 16 / 10;
    border-radius: radius('md');
    inline-size: 100%;
    object-fit: cover;
  }

  &__body {
    @include layout-stack('s');
  }

  // The tags and the author link sit above the stretched title link, so they stay clickable.
  &__tags a,
  .post-author__name {
    position: relative;
    z-index: 1;
  }

  &:focus-within #{$this}__title a,
  &:hover #{$this}__title a {
    color: color('primary');
  }

  &__meta {
    color: color('muted');
    display: flex;
    flex-wrap: wrap;
    font-size: config('font-size-sm', $typography);
    gap: spacer('xs') spacer('m');
    line-height: 1;
  }

  &__title {
    font-size: font-size('h3');

    a {
      color: color('heading');
      text-decoration: none;
    }
  }
}
<div class="post-card">
    <img class="post-card__thumbnail" src="/img/blog/about-modern-sass.png" alt="" loading="lazy" width="640" height="360">
    <div class="post-card__body">
        <div class="post-card__meta">
            <div class="post-card__tags">
                <a href="#">#sass</a>
            </div>
            <time datetime="2022-12-03T00:00:00.000Z">December 3, 2022</time>
        </div>
        <h2 class="post-card__title">
            <a class="post-card__link" href="#">About Modern Sass</a>
        </h2>
        <div class="post-author post-author--sm">
            <img class="post-author__avatar" src="https://secure.gravatar.com/avatar/fd9479a898c593601efd2fe758a86dba?s=96&amp;d=mm&amp;r=g" alt="" width="32" height="32" loading="lazy">
            <a class="post-author__name" href="https://adamlaki.com" rel="author">Adam Laki</a>
        </div>
    </div>
</div>
<div class="post-card post-card--horizontal">
    <img class="post-card__thumbnail" src="/img/blog/cracking-the-theme-switcher-and-dark-mode.png" alt="" loading="lazy" width="640" height="360">
    <div class="post-card__body">
        <div class="post-card__meta">
            <div class="post-card__tags">
                <a href="#">#css</a>
                <a href="#">#ui</a>
            </div>
            <time datetime="2023-05-30T00:00:00.000Z">May 30, 2023</time>
        </div>
        <h2 class="post-card__title">
            <a class="post-card__link" href="#">Cracking the Theme Switcher and Dark Mode</a>
        </h2>
        <div class="post-author post-author--sm">
            <img class="post-author__avatar" src="https://secure.gravatar.com/avatar/fd9479a898c593601efd2fe758a86dba?s=96&amp;d=mm&amp;r=g" alt="" width="32" height="32" loading="lazy">
            <a class="post-author__name" href="#" rel="author">Adam Laki</a>
        </div>
    </div>
</div>

Post Card

The post card component is a generic UI element we can use in many cases, but it is the most common in post (blog, news) listings.

There isn’t much work with the card component because the requirements mainly depend on the context. Nonetheless, it would be best if you watched for the following:

  • If the image only illustrates the title, leave its alt empty: the title right next to it already names the post. Write an alt text when the image carries information of its own.
  • If you list them, use a ul with li items (like the Post List), so the number of posts is announced.
  • If you have a publication date, use the time element.

Variations

  • default: the default view is a vertically stacked layout.
  • --horizontal: You can make the card horizontally stacked using the .post-card--horizontal modifier.

Technical Details

  • It uses the a11y-card-link mixin to stretch the title link over the whole card. The tag and author links get position: relative and a z-index, so they stay clickable above it.
  • The title turns to the primary color on hover and when the link has focus.
  • There is an aspect-ratio for the thumbnail to control the dimensions better.

Dependencies

Resources

Documentation

Learn about Spruce CSS through our extensive documentation.

Components

Explore our UI library of accessible components built with Spruce CSS.

Blocks and Pages

Full-width page blocks and complete page templates, ready to copy.

Find us on GitHub

Did you find a bug? Have an idea or a question? Please open an issue to help us develop the project.