CSS Grid is ideal for responsive layout. Introduction to CSS Grid Layout. Introduction to CSS Grid
Once upon a time I wrote about an unusual service, which is an AI for creating a website. Judging by the current results, they turned out “so-so”, especially considering the stated price. in this regard they will be more effective.
Today I want to talk about a “technology” that is similar in name, but completely different in essence - CSS Grid! The post consists of several parts:
This approach to building layouts makes it easy to create fairly flexible website grids with using CSS. In the future it will probably be used everywhere. If I understand correctly, it is perfect for the same tasks as Flexbox, but unlike it, it can simultaneously work in two-dimensional dimensions (columns and rows). Below I offer a translation of the article, which is a kind of introduction to the topic and contains simple educational examples. Attention! The author of the note is conducting a free(!) course on CSS Grid in December, if you want, send a request to his Email.
I also found a useful video on the topic on the Internet. Perhaps it is easier for someone to perceive information like this:
Introduction to CSS Grid
This year, this technology received built-in support in Chrome, FF, Safari, so it is likely that it will become a popular tool in the near future. But for now, you need to remember to take care of outdated and lagging web browsers.
The simplest mesh
There are 2 main objects here:
- parent / wrapper (wrapping all the internal blocks that make it up);
- children/items (the elements themselves).
Here is the simplest design:
Wrapper ( display: grid; )
However, nothing much will change immediately after this, since the display format is not defined. You will see 6 DIVs following each other.
Columns and Rows
To make your grid two-dimensional you need to specify the parameters of the rows and columns - use the options grid-template-row And grid-template-colum respectively:
.wrapper ( display : grid; grid-template-columns : 100px 100px 100px ; grid-template-rows : 50px 50px ; ) |
Wrapper ( display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 50px 50px; )
Since there are three values for the columns, the same number of them will be generated, therefore, there are only 2 rows. The numbers in pixels set in the first case the width of the elements (100px each), in the second - the height (50px).
Here's another example to help you better understand how it works:
.wrapper ( display : grid; grid-template-columns : 200px 50px 100px ; grid-template-rows : 100px 30px ; ) |
Wrapper ( display: grid; grid-template-columns: 200px 50px 100px; grid-template-rows: 100px 30px; )
The following image will be displayed:
Location and dimensions
With this feature you will get really very powerful capabilities for creating site grids. Let's say we have a size of 3x3:
.wrapper ( display : grid; grid-template-columns : 100px 100px 100px ; grid-template-rows : 100px 100px 100px ; ) |
Wrapper ( display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; )
If the HTML code, as in our example, has 6 DIV elements (see at the very beginning), then in in this case Only 2 rows will be displayed on the site, the third will temporarily not be filled. However, when we start using different parameters for position and block sizes - grid-columnand grid-row, the situation will change.
We assign styles to one of the objects:
.item1 ( grid-column-start : 1 ; grid-column-end : 4 ; ) |
Item1 ( grid-column-start: 1; grid-column-end: 4; )
This means that the DIV with the class item1 starts from the first line of the column in the grid and ends on the 4th, that is, it fills the entire row.
Actually, now some objects have moved to last line, which we had written down in advance (3x3). There is a simpler option:
.item1 ( grid-column : 1 / 4 ; ) |
Item1 ( grid-column: 1 / 4; )
.item1 ( grid-column-start : 1 ; grid-column-end : 3 ; ) .item3 ( grid-row-start : 2 ; grid-row-end : 4 ; ) .item4 ( grid-column-start : 2 ; grid-column-end : 4 ; |
Item1 ( grid-column-start: 1; grid-column-end: 3; ) .item3 ( grid-row-start: 2; grid-row-end: 4; ) .item4 ( grid-column-start: 2; grid-column-end: 4 )
It gives us the following picture:
Did you manage to figure it out? In principle, it’s not difficult, but you need to understand that this is only the basic part of the nuances of CSS Grid.
Prototyping and Layout Areas with CSS Grid
The markup code is as follows (HTML):
Container ( display: grid; grid-template-columns: repeat(12, 1fr); grid-template-rows: 50px 350px 50px; grid-gap: 5px; )
If everything is clear in principle about the rows, then the columns need to be clarified. Here in the meaning grid-template-columns a grid is created that consists of 12 identical columns (the width of each = 1/12 of the total) + 5px margins between them (grid-gap).
Add grid-template-areas
The option gives even more flexibility and amazing features. Perhaps the syntax and formatting of the parameter grid-template-areas looks a little unusual, but further you will understand why everything is exactly like this:
.container ( display : grid; grid-gap: 5px ; grid-template-columns : repeat (12 , 1fr) ; grid-template-rows : 50px 350px 50px ; grid-template-areas : "h h h h h h h h h h h h" "m m c c c c c c c c c c c" "f f f f f f f f f f f f " ; ) |
Container ( display: grid; grid-gap: 5px; grid-template-columns: repeat(12, 1fr); grid-template-rows: 50px 350px 50px; grid-template-areas: "h h h h h h h h h h h h" "m m c c c c c c c c c c c" "f f f f f f f f f f f f f" ; )
It's kind of visual representation your site grid in CSS. All characters in this parameter form 3 rows and 12 columns, defined by the line above. Each letter corresponds to one cell. The names in the example correspond to the blocks of HTML code: header (h), menu (m), content (c) and footer (f), but you can use any other options.
Assigning Areas Templates and Experiments
On next step you "logically link" container symbols and DIV elements:
.header ( grid-area : h; ) .menu ( grid-area : m; ) .content ( grid-area : c; ) .footer ( grid-area : f; ) |
Header ( grid-area: h; ) .menu ( grid-area: m; ) .content ( grid-area: c; ) .footer ( grid-area: f; )
The site will display a layout like this:
And now the magic begins. Let's swap some letters in the parameter grid-template-areas, for example, “c” and “m” in places:
grid-template-areas : "h h h h h h h h h h h h h" "c c c c c c c c c c c m m" "f f f f f f f f f f f f" ; |
grid-template-areas: "h h h h h h h h h h h h h" "c c c c c c c c c c c m m" "f f f f f f f f f f f f";
The grid will look different:
Adding adaptability to this design generally looks amazing. You couldn't do this with HTML alone, but in CSS everything is possible: ". h h h h h h h h h h ." "c c c c c c c c c m m" ". f f f f f f f f f f ." ;
grid-template-areas: ". h h h h h h h h h h ." "c c c c c c c c c m m" ". f f f f f f f f f f .";
The website looks like this:
No scripts or HTML - just the magic of CSS Grid!
Additional Grid and Flex Examples
Later on CSS Tricks I found another article on the topic, some options from it may be useful to you. Follow the links to (logo in the code block on the top right) to see the example in action on the big screen.
Classic site layout
We have a header and footer stretched to the full width, and between them there is a content block with two sidebars. In the first case, when the screen is reduced, all objects will remain in the same positions as before.
If you need the blocks to be located one below the other, then:
2 column blog grid
Let's say in our center big block content and sidebar. The code below keeps all objects in place when the page size is reduced.
The second technique is to arrange them one after the other.
Width distribution of elements
Visually, the example resembles a gallery, when all the pictures are next to each other in several columns. As you decrease the size, fewer and fewer of them will appear on each line.
Picture inside the article
Here the content is divided into 2 parts, and between them there is a full-screen block.
Total. In general, I discovered the concept of CSS Grid, which, to be honest, pleasantly impressed me - especially the frames where layout areas with different letters were considered. Of course, these are all just the basics of the “technology”, plus I did not describe in detail each parameter of the code. The examples in the last section of the article are more complex and should be reviewed carefully. It combines both Flex and Grid. If this topic is interesting, subscribe to the courses of the first author - Email entries in the original first or second note.
Do you have anything to add regarding CSS Grid? — write thoughts, advice or send interesting links.
Hi all! Today we will talk about what are grid systems? or simply flexible grids in adaptive layout.
First let's define what it is Grid System.
Grid System- a collection of class-based styles that allow the user to control the page layout using a system of rows and columns.
Let's imagine we have a blog page. It is divided into 2 columns: the main part on the left, and the sidebar on the right. Let's try to create a flexible grid for such a page.
Well, first we have to do something basic, but... html markings.
Here we have a block that contains the entire page, it contains a block with a blog, which contains 2 blocks: the main part of the page and the sidebar.
So our entire page will be the size 960px. The entire grid is divided into 12 columns 69px. each. The blog part will be wide 900px. The main part of the page will be 566px, sidebar - 331px.
This is what we get in the end
#page (
margin: 36px auto;
width: 960px;
}
Blog (
margin: 0 auto 53px;
width: 900px;
}
Blog.main (
float: left;
width: 566px;
}
Blog .sidebar (
float: right;
width: 331px;
}
Everything would be fine, but, as you can see, all this is static, specified in pixels. We want our grid to change its size depending on what screen the page is viewed on, therefore, we need to set everything in percent. Let's do this.
For this there is the same formula as for fonts
goal / context = result
Let's convert a block of the entire page from pixels to percentages.
#page (
margin: 36px auto;
width: 90%;
}
90% chosen because in this case we will also have indents along the edges along 5% . However, you can choose a different value.
We use our formula: 900 / 960 = 0.9357
Let's multiply the result by 100 to receive interest, and we will register it in our css.
Blog (
margin: 0 auto 53px;
width: 93.75%;
}
The same thing needs to be done with the columns, but notice that the context has changed. Because the columns are inside a block with a class .blog, then it will be the context. Let's do the math.
566 ÷ 900 = .628888889
331 ÷ 900 = .367777778
We convert everything into percentages and write it in the style sheet.
Blog.main (
float: left;
width: 62.8888889%;
}
Blog .sidebar (
float: right;
width: 36.7777778%;
}
That's all! Now we have a flexible grid and can use it for layout.
As you can see, everything is very simple. The basis of a flexible grid, like a flexible font, is the same formula, remembering which, you can easily create responsive websites.
The note! As you can see, we ended up with quite long percentage values. Some may advise you to round them up, but you should never do this! Remember!
And that's all for me, thank you for your attention and good luck adaptive layout!
In this article you will find full course by CSS grids. We will look at what it is, what are the differences with FlexBox and how you can work with CSS Grid.
CSS grids are new approach to creating adaptive websites with many blocks located anywhere on the site. Besides CSS Grid There is also a technology that is very similar to meshes. We will not go into the smallest differences between them, since this would require a separate article, but we will briefly describe the main differences.
CSS Grid can be called a cooler and improved version FlexBox, because FlexBox allows you to work only in one plane: either create columns or create rows.
CSS grids allow you to do more because they work on both planes at the same time. Thus, creating adaptive website blocks is much easier, and the possibilities for arranging objects as you please are simply endless.
We invite you to watch a full video on learning CSS grids to instantly understand the essence CSS Grid:
- Lesson on ;
During the lesson, three pages were created, the code for each page can be copied below.
First file
Second file
Third file
Principle of operation
Working with grids is very simple. The algorithm of actions here is as follows:
- We create one main block and place other blocks in it ( sections);
- Add the display: grid property to the main block;
- CSS grid properties can now be applied to all main block elements;
- Adding various properties. You can find documentation on all possible properties;
Each block can be set in width, height, and location. Blocks that are created based on grids immediately have an adaptive design that adjusts the blocks to different screen resolutions.
This way you can write less and get more. What else could be cooler? Share your experience of using grids in your projects in the comments to this article ⇣
I noticed the Grids technique about a year ago. Then this technique, after a very superficial study, seemed useless and very experimental to me; I was repulsed by the fact that for implementations it was necessary to create extra markup. But now it is becoming difficult not to notice the number of websites built on the grid, as well as the number of articles and tutorials about it. Thanks to the latter, it has become much easier to study and understand the principles and concepts, and draw more or less real conclusions. My conclusion a year later is: “It’s simple and useful solution which was ever created for the layout of web pages, every self-respecting web designer should know.”
Anyone who has ever worked with a grid knows what a grid is. graphic editors (Photoshop, Fireworks, Gimp, etc.) and of course appreciated its necessity when creating any design. But how to implement a Grid on the web? In fact Tabular the layout was a real web page design with a Grid, undoubtedly very convenient. But not the intended use of elements table was horrifying.
Fortunately, the enormous popularity of web standards has grown and continues to grow over last years, as well as their support in modern browsers, gave us the opportunity to create multi-functional pages with very little, logical markup. This layout was called Block. But also Block layout turned out to be weak side. When creating pages with a huge set of elements of different sizes and meanings, marking up such pages has become a very difficult task, and if more than one person works on the markup, such work can become a nightmare.
And then the technique using the Grid came to the rescue. This technique is a hybrid between Block And Tabular layout. Using it gives us:
- speed and ease of development
- freedom of positioning
- proportionality of page elements and their placement
What is layout with Grid? First of all, it's a concept. A concept that includes many design aspects and very few rules for its implementation. This gives complete freedom and no standardization in its execution. I will say even more - the same Grid can be implemented by the most different ways. If you have already read about the Grid, you may have noticed that each author starts from a new angle, going deep into the details, this is, to put it mildly, confusing. Luckily they started to appear Grid Systems - CSS libraries and for working with the Grid. And using their example, you can very quickly master the basic principles of this technique.
I think it makes no sense to write about all aspects of the Grid; you can easily find information about it on the Internet. I suggest creating your own simple Grid System.
First you need to understand that the grid consists of columns and spaces between them. There are three main values - the grid width, the column width, and the width of the space between the columns. The width of the columns depends on their number.
Let's try to make a grid 950 pixels wide with 16 columns with 10 pixel spacing, so one column should be 50 pixels wide. Having understood all the values, we open any graphic editor known to us and create a layout. You can also add padding to the Grid on the left and right, say 20 pixels each, and this will result in a layout with a width of 990 pixels. You can see my example.
Now we can start creating our library. Like most CSS libraries ours needs a global reset, I suggest Eric Mayer's CSS Reset, keeping reset.css let's create grid.css to which we can immediately add a method for cleaning containers containing floating blocks - Clear Fix. The first thing we need is a rule for a container that will contain all our columns. The width of each container is equal to the width of our grid.
.container(
margin: 0 auto;
width: 950px;
}
Now we can add a rule for our columns, it contains the width and padding. The indent acts as a gap (gutter) between the columns.
.column(
float: left;
margin-right: 10px;
overflow: hidden;
width: 50px;
}
The last column does not need an indentation; to do this, let’s add a rule for it as well:
Our containers contain columns, the columns are floating blocks, so they have to be cleaned. To avoid unnecessary .clearfix in the markup, you can apply this rule to containers:
.clearfix:after .container:after{
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}clearfix, .container( display: inline-block; )
/* Hides from IE-mac \*/
*html.clearfix, *html.container(height: 1%;)
.clearfix, .container(display: block;)
/* End hide from IE-mac */
Now we can start with our columns. Columns can be two or three wide, and so on. To do this, we can apply the following rules to them:
.span-1 ( width: 50px; )
.span-2 (width: 110px; )
.span-3 (width: 170px; )
.span-4 (width: 230px; )
.span-5 (width: 290px; )
.span-6 (width: 350px; )
.span-7 (width: 410px; )
.span-8 (width: 470px; )
.span-9 (width: 530px; )
.span-10 (width: 590px; )
.span-11 (width: 650px; )
.span-12 (width: 710px; )
.span-13 (width: 770px; )
.span-14 (width: 830px; )
.span-15 (width: 890px; )
.span-16 ( width: 950px; margin-right: 0; )
In principle, this is all that is needed for Grid implementations; you can also add a wrapper and a class for viewing the Grid with a layout. Let's create main.css and add to it:
.wrapper(
margin: 0 auto;
width: 990px;
}
.overlay(
background: transparent url(overlay.png) repeat-y scroll top left;
}
Here's what the layout might look like:
I think this is enough for a start.
You can see my example
Around 2012 or 2013, I was introduced to web development. Gradually I began to study this direction on my own. I soon realized that CSS makes a lot of things meaningful, but it doesn't create adequate markup. There are so many hacks that it is too difficult to understand them. That is why in modern standards, which will be discussed in this article, special attention was paid to working with markup.
What you will learn from this article:
- how you used to work with CSS markup;
- difference between legacy approaches and modern standards;
- how to get started with new standards (Flexbox and Grid);
- why should you care about these modern standards.
How we used to work with CSS markup
Task
Let's model a fairly standard problem: how to create a page with two sections - a sidebar and a main content area - that are the same height, regardless of the size of the content?
Here's an example of what we're aiming for:
Sidebar and main content area same height, regardless of content size
First, I'll show you how to solve this problem using legacy approaches.
1. Create a div with two elements
For example,
Obviously main has more text.
Let's highlight aside with a color to make it easier to distinguish:
Aside ( color: #fff; background-color: #8cacea; )
2. Place both sections side by side
To do this, we will write the following:
Aside, main( float: left; )
This style shares available space in the required proportions and sets float to aside and main .
Two blocks located next to each other
For those unfamiliar with float, it is a relatively old way of moving elements left or right.
As you can see in the image above, the task is still not completed - the height of the sidebar does not match the height of the main area. Let's fix this.
3. Use the display: table trick
To solve this problem, you need to use display: table .
If you're not familiar with it, here's what to do:
- We make the parent container (in our case the body element) a table: body ( display: table; )
- Remove float and do child elements aside and main by table cells: aside, main( display: table-cell; )
Once we have done this, the problem can be considered solved. At least that's how it was solved before.
Both blocks are now the same height.
Difference between legacy approaches and modern standards
Now that you have an idea of how the problem has been solved in the past, let's take a look at what Flexbox and Grid can do.
Flexbox and Grid are ideal solutions for CSS markup, which are modern standards. If you need to write CSS code in good level, then be sure to study them.
Solving a problem using Flexbox
The Flexbox formatting context is initialized by creating a flex container.
The parent element is body . It contains a sidebar and a main area. Create a container:
Body ( display: flex; )
Flexbox in action
Let's not forget about proportions:
Aside (width: 25%; ) main (width: 75%; )
Problem solved
You can do many different things with Flexbox:
![](https://i2.wp.com/cdn.tproger.ru/wp-content/uploads/2017/05/layout-6.png)
And I've only touched on the tip of the Flexbox iceberg.
Flexbox is available in most supported browsers. It can be fully used in Firefox from version 28, Chrome from 29, Safari from 6.1 and Edge from 12.
Note translation Ours will help you understand Flexbox.
Solving the problem using Grid
Unlike Flexbox, which mostly works in one dimension, with CSS Grid you can use both rows and columns. Let's see how to solve our problem with its help.
It all starts the same way as in the case of Flexbox. Create a container:
Body ( display: grid; )
Here's all the CSS:
Body ( display: grid; background-color: #eeeeee; ) aside ( color: #fff; background-color: #8cacea; )
And this is what we get:
Primary option
Grid is available in most supported browsers, although in smaller quantities. It can be fully used in Firefox from version 52, Chrome from 57, Safari from 10.1 and Edge from 16.
Our latest image is still no different from the previous ones. So what is the magic?
What if we split the width proportions like before:
Aside (width: 25%; ) main (width: 75%; )
Proportional width distribution for child elements
Yes, the result is different, but it does not suit us. The sidebar is not yet located to the side of the main area.
Here we come to the essence of Grid markup. After initializing the Grid container with display: grid, you need to define the rows and rows within it.
Here's how it's done:
Body ( grid-template-columns: 30% 70%; )
A one-line solution is beautiful, isn't it? grid-template-columns defines the proportions of the columns in the grid.
Problem solved
But you can do so much more with Grid.
First, let's add a little color to the main block to make the examples more clear:
Main ( background-color: rgba(39,174,96,1); color: #fff; )
This is what we should get:
Filling the main content area with color
Let's see what good Grid has to offer:
- You can define a gap between columns: body ( grid-column-gap: 15px; )
Here is the result:
It looks much neater with a gap between the columns.
There is no need to add padding to the aside and main blocks: grid-column-gap does that.
- You can make as many columns as you need. The examples above used only one element aside and main . Let's add two more:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Added a line break
To simplify this, you can use the shortcut: grid-gap: 15 px instead of grid-row-gap and grid-column-gap .
- You can define row sizes: body ( grid-template-rows: 200px 300px 400px; )
Different line heights
Now the height of the first, second and third lines is 200, 300 and 400 pixels respectively.
This is enough for a start - but this is not all.
How to get started with the new standards?
You've seen an example of how Flexbox and Grid provide better layout solutions. So how to start?
Why should you care about these standards?
If you still don't understand why Flexbox and Grid are worth using, here are a few problems you may encounter with the previous solutions:
- if you use floating blocks and clearfix, you know that they can cause problems;
- with absolute positioning, elements can overlap each other;
- using display: table leaves a lot of unnecessary markup;
- at using inline-block there will be problems with empty spaces.