Css Grid: Stacking elements
Tags: css, webdev
Published: 2022-09-07 10:10:44

---
title: Css Grid: Stacking elements
published: true
description: In this article we are to have a look at how we can use the power of grid to stack/layer elements on top of each other.
tags: #css #webdev
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf5mj4fymtejc7naz3q8.jpg
# Use a ratio of 100:42 for best results.
---

{% youtube IuhcUosPHMI %}

Follow me on [Youtube](http://www.youtube.com/channel/UCeyqA3y26RlnfUSx3ufgFoA?sub_confirmation=1)

## Stacked elements

What do you mean by "stacked elements"!? Well, as you can see in the image below i use this term for elements that are stacked in z space! Like Photoshop layers as you will! In this case the layers are the image, the gradient and then the text.

![Example of text and gradient placed over an image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wukibklv2e595nww9y5.jpg)

In this article/video i want to show you an elegant way of stacking these elements! But instead of using floats or position absolute we are going to use the power of [css grid](https://developer.mozilla.org/en-US/docs/Web/CSS/grid).

Lets have a look at this html snippet.

```html
<div class="card stack">
    <div class="img"></div>
    <div class="gradient"></div>
    <div class="title">
        <h2>Lorem ipsum dolor sit amet.</h2>
    </div>
</div>
```
Here we have a card "component" that has three (div) children. One to hold an image, on for a gradient and one for the text that should be placed over the image. 

Also notice that the card element has an extra class of "stack". We are going to use this class to create a small reusable utility. 

So lets add the css for that.

```css
.stack {
    display: grid;
}
```
This turns our stack element into an actual grid that is one row high and one column wide. An important concept here is that even though our grid now has only one "cell" it has 4 grid lines like shown in the image below.

![Diagram of the four grid lines](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx484m94ewxzqbyx0im2.jpg)

Here you can see that our grid has a grid line for the left, top, right and bottom side. We can place grid items in between these grid lines by giving them a css [grid-area](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-area?retiredLocale=nl) property.

```css
.stack > * {
  grid-area: <left> / <top> / <right> / <bottom>;
}
```
To place our "layers" we have to tell the grid item between which grid lines it should place itself by giving it a grid-area property and giving it the four grid line numbers. The example above shows you what the order of these values should be. 

```css
.stack > * {
  grid-area: 1 / 1 / 2 / 2;
}
```
With the code above we tell all the child elements to place them self between the first en second horizontal grid lines, and between the first en second vertical grid lines.

Because we place them at the same location in the grid, they will be placed perfectly on top on one another.

We can shorten the grid-area value by only giving it the starting grid lines. Grid will assume that we only want to cover the adjacent grid cell.

```css
.stack > * {
  grid-area: 1 / 1;
}
```

### Pushing the text to the bottom.

In the example layout the text is positioned at the bottom of the image. for this task we can use grid's [align-self](https://developer.mozilla.org/en-US/docs/Web/CSS/align-self)

```css
.card .title {
    align-self: end;
}
```
Here we added the align-self property with a value of end to the title div, which will push anything inside this element to the bottom.

Subscribe to my [Youtube channel](http://www.youtube.com/channel/UCeyqA3y26RlnfUSx3ufgFoA?sub_confirmation=1)
Buy me a [coffee](https://www.buymeacoffee.com/since1979)
Come chat on [Twitter](https://twitter.com/Vanaf1979) 
And follow me [here](https://dev.to/vanaf1979) for more articles

**Thanks for your precious time, and have an awesome day!**