css网格_一个CSS网格可以全部统治

css网格

The case for using one CSS grid for your entire website

在整个网站上使用一个CSS网格的情况

CSS网格与Flexbox (CSS Grid vs Flexbox)

In the dark ages, we used table, a few years ago we used float and before today most of us used flex . Of course, these older technologies still serve a purpose but when it comes to good old fashioned layout, CSS grid is where it’s at.

在黑暗时代,我们使用table ,几年前我们使用float而在今天之前,我们大多数人都使用flex 。 当然,这些较旧的技术仍然可以达到目的,但是当涉及到良好的老式布局时,CSS grid就是它的用武之地。

范例(JSX) (Examples (JSX))

A simple three-column grid in flexbox with 20px gap and 30px of gutter on the left and right:

flexbox一个简单的三列网格, flexbox20px间距和30px的装订线:

3 column flexbox grid example
3列Flexbox网格示例

There are a few annoying things that we have to do to accomplish this. Do you see them?

为此,我们需要做一些烦人的事情。 你看到他们了吗?

  1. Negative margins on the parent to account for the padding on the left/right of the first/last columns.

    父级上的负边距用于说明第一列/最后一列的左侧/右侧的填充。
  2. Half padding on left/right of each column to form the column gutters.

    在每列的左侧/右侧填充一半以形成列装订线。

Let’s do the same thing with CSS Grid:

让我们对CSS Grid做同样的事情:

3 column CSS grid example
3列CSS网格示例

Easy peasy! Just define the number of columns (three, filling an equal amount of space) and the gap size and then sit back and relax. The benefits of CSS grid over the other layout techniques become even more apparent as grids get more complex but hopefully, you get the picture.

十分简单! 只需定义列数(三列,填充相等的空间)和gap大小,然后坐下来放松即可。 随着网格变得越来越复杂,CSS网格相对于其他布局技术的优势变得更加明显,但希望您能理解。

多个网格与单个网格 (Multiple Grids vs a Single Grid)

多个网格可能是个好主意,但这就是为什么它们不是这样的原因 (Multiple grids may feel like a good idea, but here’s why they aren’t)

Once you start using CSS grids, I can almost guarantee that you’ll want to create a new grid for each component. At least I did! However, it turns out this could easily result in inconsistencies all over the place, causing your code to never match up with the designer’s intention (assuming they designed on a grid). A designer typically creates a single grid and lays out the entire site on that grid. Sure, a component might be a 2 column component, but it still fits on their 12 column grid. Here’s an example of the wrong way to do things:

一旦开始使用CSS网格,我几乎可以保证您将要为每个组件创建一个新的网格。 至少我做到了! 但是,事实证明,这很容易导致整个地方的不一致,从而导致您的代码永远不会与设计者的意图相符(假设他们是在网格上进行设计的)。 设计人员通常创建单个网格,然后在该网格上布置整个站点。 当然,一个组件可能是2列的组件,但仍适合其12列的网格。 这是做事方法错误的一个示例:

Image for post
A simple 2 column grid?
一个简单的2列网格?

While coding the component above, you may be tempted to create a very simple grid:

在对上面的组件进行编码时,您可能会想创建一个非常简单的网格:

<div style={{ display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', gap: 30 }} />

Intuitively, this makes sense! After all, this is just a 2 column grid with each column spanning half of the parent. This is actually not the right approach but let's look at one more example before going on.

凭直觉,这是有道理的! 毕竟,这只是一个2列网格,每列跨越父对象的一半。 这实际上不是正确的方法,但在继续之前让我们再看一个示例。

Image for post
A simple 4 column grid?
一个简单的4列网格?

In this example from the same project, we might be tempted to create the following 4 column grid:

在同一项目的此示例中,我们可能很想创建以下4列网格:

<div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 30 }} />

Wrong again!

又错了!

单格图案 (The single grid pattern)

I advise that we follow the designer’s workflow and use a single grid for the entire webpage, and follow that same grid for every component. In the case of the examples above, both grids would be a 12 column grid:

我建议我们遵循设计者的工作流程,对整个网页使用单个网格,并对每个组件使用相同的网格。 在上面的示例中,两个网格均为12列网格:

<div style={{ display: 'grid', gridTemplateColumns: 'repeat(12, 1fr)', gap: 30 }} />

Now let’s take a look at the components above built for the 12 column grid:

现在,让我们看一下上面为12列网格构建的组件:

