Skip to content

Commit ecd4602

Browse files
committed
Update
1 parent f2f4a46 commit ecd4602

File tree

7 files changed

+333
-175
lines changed

7 files changed

+333
-175
lines changed

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,87 @@
387387

388388
* Manually giving article a flex-basis of 825px(which involves calculation) defeats the purpose of having flex-box
389389
* So we just use flex-grow to 1 to take up available space
390-
* Add flex: 1 to both the layouts to have equal width
390+
* Add flex: 1 to both the layouts to have equal width
391+
392+
### Introduction to CSS Grid
393+
394+
* The basic difference between CSS Grid Layout and CSS Flexbox Layout is that flexbox was designed for layout in one dimension - either a row or a column. Grid was designed for two-dimensional layout - rows, and columns at the same time
395+
* Grid Container and Grid Items
396+
* display: grid to the Grid Container
397+
* Just like flexbox, the elements stretch across the entire cell
398+
* Each of the Items occupies **exactly the space it needs for its Contents vertically by default**
399+
* Vertically in our case all the Items are as tall as the Tallest element(One item given height of 150px) of the Grid Item
400+
* Horizontally it will stretch
401+
* grid-template-columns to define as many width columns as we want
402+
* Rows would be accommodated accordingly after all the columns are placed
403+
* Rows width can also be specified using grid-template-rows
404+
* gap for Grid gap. There is also separate gaps for row and column
405+
* row-gap and column-gap
406+
* We just have Row Axis and Column Axis(No flex-direction kind of property)
407+
* Grid lines: Separate rows and columns
408+
* Use Chrome Inspect and click on grid
409+
* Grid lines for rows: #rows + 1
410+
* Grid lines for cols: #cols + 1
411+
* Intersection of Grid lines produce Grid Cells
412+
* Gutters are Gaps(row and column-gap)
413+
* Grid track of row and column
414+
![grid](img/grid.png)
415+
416+
### Sizing Grid Columns and Rows
417+
418+
* Using px is rigid
419+
* We will use fr unit
420+
* grid-template-columns: 150px 300px 200px 1fr;
421+
* For last column to take up all available empty space
422+
* Similar to setting flex: 1
423+
* auto keyword to take size to fill exactly its content
424+
* repeat keyword to repeat the unit n number of times
425+
* Explicit rows and Implicit rows
426+
* Explicit because we explicitly specify the size for them in grid-template-rows
427+
* Implicit when it exceeds the space and is added automatically
428+
* With 1fr for all, all the Items are as tall as the Tallest element(One item given height of 150px) of the Grid Item
429+
430+
### Placing and Spanning Grid Items
431+
432+
* Using grid-column and grid-row on the Grid Itemsa
433+
* Use Dev Tools to see the numbers and use them for start and end
434+
* grid-column: 2 / 3
435+
* If the second value is just +1, it can be omitted
436+
* For the above, we can write just, grid-column: 2
437+
* The second value is to span till
438+
* We can use span keyword
439+
* grid-column: 1 / span 3; which is equivalent to grid-column: 1 / 4;
440+
* Start at 1 and span across 3 cells
441+
* -1 to span till the end (grid-column: 1 / -1;)
442+
443+
### Aligning Grid Items and Tracks
444+
445+
* Aligning Grid Tracks(Columns and Rows) inside the Grid Container(Only possible if it is smaller than the Grid Container)
446+
* Just like flexbox, you have justify-content
447+
* align-content instead of align-items for centering vertically
448+
* space-between, start and end
449+
* Now for aligning within the tracks
450+
* align-items for vertically
451+
* justify-items for horizontally
452+
* For overriding individually,
453+
* align-self and justify-self
454+
455+
### Building a Simple CSS Grid Layout
456+
457+
* CSS grid works perfectly well with Flexbox
458+
* So all the 1D layouts, continue using Flexbox
459+
* Like Author
460+
* Navigation
461+
* Overall Page layout is 2D, so we use Grid there
462+
* Just with display: grid, it display in rows by default
463+
* Used grid-template-columns of 2 and then for header and footer stretched it all the way
464+
* We use 300px for the aside but remaining space we just use 1fr to take up available empty space
465+
* The rows take the space required for its content
466+
* So it's **pretty common to only define grid-template-columns**
467+
468+
### Challenge using CSS Grid
469+
470+
* It's very easy for having a 2D layout with Grid
471+
* Wherever 1D layout is used, continue using Flex
472+
* Use grid with 3 columns, 1 of 250px since img is that width and use 1fr for the other 2 columns
473+
* For h2 and button, grid-column: 1 / -1;

img/grid.png

378 KB
Loading

starter/04-CSS-Layouts/css-grid.html

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,67 @@
4040
margin: 40px;
4141

4242
/* CSS GRID */
43+
display: grid;
44+
/* grid-template-columns: 150px 300px 1fr 200px; */
45+
/* grid-template-columns: 1fr 1fr 1fr auto; */
46+
grid-template-columns: repeat(4, 1fr);
47+
/* grid-template-rows: 300px 200px; */
48+
grid-template-rows: 1fr 1fr;
49+
/* height: 600px; */
50+
/* gap: 30px */
51+
row-gap: 40px;
52+
column-gap: 20px;
53+
display: none;
54+
4355
}
56+
/* .el--8 {
57+
grid-row: 1 / 2;
58+
grid-column: 2 / 3;
59+
} */
60+
61+
/* .el--2 { */
62+
/* grid-row: 2 / 3;
63+
grid-column: 1 / 2;
64+
grid-column: 1 / 4; The below is equivalent
65+
grid-column: 1 / span 4; */
66+
/* grid-row: 2;
67+
grid-column: 1 / -1;
68+
} */
69+
70+
/* .el--1 {
71+
grid-column: 1 / 4;
72+
grid-row: 1;
73+
} */
4474

4575
.container--2 {
46-
display: none;
4776
/* STARTER */
4877
font-family: sans-serif;
4978
background-color: black;
5079
font-size: 40px;
51-
margin: 100px;
80+
margin: 40px;
5281

53-
width: 1000px;
82+
width: 700px;
5483
height: 600px;
5584

5685
/* CSS GRID */
86+
display: grid;
87+
grid-template-columns: 125px 200px 125px;
88+
grid-template-rows: 250px 100px;
89+
gap: 50px;
90+
91+
/* Aligning tracks inside container: Distributing empty space */
92+
justify-content: center;
93+
/* justify-content: space-around; */
94+
align-content: center;
95+
96+
/* Aligning items inside cells: Moving items around inside cells */
97+
align-items: center;
98+
justify-items: center;
99+
}
100+
101+
.el--4 {
102+
align-self: start;
103+
justify-self: end;
57104
}
58105
</style>
59106
</head>
@@ -65,7 +112,7 @@
65112
<div class="el el--4">(4) are</div>
66113
<div class="el el--5">(5) amazing</div>
67114
<div class="el el--6">(6) languages</div>
68-
<div class="el el--7">(7) to</div>
115+
<!-- <div class="el el--7">(7) to</div> -->
69116
<div class="el el--8">(8) learn</div>
70117
</div>
71118

0 commit comments

Comments
 (0)