跳至主要内容

3 篇文章 含有標籤「Grid」

檢視所有標籤

Grid 網頁排版技巧入門教學筆記 | 學習筆記

· 閱讀時間約 5 分鐘
kdchang

前言

在現今網頁設計中,排版的結構與呈現方式對於用戶體驗至關重要。隨著網頁設計的演進,CSS Grid 成為了最強大且靈活的排版工具之一。透過 CSS Grid,你可以輕鬆創建複雜的佈局,並且適應不同設備的需求。本文將介紹 CSS Grid 的基本概念與使用技巧,幫助你迅速掌握如何在網頁設計中使用 Grid。

1. 什麼是 CSS Grid?

CSS Grid Layout(簡稱 Grid)是 CSS3 的一個強大功能,允許開發者以列和行的方式來設計網頁佈局。它使得網頁設計更加直觀和靈活,不再需要依賴浮動(float)或定位(position)等老舊技巧,簡化了許多複雜的布局問題。

Grid 是由「容器」和「項目」兩部分組成。容器定義了網格的結構,而項目則是容器內部的元素。你可以在容器內輕鬆地將項目放置在指定的網格區域中,從而創建各種排版樣式。

2. CSS Grid 的基本語法

在使用 CSS Grid 時,首先需要定義一個容器元素並啟用 Grid 布局。這樣,容器內的子元素將成為 Grid 項目。以下是基本的設置步驟:

.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
gap: 10px;
}
  • display: grid; 啟用容器的 Grid 布局。
  • grid-template-columns: repeat(3, 1fr); 設定三列,每列的寬度為等比例(1fr)。1fr 代表容器寬度的 1/3。
  • grid-template-rows: auto; 使得行的高度根據內容自動調整。
  • gap: 10px; 定義網格項目之間的間距。

3. 定義 Grid 容器的列和行

你可以使用 grid-template-columnsgrid-template-rows 來設置容器內列和行的數量、大小及比例。

定義列

例如,若想創建四列的佈局,可以這樣寫:

.container {
display: grid;
grid-template-columns: 200px 300px 400px 1fr;
}

這裡,我們設置了四列的寬度,前三列有固定的像素寬度,最後一列使用 1fr,即佔據剩餘的可用空間。

定義行

類似地,你可以設置行的大小。若要讓每一行的高度根據內容自動調整,可以使用:

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

這表示網格容器有兩行,第一行的高度固定為 100px,第二行的高度將根據內容自動調整。

4. 放置 Grid 項目

Grid 容器的子元素會自動成為 Grid 項目。你可以使用 grid-columngrid-row 屬性來指定某個項目占據的列與行。

例子
.item1 {
grid-column: 1 / 3;
grid-row: 1;
}

這表示 .item1 元素會從第 1 列跨越到第 3 列,占據第一行。

  • grid-column: 1 / 3; 表示該元素會從第 1 列起,跨越兩列,直到第 3 列結束。
  • grid-row: 1; 表示該元素位於第 1 行。

5. 使用 fr 單位設置比例

CSS Grid 中最重要的單位之一是 fr(fraction,分數),它讓你可以設定元素在可用空間中所佔比例。

假設你有三列,且希望第一列占 1 部分,第二列占 2 部分,第三列占 3 部分:

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

這樣會將總寬度分成六等份,其中第一列占 1 份,第二列占 2 份,第三列占 3 份。這樣的佈局適合於動態響應式設計。

6. 設置間距

除了 gap 屬性,你還可以為列與行的間距分別設置不同的值:

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

這裡,gap: 10px 20px; 的第一個數值設定了行之間的間距為 10px,第二個數值設定了列之間的間距為 20px。

7. 嵌套 Grid

Grid 允許你在網格項目內再次使用 Grid 佈局,這樣可以創建更複雜的排版結構。例如,假設你在某個項目內再創建一個小的網格:

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

在這個範例中,.item1 是一個 Grid 項目,它內部再次使用 Grid 排版,定義兩列,其中第一列占據 1 部分,第二列占據 2 部分。

8. 響應式設計

Grid 讓你輕鬆應對不同設備的佈局需求。你可以使用媒體查詢來調整 Grid 排版。例如:

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

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

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

這段程式碼會根據螢幕寬度變化調整容器的列數:在寬度大於 768px 時顯示四列,在寬度小於 768px 時顯示兩列,在寬度小於 480px 時顯示單列。

9. 小結

CSS Grid 為現代網頁設計提供了一個簡單、強大且靈活的排版解決方案。它不僅能夠解決過去使用浮動或定位方法無法達成的複雜佈局,還能輕鬆適應響應式設計的需求。掌握 CSS Grid,你將能夠更高效地創建現代化的網頁佈局,提升設計的精確度與可維護性。希望本文的介紹能幫助你快速入門並將這項技術應用到你的項目中。

CSS Flexbox align-items 和 align-content 入門教學筆記 | 學習筆記

· 閱讀時間約 3 分鐘
kdchang

在 CSS Flexbox 和 Grid 佈局中,align-itemsalign-content 兩者都與「對齊」有關,但適用的情境不同(單行 vs 多行):

1. align-items

  • 作用於單行內容(單行 Flexbox 或 Grid 容器內的項目)。
  • 控制子元素在**交叉軸(cross-axis)**上的對齊方式。

常見值

說明
stretch預設值,子元素會拉伸填滿容器的交叉軸
flex-start對齊交叉軸的起始點
flex-end對齊交叉軸的結束點
center置中對齊
baseline以文字基線(baseline)對齊

範例

.container {
display: flex;
align-items: center; /* 子元素在交叉軸上置中對齊 */
height: 200px;
border: 1px solid black;
}

.item {
width: 50px;
height: 50px;
background-color: lightblue;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
</div>

效果item 會在 container 的交叉軸(垂直方向)置中對齊。


2. align-content

  • 作用於多行內容(當 flex-wrap: wrap 或 Grid 有多行時)。
  • 控制整體行的對齊,而不是單個項目。

常見值

說明
stretch預設值,行會拉伸填滿容器
flex-start行對齊交叉軸的起始點
flex-end行對齊交叉軸的結束點
center行置中對齊
space-between第一行靠起始點,最後一行靠結束點,其他行平均分布
space-around每行之間有相等的間距
space-evenly每行之間及兩側間距相等

範例

.container {
display: flex;
flex-wrap: wrap;
align-content: center; /* 整體行群組在交叉軸上置中 */
height: 400px;
border: 1px solid black;
}

.item {
width: 100px;
height: 100px;
background-color: lightcoral;
margin: 5px;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>

效果:當 .container 高度足夠時,多行 .item 會整體置中排列。


總結

屬性影響對象適用於主要功能
align-items子元素單行(未換行)控制子元素在交叉軸上的對齊
align-content整體行多行(flex-wrap: wrap控制多行的整體對齊

如果容器內只有一行align-content 通常不會有影響,這時候應該使用 align-items

需要 align-content 發揮作用時,請確保:

  1. 容器有 flex-wrap: wrap;(多行)
  2. 容器有足夠的高度(不然內容會自動填滿)

Grid Garden 入門教學筆記 | 學習筆記

· 閱讀時間約 11 分鐘
kdchang

Grid(Grid Layout)是一種 CSS3 佈局模式,專門用來設計一維的彈性佈局,適用於水平或垂直排列元素,使網頁排版更加靈活且易於維護。

Grid Garden 透過花圃澆花遊戲化方式去介紹 Grid 使用方式

Level 1

Welcome to Grid Garden, where you write CSS code to grow your carrot garden! Water only the areas that have carrots by using the grid-column-start property.

For example, grid-column-start: 3; will water the area starting at the 3rd vertical grid line, which is another way of saying the 3rd vertical border from the left in the grid.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column-start: 3;
}

Level 2

Uh oh, looks like weeds are growing in the corner of your garden. Use grid-column-start to poison them. Note that the weeds start at the 5th vertical grid line.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#poison {
grid-column-start: 5;
}

Level 3

When grid-column-start is used alone, the grid item by default will span exactly one column. However, you can extend the item across multiple columns by adding the grid-column-end property.

Using grid-column-end, water all of your carrots while avoiding the dirt. We don't want to waste any water! Note that the carrots start at the 1st vertical grid line and end at the 4th.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column-start: 1;
grid-column-end: 4;
}

