Shocked! Many people don't know that CSS grid framework has been around for a long time!

Sudiri 2020-11-08 23:46:07
shocked people don know css


Preface

Writing this article originates from a question of Zhihu :【CSS Grid The layout is so good , Why hasn't anyone developed it based on Grid The front-end framework of the layout ?

This article has been delayed for two months , It's because I really don't know where to say it . Almost all the answers to this question don't get to the essence of the question , And right CSS Grid There are also deep misunderstandings . In addition, the description of this problem may not be appropriate , Because it is based on CSS Grid There's a framework for . Interested friends can learn about Flex-Layout, It's a artifact based on command layout . Don't be misled by the project name , Although it's called flex-layout, In fact, there are flex and grid Two instructions .

I think a more appropriate description of this problem should be 【 Why not based on Grid Pure layout CSS The front frame 】, be based on Float and Flex Of CSS There are so many layout frames , But I haven't seen it based on CSS Grid The implementation of the . I'm writing Snack I also thought about it in v3 Integration in the version CSS Grid, But it turns out that it's unrealistic , Detailed below . It is said that Bootstrap I also planned to be in v5 Version integration CSS Grid, But for now, it's still used Flex Layout . I personally think it's based on instructions flex-layout It should be integration CSS Grid The best way .

This article intends to talk about it from the opposite angle , If you want to implement a system based on CSS Grid The Layout Framework of , What problems will we encounter ?

Browser compatibility

Let's talk about compatibility first , A lot of people always think CSS Grid Compatibility is not good . It's not , The statistics in the figure below show that most browsers are interested in CSS Grid It's still good to support , Need to know Flex stay IE10 The properties that can be used above are also very limited . According to statistics, at present CSS Grid Utilization rate and sum Flex It's basically flat . So compatibility doesn't answer the opening question .

CSS The features of the Layout Framework

CSS There are four main layouts , It was used in ancient times table, Don't say that , We'll only talk about what is commonly used in the new century Float、Flex、Grid. These three layouts are progressive , Gradually strengthened CSS The convenience of layout .CSS The best practice of layout is Bootstrap, among Float and Flex The layout has been Bootstrap Realization , only Grid There is no sign of the plan .

CSS The key to the layout framework is how to design the grid , Let's first look at Bootstrap How does the layout work in China .

<div class="row">
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
<div class="col-md-1">.col-md-1</div>
</div>
<div class="row">
<div class="col-md-8">.col-md-8</div>
<div class="col-md-4">.col-md-4</div>
</div>
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4">.col-md-4</div>
</div>
<div class="row">
<div class="col-md-6">.col-md-6</div>
<div class="col-md-6">.col-md-6</div>
</div>

Used to Bootstrap The above structure must not be unfamiliar to the people of . stay Bootstrap v4 in , because Flex The enhancement of layout function ,row It can also be matched with utility Use .

<div class="row align-items-start">
<div class="col">One of three columns</div>
<div class="col">One of three columns</div>
<div class="col">One of three columns</div>
</div>
<div class="row align-items-center">
<div class="col">One of three columns</div>
<div class="col">One of three columns</div>
<div class="col">One of three columns</div>
</div>
<div class="row align-items-end">
<div class="col">One of three columns</div>
<div class="col">One of three columns</div>
<div class="col">One of three columns</div>
</div>

row and col The combination of grid layout has almost become the consensus and standard of grid layout . And then we're going to tear one by hand in this way Grid Grid to see the effect .

Tear your hands CSS Layout grid

In fact, the hand tears one CSS It's not hard to lay out the grid , The key is to be familiar with the loop function of the preprocessor . Let's use Float、Flex And Grid Implement a simplified version of the grid layout and compare the differences between them .

Float

Float It was the only way to achieve fluid layout in the past , The difficulty is dealing with the collapse of the parent element , This is a cliche , This article will not be repeated . Here is a simplified version of Float grid :

See the Pen float-columns by Zongbin (@nzbin) on CodePen.

This grid is simple and practical , Classic . however Float The layout is very limited , If the height of the grid is not the same ( For example, the height of the first grid is increased ), The layout will collapse .

Flex

Flex The birth of the almost solved Float All the pain points of the grid , For example, the collapse problem mentioned above . Here is a simplified version of Flex grid :

See the Pen flex-columns by Zongbin (@nzbin) on CodePen.

In fact, in a one-dimensional layout ,Flex It's good enough to use , At least there is no need to improve the fluid layout , So is it necessary to introduce a two-dimensional layout ?

Grid

The key is coming. ! The key is coming. ! The key is coming. !

Grid It's better than Flex More advanced layout technology , So simply replace Flex There is no problem in implementing a grid layout system . Here's an extremely simple Grid grid , Almost and Flex There's no difference in the effect of the grid .

See the Pen grid-columns by Zongbin (@nzbin) on CodePen.

Grid In addition to being able to achieve Flex Outside the layout of , You can also adjust each row in the vertical space ( Setting up grid-template-rows: 1fr 2fr). in addition ,Grid It's also easier to set the grid for other column numbers , Setting up 10 Column grid , Just set up grid-template-columns: repeat(10, 1fr) that will do ; If it is Flex, You need to recalculate the width of the grid .

The example above shows CSS Grid It's just the tip of the iceberg . As a two-dimensional layout technology , Its attribute writing is very complicated , For example, another powerful feature grid-template-areas MDN Demos. It is very difficult to use the superposition combination of class names to achieve the corresponding functions .

If you just simply put Flex Replace the grid with Grid grid , I personally feel that it doesn't make sense , One is based on CSS Grid The new grid system should give full play to all its advantages . This may be CSS Grid The reason why the grid system hasn't appeared yet . I don't know if this article clearly expresses my opinion , Or can we answer your questions about the opening question . Let's take a look at flex-layout Medium Grid.

Flex-Layout Medium Grid

because flex-layout It's an instruction based implementation , So it's not as limited as the superposition of class names , It can almost play CSS Grid All the advantages of . The following is grid-template-areas How to define .

<div gdAreas.xs="header | sidebar | content | sidebar2 | footer" gdGap="1em"
gdColumns.xs="none"
gdAreas.sm="header header | sidebar content | sidebar2 sidebar2 | footer footer"
gdColumns.sm="20%!"
gdAreas.gt-sm="header header header | sidebar content sidebar2 | footer footer footer"
gdColumns.gt-sm="120px auto 120px" gdGap.gt-sm="20px" [ngStyle]="{'max-width': 'auto'}"
[ngStyle.gt-sm]="{'max-width': '600px'}">
<div class="blocks one" gdArea="header">Header</div>
<div class="blocks two" gdArea="sidebar">Sidebar</div>
<div class="blocks three" gdArea="sidebar2">Sidebar 2</div>
<div class="blocks four" gdArea="content">Content
<br /> More content than we had before so this column is now quite tall.</div>
<div class="blocks five" gdArea="footer">Footer</div>
</div>

flex-layout Whether it is the design idea or the way of implementation, let me worship , It and traditional Bootstrap The grid of form is essentially different . The point of this article is not to introduce flex-layout, If you're interested , Let's look at the official website Flex-Layout.

summary

I've been brewing articles for two months , I still don't express myself clearly . Back to the question itself ,【 Why not based on Grid Pure layout CSS The front frame 】, I think the most important thing is that the traditional writing method can't be used CSS Grid All the advantages of , At least we can't do it with the same way that we used to do it .CSS Grid The particularity of attributes also determines that it is impossible and should not be just a simple superposition of class names .

版权声明
本文为[Sudiri]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple