DEV Community

Cover image for Back-to-Basics: CSS Grid Definitions: Rows & Columns Only
⚡️Ren⚡️
⚡️Ren⚡️

Posted on • Edited on

3 1

Back-to-Basics: CSS Grid Definitions: Rows & Columns Only

Grid is a layout model in CSS that allows the control of sizing and positioning of boxes and their contents. Grid was developed around the idea of adapting to the space available, intelligently resizing elements within a webpage.

Defining a grid and placing its items:

  • grid-template-columns and grid-template-rows only

preview of grid

to get your grid looking like this you'll need to set up you HTML appropriately and then define your grid:

  • HTML:
    <div class="Grid">
    <div class="Item1"></div>
    <div class="Item2"></div>
    <div class="Item3"></div>
    </div>
Enter fullscreen mode Exit fullscreen mode
  • .Grid:
.Grid {
    display: grid;
    grid-gap: 0.5rem;
    grid-template-columns: 0.7fr 1fr 1fr;
    grid-template-rows: 0.5fr 1fr 1fr;
}
// I use grid-gap to give a defining space so that the content is easier to read.
Enter fullscreen mode Exit fullscreen mode

Now that we have our grid defined, we need to place Item1, Item2and Item3 within the grid. For this, we will be going off Line reference of the columns and rows.

line references

To place in a single row or column you just need to assign the starting line of the row or column. You'll see this withItem1's column assignment. To span multiple rows or columns you assign in this configuration starting line / ending line, you'll see this with Item1's row assignment as well as in Item2 and Item3's column assignments:

.Item1 {
    grid-column: 1;
    grid-row: 1 / 4;
}
.Item2 {
    grid-column: 2 / 4;
    grid-row: 1;
}
.Item3 {
    grid-column: 2 / 4;
    grid-row: 2 / 4;
}

Enter fullscreen mode Exit fullscreen mode

This is a super simple start to CSS Grid, only using the basics of grid-template-columns and grid-template-rows with item assignments of grid-column and grid-row.

Here's the pen:

Grid Definition using template Columns & Rows only by Ren Estep (@ren_estep) on CodePen.

At this point, you're probably like CSS Grid is awesome but Can I use it ?
Can I use CSS Grid
Well, it's partially supported for IE11, but for safety sake, I highly suggest a polyfill.
(If you have a preferred polyfill post it in the comments)

Look for a follow-up on defining a grid using grid-template-areas & grid-area

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay