Understanding CSS Alignment Properties Justify and Align

After defining the layout, the next step is to deal with the alignment of items. There are nine different alignment properties, as shown in the list below:

  • align-content
  • align-items
  • align-self
  • justify-content
  • justify-items
  • justify-self
  • place-content
  • place-items
  • place-self

This is one of the most complex topics in HTML and CSS, as they do different things for different layout systems.

We'll start by discussing how they work inside a grid layout.

Grid layout

When it comes to aligning items in a grid, we have to talk about both row and column alignments. As an example, this is a grid with six items, and the grid container is 300px high:

html
1<div class="container">
2  <div class="item">1</div>
3  <div class="item">2</div>
4  <div class="item">3</div>
5  <div class="item">4</div>
6  <div class="item">5</div>
7  <div class="item">6</div>
8</div>
css
1.container {
2  display: grid;
3  grid-template-columns: auto auto auto;
4  gap: 10px;
5  height: 300px;
6}

Row alignment

Open demo in new tab

The align-content property is used to specify how the rows are positioned in a multi-row grid or flex container. For a grid layout, the common values include:

  • start
css
1.container {
2  align-content: start;
3}

align content start

  • end
css
1.container {
2  align-content: end;
3}

align content end

  • center
css
1.container {
2  align-content: center;
3}

align content center

  • space-between
css
1.container {
2  align-content: space-between;
3}

align content space between

  • space-around
css
1.container {
2  align-content: space-around;
3}

align content space around

  • space-evenly
css
1.container {
2  align-content: space-evenly;
3}

align content space evenly

  • stretch
css
1.container {
2  align-content: stretch;
3}

align content stretch

Open demo in new tab

align-items, on the other hand, is used to define how the items are positioned within their respective grid cell. Common values include:

  • start
css
1.container {
2  align-items: start;
3}

align items start

  • end
css
1.container {
2  align-items: end;
3}

align items end

  • center
css
1.container {
2  align-items: center;
3}

align items center

  • stretch
css
1.container {
2  align-items: stretch;
3}

align items stretch

  • baseline
css
1.container {
2  align-items: baseline;
3}

align items baseline

Open demo in new tab

Lastly, the align-self property works just like align-items, except it is used on individual grid cells. If the container has the align-items set, it will be overwritten by align-items.

css
1.container {
2  align-items: start;
3}
4
5#Item {
6  align-self: end;
7}

align self

Column alignment

Open demo in new tab

The column alignment, controlled by the justify properties, works in a similar way. First, the justify-content property defines how grid items are distributed horizontally along the main axis. Some common values are:

  • start
css
1.container {
2  justify-content: start;
3}

justify content start

  • end
css
1.container {
2  justify-content: end;
3}

justify content end

  • center
css
1.container {
2  justify-content: center;
3}

justify content center

  • space-between
css
1.container {
2  justify-content: space-between;
3}

justify content between

  • space-around
css
1.container {
2  justify-content: space-around;
3}

justify content around

  • space-evenly
css
1.container {
2  justify-content: space-evenly;
3}

justify content evenly

Open demo in new tab

The justify-items controls how grid items are aligned horizontally within their cells. Some common values are:

  • start
css
1.container {
2  justify-items: start;
3}

justify items start

  • end
css
1.container {
2  justify-items: end;
3}

justify items end

  • center
css
1.container {
2  justify-items: center;
3}

justify items center

  • stretch
css
1.container {
2  justify-items: stretch;
3}

justify items stretch

Open demo in new tab

Similarly, the justify-self property is used on individual grid items to overwrite the default alignment rule set by justify-items.

css
1.container {
2  justify-items: start;
3}
4
5.item-sm {
6  justify-self: end;
7}

justify self

Alignment in both directions

The place properties are a set of shorthand properties that control how the items are justified and aligned simultaneously. For example,

css
1.container {
2  display: grid;
3  grid-template-columns: auto auto auto;
4  gap: 10px;
5  height: 300px;
6
7  place-content: center;
8}

This is the same as:

css
1.container {
2  display: grid;
3  grid-template-columns: auto auto auto;
4  gap: 10px;
5  height: 300px;
6
7  align-content: center;
8  justify-content: center;
9}

place-content

Open demo in new tab

place-items

Open demo in new tab

place-self

Open demo in new tab

Flex layout

The exact same alignment properties can be used for flexbox layouts. However, instead of row and column alignments, the justify properties deal with the alignment in the flexbox's main axis, which depends on the flex-direction property.

If flex-direction is set to row, the main axis is horizontal:

main axis row

When you change the flex-direction property, the main axis also changes.

main axis column

Open demo in new tab

Parallel to the main axis

The justify properties deal with alignments in the direction parallel to the flex main axis. For example, when the flex-direction is set to row:

css
1.container {
2  display: flex;
3  flex-direction: row;
4  flex-wrap: wrap;
5
6  height: 400px;
7
8  /* align-content: center; */
9  justify-content: center;
10}

flex justify center row

And when the flex-direction is set to column:

css
1.container {
2  display: flex;
3  flex-direction: column;
4  flex-wrap: wrap;
5
6  height: 400px;
7
8  /* align-content: center; */
9  justify-content: center;
10}

flex justify center column

Note that the justify-items and justify-self properties are ignored in a flex layout.

Also, instead of start and end, there are flex-start and flex-end, designed specifically for a flex layout. In most cases, the keywords start and end would also work for a flex layout, but you might need to use the flex-* keywords for the best browser compatibility.

Perpendicular to the main axis

On the other hand, the align properties deal with the alignment in the direction perpendicular to the main axis. When the flex-direction is set to row:

css
1.container {
2  display: flex;
3  flex-direction: row;
4  flex-wrap: wrap;
5
6  height: 400px;
7
8  align-content: center;
9}

flex align center row

And when the flex-direction is set to column:

css
1.container {
2  display: flex;
3  flex-direction: column;
4  flex-wrap: wrap;
5
6  height: 400px;
7
8  align-content: center;
9}

flex align center column

Unlike the justify properties, the align-items and align-self properties work in a flex layout.

Similarly, the place properties are a set of shorthand properties that allow you to define justification and alignment at the same time.

You may experiment with the above CodePen demo.