The Card component displays content in a contained, titled block. When an href
is provided the entire card renders as an <a> element, making the whole
surface clickable. Without href, it renders as a <div>.
---
import Card from "astro-theme-university/components/Card.astro";
---
Props#
| Prop | Type | Default | Description |
|---|---|---|---|
title * | string | --- | The card heading text. |
href | string | --- | URL to link to. When provided, the card renders as an anchor element. Internal absolute paths (starting with `/`) are automatically prefixed with `import.meta.env.BASE_URL`. |
image | ImageMetadata | RemoteImage | --- | Optional card image displayed above the body. Pass an imported image for pipeline optimisation, or a `RemoteImage` for a pre-encoded external source rendered as a plain `<img>`. |
imageAlt | string | --- | Alt text for the card image. Set to a meaningful description if the image conveys information. |
imageWidths | number[] | [320, 640, 960] | Srcset widths (px) for the card image. The default covers the card grids up to 2x DPR. |
imageSizes | string | "(min-width: 48rem) 24rem, 100vw" | The `sizes` attribute paired with `imageWidths`. The default matches the card-grid layout (full content column below the content breakpoint, ~24rem columns above it). |
imageFormat | ImageOutputFormat | themeImageFormat | Output format for the card image. Defaults to the `imageFormat` integration option, itself `"webp"` unless the site opts into something else. Ignored for an SVG source, which is always passed through unrasterised. |
headingLevel | HeadingLevel | "h3" | HTML heading level for the card title. |
The component also accepts a default slot for body content below the title, and
a named actions slot for a control that does something other than follow the
card’s link.
Examples#
Basic card
Getting started
Learn how to install and configure the theme for your project.
Linked card
Card with description
Accessibility
All components are built with semantic HTML and tested against WCAG guidelines. Colour contrast ratios meet AA standards in both light and dark modes.
Card with an action
Deployed prototype
The action sits in the top corner, above the card’s link.
Actions#
A card can carry a control that isn’t the card’s link — a copy button, a QR
toggle, a dismiss. Put it in the actions slot and it renders as a badge in the
card’s top corner.
<Card title="Deployed prototype" href={url}>
<button slot="actions" type="button" aria-label="Show QR code">…</button>
<p>Card body.</p>
</Card>
Filling the slot changes how a linked card is marked up. Normally the whole card
is one <a>, which can’t contain a button; with actions, the anchor shrinks to
the title and stretches back over the card through a ::after overlay. The card
stays clickable end to end, the action stays clickable in its corner, and the
link’s accessible name becomes the title alone rather than every word on the
card. A card without an href is unaffected either way.
Style the slot’s contents yourself — the theme positions the badge and stacks it above the overlay but doesn’t dictate how a control inside it looks.
Notes#
When using the image prop, provide meaningful imageAlt text if the image
conveys information. When imageAlt is omitted the card’s title is used as
the alt text, which suits an image that illustrates its subject; pass "" to
mark a purely decorative image.
The image prop also accepts a RemoteImage object
({ src, width?, height?, srcset?, sizes? }) for a pre-encoded external source
— a CDN or object-store URL — rendered as a plain <img> with no build-time
processing. Supply srcset (and per-image sizes if the default doesn’t fit)
for responsive variants you’ve encoded yourself; the card’s CSS aspect ratio
handles layout either way.
Cards inside a CardGrid use CSS subgrid to align their image and body rows across the grid row, keeping cards aligned when titles or body text vary in length.