And here’s the 4 column layout:

这是4列的布局:

By sharing a single 12 column grid between both components and simply spanning the appropriate amount of columns, we ensure that both of these components actually line up with each other on the global grid as the designer intended. If you choose to use different grids for each component, your components will no longer fit on the global grid and the designer will let you know about it.

通过在两个组件之间共享单个12列网格并简单地跨适当数量的列,我们可以确保这两个组件确实按照设计者的意图在全局网格上彼此对齐。 如果您为每个组件选择使用不同的网格,则您的组件将不再适合全局网格,设计人员将让您知道。

React全球网格 (React Global Grid)

To aid with this, I’ve created a very simple collection of components for React called react-global-grid.

为了帮助这一点,我为React创建了一个非常简单的组件集合,称为react-global-grid 。

安装 (Installation)

npm i react-global-grid

用法 (Usage)

At the moment, this library requires the following peer dependencies:

目前,该库需要以下对等依赖项:

  • React

    React
  • Styled Components

    样式化的组件

  • Rebass

    里巴斯

If you’re already using those then you’re set. If not, don’t fret! Just create a Grid component and define some global styles for it. Then make sure to use that component all over the place and just span the number of columns necessary for each module.

如果您已经在使用这些功能,则可以开始使用。 如果没有,请不要担心! 只需创建一个Grid组件并为其定义一些全局样式即可。 然后,请确保在各处使用该组件,并且仅跨每个模块所需的列数。

翻译自: https://medium.com/swlh/one-css-grid-to-rule-them-all-3e3386ad6155

css网格

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/275822.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java 高级泛型_Java 泛型高级

1、限制泛型可用类型在定义泛型类别时&#xff0c;预设可以使用任何的类型来实例化泛型中的类型&#xff0c;但是如果想要限制使用泛型的类别时&#xff0c;只能用某个特定类型或者其子类型才能实例化该类型时&#xff0c;使用extends关键字指定这个类型必须是继承某个类&#…

做Web前端开发的你必须会这几点!

1、Web开发分类与区别 人们通常将Web分为前端和后端&#xff0c;前端相关的职位有前端设计师(UI/UE)&#xff0c;前端开发工程师&#xff0c;后端相关的有后端开发工程师。 2、技术栈区别 看各大招聘网站上&#xff0c;公司对前端开发工程师的要求莫过于精通HTML&#xff0c;CS…

PDA开发系列:GPS模块的调用

摘要在wince6.0中&#xff0c;要调用GPS模块&#xff0c;其实是一件很容易的事情。 正文在wince6.0中&#xff0c;如果要调用GPS模块&#xff0c;其实很简单&#xff0c;微软已经为我们做好了这一切&#xff0c;我们只需要在自己的解决方案中&#xff0c;添加对Microsoft.Windo…

2021 年最值得使用的 Node.js 框架

大家好&#xff0c;我是若川。今天推荐一篇译文&#xff1a;2021年最值得使用nodejs框架&#xff0c;值得收藏&#xff0c;很快能看完。点击下方卡片关注我、加个星标。学习源码整体架构系列、年度总结、JS基础系列Node.js 是最敏捷的服务端 web 应用平台&#xff0c;因为它为应…

面试被拒绝如何争取_争取反馈

面试被拒绝如何争取Let me start by saying that Dribbble is awesome. I’ve been a member of the Dribbble community for years. I swoon over the beautiful design I see throughout the site. Dribbble is among the most popular places to go for inspiration and whi…

highgui java opencv_java – OpenCV 3.0.0 JAR缺少HighGUI

我正在使用Java支持来编译OpenCV 3.0.0。我的脚本是&#xff1a;mkdir /opt/opencv /opt/opencv/opencv-buildcd /opt/opencvgit clone https://github.com/Itseez/opencv.gitcd /opt/opencv/opencv-buildexport OPENCV_INSTALL_PATH/opt/opencvexport FFMPEG_PATH/opt/ffmpeg/…

中台之上(二):为什么业务架构存在20多年,技术人员还觉得它有点虚?

业务架构这个词大家时常听到&#xff0c;但是能解释得清楚的却不多&#xff0c;撩撩度娘&#xff0c;你就会发现&#xff0c;不少人问及业务架构和应用架构的关系&#xff0c;聊天时&#xff0c;也常有人问起业务架构师和产品经理什么区别&#xff1f;业务架构分析和需求分析什…

