HTML tutorial: How to use Grid layout for page layout

HTML Tutorial: How to use Grid layout for page layout
From the past table layout to the current Flex layout, CSS has been developing and evolving in page layout. Now, CSS Grid layout has become a powerful and flexible layout method. In this tutorial, we will learn how to use CSS Grid layout to create complex and beautiful page layouts.
CSS Grid layout is a two-dimensional grid system that allows us to divide the page into multiple rows and columns, and then place content into these rows and columns. Although Grid layout is still a relatively new feature, it is already supported by modern browsers and is widely used in practical applications.
Before we begin, we need to understand the basic terms and concepts of Grid layout. A Grid layout consists of the following main components:
- Container (Container): contains the parent element of all Grid items. Enable Grid layout by setting the element's
displayproperty togridorinline-grid. - Row: The horizontal part of the Grid layout. You can define the size and number of rows by setting the
grid-template-rowsof the container. - Column: The vertical part of the Grid layout. You can define the size and number of columns by setting the
grid-template-columnsof the container. - Grid Item: An element placed in the grid. Elements written directly in the container are automatically treated as Grid items.
Now let’s get hands-on, assuming we want to create a simple grid layout with a header, a sidebar, and a main content area.
-
Create HTML structure:
<!DOCTYPE html> <html> <head> <title>Grid布局教程</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="container"> <header>头部</header> <aside>侧边栏</aside> <main>主要内容区域</main> </div> </body> </html>Copy after login Define Grid layout in CSS:
.container { display: grid; grid-template-rows: 100px auto; grid-template-columns: 200px 1fr; grid-gap: 10px; } header { grid-row: 1; grid-column: 1 / span 2; background-color: #f2f2f2; padding: 20px; } aside { grid-row: 2; grid-column: 1; background-color: #e9e9e9; padding: 20px; } main { grid-row: 2; grid-column: 2; background-color: #d9d9d9; padding: 20px; }Copy after login
In In the above code, we first set up the Grid layout in the container and defined two rows and two columns. The header element occupies two columns of the first row, and the sidebar and main content occupy the first and second columns of the second row respectively. At the same time, we also set the spacing between grid items to 10 pixels.
Through this simple example, we can see the power of Grid layout. In addition to specifying the size and number of rows and columns, we can also specify the position of grid items through the grid-row and grid-column properties to make the layout more flexible.
In addition to the basic usage above, Grid layout also provides more powerful functions, such as adaptive size, automatic filling of space, etc. For those who want to learn and master Grid layout in depth, you can learn more about grid-template-areas, grid-auto-rows, grid-auto-columns and other attributes.
I hope that through this tutorial, you can have a preliminary understanding and mastery of how to use CSS Grid layout for page layout. By using Grid layout flexibly, you can create unique, beautiful and responsive web page layouts. Have fun with CSS layout!
The above is the detailed content of HTML tutorial: How to use Grid layout for page 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
1386
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.
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
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
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.
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
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
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.


