Performance: Card component patterns
This affects page load speed and rendering performance by how card components are structured and styled.
<article class="p-6 bg-white rounded-lg shadow-lg"> <img src="image.jpg" alt="Card image" class="w-full h-auto rounded-t-lg mb-4"> <h2 class="text-xl font-bold mb-2">Card Title</h2> <p class="text-gray-700">This is a description of the card content.</p> </article>
<div class="p-6 bg-white rounded-lg shadow-lg"> <div class="flex flex-col"> <div class="mb-4"> <img src="image.jpg" alt="Card image" class="w-full h-auto rounded-t-lg"> </div> <div class="px-4"> <h2 class="text-xl font-bold mb-2">Card Title</h2> <p class="text-gray-700">This is a description of the card content.</p> </div> </div> </div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Nested divs with flex inside card | High (7+ nodes) | 3 reflows | Medium | [X] Bad |
| Flat structure with semantic <article> | Low (4 nodes) | 1 reflow | Low | [OK] Good |
p-4 adds padding inside an element, creating space between content and edges.m-4 adds margin outside the element, text-center centers text, and bg-gray-200 sets background color.<div class="max-w-sm rounded overflow-hidden shadow-lg p-6"> <img class="w-full" src="image.jpg" alt="Sample" /> <div class="pt-4">Card content here</div> </div>
max-w-sm limits width to small size, rounded adds rounded corners, shadow-lg adds a large shadow, and p-6 adds padding inside the card.w-full on the image makes it full width inside the card, and pt-4 adds top padding to the content below the image.<div class="rounded shadow p-4"> <img src="photo.jpg" /> <button class="bg-blue-500 text-white p-2">Click</button> </div>What is missing for accessibility?
img tag lacks an alt attribute, which is important for screen readers to describe the image.img tag is missing an alt attribute -> Option Bgrid-cols-1 sets 1 column by default (small screens), and lg:grid-cols-3 changes to 3 columns on large screens. gap-6 adds spacing between grid items.block which disables grid. grid-cols-3 grid gap-6 lg:grid-cols-1 reverses column counts incorrectly.Pass the test and this pattern moves unknown