How to use CSS Grid and Flexbox together tutorial
Jun 27, 2025 am 12:40 AMCSS Grid and Flexbox each have their own expertise, which works best together. Grid is a two-dimensional layout that is suitable for the overall page structure, such as the arrangement of the header, sidebar, main content area, and footer; Flexbox is a one-dimensional layout that is more suitable for the internal arrangement of components, such as navigation bar, button group, card list, etc. For example, use Grid in the middle of the three-column layout and then block up and down, and use Flexbox to automatically align several buttons in a row. The actual combination method is: the outer container uses display: grid to define the overall framework, and the child elements are arranged using display: flex in each area. Common structures include using Grid to divide blocks on the entire page, and using Flexbox to align elements inside the navigation bar, button group and card list. Note that you should not set Grid and Flexbox on the same container at the same time. After the parent uses Grid, the child needs to set display: flex separately to take effect. In the nested structure, you should also avoid maintenance difficulties due to too deep levels.
When you want to make a modern web layout, CSS Grid and Flexbox are the two most practical tools. They each have different scenarios, but when used together, they can solve most complex layout problems. The key is not who to choose, but who to use, but who to use.

When to use Grid, when to use Flexbox?
Simply put: Grid is a two-dimensional layout (controlled by rows and columns together) , which is suitable for the overall page structure, such as arranging the header, sidebar, main content area, and footer. Flexbox is a one-dimensional layout (only one row or one column is controlled at a time) , which is more suitable for internal arrangement of components, such as navigation bars, button groups, card lists, etc.

For example:
- If you make a three-column layout and can be divided up and down in the middle, then Grid is more suitable.
- If you just want a few buttons to line up and align automatically, Flexbox is more straightforward.
So, in a complete page, you can use Grid to divide the area on the outer layer and arrange elements on the inner layer with Flexbox .

How to use it in combination? Depend on the structure
Common practices are:
- Use
display: grid
to define the entire page frame - Use
display: flex
in each area to arrange child elements
For example:
.container { display: grid; grid-template-columns: 200px 1fr 300px; } .sidebar { display: flex; flex-direction: column; justify-content: space-between; }
In this way, the left column becomes a vertically distributed structure and will not affect the layout of other areas.
Common combination structures:
- The entire page is divided into partitions using Grid
- Use Flexbox for navigation bars and button groups
- Use Flexbox to align titles, pictures and other elements inside the card list
Note: Don't mess with direction and level
Although the two can be used together, be aware of some error-prone areas:
- Do not set Grid and Flexbox on a container, as only one will take effect
- The parent uses Grid. If the child wants to use Flexbox, it must set
display: flex
for the child separately. - In the nested structure, try to keep the logic clear and not nest too deep, otherwise it will be troublesome to maintain.
Let me give you an example of actually trampling on tricks:
<div class="grid-container"> <div class="flex-box"> <span>Item 1</span> <span>Item 2</span> </div> </div>
.grid-container { display: grid; grid-template-columns: repeat(3, 1fr); } .flex-box { display: flex; gap: 10px; }
This is a good idea, but if you accidentally add a grid
attribute to .flex-box
, it will become a Grid container and Flex will fail.
Basically that's it. It may feel a little confused at first, but just remember: Grid controls the large structure, Flexbox is responsible for the arrangement of details , and then uses it separately at different levels, so it can easily handle most layout needs.
The above is the detailed content of How to use CSS Grid and Flexbox together tutorial. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

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

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

CSS layout attribute optimization tips: positionsticky and flexbox In web development, layout is a very important aspect. A good layout structure can improve the user experience and make the page more beautiful and easy to navigate. CSS layout properties are the key to achieving this goal. In this article, I will introduce two commonly used CSS layout property optimization techniques: positionsticky and flexbox, and provide specific code examples. 1. positions

HTML Tutorial: How to Use Flexbox for Vertical Height Layout In web development, layout has always been an important issue. Especially when it is necessary to implement vertical equal-height layout, the traditional CSS layout method often encounters some difficulties. This problem can be easily solved using Flexbox layout. This tutorial will introduce in detail how to use Flexbox for vertical equal height layout and provide specific code examples. Flexbox is a new feature in CSS3 that can be used to create flexible, responsive layouts.

HTML tutorial: How to use Flexbox for adaptive equal-height, equal-width, equal-spacing layout, specific code examples are required. Introduction: In modern web design, layout is a very critical factor. For pages that need to display a large amount of content, how to reasonably arrange the position and size of elements to achieve good visibility and ease of use is an important issue. Flexbox (flexible box layout) is a very powerful tool through which various flexible layout needs can be easily realized. This article will introduce Flexbox in detail

How to use CSS3’s flexbox technology to achieve even distribution of web content? With the development of web design, people have higher and higher requirements for web page layout. In order to achieve even distribution of web content, CSS3's flexbox technology has become a very effective solution. This article will introduce how to use flexbox technology to achieve even distribution of web content, and give some practical examples. 1. What is flexbox technology? Flexbox (elastic layout) is a new feature added in CSS3.

HTML Tutorial: How to Use Flexbox for Evenly Distributed Layout Introduction: In web design, it is often necessary to layout elements. Traditional layout methods have some limitations, and Flexbox (flexible box layout) is a layout method that can provide more flexibility and power. This article will introduce how to use Flexbox to achieve even distribution layout, and give specific code examples. 1. Introduction to Flexbox Flexbox is a flexible box layout model introduced in CSS3, which allows elements to

HTML tutorial: How to use Flexbox for adaptive equal-height layout, specific code examples are required. Introduction: In web design and development, implementing adaptive equal-height layout is a common requirement. Traditional CSS layout methods often face some difficulties when dealing with equal height layout, and Flexbox layout provides us with a simple and powerful solution. This article will introduce the basic concepts and common usage of Flexbox layout, and give specific code examples to help readers quickly master the use of Flexbox to implement their own

HTML tutorial: How to use Flexbox for scalable equal-height layout, specific code examples are required. Introduction: In web page layout, we often encounter the need to achieve equal-height layout effects. The traditional method is more cumbersome and needs to be implemented using JavaScript or table layout. Using Flexbox, you can easily implement scalable equal-height layouts without relying on other technologies. This article will introduce how to use Flexbox to implement scalable equal-height layout, and attach detailed code examples. one,
