亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
How is the CSS Grid layout different from other CSS layout methods?
How to get started with CSS Grid layouts?
Can I use CSS Grid layout with other CSS layout methods?
How to create a grid using CSS Grid layout?
How to place an item on a grid?
What are grid lines and grid tracks?
What is the fr unit in the CSS Grid layout?
How to make the grid responsive?
Can I nest my grid in my grid?
Does CSS Grid layouts be supported by all browsers?
Home Web Front-end CSS Tutorial Introducing the CSS Grid Layout

Introducing the CSS Grid Layout

Feb 21, 2025 am 09:35 AM

CSS Grid Layout: Build a powerful and flexible website layout

Core points

  • CSS Grid layouts provide a more powerful and flexible way to create complex website layouts without using properties such as inline and float or separate grid system stylesheets.
  • Currently, only IE 10 and Edge support CSS Grid layouts, but it can be enabled via specific flags in Chrome and Firefox or using polyfill.
  • CSS Grid layouts use units of measurement, lines, tracks, cells, and regions called "fr" to define the layout of elements on a web page.
  • CSS Grid layout allows for a complete separation of markers and layouts, making CSS easier to maintain and offers endless possibilities for design.

Introducing the CSS Grid Layout

Pictures are from SitePoint/Natalia Balska.

Grids are crucial when creating complex websites. The importance of grids in modern web design can be seen from the number of frameworks that implement grid systems to speed up development.

With the introduction of the CSS Grid layout specification, you no longer need to include a separate stylesheet to use the grid system. Another advantage is that you no longer need to rely on attributes such as inline and float to arrange elements on the web page. In this tutorial, we will cover the basics of grid systems and create a basic blog layout.

Browser support

Currently, only IE 10 and Edge support Grid layouts – you can’t use it on commercial sites yet.

It can be enabled in Chrome via the "Experimental Web Platform Features" flag in chrome://flags. You can enable it in Firefox using the layout.css.grid.enabled flag.

Another option is to use polyfill. CSS Grid Polyfill does exist! With the various options mentioned above, you can start experimenting with the Grid layout and learn as much as you can when it is still in its infancy.

Note: Internet Explorer currently implements the specifications for older versions. Unfortunately, this means it is not fully compatible with the latest specifications. When studying the examples in this tutorial, it is recommended that you use Chrome or Firefox with the corresponding flag enabled.

Grid system terminology

In terms of laying out elements, the CSS grid system is similar to a table. However, it is more powerful and flexible. In this section, I will discuss some terms that need to be aware of when using a grid:

fr Unit: This unit is used to specify a portion of the available space. It is designed to be used with grid-rows and grid-columns. According to the specification—

Allocating fraction space occurs after all "length" or content-based row and column sizes reach their maximum.

Line: Lines define the boundaries of other elements. They run vertically and horizontally. In the figure below, there are four vertical lines and four horizontal lines.

Road: The track is the space between parallel lines. In the figure below, there are three vertical tracks and three horizontal tracks.

Cell: Cells are the basic building blocks of the grid. In the figure below, there are nine cells in total.

Area: The area is a rectangular shape with any number of cells. Therefore, the track is a area, and the cell is also a area.

Introducing the CSS Grid Layout

Position elements in the grid

Let's start with the basics. In this section, I will teach you how to use a grid to locate elements to specific locations. To use the CSS Grid layout, you need a parent element and one or more children. For demonstration, we will use the following tag as our grid system:

<div class="grid-container">
  <div class="grid-element item-a">A</div>
  <div class="grid-element item-b">B</div>
  <div class="grid-element item-c">C</div>
  <div class="grid-element item-d">D</div>
  <div class="grid-element item-e">E</div>
  <div class="grid-element item-f">F</div>
</div>

After completing the tag, you need to apply display: grid or display: inline-grid on the parent element, as shown below:

.grid-container {
  display: grid;
  grid-template-columns: 200px 10px 0.3fr 10px 0.7fr;
  grid-template-rows: auto 20px auto;
}
The

grid-template-columns and grid-template-rows properties are used to specify the width of various rows and columns. In the example above, I defined five columns. The 10px column acts as slots to provide the required spacing between elements. The first column has a width of 200px. The third column occupies part 0.3 of the remaining space. Similarly, the fifth column occupies the 0.7 portion of the remaining space.

Use grid-template-rows for the first line in auto to allow the line to expand based on its internal content. 20px The row acts as a slot.

At this point, the elements are closely arranged, as shown in the following demonstration. (The CodePen demo link is omitted here)

Observe that element B is located in the second column we plan to use as a slot. If you do not specify the location of child elements within the grid, the browser places one element in each cell until the first row is fully filled and the rest then goes to the next row. This is why we have four spare columns left in the second row.

To move elements to specific cells in the grid, you need to specify their location in CSS. Before explaining how to move elements using a grid system, check out the image below. (The image link is omitted here)

In this case, we will use "line-based placement". Line-based placement means that lines in our grid system will serve as a guide to placement and limiting elements. Let's take element B as an example. In the horizontal direction, it starts from column 3 and ends from column 4. On the vertical axis, it is located between the line 1 and line 2.

We use grid-column-start to specify the starting vertical line of the element. In this case, it will be set to 3. grid-column-end Indicates the end vertical line of the element. In this case, this property will be equal to 4. The corresponding row values ??will be set similarly.

Considering all of the above, to move element B to the second cell, you will use the following CSS:

<div class="grid-container">
  <div class="grid-element item-a">A</div>
  <div class="grid-element item-b">B</div>
  <div class="grid-element item-c">C</div>
  <div class="grid-element item-d">D</div>
  <div class="grid-element item-e">E</div>
  <div class="grid-element item-f">F</div>
</div>

Similarly, to move element F to the sixth cell, you will use the following CSS:

.grid-container {
  display: grid;
  grid-template-columns: 200px 10px 0.3fr 10px 0.7fr;
  grid-template-rows: auto 20px auto;
}

After making these changes to CSS, the elements should be spaced correctly as in this demo. (The CodePen demo link is omitted here)

Create a basic layout

It's time to create a basic blog layout. The blog will have a header, footer, sidebar and two sections for actual content. Let's start with the marker:

.element-b {
  grid-column-start: 3;
  grid-column-end: 4;
  grid-row-start: 1;
  grid-row-end: 2;
}

Remember that the order of elements in the tags does not affect the position of elements on the web page. As long as you don't change the CSS, you can place the footer above the header in the tag, and the position of the elements on the web page does not change. Of course, I don't recommend this. The point is – your marker will no longer determine the location of the element.

What we have to do now is to determine the values ??of the grid-row-end and other attributes of various elements. Just like in the last example, we will use the grid graph to determine the values ??of all grid properties. (The image link is omitted here)

As shown in the figure above, the header goes from column 1 to column 4, and from row 1 to row 2. This should look like this:

.element-f {
  grid-column-start: 5;
  grid-column-end: 6;
  grid-row-start: 3;
  grid-row-end: 4;
}

Similarly, "extra" goes from column 3 to column 4, and from row 5 to row 6. So the CSS would be:

<div class="grid-container">
  <div class="grid-element header">Header</div>
  <div class="grid-element sidebar">Sidebar</div>
  <div class="grid-element main">Main Content</div>
  <div class="grid-element extra">Extra Info</div>
  <div class="grid-element footer">Footer</div>
</div>

The grid properties of all other elements can now be easily determined. Check out the CodePen demonstration and experiment with various grid values ??to better understand line-based placement. (The CodePen demo link is omitted here)

Conclusion

CSS grid layout specifications enable us to easily create complex layouts. The CSS we need to write is simpler and easier to maintain. When creating complex layouts in design, we no longer need to use float or other such attributes. Another huge advantage is the complete separation of marking and layout. With CSS Grid layout, the possibilities are endless.

