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
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><pre class='brush:html;toolbar:false;'><div class="grid-container">
... 网格项内容 ...
</div></pre><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><pre class='brush:css;toolbar:false;'>.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 三列等宽 */
grid-template-rows: auto; /* 自动调整行高 */
gap: 20px; /* 网格项间距为20px */
}</pre><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>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; }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 layoutIn 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:
and
grid-column-end: define the column start and end of grid items Location.
and
grid-row-end: Define the starting and ending positions of the rows of grid items.
: Define the row and column positions of grid items at the same time.
: Defines the alignment of grid items in the column direction.
: 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!