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
andgrid-column-end: define the column start and end of grid items Location. - grid-row-start
andgrid-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
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
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
1389
52
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.
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 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.
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
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.
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 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
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.


