HTML tutorial: How to use Grid layout for adaptive grid item layout

WBOY
Release: 2023-10-18 09:38:05
Original
997 people have browsed it

HTML tutorial: How to use Grid layout for adaptive grid item layout

HTML tutorial: How to use Grid layout for adaptive grid item layout

In modern web design, adaptive layout is crucial. Through adaptive layout, web pages can present the best display effect on different devices and screens, providing a better user experience. In this regard, CSS Grid layout is a powerful tool that can help us achieve adaptability in web page layout.

This article will introduce how to use Grid layout to implement adaptive grid item layout, and provide specific code examples.

First, create a basic structure in HTML. We use a div container as the parent element of the Grid layout, which contains multiple child elements as items in the grid. The following is the HTML structure of an example:

<div class="grid-container">
  <div class="grid-item">Item 1</div>
  <div class="grid-item">Item 2</div>
  <div class="grid-item">Item 3</div>
  <div class="grid-item">Item 4</div>
  <div class="grid-item">Item 5</div>
  <div class="grid-item">Item 6</div>
</div>
Copy after login

Next, we need to define the Grid layout in CSS. By setting display: grid, we can set the container element to Grid layout. In this example, we set the container's class to grid-container:

.grid-container {
  display: grid;
}
Copy after login

Then, we can use the grid-template-columns attribute to define the grid of columns. Here is an example that divides the container into three equal-width columns:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}
Copy after login

In this example, 1fr represents a scalable unit, indicating that each column has equal width.

In addition, we can also use the grid-template-rows attribute to define the rows of the grid. Here is an example that defines the grid as three rows and three columns:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
}
Copy after login

If we want to set the spacing of each grid item, we can use the grid-gap property. Here is an example that sets the spacing between each grid item to 20 pixels:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-gap: 20px;
}
Copy after login

In addition to equal-width columns, we can also set the width of each column using a specific width value. Here's an example that sets the width of the first column to 200 pixels, and the second and third columns to automatic width:

.grid-container {
  display: grid;
  grid-template-columns: 200px auto auto;
  grid-template-rows: 1fr 1fr 1fr;
  grid-gap: 20px;
}
Copy after login

In this example, the width of the first column is fixed at 200 pixels, while The width of the second and third columns will automatically fit into the remaining space.

In addition, we can also define the height of the automatic rows by setting the grid-auto-rows attribute. Here is an example that sets the height of the autorow to a minimum height of 50 pixels:

.grid-container {
  display: grid;
  grid-template-columns: 200px auto auto;
  grid-template-rows: 1fr 1fr 1fr;
  grid-gap: 20px;
  grid-auto-rows: minmax(50px, auto);
}
Copy after login

In this example, the height of the autorow will automatically adjust based on the height of the content, but the minimum height will not be less than 50 pixels.

Finally, we can also use the grid-template-areas attribute to define the layout of the grid area. Here is an example of defining a grid as a layout with four areas:

.grid-container {
  display: grid;
  grid-template-areas: 
    "header header"
    "content sidebar"
    "content sidebar"
    "footer footer";
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto 1fr auto;
  grid-gap: 20px;
}
Copy after login

In this example, we divide the grid area into four parts: header, content, sidebar, and footer. Each area will be placed according to the defined layout.

Through the above code example, we can use Grid layout to implement adaptive grid item layout. By setting different properties and values, we can flexibly control the number of columns, rows, width and height of the grid to achieve various complex layout effects.

I hope this article will help you understand and use Grid layout. Once again, CSS Grid layout is a very powerful tool that can greatly simplify the implementation of web page layout and improve development efficiency.

The above is the detailed content of HTML tutorial: How to use Grid layout for adaptive grid item layout. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template