The Img component provides a styled image element with consistent styling and accessibility features. It's designed to be responsive and accessible by default.
import { Img } from '@dashflowx/core'export default function ImgExample() {return (<Imgsrc="https://example.com/image.jpg"alt="Description of the image"/>)}
Preview:
| Prop | Type | Default | Description |
|---|---|---|---|
src | string | - | The image source URL |
alt | string | - | Alternative text for accessibility |
className | string | '' | Additional CSS classes for the image |
import { Img } from '@dashflowx/core'export default function BasicImg() {return (<Imgsrc="https://picsum.photos/400/300"alt="Random placeholder image"/>)}
Preview:
import { Img } from '@dashflowx/core'export default function StyledImg() {return (<Imgsrc="https://picsum.photos/400/300"alt="Styled image"className="border-4 border-blue-500 shadow-lg"/>)}
Preview:
import { Img } from '@dashflowx/core'export default function ResponsiveImg() {return (<Imgsrc="https://picsum.photos/800/600"alt="Responsive image"className="w-full h-auto max-w-md mx-auto"/>)}
Preview:
import { Img } from '@dashflowx/core'export default function CircularImg() {return (<Imgsrc="https://picsum.photos/200/200"alt="Circular image"className="rounded-full w-32 h-32 object-cover"/>)}
Preview:
import { Img } from '@dashflowx/core'export default function CustomStyledImg() {return (<Imgsrc="https://picsum.photos/500/400"alt="Custom styled image"className="rounded-xl border-2 border-purple-500 shadow-2xl transform hover:scale-105 transition-transform"/>)}
Preview:
const profilePicture = (<Imgsrc="/profile.jpg"alt="User profile picture"className="rounded-full w-24 h-24 object-cover"/>);
const productImage = (<Imgsrc="/product.jpg"alt="Product image"className="w-full h-64 object-cover rounded-lg"/>);
const heroImage = (<Imgsrc="/hero.jpg"alt="Hero image"className="w-full h-96 object-cover"/>);
const thumbnailImage = (<Imgsrc="/thumbnail.jpg"alt="Thumbnail"className="w-20 h-20 object-cover rounded"/>);
const galleryImage = (<Imgsrc="/gallery.jpg"alt="Gallery image"className="w-full h-48 object-cover rounded-lg hover:opacity-80 transition-opacity"/>);
Include descriptive alt text for accessibility.
// ✅ Good - Descriptive alt text<Img src="/product.jpg" alt="Red Nike Air Max running shoes" />// ❌ Avoid - Missing or generic alt text<Img src="/product.jpg" alt="Image" /><Img src="/product.jpg" />
Optimize images for different screen sizes.
// ✅ Good - Responsive sizing<Imgsrc="/image.jpg"alt="Description"className="w-full h-auto max-w-md"/>// ❌ Avoid - Fixed sizes<Imgsrc="/image.jpg"alt="Description"className="w-500 h-300"/>
Use appropriate loading strategies.
// ✅ Good - Lazy loading for below-fold images<Imgsrc="/image.jpg"alt="Description"loading="lazy"/>// ✅ Good - Eager loading for above-fold images<Imgsrc="/hero.jpg"alt="Description"loading="eager"/>
Use semantic sizing classes for consistency.
// ✅ Good - Semantic sizing<Img className="w-full h-64 object-cover" />// ❌ Avoid - Arbitrary sizing<Img className="w-347 h-198" />
Provide fallbacks for loading states.
// ✅ Good - Loading stateconst [isLoading, setIsLoading] = useState(true);<Imgsrc="/image.jpg"alt="Description"onLoad={() => setIsLoading(false)}className={isLoading ? 'opacity-0' : 'opacity-100'}/>
You can customize the image appearance using CSS classes:
<Imgsrc="/image.jpg"alt="Description"className="rounded-lg border-2 border-gray-300 shadow-md"/>
Use different themes for different contexts:
// Light theme<Img className="border border-gray-200" />// Dark theme<Img className="border border-gray-700" />// Brand theme<Img className="border-2 border-blue-500" />
Use different sizes for different purposes:
// Small image<Img className="w-16 h-16" />// Medium image<Img className="w-32 h-32" />// Large image<Img className="w-64 h-64" />
Handle image loading errors gracefully:
const [imageError, setImageError] = useState(false);<Imgsrc={imageError ? '/fallback.jpg' : '/image.jpg'}alt="Description"onError={() => setImageError(true)}/>
Implement lazy loading for performance:
<Imgsrc="/image.jpg"alt="Description"loading="lazy"className="w-full h-auto"/>
Use responsive images for different screen sizes:
<Imgsrc="/image-small.jpg"alt="Description"className="w-full h-auto sm:hidden"/><Imgsrc="/image-large.jpg"alt="Description"className="w-full h-auto hidden sm:block"/>
Add overlay effects to images:
<div className="relative"><Imgsrc="/image.jpg"alt="Description"className="w-full h-64 object-cover"/><div className="absolute inset-0 bg-black bg-opacity-50 flex items-center justify-center"><span className="text-white text-xl font-bold">Overlay Text</span></div></div>
- Avatar: For user profile images
- Card: For image containers
- Gallery: For image collections
- Modal: For image lightboxes
- Carousel: For image sliders
For the complete API reference and advanced usage patterns, see the Img API documentation.
