LWC Card with Image
Here is the another post on building re-usable lighting web components. We will be building slds and lighting card with image. In the demo first card is created using slds-card and second is using standard Lighting Card. These cards are fully responsive. Using slds-grid system we can build multi col layout in salesforce lwc.
Code Files
<div class="slds-card">
<div class="slds-grid slds-wrap">
<!-- Image Section Start Here-->
<div class="slds-col slds-size_1-of-4 ">
<div class="slds-m-around_xx-small slds-m-top_x-small slds-align_absolute-center">
<img src="https://cdn.pixabay.com/photo/2024/10/27/07/12/women-9152739_1280.jpg" alt="Source pixabay">
</div>
</div>
<!-- Content Section Start Here -->
<div class="slds-col slds-size_3-of-4">
<div class="slds-m-around_xx-small">
<h2 class="slds-text-heading_medium"><a href="#">Asam Treding Company</a></h2>
<p>Assam is a state in northeastern India known for its wildlife, archeological sites and tea plantations. In the west, Guwahati. </p>
</div>
</div>
</div>
</div> <lightning-card >
<div class="slds-grid slds-wrap">
<!-- Image Section Start Here-->
<div class="slds-col slds-size_1-of-4 ">
<div class="slds-m-around_xx-small slds-m-top_x-small slds-align_absolute-center">
<img src="https://cdn.pixabay.com/photo/2021/12/13/19/10/nature-6868923_1280.jpg" alt="Source pixabay">
</div>
</div>
<!-- Content Section Start Here -->
<div class="slds-col slds-size_3-of-4">
<div class="slds-m-around_xx-small">
<h2 class="slds-text-heading_medium"><a href="#">Puducherry Tea Company</a></h2>
<p>Pondicherry Airport is a domestic airport serving the union territory of Puducherry,
India. It is located at Lawspet, situated 8.1 km from the international community of Auroville </p>
</div>
</div>
</div>
</lightning-card> Building something similar?
Explore more reusable LWC patterns or Apex fundamentals to go with it.
How this card works
The first card uses the plain slds-card class, and the second uses the standard lightning-card base component. Inside each card, a slds-grid slds-wrap container splits the space into two columns: the image takes one quarter of the width (slds-size_1-of-4) and the text takes three quarters (slds-size_3-of-4).
Differences between the two cards
- slds-card: only CSS classes. You write all the markup, so you have full control, but you have to add the header and footer yourself.
- lightning-card: a base component that supports a
title, anicon-name, an actions slot and a footer slot, and matches the Lightning Experience styling automatically.
Make it responsive on mobile
On a small screen, a quarter-width image is too small. Use full width on mobile and the split layout on larger screens:
<div class='slds-col slds-size_1-of-1 slds-medium-size_1-of-4'>...image...</div>
<div class='slds-col slds-size_1-of-1 slds-medium-size_3-of-4'>...text...</div>Make it reusable
To use the card many times, put the data in public properties and pass them from a parent component:
// imageCard.js
import { LightningElement, api } from 'lwc';
export default class ImageCard extends LightningElement {
@api title;
@api description;
@api imageUrl;
@api imageAlt = 'Card image';
}
<!-- imageCard.html -->
<template>
<lightning-card>
<div class='slds-grid slds-wrap'>
<div class='slds-col slds-size_1-of-1 slds-medium-size_1-of-4 slds-align_absolute-center'>
<img src={imageUrl} alt={imageAlt} />
</div>
<div class='slds-col slds-size_1-of-1 slds-medium-size_3-of-4 slds-p-horizontal_small'>
<h2 class='slds-text-heading_medium'>{title}</h2>
<p>{description}</p>
</div>
</div>
</lightning-card>
</template>Tips
- Always add a meaningful
alttext to images. - For a real project, store images in a Static Resource or in Salesforce Files instead of linking to another site. If an external image is blocked, add its domain to the Trusted URLs in Setup.
- Use
slds-m-around_*andslds-p-around_*classes for spacing instead of custom CSS, so the card follows the Salesforce design system.