search
HomeWeb Front-endHTML TutorialHTML Tutorial: How to Use Grid Layout for Grid Adaptive Grid Layout

HTML Tutorial: How to Use Grid Layout for Grid Adaptive Grid Layout

Oct 27, 2023 pm 06:28 PM
htmlgrid layoutGrid adaptive

HTML Tutorial: How to Use Grid Layout for Grid Adaptive Grid Layout

HTML tutorial: How to use Grid layout for grid adaptive grid layout, specific code examples are required

Introduction:
With the development of the Internet, web pages Layout is becoming increasingly important. Traditional web page layout methods, such as using tables or floating layouts, often require a lot of code and adjustments to achieve adaptive effects. The Grid layout introduced in CSS3 provides a more concise and flexible way to build a grid-adaptive grid layout. This article will introduce you to the basic concepts and practical applications of Grid layout, and provide you with specific code examples.

  1. Grid layout introduction
    Grid layout is a new grid layout system in CSS3. It can divide the content of the web page into rows and columns to create a grid layout. Grid layout can be implemented through two parts: grid container and grid item. The grid container is the parent element containing the grid item, and the grid item is the child element under the grid container.
  2. Create a grid container
    First, we need to define an element as a grid container. In HTML, you can use a <div> element to act as a grid container. As shown below: <pre class='brush:php;toolbar:false;'>&lt;div class=&quot;container&quot;&gt; &lt;!-- 网格项 --&gt; &lt;div class=&quot;item1&quot;&gt;1&lt;/div&gt; &lt;div class=&quot;item2&quot;&gt;2&lt;/div&gt; &lt;div class=&quot;item3&quot;&gt;3&lt;/div&gt; &lt;div class=&quot;item4&quot;&gt;4&lt;/div&gt; &lt;/div&gt;</pre><ol start="3"><li>Define grid layout<br>By setting CSS properties for the grid container<code>display: grid;, we can define it as a grid Grid layout. In addition, you can also use the grid-template-columns and grid-template-rows properties to specify the number of columns and rows of the grid. For example, the following code defines the grid container as a grid layout with 3 columns and 2 rows.
.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}
  1. Set the position and size of the grid item
    By setting the CSS properties grid-column and grid-row## for the grid item #, we can specify the position of each grid item in the grid layout. For example, the following code would place grid item 1 in the first row of the first column, grid item 2 in the first row of the second column, grid item 3 in the second row of the third column, and grid item 4 in the Second row of first column.
  2. .item1 {
      grid-column: 1;
      grid-row: 1;
    }
    
    .item2 {
      grid-column: 2;
      grid-row: 1;
    }
    
    .item3 {
      grid-column: 3;
      grid-row: 2;
    }
    
    .item4 {
      grid-column: 1;
      grid-row: 2;
    }
    Adaptive Grid Layout
  1. In Grid layout, the size and position of grid items can be adjusted adaptively. We can use the
    grid-template-areas attribute to specify the position of each grid item in the grid layout and represent spaces by using the . character. For example, the following code defines the grid container as a grid layout with two columns and two rows, and places each grid item in a different position.
  2. .container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-template-rows: 1fr 1fr;
      grid-template-areas:
        "header header"
        "sidebar main";
    }
    
    .item1 {
      grid-area: header;
    }
    
    .item2 {
      grid-area: sidebar;
    }
    
    .item3 {
      grid-area: main;
    }
    Responsive Grid Layout
  1. Grid layout can also easily implement responsive grid layout. We can use CSS media queries to adjust the style of the grid layout according to different screen sizes and device types. For example, the following code will change the grid layout to a single column layout when the window is smaller than 600px.
  2. .container {
      display: grid;
      grid-template-columns: 1fr;
      grid-template-rows: 1fr;
    }
    
    @media screen and (min-width: 600px) {
      .container {
        grid-template-columns: 1fr 1fr;
        grid-template-rows: 1fr 1fr;
      }
    }
Conclusion:

Grid layout provides a concise and flexible way to implement grid-adaptive grid layout. By using grid containers and grid items, we can easily create complex web layouts and adapt them to different screen sizes and device types with adaptive and responsive features. I hope this article can help you understand and apply Grid layout and write a more flexible and beautiful web page layout.

Reference code:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="style.css">
  <style>
    .container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-template-rows: 1fr 1fr;
    }

    .item1 {
      grid-column: 1;
      grid-row: 1;
      background-color: red;
    }

    .item2 {
      grid-column: 2;
      grid-row: 1;
      background-color: green;
    }

    .item3 {
      grid-column: 1 / span 2;
      grid-row: 2;
      background-color: blue;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="item1">1</div>
    <div class="item2">2</div>
    <div class="item3">3</div>
  </div>
</body>
</html>

CSS file (style.css):

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

.item1 {
  grid-column: 1;
  grid-row: 1;
  background-color: red;
}

.item2 {
  grid-column: 2;
  grid-row: 1;
  background-color: green;
}

.item3 {
  grid-column: 1 / span 2;
  grid-row: 2;
  background-color: blue;
}

The above is an HTML tutorial on how to use Grid layout for grid-adaptive grid layout. Hope it helps. Remember, flexible use of Grid layout can bring better user experience and aesthetics to your web pages. Start trying to use Grid layout!

The above is the detailed content of HTML Tutorial: How to Use Grid Layout for Grid Adaptive Grid Layout. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.