CSS Grid 讀書筆記

来源:https://www.cnblogs.com/rynxiao/archive/2018/09/19/9674944.html
-Advertisement-
Play Games

基本概念 "MDN" 上的解釋是這樣的 CSS Grid Layout excels at dividing a page into major regions or defining the relationship in terms of size, position, and layer, b ...


基本概念

MDN上的解釋是這樣的

CSS Grid Layout excels at dividing a page into major regions or defining the relationship in terms of size, position, and layer, between parts of a control built from HTML primitives.

另外,下麵一段話摘自A Complete Guide to Grid,對於CSS Grid會有更加清楚地釋義

CSS Grid Layout (aka "Grid"), is a two-dimensional grid-based layout system that aims to do nothing less than completely change the way we design grid-based user interfaces.

總結來說:

  • CSS Grid 是一個二維的佈局系統
  • CSS Grid 相比傳統佈局在頁面整體劃分佈局上更加出色
  • CSS Grid 並不是只能單獨使用,依然可以搭配Flexbox以及傳統定位佈局一起使用

相容性

摘自Can I Use中對CSS Grid的相容性分析。

compatibility

從圖中可以看出瀏覽器的相容率整體達到84.16%,並且都是無需帶首碼的。

基本概念

網格是一組相交的水平線和垂直線,它定義了網格的列和行。我們可以將網格元素放置在與這些行和列相關的位置上。

Grid Container (網格容器)

在一個元素上應用了display: grid;或者display: inline-grid;那麼就創建了一個網格容器,它下麵的直接子元素都會成為網格元素,例如:

<style>
.wrapper {
  display: grid;
}
</style>

<div class="wrapper">
  <div class="custom">One</div>
  <div class="custom">Two
    <p>I have some more content in.</p>
    <p>This makes me taller than 100 pixels.</p>    
  </div>
  <div class="custom">Three</div>
  <div class="custom">Four</div>
  <div class="custom">Five</div>
</div>

normal-grid

從網頁的基本表現看,和平常的佈局沒有什麼差別,Mac OSX 【alt + command + I】,Windows 【F11】打開網頁檢查器即可看出網格佈局。

inspector-grid

Grid Tracks (網格軌跡)

從字面上的意譯來看還是比較繞口,但是我換一種說法你可能就會明白了。可以把tracks看做是table中的行和列就好了。

grid-track

定義一個網格中的行和列的數量分別使用grid-template-rowsgrid-template-columns來確定這個表格會有多少行以及多少列。例如:

.container {
  display: grid;
  grid-template-rows: 100px 50px 100px;
  grid-template-columns: 100px 100px 100px;
}

上面的代碼寫出了一個3 x 3的網格

sure-grid

在圖中可以看出網格的數量,其中的子元素會根據這些網格的數量自動填充。如果事先不知道要劃分多少行,可以使用只使用grid-template-columns來確定多少列,行數會根據有多少子item來自動計算,例如下麵的:

.container {
  display: grid;
  grid-template-columns: 200px 200px 200px;
}

single-col

5個元素如果是劃分3列,那麼就應該會有兩行。

fr

如果想要均分容器的寬度,那麼可以使用新引入的單位fr,新的fr單位代表網格容器中可用空間的一等份。例如:

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

repeat()

可以使用repeat()函數來標記軌道重覆使用的部分,例如上面的樣式就可以寫成:

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

每行高度

如果想確定使用每行高度,可以使用grid-auto-rows: 100px;來確定每行只有100px

minmax()

如果想讓每行的高度隨著內容自動填充,那麼可以使用minmax()來確定最小值與最大值,例如:

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(100px, auto);
}

上面的樣式規定了一個3列佈局,每行的高度最少為100px的網格。

auto-rows

Grid Line (網格線)

網格線用來構建整個網格,包括水平的和豎直的

grid-line

當一個網格被構建出來,網格線就會被預設地有一個標識,看下圖:

grid-numbers

