搜尋
首頁web前端css教學CSS 網格 - 深入探討

CSS 網格 - 深入探討

Sep 07, 2024 am 06:41 AM

CSS Grid - A Deep Dive

第 9 講:CSS 網格 - 深入探討

歡迎來到《從基礎到輝煌》課程第九講。在本次講座中,我們將探索 CSS 網格,這是一個強大的佈局系統,可讓您輕鬆建立複雜的網頁佈局。雖然 Flexbox 非常適合單維佈局(行或列),但 CSS Grid 提供了二維佈局系統,使您能夠同時控制行和列。


什麼是 CSS 網格?

CSS 網格是 CSS 中的佈局系統,可以創建靈活的、響應式的、基於網格的佈局。它允許您將元素對齊到行和列中,比 Flexbox 提供更多對佈局結構的控制。


網格術語

在深入範例之前,讓我們先熟悉一些關鍵術語:

  • 網格容器:包含網格的父元素。
  • 網格項目:網格容器內的子元素。
  • 網格線:網格的水平和垂直分割線。
  • 網格軌道:兩條網格線之間的空間,形成行或列。
  • 網格單元:由行和列相交形成的網格中最小的單一單元。

1.基本網格結構

要開始使用 Grid,請將 display: grid 應用到容器。

  • 範例
  .grid-container {
    display: grid;
  }

一旦應用了display: grid,容器的子元素就成為網格項。


2.定義網格列和行

您可以使用 grid-template-columns 和 grid-template-rows 屬性定義網格的行數和列數。

  • 範例:建立一個 3 列 2 行的網格。
  .grid-container {
    display: grid;
    grid-template-columns: 100px 200px 100px;
    grid-template-rows: 50px 150px;
  }

這將建立一個網格:

  • 3 列:第一列寬 100px,第二列寬 200px,第三列寬 100px。
  • 2 行:第一行高 50 像素,第二行高 150 像素。

3.使用小數單位 (fr)

CSS Grid 引入了分數單位 fr,它表示網格容器中可用空間的一小部分。這是在網格項之間分配空間的靈活方式。

  • 範例:使用 fr 平均分割空間。
  .grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
  }

在此範例中,三列將具有相等的寬度,每列佔用可用空間的一小部分。


4.放置網格項目

您可以使用 grid-column 和 grid-row 屬性來控制每個網格項目的放置位置。這些屬性允許您指定項目的開始和結束位置。

  • 範例
  .grid-item {
    grid-column: 1 / 3; /* This item spans from column 1 to column 3 */
    grid-row: 1 / 2;    /* This item is placed in the first row */
  }

在這種情況下,網格項目將跨越前兩列,但會放置在第一行。


5.網格間隙

grid-gap 屬性在水平和垂直方向上新增網格項之間的空間。

  • 範例:在列和行之間新增間隙。
  .grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-gap: 20px;
  }

這會在所有網格項目之間建立相等的 20px 間隙。


6.自動調整與自動填充

自動調整和自動填充是強大的功能,允許網格根據容器的大小自動放置盡可能多的列。

  • 自動調整範例
  .grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  }

在這裡,網格將自動適應盡可能多的列,確保每列至少 100 像素寬,但可以增長以填充可用空間。


實際範例:簡單的網格佈局

讓我們使用 CSS Grid 建立一個簡單的網格佈局。

HTML:

<div class="grid-container">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>
</div>

CSS:

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

.grid-item {
  background-color: #ddd;
  padding: 20px;
  text-align: center;
}

在此範例中:

  • .grid-container 具有使用 Repeat(3, 1fr) 建立的三個等寬列。
  • 網格間隙設定為 10px 以在網格項目之間新增空間。
  • 每個 .grid-item 都應用了填充和背景顏色,以提高可見性。

7.嵌套網格

您也可以巢狀網格,其中網格項目本身成為網格容器。這允許更複雜的佈局。

  • 範例
  .nested-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-gap: 10px;
  }

您可以應用此概念在另一個網格內建立一個網格,以便更精細地控制佈局。


Responsive Design with CSS Grid

CSS Grid is great for responsive design. You can adjust the number of columns based on the screen size using media queries.

  • Example: Creating a responsive grid.
  .grid-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 10px;
  }

  @media screen and (max-width: 768px) {
    .grid-container {
      grid-template-columns: repeat(2, 1fr);
    }
  }

  @media screen and (max-width: 480px) {
    .grid-container {
      grid-template-columns: 1fr;
    }
  }

In this example:

  • The grid starts with three columns.
  • On screens smaller than 768px, the grid switches to two columns.
  • On screens smaller than 480px, the grid collapses to a single column.

Practice Exercise

  1. Create a webpage layout using CSS Grid with a header, main content, sidebar, and footer.
  2. Use grid-template-columns and grid-template-rows to define the grid structure.
  3. Make the layout responsive by adjusting the number of columns on different screen sizes.

Next Up: In the next lecture, we’ll explore Advanced CSS Grid Techniques, including grid areas, template layouts, and combining Grid with Flexbox. Stay tuned!


Follow Me on LinkedIn-

Ridoy Hasan

以上是CSS 網格 - 深入探討的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
CSS Flexbox與網格:全面評論CSS Flexbox與網格:全面評論May 12, 2025 am 12:01 AM

選擇Flexbox還是Grid取決於佈局需求:1)Flexbox適用於一維佈局,如導航欄;2)Grid適合二維佈局,如雜誌式佈局。兩者在項目中可結合使用,提升佈局效果。

如何包括CSS文件:方法和最佳實踐如何包括CSS文件:方法和最佳實踐May 11, 2025 am 12:02 AM

包含CSS文件的最佳方法是使用標籤在HTML的部分引入外部CSS文件。 1.使用標籤引入外部CSS文件,如。 2.對於小型調整,可以使用內聯CSS,但應謹慎使用。 3.大型項目可使用CSS預處理器如Sass或Less,通過@import導入其他CSS文件。 4.為了性能,應合併CSS文件並使用CDN,同時使用工具如CSSNano進行壓縮。

Flexbox vs Grid:我應該學習兩者嗎?Flexbox vs Grid:我應該學習兩者嗎?May 10, 2025 am 12:01 AM

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

軌道力學(或我如何優化CSS KeyFrames動畫)軌道力學(或我如何優化CSS KeyFrames動畫)May 09, 2025 am 09:57 AM

重構自己的代碼看起來是什麼樣的?約翰·瑞亞(John Rhea)挑選了他寫的一個舊的CSS動畫,並介紹了優化它的思維過程。

CSS動畫:很難創建它們嗎?CSS動畫:很難創建它們嗎?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@KeyFrames CSS:最常用的技巧@KeyFrames CSS:最常用的技巧May 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcsssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingMultatingMultationMultationProperPertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用CombanningWiThjavoFofofofoftofofo

CSS計數器:自動編號的綜合指南CSS計數器:自動編號的綜合指南May 07, 2025 pm 03:45 PM

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他們可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑戰挑戰InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)創造性

使用捲軸驅動動畫的現代滾動陰影使用捲軸驅動動畫的現代滾動陰影May 07, 2025 am 10:34 AM

使用滾動陰影,尤其是對於移動設備,是克里斯以前涵蓋的一個微妙的UX。傑夫(Geoff)涵蓋了一種使用動畫限制屬性的新方法。這是另一種方式。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!