Что такое Grid-View?
Grid-view делит страницу на строки и столбцы, и многие веб-страницы построены на его основе.
Адаптивные grid-view часто используют 6 или 12 столбцов, а также сжимаются и расширяются вместе с окном браузера.
Создание Grid View
Прежде всего задайте каждому HTML-элементу значение border-box для box-sizing. Тогда общая ширина и высота элемента будут включать его отступы и рамку.
Поместите это правило в начало вашего CSS:
Сброс box-sizing
Благодаря универсальному селектору каждый элемент на странице получает box-sizing: border-box.
* {
box-sizing: border-box;
}
HTML
Grid-контейнер содержит пять элементов сетки: шапку, меню, основное содержимое, раздел фактов и подвал.
HTML-структура сетки
Вот весь HTML сеточной раскладки. У каждого раздела есть собственный <div>, и именно имя его класса связывает его с областью сетки.
<div class="grid-container">
<div class="header"><h1>Chania</h1></div>
<div class="menu">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<div class="content">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete.</p>
<p>The city can be divided in two parts, the old town and the modern city. The old town is situated next to the old harbour and is the matrix around which the whole urban area was developed.</p>
<p>Chania lies along the north west coast of the island Crete.</p>
</div>
<div class="facts">
<h2>Facts:</h2>
<ul>
<li>Chania is a city on the island of Crete</li>
<li>Crete is a Greek island in the Mediterranean Sea</li>
</ul>
</div>
<div class="footer"><p>A footer.</p></div>
</div>
CSS
Затем стили и цвета улучшают внешний вид сеточной раскладки.
Примечание: В следующей главе показано, как медиазапросы и точки перелома работают с разными размерами экрана.
Пример
Вот весь CSS сеточной раскладки. Именованные области для каждого раздела задаёт grid-template-areas, а шапка, меню, содержимое, факты и подвал получают свои цвета и отступы.
Результат: полная HTML-страница
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid Layout</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: "Lucida Sans", sans-serif;
font-size: 17px;
}
.grid-container {
display: grid;
grid-template-areas:
'header'
'menu'
'main'
'facts'
'footer';
background-color: white;
gap: 10px;
}
.header {
grid-area: header;
background-color: purple;
text-align: center;
color: #ffffff;
}
.header > h1 {
font-size: 40px;
}
.menu {
grid-area: menu;
}
.menu ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.menu li {
padding: 8px;
margin-bottom: 7px;
background-color: #33b5e5;
color: #ffffff;
}
.menu li:hover {
background-color: #0099cc;
}
.content {
grid-area: main;
}
.content > h1 {
font-size: 30px;
margin-bottom: 7px;
}
.content > p {
margin-bottom: 7px;
}
.facts {
grid-area: facts;
border: 1px solid #0099cc;
background-color: beige;
padding: 10px;
}
.facts > h2 {
font-size: 20px;
}
.facts li {
margin-bottom: 5px;
}
.footer {
grid-area: footer;
background-color: #0099cc;
color: #ffffff;
text-align: center;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="header"><h1>Chania</h1></div>
<div class="menu">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<div class="content">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete.</p>
<p>The city can be divided in two parts, the old town and the modern city. The old town is situated next to the old harbour and is the matrix around which the whole urban area was developed.</p>
<p>Chania lies along the north west coast of the island Crete.</p>
</div>
<div class="facts">
<h2>Facts:</h2>
<ul>
<li>Chania is a city on the island of Crete</li>
<li>Crete is a Greek island in the Mediterranean Sea</li>
</ul>
</div>
<div class="footer"><p>A footer.</p></div>
</div>
</body>
</html>