Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

Setting up a grid

4m:38s

Although most grids we see used in web design are uniformly-sized, they don’t have to be. CSS grid introduces a number of new properties that make the task of defining a grid much more intuitive and straightforward than before.

Let’s use a basic scenario to illustrate how Grid is used, and better understand some of these new properties. Say you would like to have a grid with 4 equal width columns that have a 20px gap between each column. How would you go about coding this up? Maybe you could do something like this:

.container {
display: grid;
grid-template-columns: 200px 20px 200px 20px 200px 20px 200px;
grid-template-rows: 150px 150px;
}

This code gives you a 7x2 grid that would look like this:

Screenshot of Firefox devtools showing basic 7x2 grid

This sort of works but there are a couple issues with this approach. In this grid, the spacing between our “main” columns are actually columns themselves, which means content could get rendered in these “faux” gaps. There is a better way.

The gap property

The gaps between columns should be actual empty space, and Grid does provide a means of doing that cleanly, with the gap property.

.container {
display: grid;
grid-template-columns: 200px 200px 200px 200px;
grid-template-rows: 150px 150px;
gap: 20px;
}
Screenshot of Safari devtools showing use of the gap property

A responsive grid

The next issue is with the use of fixed values. If the viewport is any bigger or smaller than the size of the grid container, you would end up with either empty white space or having the grid overflow. You can make the column widths relative to the width of the viewport with percentages, so for 4 columns, you might try setting the column widths to 25%:

.container {
display: grid;
grid-template-columns: 25% 25% 25% 25%;
grid-template-rows: 150px 150px;
gap: 20px;
}

However, you would end up with a situation where your grid overflows the size of its container because the total size of the columns plus the requisite gaps have exceeded 100%. That means you would have to take into account the size of the gaps when setting the column widths.

Screenshot of Chrome devtools showing grid columns sized with percentages

You could fix this situation manually by doing some math in your code with the calc() function. You would subtract the total width of all the 3 gaps (3 * 20px) between the columns from the entire viewport width (100%), then divide that value by 4.

.container {
display: grid;
grid-template-columns:
calc((100% - 60px) / 4) calc((100% - 60px) / 4) calc((100% - 60px) / 4)
calc((100% - 60px) / 4);
grid-template-rows: 150px 150px;
gap: 20px;
}

Finally you end up with a 4 column grid that has a 20px spacing between each column which responds to the size of the viewport.

Screenshot of Firefox devtools showing grid columns sized with calc function

But honestly, this is not ideal. Can you imagine having to update this code with new calculations everytime you want to change the size of the gap or the number of columns?

The repeat() function

A grid with 4 columns is reasonably easy to define. But what if your grid has a lot more columns or rows? It would get rather tedious to list out all the values, especially if the column or row widths are exactly the same. In this case, you can use the repeat() function to save you from having to repeat yourself incessantly.

This function allows you to repeat patterns of grid row or grid column sizes. It takes in 2 arguments. The first argument defines the number of repetitions, while the second argument defines the pattern to be repeated. So you could rewrite the grid-template-columns value to as follows instead:

grid-template-columns: repeat(4, calc(100% - 60px) / 4);

This looks slightly better, but there is still the issue of manual recalculations in the event something about the grid changes.

The fr unit

Fortunately, Grid also introduces something called the fr unit. It is a flexible length unit that represents a fraction of leftover space in the grid container. The advantage of having the browser figure out how to handle free space in the browser reduces the odds of having layouts “break” when you hit certain edge cases.

The fr unit becomes very useful in this use case because it lets the browser decide how to allocate space to each column. Instead of doing all that math yourself, you can let the browser handle that for you. Honestly, browsers are better at math than we are. So if you want the browser to allocate all the space available equally between 4 columns, you would do something like this:

.container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 150px 150px;
gap: 20px;
}

The value repeat(4, 1fr) translates to: give 1 unit of available free space to each of the 4 columns. And you end up with a grid that looks like this:

Screenshot of Safari devtools showing use of repeat for defining grid columns

It looks exactly like the version where you used calc() but the code is much shorter and easier to write because you don’t have to do the math. We managed to cover a few of the grid-specific properties in this relatively straight-forward example and will go into more of them when we get to the chapter which dives into layout.