標識的線可以是正向也可以是逆向的,例如第一行的線的標識就是[1|-4],那麼線標識有什麼用?線標識主要用來確定一個子元素要占有的面積,也成為Grid Area,例如下麵的代碼:

.one {
  grid-column-start: 1;
  grid-column-end: 4;
  grid-row-start: 1;
  grid-row-end: 3;
}

line

給線命名

預設的線的標識都是用數字來表示的,當然也可以給線來命名,具體如下:

.container {
  grid-template-columns: [first] 40px [line2] 50px [line3] auto [col4-start] 50px [five] 40px [end];
  grid-template-rows: [row1-start] 25% [row1-end] 100px [third-line] auto [last-line];
}

grid-names

從圖中可以看出,第一列的第一根線被命名成了first,那麼我們就可以將之前的寫法稍微改一下了:

.one {
  grid-column-start: first;
  grid-column-end: col4-start;
  grid-row-start: 1;
  grid-row-end: 3;
}

關於更多的線的命名使用方法,可以參看Naming lines when defining a grid,這裡只是簡單的介紹

Grid Cell (網格單元)

可以簡單理解為一個table中的一個單元格

grid-cell

Grid Area (網格面積)

可以通過規定一個item的起始行和起始列來規定一個area,註意:area必須為一個 規則的舉行,而不能為一個類似於L形狀的圖形

grid-area

通過行和列標識來確定一個面積,例如:

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(100px, auto);
}
.one {
  grid-column-start: 1;
  grid-column-end: 3;
  grid-row-start: 1;
  grid-row-end: 3;
}

上面的代碼就劃定了一個兩行兩列的區域,上面的寫法可以簡寫為:

.one {
  grid-column: 1 / 3;
  grid-row: 1 / 3;
  
  /* 這種寫法對應: */
  grid-column: [grid-column-start] / [grid-column-end];
  grid-row: [grid-row-start] / [grid-row-end];
}

或者使用grid-area

.one { 
  grid-area: 1 / 1 / 3 / 3;
  
  /* 這種寫法分別對應: */
  grid-area: [grid-row-start] / [grid-column-start] / [grid-row-end] / [grid-column-end];
}

還可以配合grid-template-areas來提前劃分區域,例如:

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(100px, auto);
  grid-template-areas: 
   "header header header"
   "siderbar main main"
   "footer footer footer"
}

上面的樣式中,規定了一個3 x 3的網格,並且劃分了區域,第一行為header,第二行為左側為siderbar,右側為main,第三行為footer,那麼剩餘的工作就是制定子元素對應的區域即可,例如:

.header {
  grid-area: header;
}
.siderbar {
  grid-area: siderbar;    
}
.main {
  grid-area: main;    
}
.footer {
  grid-area: footer;
}

對應的網頁文件為:

<div class="container">
  <div class="custom header">header</div>
  <div class="custom siderbar">siderbar</div>
  <div class="custom main">main</div>
  <div class="custom footer">footer</div>
</div>

最終的效果為:

template-areas

Grid Gutters (網格間距)

分為行間距和列間距,類似於table中的colspanrowspan,具體例子如下:

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(100px, auto);
  grid-gap: 10px;
  
  /* 這裡的grid-gap相當於: */
  grid-column-gap: 10px;
  grid-row-gap: 10px;
}

從上一個例子中,我也設置了10px的間距,可以從圖中看出來。

Grid z-index

類似於position: absolute;絕對定位之後的層級,後面渲染的item會覆蓋前面的,因此下例中的item為Two的會覆蓋在One

.z-index-1 {
  grid-column: 1 / 3;
  grid-row: 1;
  background-color: aliceblue;
}
.z-index-2 {
  grid-column: 2 / 4;
  grid-row: 1 / 3;
  background-color: antiquewhite;
}

z-index-1

調整item1的index之後z-index: 2;,會看到item1會覆蓋在item2上面

<div class="custom z-index-1" style="z-index: 2;">One</div>
<div class="custom z-index-2" style="z-index: 1;">Two</div>

z-index-2

網格佈局中的對齊

