|
387 | 387 |
|
388 | 388 | * Manually giving article a flex-basis of 825px(which involves calculation) defeats the purpose of having flex-box
|
389 | 389 | * 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 | + |
| 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; |
0 commit comments