SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

Getting Started with CSS Grid Layout

June 25, 2018 by Alex Fox

CSS has been a central web design tool for years and years now. And yet, CSS layouts have never been quite that elegant. Positioning two elements side-by-side has often required hacks or uncomfortable compromises. It feels like the language has been lagging behind its use since its creation. But now, CSS is jumping to the forefront of web layout tech by finally giving web designers what they need to easily layout elements independently of one another. CSS Grid Layout gives you the tools you need to create flexible, responsive CSS layouts without hacks or compromises. It’s a relatively new technology that more senior designers might not yet be familiar with. Let’s examine how CSS Grid Layout works, and see what it can do.

What Does CSS Grid Layout Do?

CSS Grid Layout lets users line objects up in rows and columns. But surely that was already included in the existing CSS library. After all, websites have shown side-by-side elements since their inception. But now, we can position elements in rows and columns by using flexible tools designed specifically for that purpose. And while CSS grid might smell like tables to some designers, the toolset is far more robust and flexible than rigid table layouts could ever be.

If you’ve ever used frameworks to position objects within a grid, CSS grid will feel extremely familiar to you. There’s only limited syntax to learn, and while it might not seem initially intuitive, you should grasp the concepts relatively quickly. It’s also a little less confusing to visualize layouts than Flexbox, which might be a big step up.

Using CSS Grid Layout

Let’s start with a very basic HTML file, which we will convert to display as a grid. Let’s start with a simple HTML example, which displays a list of inline elements stacked on top of one another.

See the Pen CSS Grid Example on CodePen.

To turn on CSS Grid Layout, we will want to specify the display: grid; property in our CSS. This property can be attached to any item and causes all children of that item to be displayed in grid. We will add the property to our container class, like so:

.container {
    display: grid;
}

Now the grid hasn’t done anything yet. Just setting the grid property creates a one-column grid, so it’s functionally identical to standard layout. We will need to set some properties for our grid to make it work. We can use the code below to set four columns on our grid with a width of 150px each.

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

Fractional Layouts

Of course, setting strict pixel values isn’t ideal, since they don’t transform with the viewport. We can instead set column width with fractions, or fr. These slice up the available space and distribute it to your specification. If we use something like the example below, the columns will cut the available space into four fractions.

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

This will produce an even distribution of the available space.

We can also set columns to take up more than one fraction.

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

This distributes the available space over five fractions, with 1fr indicating 20% of the available column width. These fractions will resize with viewport and CSS properties limiting width.

Setting Gaps

Let’s give our grids a little room to breathe with grid spacing using grid-gap:

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

This produces a 20 pixel buffer around the side of each grid cell. This space also adjusts the available space for fractional division, shrinking the relative size of the boxes.

Repeating

Of course, if you have a complex grid, you don’t want to write out fractions by hand. We can duplicate fraction listings using the repeat function.

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

This allows us to create eight columns at once, all with a setting of 1fr.

Adjusting row size

By default, the browser will create grid sizes that are necessary to contain the content within the grid items. We can ask the browser to make specific adjustments to that automatic process based on our preferences with grid-auto-rows.

.container {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr 1fr;
    grid-auto-rows: 75px;
    grid-gap: 20px;
}

Adjusting Flexibly with minmax()

As always, forcing objects to be a specific size in your CSS is typically not a great idea. We can instead use mixmax(), a function that allows us to select the minimum and maximum size properties for grid-auto-rows and its companion grid-auto-columns.

.container {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr 1fr;
    grid-auto-rows: minmax(75px, auto);
    grid-gap: 20px;
}

This sets the rows’ minimum size to 75px and the maximum size to auto. This change won’t create an immediately obvious visual difference. But if we put content within the tracks, the track will stretch to accommodate it.

Learning More about CSS Grid Layout

This post only touches on the basics of CSS grid layout. If you want to learn more about how to implement it, check out MDN’s excellent tutorials.

You might also like the following posts:

What is Z-Index and How Does It Work?

Using CSS Pseudo-elements and Pseudo-classes like ::before and ::after

Avoiding Common Problems with CSS Grid

Filed Under: CSS Tagged With: css, grid-based editorial design, grids, web design

Recent Posts

  • What Factors Determine the Best Digital Marketing Agency?
  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022

Archives

  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022