Family Magazine

CSS Grid for Beginners [2022]

By Geoff Griffiths @mmatraining1980

CSS Grid always you to layout items in 2 dimensions. It is similar to Flexbox but in 2D, instead of 1D (apparently).

You have a grid container, like in Flexbox. It is like a square perimeter around all the items or elements.

Grids has lines or tracks between the elements.

Getting Started with CSS Grid

You’ll want a code editor thingy, like Sublime Text, Visual Studio or if you’re old school, Notepad.

The easiest way to do this is to use codepen, but if you have Visual Studio, that’s ideal as you can test your code in real time.

Unless your using inline styles, you’ll want to create an HTML doc and a separate CSS sheet.

  • Getting started

If you want to follow along, you’ll want to add the <HTML> tags to declare you’re created a webpage with HTML, and add a <head>, a page title in <title> </title> tags and a <body> to add your content.
You should then save the HTML doc as something like “index.html”, just make sure it has .HTML at the end of the name.

In a separate doc/sheet, create your style sheet and save it as “styles.css”

You can then link to/connect your HTML doc to your CSS sheet in the <head> of the HTML doc with the code in bold:

<html>
 <head>
 <link rel="stylesheet" href="styles.css">
 <title>Grid Example</title>
 </head>
 <body>
 
 </body>
</html>
  1. In your HTML, create a container to wrap all your items in.

You can create a div to wrap all your items or elements within*

<div class = "grid-container">

2. You also need a div (or tag of some kind) to put yo grid items in

<div class="grid-item grid-item-1"> </div>
<div class="grid-item grid-item-2"> </div>
<div class="grid-item grid-item-3"> </div>

So now your HTML doc should look something like this:

<html>
 <head>
 <link rel="stylesheet" href="background_styles.css">
 <title>Grid Example</title>
 </head>
 <body>
  <div class = "grid-container">
   <div class="grid-item grid-item-1"> </div>
   <div class="grid-item grid-item-2"> </div>
   <div class="grid-item grid-item-3"> </div>
  </div>
 </body>
</html>

In your Styles.css sheet, you now need to declare the CSS Grid stuff.

.grid container {
 display: grid;
}

The code above just tells CSS lords that you are going to use grid, now you need to style the columns or rows.

We’ll declare the grid column sizes. You can use pixels, rems, percentages and all sorts. Pixels is prob the easiest.

To create a table/grid with 2 columns, the first being 200px wide and the second 100px, use the CSS code:

.grid container {
 display: grid;
 grid-template-columns: 200px 100px;
}

Because we declared 3 grid items in our CSS, the 3rd item will be put onto a second row:

CSS Grid for Beginners [2022]

CSS Grid Fractions

People like to use “fractions” for CSS columns. Keeps things relative and simple.

.grid container {
 display: grid;
 grid-template-columns: 2fr 1fr;
}

The code above, would produce the same output as the first example.

we’ve used a total of 3fr’s. 2 thirds are used to set the width of the first column, and 1 third for the final column.

CSS Grid Repeats

If you wanted to have 4 columns, all 100px wide, instead of typing 100px out x 4, you can just use the repeat command (

.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 100px);

CSS Grid Auto-Rows

If you don’t know how big your table or whatever will be, you can use:

grid-auto-rows: 150px;

Now, all the rows that are created, will be 150px tall

If you want to make sure you content, copy or images or whatever, fit in the rows, you can use the MinMax thing.

grid-auto-rows: minmax (150px, auto);

CSS Grid Gaps

If grid is a table, then the gaps are the thicknesses of the lines of the table.

You can use “grid-gap: 10px;” to set both the row and column gap width, or

grid-row-gap: 20px;
grid-column-gap:10px;

To set the row gaps to 20px and the column gap to 10px.

Grid template areas

You can name and define different rows with “grid template areas”

CSS sheet


.grid-container {

grid-template-areas:
 
   "header header"
   "sidebar content"
   "sidebar content"
   "sidebar content"
}

More info here – https://www.w3schools.com/cssreF/tryit.php?filename=trycss_grid-template-areas3

The video should start at the grid template areas section:

Grid Alignment

Works similar to flexbox when it comes to aligning items.

  justify-content: center;

justify-content: center; will place all the items in the middle of the page

Centred Grid

To place the content to the left:

  justify-content: start;
justify-content: space-evenly;

Space-evenly, will spread the grid items evenly across the page:

*Everyone tends to use divs, but according to W3 (the internet police) you should be using divs as a last resort. More info here

Instead of <div class =classname>, you could for example, use <article class=”classname”> or <section class = “classname”>

***Remember to add specific styles to the .grid-container in the CSS sheet, and text-specific styles, such as text-align, in the grid.item styles section***

E.g. put this in the .grid-item styles, not the .grid-container:

.grid-item {
background: green;
text-align: center;
}

Styling a Single Grid Item

Let’s say you have made a grid, with 6 items, but you want the first one to be different to the rest. Probably the easiest way to achieve this, is to add a “.grid-item-1” styles section, below the CSS you’ve created for the rest of the grid items.

CSS Grid for Beginners [2022]

CSS Grid Layout Generator

The Grid Layout Generator that I find easiest to use, is the try it yourself grid layout tool on w3 schools – https://www.w3schools.com/css/tryit.asp?filename=trycss_grid

I find it easier, as the HTML and CSS is divided nicely to .grid-container and .grid-items.

I made this by copying and pasting the code from the W3schools try it yourself grid tool and adding column and row gaps.

If you use the W3 try it yourself tool, remember to click “Run” on the top left, as the code doesn’t update real time.

css grid equal height rows

If you add the value/property thing in CSS, to the .grid-container section of the CSS (assuming you’ve named the class of your container .grid-container) to “grid-auto-rows: 1fr;” – then all the rows should be equal height. Or you could just set the heights to 100px each

css grid background color

In this codepen example, the line

 background-color: #2196F3;

sets the background color to blue.


Back to Featured Articles on Logo Paperblog