search
HomeWeb Front-endHTML TutorialCSS3 网格布局(grid layout)基础知识

网格模板(grid-template)属性及其普通写法(longhands)定义了一个固定数量的轨道,构成显式网格。 

当网格项目定位在这些界限之外,网格容器通过增加隐式网格线生成隐式网格轨道。 

这些隐含的和显式的网格线一起构成隐式网格(implicit grid)。 

隐式网格轨道的尺寸由网格自动行(grid-auto-rows)和网格自动列(grid-auto-columns)属性来确定。


网格自动流(grid-auto-flow)属性用来控制无明确位置的网格项的自动定位(auto-placement), 

一旦显式网格被填满(或者如果没有显式的网格),auto-placement也将导致隐式网格轨道的生成。

自动生成行和列的尺寸:grid-auto-rows 和 grid-auto-columns 属性



如果一个网格项被放到一个未明确定义尺寸的行或列中,隐式网格轨道将被创建来容纳它。 

这在两种情况下会发生:通过显式定位到一个超出范围的行或列中,或是由自动定位算法创建额外的行或列。 

网格自动列和网格自动行属性用来指定这样的隐式创建的轨道的大小。


我们仍然通过实例来学习这个属性的用法:

#grid {    display: grid;    grid-template-columns: 20px;    grid-template-rows: 20px }  #A { grid-column: 1;          grid-row: 1; }  #B { grid-column: 5;          grid-row: 1 / span 2; }  #C { grid-column: 1 / span 2; grid-row: 2; }

<div id="grid">  <div id="A">A</div>  <div id="B">B</div>  <div id="C">C</div>
</div>

如果一个网格项被放到一个未明确定义尺寸的行或列中,隐式网格轨道将被创建来容纳它。 

这在两种情况下会发生:通过显式定位到一个超出范围的行或列中,或是由自动定位算法创建额外的行或列。 

网格自动列和网格自动行属性用来指定这样的隐式创建的轨道的大小。


上面这个例子演示了隐式网格轨道的使用。本例中共创建了2行5列,其中显式声明的只有第1行/第1列,除此之外有1个隐含行和4个隐含列。 

网格项B位于网格线5,这将自动创建4个隐式网格列,但是由于列3和4中没有任何网格项,所以其宽度塌缩为0。


你可以通过 在线实例自己测试下。(请使用Chrome打开)

注意:由于网格布局是制定中的W3规范,所以浏览器尚未完全支持,目前Chrome已支持。

自动布局:网格自动流(grid-auto-flow)属性



未明确指定位置的网格项将通过自动定位算法来自动放置到网格容器的未占用空间中。 

网格自动流就是用来控制自动布局算法如何工作的,指定如何把自动布局的网格项放进网格中。


row
自动布局算法把网格项按顺序填充到每一行,必要时添加新行。这是grid-auto-flow的缺省值。
column
自动布局算法把网格项按顺序填充到每一列,必要时添加新列。
dense
如果指定了dense,将使用“密集”填充算法,它试图把排在后面的体积小的网格项填充到“大个头”网格留下的空隙中,就像我们在汽车后备箱塞物品一样。这有可能会导致填充顺序和代码顺序不同。
如果未指定,则将使用“稀疏”填充算法,只是按顺序摆放,不回溯填空。这可以确保所有网格项保持原有的代码序,但有可能会出现页面空间浪费。


同样的我们通过 在线实例来学习。其中代码注释已经讲得很清楚,这里不再重复。


by iefreer

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools