CSE326 Examples77 topics · one complete example each

All topics › Unit 3 · Cascading Style Sheets

Topic 31 · 31 of 43

CSS Grid Layout

All in one: every Grid property and value, side by side

What this example compares
  • One dimension or two: normal flow vs display: flex + flex-wrap vs display: grid alone vs Grid with columns, and a nav bar that belongs to Flexbox
  • Columns and fr: 1fr, 1fr 1fr, 1fr 1fr 1fr, 2fr 1fr, 200px 1fr vs 33.33% three times (it overflows)
  • Rows and gap: automatic rows vs grid-template-rows: 80px auto vs gap: 12px
  • repeat(): six 1fr written out vs repeat(6, 1fr) vs repeat(3, 200px)
  • The floor: repeat(4, 1fr) (squeezes) vs repeat(4, minmax(80px, 1fr)) (spills) vs repeat(auto-fit, minmax(80px, 1fr)) (fits)
  • The responsive gallery: repeat(auto-fit, minmax(200px, 1fr)) at laptop, tablet and phone width, and auto-fit vs auto-fill
  • Spanning: grid-column: span 2 vs 1 / 3 vs 1 / -1 vs grid-row: span 2, and the chapter's featured card
  • Named areas: grid-template-areas + grid-area, the desktop plan vs the phone plan with the same HTML (and the @media that switches them)
  • Aligning: justify-items, align-items, place-items (each item in its cell) vs justify-content, align-content (the whole grid in the box)
  • Centring one thing: display: grid; place-items: center vs the three Flexbox declarations vs display: grid alone
  • Project step 10: Asha Kumar's profile page, with Grid for the page and the course cards and Flexbox for the tags

Point at any word in the code to see what it means (tap on a phone); click a word to keep its meaning on screen. Read the comment above each numbered panel, press Run, then Edit & experiment to try the TRY lines yourself.

The complete code

Loading the example…

What you should see

You should see eleven panels (scroll inside the result to reach them all). In panel 1 the first and third cards stack the six subjects one under another, the second wraps them into ragged lines, and the fourth puts them in two neat columns; below them the nav bar has the name on the left and the links on the right. Panel 2 shows one, two and three equal columns, a two-thirds and one-third split, a fixed 200px box beside a stretchy one, and a last card whose third box pokes out past the dashed border. In panel 3 row 1 is two lines tall, then a fixed 80px, then the boxes move apart with gaps. In panel 4 the first two cards look identical and the third has three 200px boxes per row with empty space on the right. In panel 5 the phone box squeezes four columns, lets four 80px columns spill out, and fits two columns, while the wider box fits all four in a row. Panel 6 shows the same six subjects in 3, 2 and 1 columns, then two boxes stretched across (auto-fit) and two narrow boxes with an empty column beside them (auto-fill). In panel 7 the dark box 1 sits in one cell, then takes two columns (twice), the full width, and two rows; the last card is the featured-course grid. Panel 8 shows the page plan with the sidebar beside the main content, and the phone plan with the main content above the sidebar. In panel 9 the boxes fill their cells, shrink to the middle left-right, up-down, both ways, sit at the top, and in the last two cards the whole small grid moves to the middle and to the bottom. In panel 10 the first two Hello boxes are in the dead centre and the third fills its box. Panel 11 is Asha Kumar's profile with three course cards and four green skill pills. There is no JavaScript on this page, so the Console prints nothing.

Quick check: in panel 2, why does the third box of the 33.33% 33.33% 33.33% card poke out past the dashed border, while the 1fr 1fr 1fr card fits?

Because a percentage is a share of the whole width and does not allow for the gaps: 99.99% plus two 10px gaps is more than 100%. fr shares out only the space that is actually left after the gaps, so it always fits. Set gap: 0 and the percentage card fits as well.