如何在CSS中創建網格?
定義網格容器需設置display: grid;2. 使用grid-template-columns和grid-template-rows設定行列大小;3. 採用fr、auto、%等靈活單位實現響應式佈局;4. 利用repeat()函數簡化重複的列或行定義;5. 結合minmax()與auto-fit/auto-fill實現自適應網格;6. 可選地通過grid-column、grid-row或grid-area精確控制項目位置;完整設置包括容器聲明、軌道定義、間隙添加及響應式優化,即可構建高效靈活的二維佈局。
Creating a grid in CSS is straightforward using CSS Grid Layout , a powerful two-dimensional layout system. Here's how to set it up step by step.

1. Define a Grid Container
To create a grid, start by applying display: grid
to a container element:
.container { display: grid; }
This turns the element into a grid container, and its direct children become grid items.

2. Set Up Columns and Rows
Use grid-template-columns
and grid-template-rows
to define the size and number of columns and rows.
Example: A 3-column grid with 2 rows

.container { display: grid; grid-template-columns: 100px 150px 100px; grid-template-rows: 80px 60px; gap: 10px; /* Adds space between grid items */ }
This creates:
- 3 columns (100px, 150px, 100px wide)
- 2 rows (80px and 60px tall)
- 10px gap between items
3. Use Flexible Units (like fr
, auto
, %
)
For responsive grids, use flexible units:
.container { display: grid; grid-template-columns: 1fr 2fr 1fr; /* Middle column twice as wide */ grid-template-rows: auto 100px; gap: 1rem; }
-
fr
= fraction of available space -
auto
= size based on content -
%
= percentage of container width/height
4. Use repeat()
for Simplicity
To avoid repetition, use the repeat()
function:
.container { display: grid; grid-template-columns: repeat(3, 1fr); /* 3 equal columns */ gap: 20px; }
You can also combine patterns:
grid-template-columns: repeat(3, 150px 1fr);
5. Make It Responsive
Use minmax()
and auto-fit
/ auto-fill
with repeat()
:
.container { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; }
- This creates as many columns as fit, each at least 200px wide, expanding equally.
- Great for responsive card layouts.
6. Place Items Explicitly (Optional)
You can position items precisely:
.item1 { grid-column: 1 / 3; /* spans from column 1 to 3 */ grid-row: 1 / 2; /* spans one row */ }
Or use grid-area
:
.item2 { grid-area: 2 / 1 / 3 / 4; /* row start / col start / row end / col end */ }
Basic HTML Example
<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 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; } .item { background: lightblue; padding: 20px; text-align: center; }
That's it. CSS Grid is flexible and efficient for both simple and complex layouts. Start with display: grid
, define your tracks, add gaps, and go responsive when needed. Basically just a few lines and you're set.
以上是如何在CSS中創建網格?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

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

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

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Autoprefixer是一個根據目標瀏覽器範圍自動為CSS屬性添加廠商前綴的工具。 1.它解決了手動維護前綴易出錯的問題;2.通過PostCSS插件形式工作,解析CSS、分析需加前綴的屬性、依配置生成代碼;3.使用步驟包括安裝插件、設置browserslist、在構建流程中啟用;4.注意事項有不手動加前綴、保持配置更新、非所有屬性都加前綴、建議配合預處理器使用。

TocreatestickyheadersandfooterswithCSS,useposition:stickyforheaderswithtopvalueandz-index,ensuringparentcontainersdon’trestrictit.1.Forstickyheaders:setposition:sticky,top:0,z-index,andbackgroundcolor.2.Forstickyfooters,betteruseposition:fixedwithbot

theconic-Gradient()functionIncsscreatesCircularGradientsThatRotateColorStopSaroundAcentralPoint.1.IsidealForPieCharts,ProgressIndicators,colordichers,colorwheels和decorativeBackgrounds.2.itworksbysbysbysbydefindefingincolordefingincolorstopsatspecificains off.

創建CSS加載旋轉器的方法有三種:1.使用邊框的基本旋轉器,通過HTML和CSS實現簡單動畫;2.使用多個點的自定義旋轉器,通過不同延遲時間實現跳動效果;3.在按鈕中添加旋轉器,通過JavaScript切換類來顯示加載狀態。每種方法都強調了設計細節如顏色、大小、可訪問性和性能優化的重要性,以提升用戶體驗。

Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the

要創建內在響應式網格佈局,核心方法是使用CSSGrid的repeat(auto-fit,minmax())模式;1.設置grid-template-columns:repeat(auto-fit,minmax(200px,1fr))讓瀏覽器自動調整列數並限制每列最小和最大寬度;2.使用gap控制格子間距;3.容器應設為相對單位如width:100%、配合box-sizing:border-box避免寬度計算錯誤並用margin:auto居中;4.可選設置行高與內容對齊方式提升視覺一致性,如row

要讓整個網格佈局在視口中居中顯示,可通過以下方法實現:1.使用margin:0auto實現水平居中,需設定容器固定寬度,適用於固定佈局;2.利用Flexbox在外層容器設置justify-content和align-items屬性,結合min-height:100vh可實現垂直和水平居中,適合全屏展示場景;3.直接使用CSSGrid的place-items屬性在父容器上快速居中,簡潔且現代瀏覽器支持良好,同時需確保父容器有足夠高度。每種方式均有適用場景和限制,根據實際需求選擇合適的方案即可。

prainuredetectionIncsssusissuse@supportScheckSifabRowsEsuppecifortSupecifortEfeatureBeforeApplyingReplyingStyles.1.itusesconditionalcsssssbasssbasedonproperty-valueperty-valuepairs,suessas@supports@supports@supports@supports(display:grid)
