Hey there! ? After spending countless hours helping developers debug their CSS layouts, I've noticed we're all fighting the same battles. Let's fix that today with some battle-tested CSS solutions that actually work in production.
We've all been there. You find a CSS tutorial, copy the code, and suddenly:
Sound familiar? Let's fix these issues once and for all.
First, let's tackle the most common layout: header, scrollable content, and footer. Here's what we want:
Here's the solution:
.app { display: grid; grid-template-rows: auto 1fr auto; min-height: 100vh; gap: 1rem; } .header { position: sticky; top: 0; background: white; z-index: 10; } .footer { background: #f5f5f5; } .content { /* Prevent content from getting stuck under header */ padding-top: var(--safe-padding, 1rem); overflow-y: auto; }
<div> <p>Why this works:</p> <ul> <li>Grid's 1fr handles the space distribution automatically</li> <li>Sticky header stays visible while scrolling</li> <li>Content scrolls independently</li> <li>Footer always stays at the bottom</li> </ul> <h2> 2. The "Works Everywhere" Card Grid </h2> <p>Need cards that look good no matter how many you have? This solution handles everything from 1 to 100 cards:<br> </p> <pre class="brush:php;toolbar:false">.card-container { --min-card-width: 300px; display: grid; grid-template-columns: repeat( auto-fit, minmax(min(var(--min-card-width), 100%), 1fr) ); gap: clamp(1rem, 2vw, 2rem); padding: clamp(1rem, 2vw, 2rem); } .card { display: flex; flex-direction: column; height: 100%; padding: 1rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .card-content { flex: 1; /* Takes up remaining space */ } .card-footer { margin-top: auto; /* Pushes footer to bottom */ }
What makes this special:
Ever noticed how hard it is to read text that spans the full width of a wide screen? Here's how to fix that:
.content-wrapper { --content-width: 65ch; --padding: clamp(1rem, 5vw, 3rem); width: min(var(--content-width), 100%); margin-inline: auto; padding-inline: var(--padding); } .text-content { font-size: clamp(1rem, 1rem + 0.5vw, 1.25rem); line-height: 1.6; } /* For full-width backgrounds with contained content */ .full-width { width: 100%; padding-inline: var(--padding); } .full-width > * { width: min(var(--content-width), 100%); margin-inline: auto; }
Why developers love this:
/* Add this to your dev environment */ * { outline: 1px solid rgba(255,0,0,0.1); }
This helps spot layout issues early.
/* Start here */ .element { flex-direction: column; gap: 1rem; } /* Then enhance */ @media (min-width: 768px) { .element { flex-direction: row; } }
:root { --spacing-unit: 0.5rem; --padding-sm: calc(var(--spacing-unit) * 2); --padding-md: calc(var(--spacing-unit) * 4); --padding-lg: calc(var(--spacing-unit) * 8); }
The best way to learn is by doing. Take these snippets and try them in your project. Start with the app layout – it's the foundation everything else builds on.
Got questions? Found a way to improve these patterns? Drop a comment below! I usually respond within 24 hours.
If this helped you, consider:
The above is the detailed content of Modern CSS Layouts That Actually Work: A Developers Guide. For more information, please follow other related articles on the PHP Chinese website!