CSS Grid Practice

12-Column Grid System

The 12-column grid is a classic layout system in web development. Here's how to implement it with CSS Grid:

Example

HTML
<style>
.grid-12 {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 20px;
}
.g-col-3 { grid-column: span 3; }
.g-col-4 { grid-column: span 4; }
.g-col-6 { grid-column: span 6; }
.g-col-8 { grid-column: span 8; }
.g-col-12 { grid-column: span 12; }
.grid-item {
  background: #e8f4f8;
  border: 1px solid #3498db;
  padding: 16px;
  text-align: center;
  border-radius: 4px;
}
</style>
<div class="grid-12">
  <div class="grid-item g-col-3">3 columns</div>
  <div class="grid-item g-col-3">3 columns</div>
  <div class="grid-item g-col-3">3 columns</div>
  <div class="grid-item g-col-3">3 columns</div>
</div>
▶ Try it Yourself

Common Layout Patterns

Holy Grail Layout

The most common page layout pattern:

Example

HTML
<style>
.holy-grail {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main aside"
    "footer footer footer";
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: 60px 1fr 40px;
  min-height: 100vh;
  gap: 10px;
}
.hg-header { grid-area: header; background: #2c3e50; color: white; }
.hg-sidebar { grid-area: sidebar; background: #34495e; color: white; }
.hg-main { grid-area: main; background: #ecf0f1; }
.hg-aside { grid-area: aside; background: #34495e; color: white; }
.hg-footer { grid-area: footer; background: #2c3e50; color: white; }
.holy-grail > * { padding: 20px; display: flex; align-items: center; justify-content: center; }
</style>
<div class="holy-grail">
  <header class="hg-header">Header</header>
  <aside class="hg-sidebar">Sidebar</aside>
  <main class="hg-main">Main Content</main>
  <aside class="hg-aside">Aside</aside>
  <footer class="hg-footer">Footer</footer>
</div>
▶ Try it Yourself

Responsive Card Grid

Example

HTML
<style>
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  padding: 20px;
}
.card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 20px;
}
</style>
<div class="card-grid">
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>
  <div class="card">Card 3</div>
  <div class="card">Card 4</div>
</div>
▶ Try it Yourself

❓ FAQ

Q Why do grid systems use 12 columns?
A 12 is divisible by 2, 3, 4, and 6 — maximum flexibility. You can create 1/2, 1/3, 1/4, 1/6, 2/3, 3/4 proportions. Bootstrap and other frameworks use 12-column grids — it's the industry standard.
Q Hand-written grid vs Bootstrap grid?
A Hand-writing helps you understand the principles; Bootstrap is a packaged implementation. Real projects use Bootstrap for efficiency, but understanding principles helps when frameworks don't meet specific needs.
Q Flex grid vs Grid grid — which is better?
A Each has strengths. Flex suits one-dimensional arrangements but can't align across rows. Grid has stronger two-dimensional capabilities. For simple row distribution, Flex is enough; for grid-line alignment, use Grid.

📖 Summary

📝 Exercises

  1. Implement a complete 12-column grid system, create an HTML page showing different column combinations (3-3-3-3, 4-4-4, 2-8-2, etc.).
  2. Use responsive grid to show single column on mobile, two on tablet, four on desktop.
  3. Use col-offset-3 to center a content block on the page.
  4. Re-implement the 12-column grid using Grid, compare code differences.
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