Redi School Munich - Spring 2021
Flexbox
π What do you remember?
First, we started with tables, floats, positioning and inline-block
That was quite hacky & lacked functionality π±βπ»
Then we were given Flexbox
Much better, but for 1D layouts
Finally, Grid gives us the necessary flexibility to work on a 2D level
Parent of all grid items
Item 1Item 2Item 3Item 4Item 5
Children of grid container
Item 1Item 2Item 3Item 4Item 5
Dividing lines that structure grid
Vertical or horizontal
Item 1Item 2Item 3Item 4Item 5
Single grid unit
Space between adjacent rows and adjacent columns
Item 1Item 2Item 3Item 4Item 5
Space between adjacent grid lines
Represent columns & rows of grid
Space surrounded by 4 grid lines
Can contain any number of cells
Item 1Item 2Item 3Item 4Item 5
display
Values
Item 1Item 2Item 3Item 4Item 5.grid-container { display: grid; }
Item 1Item 2Item 3Item 4Item 5
grid-template-columns, grid-template-rows
Values
Item 1Item 2Item 3Item 4Item 5.grid-container { grid-template-columns: 100px 25% auto; grid-template-rows: 40px 100px; }
Item 1Item 2Item 3Item 4Item 5
Item 1Item 2Item 3Item 4Item 5.grid-container { grid-template-columns: [c1] 100px [c2] 25% [c3] auto [c4]; grid-template-rows: [r1] 40px [r2] 100px [r3]; }
Item 1Item 2Item 3Item 4Item 5
grid-template-areas
Values
Item 1
Item 2
Item 3
.grid-container { grid-template-columns: 25% 50% auto 100px; grid-template-rows: auto 70px; grid-template-areas: "header header header header" "main main . sidebar" } .grid-item-1 { grid-area: main; } .grid-item-2 { grid-area: sidebar; } .grid-item-3 { grid-area: footer; }
Item 1Item 2Item 3
grid-template
Values
grid-template: none | <grid-template-rows> / <grid-template-columns>;
Item 1
Item 2
Item 3
.grid-container { /* grid-template-columns: 25% 50% auto 100px; */ /* grid-template-rows: auto 70px; */ /* grid-template-areas: "header header header header" "main main . sidebar" */ grid-template: "header header header header" auto "main main . sidebar" 70px / 25% 50% auto 100px; } .grid-item-1 { grid-area: main; } .grid-item-2 { grid-area: sidebar; } .grid-item-3 { grid-area: footer; }
Item 1Item 2Item 3
column-gap, row-gap
Values
Only created between tracks!
Item 1Item 2Item 3Item 4Item 5.grid-container { grid-template-columns: auto auto auto; column-gap: 10px; row-gap: 15px; }
Item 1Item 2Item 3Item 4Item 5
gap
Values
.container {
gap: row-gap column-gap;
}
If no row-gap is specified, itβs set to value of column-gap
Item 1Item 2Item 3Item 4Item 5.grid-container { grid-template-columns: auto auto auto; /* column-gap: 10px; */ /* row-gap: 15px; */ gap: 15px 10px; }
Item 1Item 2Item 3Item 4Item 5
Properties
justify-items
: Align grid items along row axisjustify-self
: Align single grid item along row axisalign-items
: Align grid items along column axisalign-self
: Align single grid item along column axisValues: start | end | center | stretch
Item 1
Item 2
Item 3
Item 4
Item 5
.grid-container { grid-template-columns: auto auto auto; grid-template-rows: 80px 90px; justify-items: end } .grid-item-1 { justify-self: start; align-self: center; }
Item 1Item 2Item 3Item 4Item 5
Properties
justify-content
: Align tracks along row axisalign-content
: Align tracks along column axisValues: start | end | center | stretch | space-around | space-between | space-evenly
Item 1
Item 2
Item 3
Item 4
Item 5
.grid-container { grid-template-columns: auto auto auto; grid-template-rows: 80px 90px; justify-content: end }
Item 1Item 2Item 3Item 4Item 5
grid-column-start, grid-column-end, grid-row-start, grid-row-end
Values
Item will span 1, if no grid-column-end/grid-row-end is declared. Items can overlap each other. Use z-index to control stacking order.
Item 1
Item 2
Item 3
Item 4
Item 5
.grid-container { grid-template-columns: [l1] 140px [l2] 150px [l3] auto [l4] 150px [l5] 140px [end]; grid-template-rows: [r1-start] 25% [r1-end r2-start] 100px [r2-end r3-start] auto [r3-end]; } .grid-item-1 { grid-column-start: 1; grid-column-end: span l4; grid-row-start: 2; grid-row-end: span 1; }
Item 1Item 2Item 3Item 4Item 5
grid-column, grid-row
.item {
grid-column: <start-line> / <end-line> | <start-line> / span <value>;
grid-row: <start-line> / <end-line> | <start-line> / span <value>;
}
.grid-item-1 {
/* grid-column-start: 1; */
/* grid-column-end: span l4; */
/* grid-row-start: 2; */
/* grid-row-end: span 1; */
grid-column: 1 / span l4;
grid-row: 2 / span 1;
}
grid-area
.item {
grid-area: <name> | <row-start> / <column-start> / <row-end> / <column-end>;
}
/* as shorthand */
.grid-item-1 {
/* grid-column: 1 / span l4; */
/* grid-row: 2 / span 1; */
grid-area: 2 / 1 / 2 / span l4;
}
/* for naming */
.grid-item-1 {
grid-area: main-content;
}
In your group, use the documentation on CSS Grid & the web to research on your property/function.
Tasks:
Item 1
Item 2
Item 3
Item 4
Item 5
.grid-container{ display: grid; grid-template-columns: 250px 3fr 2fr; } .grid-item-1 { grid-column: 1 / 4; } .grid-item-2 { grid-row: 2 / 4; } @media only screen and (max-width: 1000px){ .grid-container{ grid-template-columns: 1fr 1fr; } .grid-item-1 { grid-column: 1 / 3; grid-row: 2 / 3; } .grid-item-2 { grid-row: 1 / 1; } }
Item 1Item 2Item 3Item 4Item 5
The "ReDI News" team wants to setup an online version of their newspaper. They ask you the help them with this task. As they would like to reach as many readers as possible, the page should be easily readable on a laptop and phone screen.
The team would like you to create a website, the start page, which comprises the following content: