How to Create a Card Layout Using CSS Grid Cards

How to Create a Card Layout Using CSS Grid Layout

Editor’s note: This guest post was written by Abbey Fitzgerald, a UX software engineer and web designer who loves the art of crafting code.

CSS Grid Layouts are changing the way web designers work, allowing for a more efficient way of laying out website content.

It’s been long awaited, but finally this approach has really taken off, and in many cases, it is in production. It hss revolutionized the way websites are designed and, once designers get the hang of it, it will give more flexibility to web layouts.

If you’ve tried designing with CSS Grid and have checked out tutorials on the web, you may have found information that pertains to an overall layout approach. Things like: header, content, footer, etc.

However, CSS Grid can also be useful for other website details, such as cards. Card layouts are a common way of displaying content, and they can be efficiently created with CSS Grid. By using this method, card content areas are easily repeatable, can be viewed on many different device types, and the size is easily controlled.

CSS Grid Layout Tools

As you’re going along, you’ll want to inspect things on the page. Firefox has great developer tools for seeing grid areas. Using Firefox developer tools, you can see an overlay that outlines the grid. Firefox Developer Edition also has this ability.

card layout css grid layout how to tutorial inspector tools

Select the Overlay Grid you want to display (depending on your design, there can be more than one). The Grid Display Settings allow you to take a closer look by displaying the line number, area names, and how you want the lines to extend. This will be helpful as you design.

Simple Cards With CSS Grid Layout

If you’re new to CSS Grid, you’ll want to brush up on the basics and create a simple CSS Grid Layout. Cards are great a great way to display content, as well as an interesting UI element that’s intuitive to use.

They also provide a great format for users to quickly scan content and engage with what they’re most interested in. Cards also allow designers to format content in a visually appealing way, with great imagery, intro content, links, and more! 

Cards and Overall Page Layout

As an aside, it’s important to know that CSS Grid does not have to be used on an entire page layout. Grids can be used in certain areas of the page if needed.

Since this is a tutorial on card layouts, a grid approach can be used exclusively here even if the rest of the page does not utilize CSS Grid. As long as the grid area is defined and display: grid; is declared on the wrapper, this approach can be used.

Working With a Set Number of Cards

In some cases, it’s easy to plan for a set number of cards. With a set amount of content, the number of cards will stay the same. For example, there may be four cards that explain business offerings, with each offering on its own card.

card layout css grid layout how to tutorial four card row

For this example (and the others), all the .card items are inside a .cards container element. To accomplish this, it’s pretty simple.

[css]
.cards {

display: grid;

grid-template-columns: repeat(3, 1fr);

grid-auto-rows: auto;

grid-gap: 1rem;

}

 

.card {

border: 2px solid #e7e7e7;

border-radius: 4px;

padding: .5rem;

}
[/css]

It’s important to display the value of .cards to grid. Without this, grid areas cannot be created. The next step is to define the grid-template-columns and choose the styling. The actual card styling will be declared with a class of .card.

card layout css grid layout how to tutorial card classes

In this example, you’ll see multiple article tags with the class of .card. See it on Codepen.

Repeat for Set Number of Cards

This is where the cards start taking shape. To create a fluid amount of columns per row, repeat is used with the repeat() function. This function takes two arguments: number of times to repeat and the value to repeat.

card layout css grid layout how to tutorial three card row

For example, grid-template-columns: repeat(3, 200px); computes to grid-template-columns: 200px 200px 200px;.

Fractional Unit

You’ll see that the example code uses a “fr.” What is that exactly? This unit of measurement became popular with CSS Grid. Fr means “fractional unit” and 1fr takes up 1 part of the available space. It helps simplify space calculations.

Fractional units can be used alone or they allow you to specify a width of 1fr and you can add child elements, too. Multiple fr will be equally shared across all child elements.

card layout css grid layout how to tutorial one fr group

Different fractional units can be specified. For example, when grid columns are 1fr 2fr 1fr, two fractional units take up twice the space as one fractional unit.

card layout css grid layout how to tutorial mixed fr group

Repeat for a Dynamic Number of Cards

In some cases, you may not know the number of cards needed, so planning for a set number won’t work. The same approach will be taken as it was with repeat but there is the option to fit as many as needed with autofill.

[css]
grid-template-columns: repeat(auto-fill, 250px);
[/css]

Width declarations are needed in addition to auto-fill. Keen in mind, with a px value, there may be space off to the right when there isn’t enough for the card to fit, but this doesn’t have to be the case, as you will see a bit later.

card layout css grid layout how to tutorial row of cards with space

Minmax for More Control Over Card Width

Minmax is the perfect solution, since a fractional unit by itself would make the cards full width. Adding both repeat and a minmax to the style declaration will get rid of the possible empty space and cards will appear more fluid.

[css]
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
[/css]
card layout css grid layout how to tutorial three card full width
This declaration will tell the browser to fit as many columns based on the sizes specified in the repeat syntax.

Columns will always fit the grid and the cards will be at least 200px. When the minimum space does not perfectly fit the space, this is where the max width comes into play. When another 200px width card won’t fit, the other cards will be larger than 200px, so they stretch to fill the row with more space and it’ll be distributed equally. 

Mobile

Since the smallest and largest cards have been planned, this is a good time to talk about mobile. Cards work well on larger and smaller devices because content is in digestible chunks, and the cards can easily scale up and down. CSS Grid helps create a great mobile experience and in some cases, it’s much easier than having a separate media query.

In this example, cards need to size down to fit in mobile and size up to be a fraction of the space available, so they appear full-width on a smaller device, no separate media query needed:

[css]
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
[/css]

The minimum width of the card is 300px. As many 300px cards that can fit will appear in the row. When the device is very small, there’s a good chance that there isn’t the available space for two cards in a row.

That’s when the 1fr comes into play. The fractional unit is great for responsive designs. When two of the 300px cards won’t fit, this is where the mobile view becomes very apparent. When 1fr has been declared as the max width, a single card will take up all of the available space and they will stack on top of each other.

css grid cards layout how to tutorial for a multi-device card layout

Grid Gap

Now that the width of the cards is established, the decision to have space between them needs to be made.

This property is available on grid containers and it makes it easy to create gutters in the design. Grid-column-gap creates the space between each card. When you see grid-gap, that refers to the shorthand for grid-row-gap and grid-column-gap. Grid-row-gap is the space on the top and bottom of the card, grid-column-gap is the space to the left and right of the card.

CSS Grid Layout Resources

When it comes to CSS Grid Layouts, there is plenty to learn and there are many possibilities. The best way to learn about CSS Grid Layout is to do some additional reading and experimenting. There are many great resources out there, but here are a few to get you started:

Want additional resources for building and designing incredible sites with WordPress? Check out WP Engine’s vast library of educational, how-to content—created for WordPress users of all technical skill levels!

Find out more about WordPress hosting with WP Engine, or speak to a representative now.

Get started.

Build faster, protect your brand, and grow your business with a WordPress platform built to power remarkable online experiences.