Css grid first row

Полное визуальное руководство/шпаргалка по CSS Grid

Сегодня мы с вами рассмотрим свойства CSS Grid (далее также — Грид), позволяющие создавать адаптивные или отзывчивые макеты веб-страниц. Я постараюсь кратко, но полно объяснить, как работает каждое свойство.

Что такое CSS Grid ?

Грид — это макет для сайта (его схема, проект).

Грид-модель позволяет размещать контент сайта (располагать его определенным образом, позиционировать). Она позволяет создавать структуры, необходимые для обеспечения отзывчивости сайтов на различных устройствах. Это означает, что сайт будет одинаково хорошо смотреться на компьютере, телефоне и планшете.

Вот простой пример макета сайта, созданного с помощью Грида.

Архитектура CSS Grid

Как же Грид работает? Элементы Грида (grid items) располагаются вдоль главной или основной (main) и поперечной (cross) оси (axis). При помощи различных свойств мы можем манипулировать элементами для создания макетов.

Помимо прочего, у нас имеется возможность объединять строки и колонки подобно тому, как мы это делаем в Excel , что предоставляет нам большую гибкость, чем Флекс ( Flexbox ).

К слову, если вас интересует Флекс, вот соответствующая статья.

Схема CSS Grid

Схема содержит все возможные свойства, предоставляемые Гридом. Эти свойства делятся на:

Обратите внимание: красным цветом отмечены сокращения для свойств:


К концу настоящей статьи у вас будет полное понимание того, как работает каждое из них.

Настройка проекта

Для данного проекта требуются начальные знания HTML , CSS и умение работать с VSCode (или другим редактором по вашему вкусу). Делаем следующее:

  1. Создаем директорию для проекта, например, Project1 и открываем ее в редакторе ( cd Project1 , code . )
  2. Создаем файлы index.html и style.css
  3. Устанавливаем в VSCode сервер для разработки ( Live Server , расширение) и запускаем его

Или вы можете просто открыть Codepen (или любую другую песочницу) и начать писать код.

Все готово, можно приступать к делу.

HTML

Создаем 3 контейнера внутри body :

Источник

grid-row

The grid-row CSS property is a shorthand that specifies the row grid lines where a grid item starts and ends in a grid layout, and does it in a single declaration.

.grid-container < display: grid; grid-template-columns: repeat(4, 1fr); >.grid-item:nth-child(2) < grid-row: 2 / 4; /* Starts on the second row line and ends on the fourth row line */ >

Because of CSS Grid’s default auto-placement behavior, the second child element of the grid in this example would normally be placed in the first row of the grid’s second column. But we declare a grid-row and set it to align the grid item’s starting edge with the second grid line and its ending edge with the fourth grid line which relocates the grid item.

  • Initial value: auto
  • Applies to: grid items and absolutely-positioned boxes whose containing block is a grid container
  • Inherited: no
  • Computed value: as specified for its longhand properties
  • Animation type: discrete

This property can take two values separated by a forward slash ( / ). The value before the slash sets the grid-row-start property, while the value after the slash sets the grid-row-end property. You can declare a single value without the slash which applies to the grid-row-start property, and sets the grid-row-end property to auto .

/* Keyword value */ grid-row: auto; /* value */ grid-row: myLineName; grid-row: myGridArea; grid-row: header-start / main-end; /* + values */ grid-row: 3; grid-row: 2 / -3; grid-row: main 2; grid-row: 3 line / 5 line; /* span + + values */ grid-row: span 3; grid-row: span 2 / 5; grid-row: 1 / span myline; grid-row: 2 / span gridline 3; /* Global values */ grid-row: inherit; grid-row: initial; /* same as `auto` */ grid-row: revert; grid-row: revert-layer; grid-row: unset;

This is the default value. It indicates the default span ( 1 ) and auto-placement behavior, which means the grid item is automatically placed in the next available empty grid cell.

Читайте также:  METANIT.COM

This syntax allows you to either use an integer to refer to a numbered grid line or a string to refer to a named grid line or a named grid area. In other words, you can specify a grid line by its numerical index or name to the starting and the ending edge of a grid item.

Positioning items by line numbers

There are two grid lines before and after each grid track with a numerical index assigned to them automatically, starting from number one.

A three by three grid of numbered rectangles. The third cell in the third row is blank.

In the first example of this article, we used this syntax to refer to the second grid line by its index ( 2 ) which aligns the starting edge of the grid item with the starting edge of the second row, while its ending edge is aligned with the fourth row using the syntax:

Note that you can also use a negative number to refer to a grid line, but it counts starting from the ending edge of the grid. The following code points to the same grid lines in the previous example, but counting in reverse:

Notice the negative integers have been assigned to our grid as well as positive ones:

Positioning items by line names

You can assign a custom name to a grid line using the grid-template-columns and grid-template-rows line-based placement grid properties to refer to that line by its name.

Let’s go back to our example and name all its row track lines like the following declaration:

We can refer to the second and the fourth line by our custom names instead of their index:

Note that the cannot take the span value. span is a reserved keyword for grid placement properties (e.g. grid-row: 1 / span 2 ).

Positioning items by grid areas

