Grid Layout
Add to favorites
The best way to create a beautiful and responsive layout
CodeSandbox link
You can find the full code for this tutorial at https://codesandbox.io/s/grid-layout-l472n.
Layout
The first method we tried was tables, positioning, and inline-block, but all of these methods were essentially hacks, leaving out a lot of important functionality. Flexbox is also a very good layout tool, but it is one-directional. We've had to hack our way around layout problems for as long as we've been building websites, so Grid was the first module created specifically to fix them.
Display
A container element must be defined as a grid.
display: grid;
Template
Columns and rows of the grid are defined by space-separated values. The value can be a length, a percentage or a fraction.
grid-template-columns: ... ...;
grid-template-rows: ... ...;
Spacing
Sets the grid line size. This is similar to setting the gutter width between columns/rows.
column-gap: 20x;
row-gap: 20x;
gap: 20px;
Container
Let's resize the browser container and you can see that it takes up the entire width of the browser. You should normally set a maximum size for your container so that it will adapt to all screen sizes.
max-width: 1200px;
margin: 0 auto;
Justify Content
This property sets how the grid will be aligned within the grid container horizontally.
- start – aligns the grid to the left
- end – aligns the grid to the right
- center – aligns the grid in the center
- space-evenly – places an even amount of space between each grid item, including the far ends
- space-around – places an even amount of space between each grid item, with half-sized spaces on the far ends
- space-between – places an even amount of space between each grid item, with no space at the far ends
Align Content
This property sets how the grid will be aligned within the grid container vertically.
- start – aligns the grid to the top
- end – aligns the grid to the bottom
- center – aligns the grid in the center
- space-evenly – places an even amount of space between each grid item, including the far ends
- space-around – places an even amount of space between each grid item, with half-sized spaces on the far ends
- space-between – places an even amount of space between each grid item, with no space at the far ends
Place Content
Using place-content will shorten your code. The first value is align-content, while the second value is justify-content.
place-content: center space-between;
Justify Items
This property sets how the items will be aligned within the grid horizontally.
- start – aligns the item to the left
- end – aligns the item to the right
- center – aligns the item to the center
Align Items
This property sets how the items will be aligned within the grid vertically.
- start – aligns the item to the top
- end – aligns the item to the bottom
- center – aligns the item to the center
Place Items
Using place-items will shorten your code. The first value is align-items, while the second value is justify-items.
place-items: center;
Responsive Columns
Let's adapt the grid now that you know how it works.
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
Learn with videos and source files. Available to Pro subscribers only.
Purchase includes access to 50+ courses, 320+ premium tutorials, 300+ hours of videos, source files and certificates.
Templates and source code
Download source files
Download the videos and assets to refer and learn offline without interuption.
Design template
Source code for all sections
Video files, ePub and subtitles
Assets
Videos
Subtitles
1
Styled Components
React library for styling your components
5:36
2
Grid Layout
The best way to create a beautiful and responsive layout
16:09
3
Media Query
Introduction to Responsive Web Design
8:24
4
Keyframes
Create beautiful animations in CSS using keyframes
12:34
5
Glassmorphism Animation
Bring your frosted glass design to life with beautiful animations
14:21
6
CSS line animations
Learn how to create a CSS line animation
15:47
Meet the instructor
We all try to be consistent with our way of teaching step-by-step, providing source files and prioritizing design in our courses.
Willie Yam
Front-end/UI developer at Design+Code
I do UI coding. HTML/CSS/JS/SWIFTUI dev.
10 courses - 37 hours
![course logo](http://images.ctfassets.net/ooa29xqb8tix/6LKKWLFkx6JrlMY2mWPocd/2513a06a163424ba900bcff844c2c3fb/play-logo.png?w=400&q=50?fm=jpg&q=50)
Design and Prototype an App with Play
Build a completely functional prototype without writing a single line of code from your phone
3 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/1bYdUtniEAUH3jdqQd3Qc1/7cf21d20882bfe59f01d7bc74e81010d/react-logo.png?w=400&q=50?fm=jpg&q=50)
Create a 3D site with game controls in Spline
Build an interactive 3D scene implemented on a ReactJS site using Figma and Spline
2 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/4bWEQMRsA6PzjiHljsxryp/55d78d84da072e9c22caddbccd67396b/swift-logo.png?w=400&q=50?fm=jpg&q=50)
Build a Movie Booking App in SwiftUI
Learn how to create an iOS app based on a beautiful UI design from Figma with interesting animations and interactions, create a custom tab bar and use navigation views to build a whole flow
1 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/628IYmTv4uib8slYz9iuok/3de9010de04ae92a23c94f9885746db2/swift-logo.png?w=400&q=50?fm=jpg&q=50)
Build Quick Apps with SwiftUI
Apply your Swift and SwiftUI knowledge by building real, quick and various applications from scratch
11 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/0cjyplK9nhXoHTHN3XfTr/0e1edc5106a4c99ca2de7eeaaa772603/ui-logo.png?w=400&q=50?fm=jpg&q=50)
CSS Handbook
A comprehensive series of tutorials that encompass styled-components, CSS, and all layout and UI developments
1 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/1bYdUtniEAUH3jdqQd3Qc1/7cf21d20882bfe59f01d7bc74e81010d/react-logo.png?w=400&q=50?fm=jpg&q=50)
Advanced React Hooks
Learn how to build a website with Typescript, Hooks, Contentful and Gatsby Cloud
5 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/4k80OoiAH74E4JWnzT7iHv/d8b989084c313064df5be11f940ea48b/unity-logo.png?w=400&q=50?fm=jpg&q=50)
Unity for Designers
If you want to make a game and don't know where to start, you are in the right place. I will teach you how to use Unity, code in C# and share essential tips and tricks to make your first game.
5 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/1YISP6mvD83Gao3lQdhWqi/8b36d37d05f77a894dd0b7d30f8e6753/js-logo.png?w=400&q=50?fm=jpg&q=50)
Create a Javascript Game
Learn how to create a web game using Phaser 3, a popular javascript game engine. Draw a map using an editor, implement the player, make the player move, apply physics, collisions, and implement the enemies.
2 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/4bWEQMRsA6PzjiHljsxryp/55d78d84da072e9c22caddbccd67396b/swift-logo.png?w=400&q=50?fm=jpg&q=50)
Build an ARKit 2 App
Introduction to ARKit and learn how to make your own playground. You will be able to add models or even your own designs into the app and play with them
4 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/4bWEQMRsA6PzjiHljsxryp/55d78d84da072e9c22caddbccd67396b/swift-logo.png?w=400&q=50?fm=jpg&q=50)
Create a SpriteKit Game
Overview of SpriteKit a powerful 2D sprite-based framework for games development from Apple and learn how to create your very own platform
3 hrs