Модуль CSS Grid Layout
Строки и колонки — основа системы раскладки на основе сетки, которую предоставляет модуль CSS Grid Layout.
С ним сложные веб-раскладки строятся легко.
Возможны и адаптивные раскладки, для которых не нужны ни float, ни position.
Grid против Flexbox: Они различаются числом измерений. Flexbox располагает содержимое в одном, строки ИЛИ колонки, тогда как CSS Grid располагает его в двух, строки И колонки.
Grid-раскладка с именованными областями
Здесь CSS Grid и именованные grid-области строят полную раскладку страницы, состоящую из заголовка, меню, области основного содержимого, правой секции и футера.
Output: complete HTML page
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Grid Layout Named Areas</title>
<style>
.container {
display: grid;
grid-template-areas:
'header header header'
'menu main right'
'footer footer footer';
grid-template-columns: 1fr 3fr 1fr;
gap: 10px;
padding: 10px;
background-color: #2196F3;
}
.container > div {
background-color: #f1f1f1;
padding: 20px;
text-align: center;
font-size: 18px;
}
.header {
grid-area: header;
}
.menu {
grid-area: menu;
}
.main {
grid-area: main;
}
.right {
grid-area: right;
}
.footer {
grid-area: footer;
}
</style>
</head>
<body>
<h1>Grid Layout with Named Areas</h1>
<div class="container">
<div class="header">
<h2>My Header</h2>
</div>
<div class="menu">
<p><a href="#">Link 1</a></p>
<p><a href="#">Link 2</a></p>
<p><a href="#">Link 3</a></p>
</div>
<div class="main">
<h2>Main Content</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam.</p>
</div>
<div class="right">
<h2>Right</h2>
<p>Some content here.</p>
</div>
<div class="footer">
<p>Footer</p>
</div>
</div>
</body>
</html>
Компоненты CSS Grid
Любая сетка состоит из двух частей:
- Grid-контейнер - родительский элемент, у которого свойство
displayимеет значениеgridилиinline-grid. - Один или несколько grid-элементов - любой прямой дочерний элемент контейнера автоматически становится grid-элементом.
Grid-контейнер с пятью grid-элементами
В примере ниже синяя область — это grid-контейнер, в котором находятся пять grid-элементов.
Примечание: В следующих главах grid-контейнеры и grid-элементы рассматриваются подробнее.
Пример
Пять grid-элементов заполняют три колонки внутри одного grid-контейнера.
Output: complete HTML page
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Grid Container with Five Items</title>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>Grid Container with Five Items</h1>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
</body>
</html>
Все свойства CSS Grid
Все свойства CSS Grid приведены в таблице ниже.
Справочник свойств CSS Grid
<table>
<tr>
<th>Property</th>
<th>Description</th>
</tr>
<tr>
<td>align-content</td>
<td>Vertically aligns the whole grid inside the container (when total grid size is smaller than container)</td>
</tr>
<tr>
<td>align-items</td>
<td>Specifies the default alignment for items inside a flexbox or grid container</td>
</tr>
<tr>
<td>align-self</td>
<td>Aligns the content for a specific grid item along the column axis</td>
</tr>
<tr>
<td>display</td>
<td>Specifies the display behavior (the type of rendering box) of an element</td>
</tr>
<tr>
<td>column-gap</td>
<td>Specifies the gap between the columns</td>
</tr>
<tr>
<td>gap</td>
<td>A shorthand property for the row-gap and the column-gap properties</td>
</tr>
<tr>
<td>grid</td>
<td>A shorthand property for the grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and the grid-auto-flow properties</td>
</tr>
<tr>
<td>grid-area</td>
<td>Either specifies a name for the grid item, or this property is a shorthand property for the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties</td>
</tr>
<tr>
<td>grid-auto-columns</td>
<td>Specifies a default column size</td>
</tr>
<tr>
<td>grid-auto-flow</td>
<td>Specifies how auto-placed items are inserted in the grid</td>
</tr>
<tr>
<td>grid-auto-rows</td>
<td>Specifies a default row size</td>
</tr>
<tr>
<td>grid-column</td>
<td>A shorthand property for the grid-column-start and the grid-column-end properties</td>
</tr>
<tr>
<td>grid-column-end</td>
<td>Specifies where to end the grid item</td>
</tr>
<tr>
<td>grid-column-start</td>
<td>Specifies where to start the grid item</td>
</tr>
<tr>
<td>grid-row</td>
<td>A shorthand property for the grid-row-start and the grid-row-end properties</td>
</tr>
<tr>
<td>grid-row-end</td>
<td>Specifies where to end the grid item</td>
</tr>
<tr>
<td>grid-row-start</td>
<td>Specifies where to start the grid item</td>
</tr>
<tr>
<td>grid-template</td>
<td>A shorthand property for the grid-template-rows, grid-template-columns and grid-areas properties</td>
</tr>
<tr>
<td>grid-template-areas</td>
<td>Specifies how to display columns and rows, using named grid items</td>
</tr>
<tr>
<td>grid-template-columns</td>
<td>Specifies the size of the columns, and how many columns in a grid layout</td>
</tr>
<tr>
<td>grid-template-rows</td>
<td>Specifies the size of the rows in a grid layout</td>
</tr>
<tr>
<td>justify-content</td>
<td>Horizontally aligns the whole grid inside the container (when total grid size is smaller than container)</td>
</tr>
<tr>
<td>justify-self</td>
<td>Aligns the content for a specific grid item along the row axis</td>
</tr>
<tr>
<td>place-self</td>
<td>A shorthand property for the align-self and the justify-self properties</td>
</tr>
<tr>
<td>place-content</td>
<td>A shorthand property for the align-content and the justify-content properties</td>
</tr>
<tr>
<td>row-gap</td>
<td>Specifies the gap between the grid rows</td>
</tr>
</table>