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

CSS Grid
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima animi, tempore. Vitae consectetur voluptate inventore soluta totam iste dicta neque nesciunt a! Incidunt aliquid quae eveniet blanditiis, laudantium assumenda natus doloribus, fuga mollitia debitis dolorem, soluta asperiores accusamus. Qui, necessitatibus autem doloremque corporis eligendi dolorum natus, eius aperiam consequatur aliquid, quaerat excepturi sequi repellendus, tempora cum sed velit. A voluptates laboriosam quibusdam consequatur quas harum unde sint minus, molestiae quo?
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit, nostrum animi, aliquid consequuntur iusto esse nulla accusamus commodi perferendis deserunt ipsa quidem, illo quam minima aspernatur vero natus?

Second file

CSS Grid
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Lorem ipsum.
Lorem ipsum.
Lorem ipsum.
Lorem ipsum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus magni atque nostrum deleniti magnam unde ad, expedita perferendis laborum aut, pariatur delectus. Deleniti dolores consequuntur sed iure ratione, laudantium exercitationem perferendis reprehenderit delectus aperiam fugiat rerum earum quidem facere aspernatur ipsam harum. Minus alias sequi inventore aspernatur expedita, odio nemo corporis consectetur labore, voluptas quasi.
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Lorem ipsum dolor sit amet, consectetur adipisicing elit.

Third file

CSS Grid
Box 1
Box 2
Box 3
Box 4
Box 5
Box 6

Principle of operation

Working with grids is very simple. The algorithm of actions here is as follows:

  1. We create one main block and place other blocks in it ( sections);
  2. Add the display: grid property to the main block;
  3. CSS grid properties can now be applied to all main block elements;
  4. 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
The price for all this is just a little extra markup. I think these days, when the cost of a person's watch is much more expensive than hardware and productivity, it's not hard to guess which way the scales are tipping.

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,