如果熟悉flex,那麼一定會知道flex中的佈局,相同的,在grid佈局中也有相應的佈局

網格佈局的兩條軸線

When working with grid layout you have two axes available to align things against – the block axis and the inline axis. The block axis is the axis upon which blocks are laid out in block layout. If you have two paragraphs on your page they display one below the other, so it is this direction we describe as the block axis.

  • 塊方向的列軸

block-axis

  • 文字方向的行軸

inline-axis

以上的文字以及圖片均摘自MDN

對齊列項目

對齊列項目主要的CSS聲明有兩個:align-items以及align-selfalign-items用於所有item的設置,align-self可以單獨進行自定義。這兩個聲明可取的屬性值有以下幾種:

  • auto
  • normal
  • start
  • end
  • center
  • stretch
  • baseline
  • first baseline
  • last baseline

下麵我們用一個慄子分別說明如下(慄子摘自MDN):

<style>
.wrapper {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-gap: 10px;
  grid-auto-rows: 100px;
  grid-template-areas: 
    "a a a a b b b b"
    "a a a a b b b b"
    "c c c c d d d d"
    "c c c c d d d d";
}
.item1 {
  grid-area: a;
  background-color: aqua;
}
.item2 {
  grid-area: b;
  background-color: aqua;
}
.item3 {
  grid-area: c;
  background-color: aqua;
}
.item4 {
  grid-area: d;
  background-color: aqua;
}
</style>

<div class="wrapper">
  <div class="custom item1">Item 1</div>
  <div class="custom item2">Item 2</div>
  <div class="custom item3">Item 3</div>
  <div class="custom item4">Item 4</div>
</div>

定義了一個8 x 4列的網格,其中劃分為均勻的四個區域,分別用item[1,2,3,4]來進行填充,預設的對齊方式為stretch

box-alignment-2

可以通過幾個select來控制對齊方式,分別通過控制整體和單個item的對齊

box-alignment

相對於容器的列對齊

使用align-content這個屬性聲明來決定整個網格在容器中的列方向的佈局,可選的值如下:

  • normal
  • start
  • end
  • center
  • stretch
  • space-around
  • space-between
  • space-evenly
  • baseline
  • first baseline
  • last baseline

align-content

可以看到圖中,當選擇了align-content: end的時候,整個網格就會在容器的下方對齊。

更多內容請參看MDN CSS_Grid_Layout

Grid 佈局和其他佈局的關係

Grid 和 Flex

The basic difference between CSS Grid Layout and CSS Flexbox Layout is that flexbox was designed for layout in one dimension - either a row or a column. Grid was designed for two-dimensional layout - rows, and columns at the same time.

Grid佈局和Flex佈局最大的不同點就是:Grid佈局是二維佈局,針對行和列的佈局,而Flex佈局為一維佈局,只針對行和列的當行佈局。

Tips: 這兩種佈局並不衝突,可以搭配使用。可以在整體佈局上採用grid佈局,而細節處理可以使用flex

下麵看一個慄子,來看看這兩種佈局之間有什麼不同(慄子來源MDN):

編寫一個自動換行適應的佈局

  • Flex方式
<style>
  .flex-wrapper {
    display: flex;
    flex-wrap: wrap;
  }
  .flex-wrapper > div {
    flex: 1 1 200px;
  }
</style>

<div class="flex-wrapper">
  <div>One</div> 
  <div>Two</div>
  <div>Three</div>
  <div>Four</div>
  <div>Five</div>
</div>
  • Grid 方式
<style>
  .grid-wrapper {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }
</style>

使用flex-wrap: wrap;來限定如果一行不足就自動換行。使用auto-fill來根據容器寬度決定會有多少列,並且使用minmax()函數來確定最小為200px,最大為容器寬度來自適應。

如果屏幕上有很多剩餘的空間,flex佈局會均分成5列,而grid佈局則會始終為3列,並且餘下的兩個item也長度也 相同,而如果屏幕寬度調整為小於200時,flex佈局會彈性地變為1列,但是grid佈局如果沒有使用auto-fill時,會始終為設置的列數。

另外,grid佈局和flex佈局還有一點不同的是,在最開始的分配的時候grid佈局會優先劃分佈局,即會優先規定出屏幕中可以最大容忍出 多少個符合條件(這裡是最小為200px, 最大為1fr)的item數量,然後依次填充。

flex-grid

Grid 和 絕對定位

.positioned {
  grid-template-columns: repeat(4,1fr);
  grid-auto-rows: 200px;
  grid-gap: 20px;
  position: relative;
}
.box3 {
  grid-column-start: 2;
  grid-column-end: 4;
  grid-row-start: 1;
  grid-row-end: 3;
  position: absolute;
  top: 40px;
  left: 40px;
}

grid-position

如果父容器有定位標識relative Or absolute等,那麼下麵的子item會根據原始它們應該佈局的位置定位,反之如果父容器沒有 定位標識,那麼應用了position: absolute;會脫離gird佈局,並且按照CSS傳統的方式佈局。

備註:

參考連接


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 算術運算符:+,-,*,/字元運算符 :+(字元串連接),+=(字元串連接複合);布爾運算符:!,&&,||;一元操作符:++,—,+,-;關係比較運算符: ,=,=,!=,==,====,!==;按位運算符: ~,&,,|,^,>,>>>賦值運算符:=,複合賦值(+=,-=,*=,/=)複合按位賦... ...
  • 老鐵們,我胡漢三又回來了,最近掃黃比較嚴,然後我就出去避了避風頭, 今天我們來總結總結對前端進行性能優化的方法吧,這篇隨筆沒啥代碼,但是我總結了總結,看一下總歸是有點用的 1.減少http請求 a.CSS sprites(精靈圖),即合併圖片,減少圖片請求次數 b.CSS,JS源碼壓縮。 c.cdn ...
  • 前端開發 前端開發前端概述一、什麼是前端二、前端開發技術棧HTMLCSSJavaScript前端三劍客一、HTML1、標記語言2、html 為前端頁面的主體,有標簽、指令與轉義字元等組成。3、html 發展史代表版本4、文檔類型二、CSS三、JavaScript1.編程語言2.js為前端頁面的腳步, ...
  • 一丶盒模型的屬性(重要) 1.padding padding是標準文檔流,父子之間調整位置 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>padding</title> <style> *{ padding: 0; margi ...
  • 微信發支付寶紅包(花唄) 原理很簡單,就是利用支付寶花唄的分享紅包到微信,微信用戶通過掃碼跳轉到支付寶領取。 第一步,獲得紅包地址 進入自己的支付寶紅包“天天領紅包”,分享二維碼到微信,然後用網上的識別二維碼的工具或網頁將支付寶二維碼解析一下,解析結果是一個url,例如: 這個就是支付寶天天領紅包的 ...
  • 使用 Array.includes 替代 Array.indexOf “如果需要在數組中查找某個元素,請使用 Array.indexOf。” 我記得在我學習 JavaScript 的課程中有類似的這麼一句話。毫無疑問,這完全正確! 在 MDN 文檔中,對 Array.indexOf 的描述是:返回在 ...
  • 這篇文章討論如何在基於Babylon.js的WebGL場景中,實現多個簡單卡牌類對象的顯示、選擇、分組、排序,同時建立一套實用的3D場景代碼框架。由於作者美工能力有限,所以示例場景視覺效果可能欠佳,本文的重點在於對相關技術的探討。 因為文章比較長,讀者可以考慮將網頁導出為mhtml格式,使用Word ...
  • 所謂的滑動門技術,就是指盒子背景能夠自動拉伸以適應不同長度的文本。即當文字增多時,背景看起來也會變長。 大多數應用於導航欄之中,如微信導航欄: 具體實現方法如下: 1、首先每一塊文本內容是由a標簽與span標簽組成 2、a標簽只指定高度,而不指定寬度。 3、a標簽 設置好背景圖後,指定一個paddi ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...