Home Web Front-end HTML Tutorial HTML tutorial: How to use Grid layout for multi-column layout

HTML tutorial: How to use Grid layout for multi-column layout

Oct 25, 2023 am 10:27 AM
html grid layout multi-column layout

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;'>&lt;div class=&quot;grid-container&quot;&gt; ... 网格项内容 ... &lt;/div&gt;</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.

4. Create grid items

Create grid items in the grid container. Grid items are cells in the grid layout and are used to accommodate web page content.

The sample code is as follows:

<div class="grid-container">
  <div class="grid-item">网格项1</div>
  <div class="grid-item">网格项2</div>
  <div class="grid-item">网格项3</div>
</div>
Copy after login

5. Define grid item styles

Define the layout and style of grid items through CSS styles.

The sample code is as follows:

.grid-item {
  background-color: #f5f5f5;
  padding: 20px;
}
Copy after login
In the above code, the background color of the grid item is defined as #f5f5f5, and the padding is 20px. Grid items can be styled according to actual needs.

6. Adjust layout

In grid layout, you can achieve different layout effects by adjusting the properties of grid containers and grid items. The following are some commonly used properties:

  1. grid-column-start and grid-column-end: define the column start and end of grid items Location.
  2. grid-row-start and grid-row-end: Define the starting and ending positions of the rows of grid items.
  3. grid-area: Define the row and column positions of grid items at the same time.
  4. justify-self: Defines the alignment of grid items in the column direction.
  5. align-self: Defines the alignment of grid items in the row direction.
7. Compatibility considerations

Although Grid layout is a commonly used layout method in modern Web design, compatibility issues need to be considered when using it. Some old browsers may not support Grid layout, and you need to use other layout methods or add compatibility code.

To sum up, this article introduces how to use Grid layout for multi-column layout and provides specific code examples. By learning and mastering Grid layout, you will be able to achieve more flexible and free web layout effects and improve your web design and development capabilities.

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!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

See all articles