Level 4

When pairing grid-column-start and grid-column-end, you might assume that the end value has to be greater than the start value. But this turns out not the case!

Try setting grid-column-end to a value less than 5 to water your carrots.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column-start: 5;
grid-column-end: 2;
}

Level 5

If you want to count grid lines from the right instead of the left, you can give grid-column-start and grid-column-end negative values. For example, you can set it to -1 to specify the first grid line from the right.

Try setting grid-column-end to a negative value.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column-start: 1;
grid-column-end: -2;
}

Level 6

Now try setting grid-column-start to a negative value.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#poison {
grid-column-start: 4;
}

Level 7

Instead of defining a grid item based on the start and end positions of the grid lines, you can define it based on your desired column width using the span keyword. Keep in mind that span only works with positive values.

For example, water these carrots with the rule grid-column-end: span 2;.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column-start: 2;
grid-column-end: 4;
}

Level 8

Try using grid-column-end with the span keyword again to water your carrots.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column-start: 1;
grid-column-end: 6;
}

Level 9

You can also use the span keyword with grid-column-start to set your item's width relative to the end position.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column-start: 3;
grid-column-end: 6;
}

Level 10

Typing both grid-column-start and grid-column-end every time can get tiring. Fortunately, grid-column is a shorthand property that can accept both values at once, separated by a slash.

For example, grid-column: 2 / 4; will set the grid item to start on the 2nd vertical grid line and end on the 4th grid line.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column: 4/6;
}

Level 11

Try using grid-column to water these carrots. The span keyword also works with this shorthand property so give it a try!

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column: 2/5;
}

Level 12

One of the things that sets CSS grids apart from flexbox is that you can easily position items in two dimensions: columns and rows. grid-row-start works much like grid-column-start except along the vertical axis.

Use grid-row-start to water these carrots.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-row-start: 3;
}

Level 13

Now give the shorthand property grid-row a try.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-row: 3/6;
}

Level 14

Use grid-column and grid-row at the same time to set position in both dimensions.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#poison {
grid-column: 2;
grid-row: 5;
}

Level 15

You can also use grid-column and grid-row together to span larger areas within the grid. Give it a try!

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column: 2/6;
grid-row: 1/6;
}

Level 16

If typing out both grid-column and grid-row is too much for you, there's yet another shorthand for that. grid-area accepts four values separated by slashes: grid-row-start, grid-column-start, grid-row-end, followed by grid-column-end.

One example of this would be grid-area: 1 / 1 / 3 / 6;.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-area: 1 / 2 / 4 / 6;
}

Level 17

How about multiple items? You can overlap them without any trouble. Use grid-area to define a second area that covers all of the unwatered carrots.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water-1 {
grid-area: 1 / 4 / 6 / 5;
}

#water-2 {
grid-area: 2 / 3 / 5 / 6;
}

Level 18

If grid items aren't explicitly placed with grid-area, grid-column, grid-row, etc., they are automatically placed according to their order in the source code. We can override this using the order property, which is one of the advantages of grid over table-based layout.

By default, all grid items have an order of 0, but this can be set to any positive or negative value, similar to z-index.

Right now, the carrots in the second column are being poisoned and the weeds in the last column are being watered. Change the order value of the poison to fix this right away!

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

.water {
order: 0;
}

#poison {
order: 1;
}

Level 19

Now the water and poison are alternating, even though all of the weeds are at the start of your garden. Set the order of the poisons to remedy this.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

.water {
order: 0;
}

.poison {
order: -1;
}

Level 20

Up to this point, you've had your garden set up as a grid with five columns, each 20% of the full width, and five rows, each 20% of the full height.

