Common application scenarios The interfaces of current APPs are basically the same, and the grid layout has become a necessity for every APP. With border, often used in "Function Navigation" page Borderless, commonly used in home page categories Design goals In the scss environment, n-grids are implemented through mixin, and can support "with or without borders" and "whether each grid is square": @include grid(3, 3, true); // 3 x 3, with borders, and each grid is a square @include grid(2, 5, false, false); // 2 x 5, without borders Final result "Padding Percentage" Tips First, let me explain a little trick, how to achieve a square. I guarantee you will understand it after reading it once. The conclusion is: Design ideas (regardless of whether you are scss or less)
So our html looks like this: <!-- a-grid is a flex container, which makes it easy to "center horizontally/vertically" its content --> <div class="a-grid"> <!-- a-grid__item is used to take up space to realize the square --> <div class="a-grid__item"> <!-- item__content is the actual container for the content--> <div class="item__content"> content... </div> </div> </div> Code (scss) Three things are done here:
.a-grid { display: flex; flex-wrap: wrap; width: 100%; .a-grid__item { text-align:center; position:relative; >.item__content { display:flex flex-flow: column; align-items: center; justify-content: center; } } } @mixin grid($row:3, $column:3, $hasBorder:false, $isSquare:true) { @extend .a-grid; .a-grid__item { flex-basis: 100%/$column; @if($isSquare) { padding-bottom: 100%/$column; height: 0; } >.item__content { @if($isSquare) { position:absolute; top:0;left:0;right:0;bottom:0; } } } @for $index from 1 to (($row - 1) * $column + 1) { .a-grid__item:nth-child(#{$index}) { @if($hasBorder) { border-bottom: 1px solid #eee; } } } @for $index from 1 to $column { .a-grid__item:nth-child(#{$column}n + #{$index}) { @if($hasBorder) { border-right: 1px solid #eee; } } } } use // Generate a 3-row, 3-column, square grid. a-grid-3-3 { @include grid(3, 3, true); } // Generate a 2-row, 5-column, borderless grid, where the height of each grid is determined by the content. a-grid-2-5 { @include grid(2, 5, false, false); } Reminder: If you want to make a nxm layout, don't forget to add nxm corresponding DOM structures in HTML after using @include grid(n, m). final The content is very simple, and there are still many areas that can be optimized, such as the border can be changed to a "hairline" border, which looks thinner on the real device. Well, that's all for now. If you have a better way to implement it, please leave a message. Thank you for reading. I'm writing a CSS style library recently, the goal is to be compatible with applet, everyone who is interested can play with it, this is the source code corresponding to this lesson: https://github.com/any86/3a.css/blob/develop/src/components/_grid.scss The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. |
<<: Web page experience: planning and design
>>: Summary of the most commonly used knowledge points about ES6 new features
Result: Implementation code: Need to be used with...
Table of contents introduction Cookie What are Co...
The so-called cascading replication is that the m...
Table of contents 1. Overview 1.1 Creating a func...
This article uses a jQuery plug-in to create an a...
The Linux stream editor is a useful way to run sc...
This article is mainly for those who do not under...
Table of contents 1. Mapped Types 2. Mapping Modi...
It is troublesome to install the db2 database dir...
1. After connecting and logging in to MySQL, firs...
Today, when developing, I encountered a method wh...
This article introduces an example of how to use ...
This article shares the specific code for JavaScr...
pssh is an open source software implemented in Py...
This article mainly introduces the example analys...