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!

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

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


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
