CSS Grid layout (Grid) can divide web pages into rows and columns with simple attributes. You can directly use CSS to position and adjust each element in the grid, and there is no need to perform multi-layer embedding in order to achieve a certain layout. Set, in short, css grid layout is very easy to use. Let’s take a look at the content of css grid layout described in this article.
1. CSS Grid Layout (Grid)
CSS Grid layout consists of two core components: parent element and child element. The parent element is the actual grid (grid). Child elements are the content within the grid.
The following is a parent element and six child elements
<div> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </div>
To turn the parent element into a grid (grid), simply set its display attribute to grid
Effect Picture:
The following is the grid layout:
.box { width: 350px; height: 350px; /* background: #ccc; */ margin: 0 auto; grid-gap: 5px; display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; } .item { border: 1px solid black; box-sizing: border-box; } .div1 { grid-column-start: 1; grid-column-end: 3; /*(div1从占据从第一条网格线开始,到第三条网格线结束)*/ line-height: 100px; text-align: center; background: rgb(252, 0, 0); /* grid-column: 1/3;(这是缩写的形式) */ } .div2 { line-height: 100px; text-align: center; background: rgb(252, 134, 0); } .div3 { grid-row-start: 2; grid-row-end: 4; /* grid-row: 2/4;(这是缩写的形式) */ line-height: 200px; text-align: center; background: rgb(21, 207, 108); } .div4 { grid-column-start: 2; grid-column-end: 4; line-height: 100px; text-align: center; background: rgb(18, 21, 189); /* grid-column: 2/4;(这是缩写的形式) */ } .div5 { line-height: 100px; text-align: center; background: rgb(16, 170, 197); } .div6 { line-height: 100px; text-align: center; background: rgb(172, 126, 199); }
The grid lines in the above code (arrows in the figure) The place pointed to is a grid line):
2. Responsive grid layout
Similar to the above (with the following content added)
Use the grid-template-columns property to create a 12-column grid, each column is a unit width (1/12 of the total width)
Create 3 rows using the grid-template-rows property
Use the grid-gap property to add a gap between grid items in the grid.
The code is as follows:
<div class="container"> <div class="header">顶部(一个点表示一个空白的格子),所以距离左边和右边各有一个格子的距离。</div> <div class="menu">中间1</div> <div class="content">中间2所以可以利用空白的格子来对你所要拍的网页来进行布局</div> <div class="footer">底部(一个点表示一个空白的格子),所以距离右边有三个格子的距离。</div> </div>
Add grid-template-areas
This attribute is called the grid area, also called the template area, which allows us to easily conduct layout experiments.
Rendering:
.container { display: grid; grid-template-columns: repeat(12, 1fr); grid-template-rows: 50px 350px 50px; grid-gap: 5px; grid-template-areas: ". h h h h h h h h h h ." " m m c c c c c c c c c c" "f f f f f f f f f . . ."; } .container>p { border: 1px solid #ccc; box-sizing: border-box; } .header { text-align: center; line-height:50px; grid-area: h; color:rgb(219, 52, 169); } .menu { grid-area: m; text-align: center; line-height:350px; } .content { text-align: center; line-height:350px; grid-area: c; color:rgb(25, 158, 69); } .footer { color:rgb(212, 112, 18); text-align: center; line-height:50px; grid-area: f; }Recommended related articles:
Detailed explanation of Grid layout in CSS
Basic knowledge of CSS3 grid layout
The above is the detailed content of Introduction to two methods of CSS grid layout (Grid) (with code). For more information, please follow other related articles on the PHP Chinese website!

The React ecosystem offers us a lot of libraries that all are focused on the interaction of drag and drop. We have react-dnd, react-beautiful-dnd,

There have been some wonderfully interconnected things about fast software lately.

I can't say I use background-clip all that often. I'd wager it's hardly ever used in day-to-day CSS work. But I was reminded of it in a post by Stefan Judis,

Animating with requestAnimationFrame should be easy, but if you haven’t read React’s documentation thoroughly then you will probably run into a few things

Perhaps the easiest way to offer that to the user is a link that targets an ID on the element. So like...

Listen, I am no GraphQL expert but I do enjoy working with it. The way it exposes data to me as a front-end developer is pretty cool. It's like a menu of

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

I've recently noticed an interesting change on CodePen: on hovering the pens on the homepage, there's a rectangle with rounded corners expanding in the back.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.