If you have any questions about this tutorial, please let me know in the comments.

FAQs about CSS Grid Layout

How is the CSS Grid layout different from other CSS layout methods?

CSS Grid layout is a two-dimensional layout system that is different from other CSS layout methods such as Flexbox, which is one-dimensional. This means that with CSS Grid, you can control both horizontal and vertical layouts, which other methods can't do. It is designed to handle more complex designs and large layouts. It is also more flexible and powerful, allowing for more creative and finer design.

How to get started with CSS Grid layouts?

To start using CSS Grid layout, you need to set the display attribute of the element to grid or inline-grid. This makes the element a grid container and its child elements a grid item. You can then use various mesh properties to define the layout of the mesh and the location of the mesh items.

Can I use CSS Grid layout with other CSS layout methods?

Yes, CSS Grid layouts can be used in conjunction with other CSS layout methods. For example, you can use Flexbox for components of your website and CSS Grid for overall layout. This allows you to take advantage of each layout approach.

How to create a grid using CSS Grid layout?

To create a grid using a CSS Grid layout, you first need to define the grid container by setting the display attribute of the element to grid or inline-grid. You can then use the grid-template-columns and grid-template-rows properties to define the number and size of columns and rows in the grid.

How to place an item on a grid?

You can use the grid-column and grid-row properties to place items on the grid. These properties allow you to specify the start and end lines of the project, effectively placing it in a specific cell or cell of the grid.

What are grid lines and grid tracks?

In CSS Grid layout, grid lines are dividers that make up the grid structure. Their numbers start at 1, and the line numbers are incremented from top to bottom and from left to right. A grid track is the space between two adjacent grid lines, which can be a column or a row.

What is the fr unit in the CSS Grid layout?

The fr unit in the CSS Grid layout represents "score". It represents a portion of the available space in the grid container. For example, if you have a grid with three columns and set the width of one column to 1fr and the other two columns to 2fr, the first column will occupy one-third of the available space, and the other The two columns will each occupy one-third.

How to make the grid responsive?

To make the grid responsive, you can use media queries with grid-template-columns and grid-template-rows properties. You can also use the auto-fill and auto-fit keywords with the repeat functions to automatically adjust the number and size of columns and rows according to the size of the viewport.

Can I nest my grid in my grid?

Yes, you can nest grids in a CSS Grid layout. This means you can make the grid project itself a grid container, creating complex nested layouts.

Does CSS Grid layouts be supported by all browsers?

All modern browsers (including Chrome, Firefox, Safari, and Edge) support CSS Grid layouts. However, Internet Explorer does not support it. Therefore, it is important to provide alternate layouts for users who use unsupported browsers.

Please note that since I cannot access external websites and pictures, I cannot directly display the pictures. Please make sure that the image link is added correctly to your final output.

The above is the detailed content of Introducing the CSS Grid 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

CSS tutorial for creating loading spinners and animations CSS tutorial for creating loading spinners and animations Jul 07, 2025 am 12:07 AM

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

Addressing CSS Browser Compatibility issues and prefixes Addressing CSS Browser Compatibility issues and prefixes Jul 07, 2025 am 01:44 AM

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Creating custom shapes with css clip-path Creating custom shapes with css clip-path Jul 09, 2025 am 01:29 AM

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

What is the difference between display: inline, display: block, and display: inline-block? What is the difference between display: inline, display: block, and display: inline-block? Jul 11, 2025 am 03:25 AM

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Styling visited links differently with CSS Styling visited links differently with CSS Jul 11, 2025 am 03:26 AM

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

What is the CSS Painting API? What is the CSS Painting API? Jul 04, 2025 am 02:16 AM

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis

How to create responsive images using CSS? How to create responsive images using CSS? Jul 15, 2025 am 01:10 AM

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

What are common CSS browser inconsistencies? What are common CSS browser inconsistencies? Jul 26, 2025 am 07:04 AM

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

See all articles