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

HTML tutorial: How to use Grid layout for page layout

Oct 21, 2023 am 11:43 AM
html Page Layout grid layout

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:

  1. Container (Container): contains the parent element of all Grid items. Enable Grid layout by setting the element's display property to grid or inline-grid.
  2. Row: The horizontal part of the Grid layout. You can define the size and number of rows by setting the grid-template-rows of the container.
  3. Column: The vertical part of the Grid layout. You can define the size and number of columns by setting the grid-template-columns of the container.
  4. 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.

  1. 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
  2. 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!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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