Overview
The Card component provides a structured way to display content in a self-contained container. It’s built with composition in mind, allowing you to combine different parts like headers, titles, descriptions, content areas, and footers to create rich card layouts.Use Cases
- Product listings and feature cards
- User profile cards and dashboard widgets
- Article previews and blog post summaries
- Settings panels and configuration sections
- Call-to-action containers
Installation
The Card component is included with the UI package. Make sure you have@zayne-labs/ui-react installed:
Basic Usage
Component Parts
The Card component is composed of several parts that can be used together:Card.Root
The root container for the card. Renders as anarticle element by default.
as- Change the rendered element (default:"article")asChild- Merge props with child element using Slot patternclassName- Custom CSS classes- All standard HTML attributes for the element type
Card.Header
Container for the card’s header section. Renders as aheader element by default.
Card.Title
The main heading for the card. Renders as anh3 element by default with semibold styling.
Card.Description
A muted text description. Renders as ap element with muted foreground color.
Card.Content
The main content area of the card. Renders as adiv by default.
Card.Action
A button element for card actions. Renders as abutton by default.
Card.Footer
Container for the card’s footer section. Renders as afooter element by default.
Examples
Product Card
User Profile Card
Minimal Card
Using asChild with Custom Elements
Styling
All Card parts acceptclassName props and include data attributes for styling:
Data Attributes
Each part includes these data attributes:data-scope="card"- Identifies the component scopedata-part- Identifies the specific part (root, header, title, etc.)data-slot- Identifies the slot name (card-root, card-header, etc.)
CSS Example
Polymorphic Components
Most Card parts support theas prop to change the rendered element:
Accessibility
- The Card.Root renders as a semantic
articleelement by default - Card.Header and Card.Footer use semantic
headerandfooterelements - Card.Title uses heading elements (h3 by default) for proper document outline
- Card.Action includes
type="button"by default