Why CSS Grid Layout doesn't replace the framework's grid

栏目: IT技术 · 发布时间: 6年前

内容简介:I've come across many articles and comments stating it's time to replace the framework's grid system with CSS Grid Layout. I can't entirely agree.A framework's gridis a "package" of utility classes that can be used in any project to distribute elements in

I've come across many articles and comments stating it's time to replace the framework's grid system with CSS Grid Layout. I can't entirely agree.

:zap:️ Design 10x faster with our library of 292 components →

Framework Grid vs CSS Grid

A framework's gridis a "package" of utility classes that can be used in any project to distribute elements in (responsive) columns and rows. It could include hundreds of lines of code or just a few rules. It could be that-framework-name 's grid or your custom grid system.

Here's an example fromCodyFrame's grid system:

<ul>
  <li/cdn-cgi/l/email-protection" data-cfemail="7e1d111253483e131a">[email protected]</a>"><!-- ... --></li>
  <li/cdn-cgi/l/email-protection" data-cfemail="284b4744051e68454c">[email protected]</a>"><!-- ... --></li>
  <li><!-- ... --></li>
</ul>

CSS Grid Layoutis a subset of CSS properties used to distribute elements in (responsive) columns and rows.

For example:

.component-name {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
}

Any framework is an abstraction that uses CSS; it doesn't replace it. Consequently, we can't compare a framework's grid system with CSS Grid. The former could use the latter (i.e., a framework's grid system based on CSS Grid). The question "Which one is better?" doesn't make sense in this case .

However, we can discuss whether it makes sense to include a package of utility classes now that we have such powerful CSS layout properties.

Short answer: it may not if you're working on a small project. It does make sense the moment your project grows in complexity.

Enough with the theory! Let me show you an example. :point_down:

Starting a project using only CSS Grid

Let's imagine we're starting a project where, at some point, we need to create a grid.

.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

As we move forward with the project, chances are we need to create more grids:

.gallery, .form-grid, .list {
  display: grid;
}

.gallery {
  grid-template-columns: repeat(2, 1fr);
}

@media (min-width: 48rem) {
  .gallery {
    grid-template-columns: repeat(4, 1fr);
  }
}

.form-grid {
  grid-template-columns: minmax(100px, 250px) 1fr 1fr;
  grid-gap: 20px;
}

.list {
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

So far, so good! If our project was mostly done, why bother even thinking about importing a grid system? CSS Grid easily gets the job done.

The project grows in complexity

Imagine we need to apply the same grid to two components. What are we supposed to do?

Option 1)Use the same class.

<div>
  <!-- ... -->
</div>

<div>
  <!-- ... -->
</div>

<style>
  .form-grid {
    display: grid;
    grid-template-columns: minmax(100px, 250px) 1fr 1fr;
    grid-gap: 20px;
  }

  /* we decide to use the same class :point_up_2: */
  /* .portfolio-gallery {
    display: grid;
    grid-template-columns: minmax(100px, 250px) 1fr 1fr;
    grid-gap: 20px;
  } */
</style>

This approach could go wrong for many reasons. First, a picky developer who loves semantics would dye inside a little. Most importantly, we would create an invisible connection between two components: changing the CSS of the first one would affect the second one. You're creating a trap for your future self.

Option 2)Create a new grid.

<div>
  <!-- ... -->
</div>

<div>
  <!-- ... -->
</div>

<style>
  .form-grid, .portfolio-gallery {
    display: grid;
    grid-template-columns: minmax(100px, 250px) 1fr 1fr;
    grid-gap: 20px;
  }
</style>

Creating a new grid for the new component and grouping the classes would be the safest approach. Besides, it won't affect the CSS size.

Now imagine we get to the point where our project needs multiple (similar) grids. Should we keep creating a new grid for each component?

.gallery, .form-grid, .portfolio-gallery, .contact-gallery, .list {
  display: grid;
}

.gallery, .form-grid, .portfolio-gallery {
  gap: 20px;
}

.form-grid, .portfolio-gallery {
  grid-template-columns: repeat(3, 1fr);
}

.gallery {
  grid-template-columns: repeat(2, 1fr);
}

@media (min-width: 48rem) {
  .gallery {
    grid-template-columns: repeat(4, 1fr);
  }
}

.contact-gallery {
  gap: 16px;
  grid-template-columns: repeat(4, 1fr);
}

.list {
  gap: 40px;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

Here are the weakest points of the above approach:

  1. Even though the grid classes are in the same SCSS file (highly unusual - typically, they'll end up in different SCSS/component files), and the number of grids is relatively small, dealing with the grid system is already getting more complicated.
  2. Each grid requires a different class name, and naming too many things can be frustrating.
  3. Because the classes are intertwined, modifying, or creating new grids requires a high concentration level. The risk of breaking things is high.

Sooner or later, we end up doing that only thing that makes sense: we create abstractions .

.grid { display: grid; }
.grid-col-2 { grid-template-columns: repeat(2, 1fr); }
.grid-col-3 { grid-template-columns: repeat(3, 1fr); }
.grid-col-4 { grid-template-columns: repeat(4, 1fr); }
.grid-auto { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.gap-sm { gap: 16px; }
.gap-md { gap: 20px; }
.gap-lg { gap: 40px; }

Our grid utility classes are born out of necessity!

Here are the main advantages of abstracting grid utility classes:

  1. All the grid utility classes are in the same SCSS file and are easy to read.
  2. You can quickly expand the system if required by the project.
  3. No more naming things.
  4. If you use the same grid system across multiple projects, you won't need to inspect the CSS at all.
  5. Dealing with simpler CSS means reducing the risk of breaking things.

Conclusion

The main goal when working with CSS is maintainability: do what makes your code easier to maintain.

When working with grids, that means using utility classes if the project becomes more complex. It also implies creating the grids in HTML rather than in CSS. The "separation of concerns" is not the topic of this article. If you're interested, here's a video where I explain how we use utility classes at CodyHouse.

"Are you suggesting we should import a package of hundreds of CSS lines in every project just to manage the grid system?" No.

The framework's grid systems are usually massive because they include all the options, some of which you'll never use: responsive modifiers for all the breakpoints, offset, gap options, and so on. However, modern tools like PurgeCSS prevent your CSS from bloating and include in your CSS only the classes you actually use.

Are you looking for a great grid system? Check outCodyFrame. 

Feedbacks/suggestions? Get in touch on Twitter . We'd love to hear what you think.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

这就是OKR

这就是OKR

【美】约翰·杜尔(John Doerr) / 曹仰锋、王永贵 / 中信出版社 / 2018-12 / 68.00元

这本书是传奇风险投资人约翰·杜尔的作品,揭示了OKR这一目标设定系统如何促使英特尔、谷歌等科技巨头实现爆炸性增长,以及怎样促进所有组织的蓬勃发展。 20世纪70年代,在英特尔担任工程师时,杜尔首次接触到OKR。之后,作为一个风险投资人,杜尔不遗余力地将这一管理智慧,分享给50多家公司和机构,包括谷歌、亚马逊、领英、脸书、比尔及梅琳达·盖茨基金会,甚至摇滚歌手波诺的公益项目。在杜尔的帮助下,任......一起来看看 《这就是OKR》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具