everything about web

A Comprehensive Guide to the World of Web Design & Development:

CSS, HTML, Performance, Web Design, and all Aspects of Front-End Development

Advertisements

CSS Grid Alignment: Everything You Need to Know

In this article,  you will be taken through a detailed explanation and exploration of the topic how does grid alignments works, and how they can be applied in various settings. So, whether you're a beginner or have prior experience, you're sure to find the information provided useful. Let's delve deeper into the fascinating world of grid alignments.

In this article, we will provide a detailed explanation and exploration of how grid alignments work and how they can be applied in various settings. If you wish to learn more about the basics of grid layout, we recommend starting with Part 1, a more basic guide: https://uipencil.com/2022/09/22/complete-tutorial-for-css-grid-layout-module/

Whether you’re a beginner or have prior experience, you’re sure to find the information provided useful. Let’s delve deeper into the fascinating world of grid alignments.

Essentially, the CSS Grid Layout Module includes 3 distinct types of alignments:

  1. Alignment of the overall grid items along the row/column axis
  2. Alignment of the grid container along the row/column axis
  3. Alignment of the individual grid item along the row/column axis

What are Rows and Columns

row
row
column
column
column
1
2
3
4
5
6

The vertical lines of grid items are columns.
The horizontal lines of grid items are rows.

For Overall Grid Items: Alignment of the grid items along the row/column axis

1: Row : justify-items

The justify-items property is used to define the alignment of all grid items along the row axis.

.grid_container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  justify-items: stretch;
}
.item{
  background: #1fb4ab;
  border:1px solid black;
  text-align: center;
}

stretch default

One
Two
Three
Four
Five
Six

end

One
Two
Three
Four
Five
Six

center

One
Two
Three
Four
Five
Six

start

One
Two
Three
Four
Five
Six

2: Column : align-items

The align-items property is used to define the alignment of all grid items along the column axis.

.grid_container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 50px);
  align-items: stretch;
}
.item{
  background: #1fb4ab;
  border:1px solid black;
  text-align: center;
}

stretch default

One
Two
Three
Four
Five
Six

start

One
Two
Three
Four
Five
Six

end

One
Two
Three
Four
Five
Six

center

One
Two
Three
Four
Five
Six

baseline

One
Two
Three
Four
Five
Six

3: Shorthand : place-items

You can combine the declaration for align-items and justify-items with place-items shorthand.

from:

align-items: center;
justify-items: end;

to:

/*first value from: align-items , 2nd value from: justify-items*/
place-items: center end;

If the second value for justify-items is not defined, it will be set as the same as the first value (align-items). for eg, you can use “place-items: center;” to center align all grid items in both row and column directions.

For Grid Container: Alignment of the grid container along the row/column axis

1: Row : justify-content

The justify-content property is used to define the alignment of the grid container along the row axis.

.grid_container {
  display: grid;
  grid-template-columns: repeat(3, 20px);
  grid-template-rows: repeat(2, 20px);
  justify-content: start;
}

stretch default

1
2
3
4
5
6

center

1
2
3
4
5
6

start

1
2
3
4
5
6

end

1
2
3
4
5
6

space-around

1
2
3
4
5
6

space-between

1
2
3
4
5
6

space-evenly

1
2
3
4
5
6

2: Column : align-content

The align-content property is used to define the alignment of the grid container along the column axis.

.grid_container {
  display: grid;
  grid-template-columns: repeat(3, 20px);
  grid-template-rows: repeat(2, 20px);
  align-content: start;
  height: 150px;
}

stretch default

1
2
3
4
5
6

center

1
2
3
4
5
6

start

1
2
3
4
5
6

end

1
2
3
4
5
6

space-around

1
2
3
4
5
6

space-between

1
2
3
4
5
6

space-evenly

1
2
3
4
5
6

3: Shorthand : place-content

You can combine the declaration for align-content and justify-content with place-content shorthand.

from:

align-content: center;
justify-content: end;

to:

/*first value from: align-content , 2nd value from: justify-content*/
place-content: center end;

If the second value for justify-content is not defined, it will be set as the same as the first value (align-content). for eg, you can use “place-content: center;” to center align the grid container in both row and column directions.

For Grid Items: Alignment of the individual grid item along the row/column axis

1: Row : justify-self

Define the alignment on the individual grid item along the row axis:

.grid_container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
}
.item_stretch{
  background: red;
  justify-self: stretch;
}
.item_start{
  background: blue;
  justify-self: start;
}
.item_end{
  background: pink;
  justify-self: end;
}
.item_center{
  background: yellow;
  justify-self: center;
}

default value is justify-self: stretch;

stretch
start
end
center

2: Column : align-self

Define the alignment on the individual grid item along the column axis:

.grid_container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  height: 100px;
}
.item_stretch{
  background: red;
  align-self: stretch;
}
.item_start{
  background: blue;
  align-self: start;
}
.item_end{
  background: pink;
  align-self: end;
}
.item_center{
  background: yellow;
  align-self: center;
}

default value is align-self: stretch;

stretch
start
end
center

3: Shorthand : place-self

You can combine the declaration for align-self and justify-self with place-self shorthand.

from:

align-self: center;
justify-self: end;

to:

/*first value from: align-self, 2nd value from: justify-self*/
place-self: center end;

If the second value for justify-selfis not defined, it will be set as the same as the first value (align-self). for eg, you can use “place-self: center;” to center align the selected grid item in both row and column directions.

Conclusion

In conclusion, the CSS Grid Layout Module allows for precise alignment of elements within a grid system. Through the use of grid lines, grid tracks, and grid cells, you can align elements both horizontally and vertically in a variety of ways.

Understanding the different types of alignments is crucial to utilizing the full potential of the grid layout and you can create more dynamic and flexible layouts for your website or application. cheers!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

If you found this article helpful and would like to show your support, you can buy me a coffee using the link below. Thanks a bunch!

Choose an amount

$5.00
$10.00
$15.00

Or enter a custom amount

$

Thank you for your contribution!

Donate

Topics:

CSS (27) css pseudo (3) debug (2) design (4) fonts (2) html (6) image (8) layout (5) optimization (10) performance (9) responsive (8) Software-development (1)


Advertisements

Recent Post:

Advertisements

Related Posts

Advertisements
Advertisements
%d bloggers like this: