Yesterday, we learned the basics of CSS Grid. Today, we're going to learn how to create a reusable grid system that makes building a variety of website layouts faster and easier. Let's dig in! The single row, 12-column gridTo support a variety of layouts, we're going to build a CSS Grid layout that's just a single row with 12 columns in it. Then, we'll create classes that let us specify how many columns wide any item in our layout should be, and which column it should start at. Why 12 columns? Because it divides easily into a variety of patterns. You can split it in half for two even columns, or into thirds, or fourths. It also supports some irregular or asymmetrical layouts. In short, it's really flexible. Why only one row? It makes coding easier and more reusable. Each row in your HTML will have its own parent element. We can stack as many of them as we need to create unique layouts. Let's get started! Defining a rowFor this system, we'll use the very uncreative class name We'll set We'll also add a
Defining columns: the simple wayOn my personal projects, I like to use fraction-based names for my columns. Things like For each This stacks the columns in order, but tells our CSS how wide to make each item.
Now, we can do stuff like this…
Defining columns: the more complex wayFor more robust systems, you might instead define your column classes by the number of classes they span. For example, This gives you a lot more control, but requires you to think a bit more when building your layouts.
Controlling where items startBeing able to define where in a layout an item starts lets you do more interesting things with your layout. For this, we'll create a handful of We also need to define a Here, I'm again using simple fraction-based name for ease.
Now, you can do stuff like this…
Single-column on small viewportsI typically wrap my grid system in a media query…
On smaller viewports, users get a single-column layout. Once they hit a large-enough viewport, the grid kicks in and shifts to a multi-column layout. Modifiers and utility classesI like to use utility classes to control spacing between rows.
I also will often include modifiers for my
Feel free to add additional classes to nudge-and-tweak things as needed. Cheers, Want to share this with others or read it later? View it in a browser. |
Related Post:
- The Contest-Winning Recipe Is...
- May updates: How to strengthen your email security and prevent cyber attacks
- [25+] Architecture De Jardin - Épinglé sur Potagers bois..
- 🔥 You're Invited: SEOSLY Pro Beta Testing Now Open - Limited Time!
- We launched over 75 UI components in E-commerce and more to follow! 🛒
0 Komentar untuk "[Go Make Things] How to build a reusable grid system with CSS grid"