CSS Grid provides a two-dimensional grid system for page layout, allowing elements to be positioned in rows and columns. Some key advantages of CSS Grid include having full control over page layout without needing additional HTML containers, and the ability to easily create complex column-based and row-based layouts. CSS Grid terminology includes grid container, grid items, grid lines, grid cells, tracks and areas. Properties like grid-template-columns, grid-template-rows and grid-area can be used to define the grid structure and position items.
World of CSSGrid
By Elad Shechter
https://www.facebook.com/groups/css.masters.israel/
2.
Advantages of CSSGrid
● Layouting with CSS, real freedom
to achieve any structure layout
with CSS only, like it should be.
(HTML div tag container is
unnecessary with CSS grid)
3.
Advantages of CSSGrid
● Has the advantages of flexbox, and can work in 2 dimension
very easily.
4.
Grid terminology
● GridContainer
● Grid Item
● Grid Line
● Grid Cell
● Grid Track
● Grid Area
● Grid Gap
Grid Item
Any elementthat is a direct
descendant of the grid.
<div class=”site”>
<header class=”child”></header>
<main class=”child”></main>
<aside class=”child”>
<div class=”child-of-child”></div>
<!-- doesn’t effected-->
</aside>
<footer class=”child”></footer>
</div>
Base method
Basic methodwe only defined the columns in a row.
The grid items automatically populate grid from top left
to bottom right based on HTML source order, and
adding rows if needed according the first columns row.
.site{
display:grid;
grid-template-columns: 2fr 1fr 1fr;
}
<div class=”site”>
<header class=”mastheader”></header>
<h1 class=”page-title”></h1>
<main class=”main-content”></main>
<aside class=”sidebar”></aside>
<footer class=”footer”></footer>
</div>
Example:
https://codepen.io/elad2412/pen/JrLeMG
18.
Before we continue
Sayhello to the
repeat function
grid-template-columns:1fr 1fr 1fr;
=
grid-template-columns:repeat(3 , 1fr);
19.
List Method
Using the“base method”and the
“repeat function” we can easily
achieve list sequence of items. (easy
to define for every responsive break points the
amount of item in a row)
.site{
display:grid;
grid-template-columns: repeat(3, 1fr);
}
<ul class=”common-list”>
<li class=”common-list-item”>1</li>
<li class=”common-list-item”>2</li>
<li class=”common-list-item”>3</li>
…
</ul>
Example:
https://codepen.io/elad2412/pen/rGdomM
20.
Positioning Method
Position griditem according the grid lines.
.site{
display:grid;
grid-template-columns: 2fr 1fr 1fr;
grid-template-rows:auto 1fr 3fr;
}
.masthead{
grid-column:2/4;
grid-row:2/3;
}
same as:
.masthead{
grid-column-start:2;
grid-column-end:4;
grid-row-start:2;
grid-row-end:3;
}
Example:
https://codepen.io/elad2412/pen/mBxagg
21.
Positioning Method
More syntaxoptions
grid-row:2/3;
=
grid-row:2; /*will take on cell*/
grid-column:2/4;
=
grid-column:2/span 2; /*will take 2 cell;
Example:
https://codepen.io/elad2412/pen/mBxagg
grid-auto-flow
The default flow(direction) of a grid is row, It means if there isn’t more space the
left item will drop to new row. But like every definition it is changeable.
.site{
display:grid;
grid-template-columns:repeat(3,1fr);
grid-template-rows:repeat(3,1fr);
grid-auto-flow:column;
}
27.
Grid Units
Beside thefr unit, you can use every unit, like pixels, percents.
grid-template-columns: 70% 30%;
=
grid-template-columns:7fr 3fr;
=
grid-template-columns:2.33fr 1fr;
28.
Grid Units -Auto Unit
Auto unit - will take what lefts from the container.
grid-template-columns: 200px auto 200px;
29.
grid-auto-rows/columns & minmaxfunction
When columns or rows aren't declared you can use this property to declare fix
height/width to undeclared columns or rows, or use the new minmax function.
30.
MinMax Function
The minmax()function accepts 2 arguments: the first is
the minimum size of the track and the second the
maximum size. Alongside length values, the values can
also be auto, which allows the track to grow/stretch based
on the size of the content.
.site{
display:grid; height:100%;
grid-template-columns:1fr 1fr 1fr;
grid-auto-rows:minmax(100px,auto);
grid-gap:10px;
}
Minimum height of 100px
Example:
https://codepen.io/elad2412/pen/MEGQBr
31.
Carousel with Grid?
grid-auto-flow:column; without rows is the nowrap of grid.
.site{
display:grid;
grid-auto-flow:column;
}
<div class="site">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
Example:
https://codepen.io/elad2412/pen/YrLErW
32.
Naming Grid Lines
Gridlines can be named when defining the grid with the grid-template-rows and grid-template-columns
properties. Line names can then be referenced to position grid items.
grid-template-rows: [row-1-start] 1fr [row-2-start] 1fr [row-2-end];
grid-template-columns: [col-1-start] 1fr [col-2-start] 1fr [col-3-start] 1fr [col-3-end];
33.
Naming Grid Lines- Multiple Names
Multiple names can be assigned to grid lines by adding names within square brackets and separating each with a whitespace.
Each line name can then be referenced when positioning grid items by line names.
grid-template-rows: [row-start row-1-start] 1fr [row-1-end row-2-start] 1fr [row-2-end row-end];
grid-template-columns: [col-start] 1fr [col-2-start] 1fr [col-3-start] 1fr [col-end];
34.
Naming Grid Lines- Positioning Items by Line Names
grid-row: row-2-start / row-end;
grid-column: col-2-start / col-end;
35.
Overlapping Grid items
Griditems can be layered/stacked by properly positioning them and assigning z-index when necessary.
Example: https://codepen.io/elad2412/pen/yzjjyd
Remember flexbox aligning?
Justify-items,justify-self, align-items, align-self, justify-content, align-content….
All this rules are applying in CSS Grid and you can use them here.
You can learn them from my presentation of world of flexbox
https://www.slideshare.net/eladsc/world-of-flexbox
Or from this awesome website
http://learncssgrid.com/#aligning-grid-items
Order property of flexbox is working too.
38.
Support
Full support withoutprefix, almost in all browsers, edge will support it in the next
version of edge.
Old IE and old edge support it, in the older version.
(14/1/2018):