When defining grid areas using the grid-template-areas property, you get implicit line names for free based on the name of the areas. For instance, a grid area with the name content generates a line named content-start before it and one named content-end after it. You can refer to these lines to set the position of a grid item.

Alternately, you can refer to the area’s name to position an item at the starting and ending line of the content named area:

This sets the position of the element to the content area in our grid.

Читайте также:  Как пользоваться printf java

This flavor of syntax allows you to position grid items by grid lines when there are repeated names. If there are grid lines with the same name, this syntax helps specify which of those lines you are referring to.

Let’s assume you want to choose the third line, but that line has the same name as the first and the last grid line — all of them are called bar . Since the second line named bar is the third grid line, you can use 2 to select it as the starting point:

A single column grid wuth three rows. The second row is green and has swapped places with the third row.

Note that the order doesn’t matter, so the previous code can also be written like this as well:

Like the previous syntax, you can use a negative integer to count the grid lines starting from the end edge of the grid too. In our example, if we want to refer to the first bar , we can count starting from the ending edge of our grid and write it like this:

Note that the integer value cannot be zero.

This syntax allows a grid item to span across the grid tracks. It can be specified in three different ways.

Note that the default value is 1 if the integer is not specified anywhere in this syntax.

Using the span keyword followed by an integer indicates the number of tracks a grid item spans from a specific grid line. For example, if we want a grid item to span three row tracks towards its ending edge, we can apply the following value:

A three by four grid of rectangles. The first three rows in the second column are selected.

It’s also possible to combine the span keyword with the name of a grid line to make the grid item expand until it reaches that specified grid line.

Since the starting line of the grid item is known ( 3 ), we can span the item until it hits a grid line named lastline .

A three by five grid with the last three rows in the first column selected.

If the specified grid line name is assigned to more than one grid line — in other words, if we have repeated named grid lines — we need to say which ones we want to target. To do that, we can add an integer to our value specifying which grid line we are referring to.

Take the following grid as an example:

.grid-container < display: grid; grid-template-rows: [y] 50px [x] 50px [x] 50px [y] 50px [x] 50px [x]; grid-template-columns: 1fr 1fr; >.grid-item:nth-child(2) < grid-row: 2 / span x 2; /* equivalent to grid-row-end: 5; */ >

We set the starting line of the grid item to the second line. Then we want it to span forward until it hits a grid line named x . And since we want it to be the second x grid line, we wind up with span x 2 .

As a result, our grid item spans from the second line, as illustrated below. The first line that it hits is the first one, x , followed by y , and finally, it hits the desired second line, named x .

A two by five grid. The middle three rows in the first column are selected.

This syntax is helpful when you want to span a grid item towards a grid line using its name. But you are aware that there is more than one grid line with the same name using this method, so we add an integer to say we want the N of that grid line.

Читайте также:  Type модуль typing python

See grid-row-start and grid-row-end for more information and examples of the syntax of these individual properties.

Let’s look at a few different scenarios where grid-row can be used.

Many of us usually think of CSS Grid as a tool to create the main structure and layout of a web page or an application, but CSS Grid is capable of much more than that.

Take the following chart as an example. If you look closely you can see that each bar spans from a certain row to another to show the range that is supposed to mark in the chart.

A bar chart with bars that span single columns and various numbers of rows, like a calendar weekly view.

If you see the chart as a grid, then you see how easily you can specify the size and the position of the bars using CSS Grid placement properties.

A bar chart with bars that span single columns and various numbers of rows, like a calendar weekly view. The grid lines are shown.

To establish a grid for this chart you can write the following code:

Now, we need to place all the bars into the right rows and columns:

.bar:nth-child(1) < grid-column: 1; grid-row: 1 / 3; >.bar:nth-child(2) < grid-column: 2; grid-row: 5 / 10; >/* . */ .bar:nth-child(10) < grid-column: 10; grid-row: 9 / 10; >

Check out the demo and see how the rest of it comes together:

When positioning items across the grid, we can stack or overlap them on top of each other. This gives us the ability to sometimes use CSS Grid as an alternative to absolute positioning. For instance, we can put a caption layer on top of an image without using the position property as demonstrated below:

 
how dare you leave alt empty?
The caption of our image

Cabin in the woods with a green to blue gradient overlay and the words the caption of our image on top.

By default, grid items stack in the source order, but you can control their level using the z-index property. In the following example, we overlap some items and we use the z-index property to bring the second item to the highest level in the stacking context:

Four overlapping grid items in a five-by-five grid.

One thing to note when using the grid placement properties is the issue caused by rearranging the items. When you change the position of an item, only the visual order of the grid items changes, and that order might not be the same as the original document order. This may cause a very bad experience for someone tabbing through the document on a keyboard or listening to a screen reader that reads the content in the same order as the HTML.

So, avoid changing the order of grid items when the HTML order of the elements matters. For example, it can be good for a random image gallery but perhaps not so much for your form inputs.

However, at the time of this writing, there is a proposal to tackle this issue that will hopefully resolve this concern in the future.

You can change the value of grid placement properties in the demo to see what happens to the third grid item:

Источник

Оцените статью