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.pswp.cn/news/275822.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275822.shtml
英文地址,請注明出處:http://en.pswp.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;但要學會看源碼。自己常用的熟悉的庫的源碼值得讀讀。點擊…