HTML tutorial: How to use Grid layout for multi-column layout
HTML tutorial: How to use Grid layout for multi-column layout
Grid layout is one of the commonly used layout methods in modern web design. It can divide web page content into Multiple columns and rows for flexible multi-column layouts. This article will introduce how to use Grid layout to create a multi-column web page layout and provide specific code examples.
1. Introduction to Grid Layout
Grid layout is a two-dimensional layout system that can divide web content into multiple grid areas. These grid areas can be defined and arranged according to design needs, making the layout of web pages more flexible and free. Grid layout implements layout by setting grid containers and grid items, and adjusts it by specifying the row and column size, position, and spacing of the grid.
2. Create a grid container
To use Grid layout, you first need to create a grid container in the HTML document. You can use the <div> element as a grid container, and then define the layout of the container through CSS styles. <p>The sample code is as follows: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><div class="grid-container">
... 网格项内容 ...
</div></pre><div class="contentsignin">Copy after login</div></div><p>3. Set grid layout</p><p>In the style sheet, you can enable Grid layout by specifying the display attribute of the grid container as grid. You can use grid templates to define the grid's row and column size, position, and spacing. </p><p>The sample code is as follows: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 三列等宽 */
grid-template-rows: auto; /* 自动调整行高 */
gap: 20px; /* 网格项间距为20px */
}</pre><div class="contentsignin">Copy after login</div></div><p>In the above code, <code>grid-template-columns
specifies the number of columns of the grid and the width of each column, and ## is used here. #1fr unit, indicating that the width of each column is evenly distributed.
grid-template-rows specifies the row height of the grid. Use
auto to automatically adjust the row height.
gap specifies the spacing between grid items, here set to 20px.
<div class="grid-container"> <div class="grid-item">网格项1</div> <div class="grid-item">网格项2</div> <div class="grid-item">网格项3</div> </div>
.grid-item { background-color: #f5f5f5; padding: 20px; }
- grid-column-start
and
grid-column-end: define the column start and end of grid items Location.
- grid-row-start
and
grid-row-end: Define the starting and ending positions of the rows of grid items.
- grid-area
: Define the row and column positions of grid items at the same time.
- justify-self
: Defines the alignment of grid items in the column direction.
- align-self
: Defines the alignment of grid items in the row direction.
The above is the detailed content of HTML tutorial: How to use Grid layout for multi-column layout. For more information, please follow other related articles on the PHP Chinese website!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
