Performance: Why complex layouts need patterns
This affects how fast the page loads and how smoothly the layout appears when resizing or interacting.
<div class="grid grid-rows-layout grid-cols-layout gap-4"> <header class="row-span-1 col-span-full">Header</header> <nav class="row-span-1 col-span-1">Sidebar</nav> <main class="row-span-2 col-span-3">Main content</main> <footer class="row-span-1 col-span-full">Footer</footer> </div> <style> @layer utilities { .grid-rows-layout { grid-template-rows: auto 1fr auto; } .grid-cols-layout { grid-template-columns: 200px 1fr 1fr; } } </style>
<div class="flex flex-col"> <div class="flex flex-row"> <div class="w-1/3">Item 1</div> <div class="w-1/3">Item 2</div> <div class="w-1/3">Item 3</div> </div> <div class="grid grid-cols-4 gap-4"> <div>Grid Item 1</div> <div>Grid Item 2</div> <div>Grid Item 3</div> <div>Grid Item 4</div> </div> <div class="absolute top-0 left-0">Overlay</div> </div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Mixed flex, grid, absolute | High (many nested nodes) | Multiple reflows on resize | High paint cost due to layout thrashing | [X] Bad |
| Single CSS Grid pattern | Moderate (flat structure) | Single reflow on resize | Lower paint cost, smoother rendering | [OK] Good |
grid grid-cols-1 md:grid-cols-2 sets one column on small screens and two on medium+ screens.gap-4 adds consistent spacing between columns.<div class='flex flex-wrap gap-2'> <div class='w-1/3 bg-blue-300 p-4'>Box 1</div> <div class='w-1/3 bg-green-300 p-4'>Box 2</div> <div class='w-1/3 bg-red-300 p-4'>Box 3</div> <div class='w-1/3 bg-yellow-300 p-4'>Box 4</div> </div>
flex-wrap allowing items to wrap. Each box is one-third width.<div class='grid grid-cols-3 gap-4'> <div class='col-span-4 bg-gray-200 p-2'>Item</div> </div>
grid-cols-3.col-span-4 tries to span 4 columns, which is more than 3 available.grid with grid-cols-1 md:grid-cols-2 lg:grid-cols-4 sets columns per screen size.gap-6 adds space between cards, p-4 adds padding inside container.Pass the test and this pattern moves unknown