This was done with the rules grid-template-columns: 20% 20% 20% 20% 20%; and grid-template-rows: 20% 20% 20% 20% 20%; Each rule has five values which create five columns, each set to 20% of the overall width of the garden.

But you can set the grid up however you like. Give grid-template-columns a new value to water your carrots. You'll want to set the width of the 1st column to be 50%.

#garden {
display: grid;
grid-template-columns: 50% 10% 20% 20%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column: 1;
grid-row: 1;
}

Level 21

Specifying a bunch of columns with identical widths can get tedious. Luckily there's a repeat function to help with that.

For example, we previously defined five 20% columns with the rule grid-template-columns: 20% 20% 20% 20% 20%;. This can be simplified as grid-template-columns: repeat(5, 20%);

Using grid-template-columns with the repeat function, create eight columns each with 12.5% width. This way you won't overwater your garden.

#garden {
display: grid;
grid-template-columns: repeat(1, 12.5%);
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-column: 1;
grid-row: 1;
}

Level 22

grid-template-columns doesn't just accept values in percentages, but also length units like pixels and ems. You can even mix different units together.

Here, set three columns to 100px, 3em, and 40% respectively.

#garden {
display: grid;
grid-template-columns: 100px 3em 40%;
grid-template-rows: 20% 20% 20% 20% 20%;
}

Level 23

Grid also introduces a new unit, the fractional fr. Each fr unit allocates one share of the available space. For example, if two elements are set to 1fr and 3fr respectively, the space is divided into 4 equal shares; the first element occupies 1/4 and the second element 3/4 of any leftover space.

Here, weeds make up the left 1/6 of your first row and carrots the remaining 5/6. Create two columns with these widths using fr units.

#garden {
display: grid;
grid-template-columns: 1fr 5fr;
grid-template-rows: 20% 20% 20% 20% 20%;
}

Level 24

When columns are set with pixels, percentages, or ems, any other columns set with fr will divvy up the space that's left over.

Here the carrots form a 50 pixel column on the left, and the weeds a 50 pixel column on the right. With grid-template-columns, create these two columns, and use fr to make three more columns that take up the remaining space in between.

#garden {
display: grid;
grid-template-columns: 50px 1fr 1fr 1fr 50px;
grid-template-rows: 20% 20% 20% 20% 20%;
}

#water {
grid-area: 1 / 1 / 6 / 2;
}

#poison {
grid-area: 1 / 5 / 6 / 6;
}

Level 25

Now there is a 75 pixel column of weeds on the left side of your garden. 3/5 of the remaining space is growing carrots, while 2/5 has been overrun with weeds.

Use grid-template-columns with a combination of px and fr units to make the necessary columns.

#garden {
display: grid;
grid-template-columns: 75px 3fr 2fr;
grid-template-rows: 100%;
}

Level 26

grid-template-rows works much the same as grid-template-columns.

Use grid-template-rows to water all but the top 50 pixels of your garden. Note that the water is set to fill only your 5th row, so you'll need to create 5 rows in total.

#garden {
display: grid;
grid-template-columns: 20% 20% 20% 20% 20%;
grid-template-rows: 12.5px 12.5px 12.5px 12.5px;
}

#water {
grid-column: 1 / 6;
grid-row: 5 / 6;
}

Level 27

grid-template is a shorthand property that combines grid-template-rows and grid-template-columns.

For example, grid-template: 50% 50% / 200px; will create a grid with two rows that are 50% each, and one column that is 200 pixels wide.

Try using grid-template to water an area that includes the top 60% and left 200 pixels of your garden.

#garden {
display: grid;
grid-template: 60% / 200px;
}

#water {
grid-column: 1;
grid-row: 1;
}

Level 28

Your garden is looking great. Here you've left a 50 pixel path at the bottom of your garden and filled the rest with carrots.

Unfortunately, the left 20% of your carrots have been overrun with weeds. Use CSS grid one last time to treat your garden.

#garden {
display: grid;
grid-template: 1fr 50px / 1fr 4fr;
}

參考文件

  1. Playing CSS Grid Garden with Answers Explained