你对webpack了解多少?

大家好&#xff0c;我是若川。之前有一个朋友给我留言&#xff1a;自己目前在一家小厂工作&#xff0c;对webpack的使用比较熟悉&#xff0c;想要跳槽去大厂&#xff0c;但是纠结是否还需要深入学习webpack&#xff08;因为觉得学了也用不到&#xff0c;但又怕面试不通过&#…

转:数据库关系模式的范式详解

关系模式的范式 主要有4种范式&#xff0c;1NF&#xff0c;2NF&#xff0c;3NF&#xff0c;BCNF&#xff0c;按从左至右的顺序一种比一种要求更严格。要符合某一种范式必须也满足它前边的所有范式。一般项目的数据库设计达到3NF就可以了&#xff0c;而且可根据具体情况适当增加…

纹理对象纹理单元纹理目标_网页设计理论:纹理

纹理对象纹理单元纹理目标Texture has become an indispensable element in web design. It is not only a trend but also a quick way to increase the depth of web pages. When designers learn to use textures, they can strengthen the appeal of web pages. It can be …

javascript-高级用法

22.1 安全的类型检测 为什么&#xff1a;typeof 不靠谱, 无法将数组从对象中区分出来&#xff0c; instanceof 有特殊情况&#xff0c;在iframe存在的情况下无法判断另一个iframe内的数组 如何做&#xff1a;Object.prototype.toString().call(Arr); 22.3 高级定时器 特点&…

java dao层和base层_详解Javaee Dao层的抽取

有时候我们在实现不同功能的时候回看到很多的Dao层的增加、修改、删除、查找都很相似&#xff0c;修改我们将他们提取BaseDao一、提取前1. 提取前的LinkDao层&#xff1a;public interface LinkManDao {Integer findCount(DetachedCriteria detachedCriteria);List findByPage(…

畅销书《深入浅出Vue.js》作者,在阿里淘系1年的收获成长

大家好&#xff0c;我是若川。今天推荐一篇95年的博文的文章。他的故事应该挺多人知道。如果不知道可以看他的博客 https://github.com/berwin/blog点击下方卡片关注我、加个星标时间好快&#xff0c;眨眼间&#xff0c;加入阿里已经一年了。这一年发生了很多事&#xff0c;整体…

GrideView属性的学习

GridView 控件可公开分页事件和排序事件&#xff0c;以及在创建当前行或将当前行绑定至数据时发生的事件。单击一个命令控件&#xff08;例如&#xff0c;包含在 GridView 控件中作为其中的一部分的 Button 控件&#xff09;时也会引发事件。下表描述了由 GridView 控件公开的事…

插图 引用 同一行两个插图_将图标变成插图的五个简单步骤

插图 引用 同一行两个插图Every creative person has probably already been in this situation: A project, be it a website, an app — or as far as I am concerned: often a news story would benefit from an appealing side visual. But neither budget nor time makes …

urllib库parse模块的使用

官方文档转载于:https://www.cnblogs.com/vector-wyw/p/10416010.html

java dateutil 获取时间戳_java DateUtil工具类时间戳类型转换详解

本文实例为大家分享了DateUtil工具类时间戳类型转换的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下package com.sinosoft.media.sms.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class DateUtil {//当前…

fluorinefx C# 版的开源rtmp服务器

fluorinefx C# 版的开源rtmp服务器 - [其它资源] 版权声明&#xff1a;转载时请以超链接形式标明文章原始出处和作者信息及本声明http://25swf.blogbus.com/logs/28529745.html fluorinefx支持的 .net 框架集有 1.1 2.0 3.5 以及 mono 1.2.4支持的东东Flex, Flash Remoting (RP…

web登录界面设计_出色的Web界面设计的7条规则

web登录界面设计When you work on a website or on the design of web pages, remember that their success is not determined by the beauty of their visual style. In fact, in his article “10 Principles Of Good Website Design”, Vitaly Friedman stated:当您在网站或…

关于为什么我推荐大家看vue代码的随想

大家好&#xff0c;我是若川。今天给大家推荐一篇大圣老师在知乎的回答&#xff0c;很快能看完。我也曾经回答过这个问题。若川知乎高赞&#xff1a;有哪些必看的 JS 库&#xff1f;不要为了读源码而读源码&#xff0c;但要学会看源码。自己常用的熟悉的库的源码值得读读。点击…