offline version v3


184 of 298 menu

justify-content property

The justify-content property sets an alignment of elements along the main axis for flex blocks and along the inline axis for grids. It applies to parent element.

Syntax

selector { justify-content: flex-start | flex-end | center | space-between | space-around; }

Values

Value Description
flex-start Blocks are pressed to the start of the main (inline) axis.
flex-end Blocks are pressed to the end of the main (inline) axis.
center Blocks are centered on the main (inline) axis.
space-between Blocks are distributed along the main (inline) axis, with the first element pressed to the start of the axis, and the last one to the end.
space-around Blocks are distributed along the main (inline) axis, with the same gap between the first block and the start of the axis, the last block and the end of the axis as between the other blocks.
However, they are not equal, as it might seem: the gaps are summed up and the distance between two blocks is twice as large as between the block and the start/end of the axis.

Default value: flex-start.

Example . The flex-start value

Now the axis is directed from left to right (this is done by flex-direction: row), and the blocks are pressed to the left side:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: flex; justify-content: flex-start; flex-direction: row; border: 1px solid #696989; } #parent > div { width: 100px; height: 50px; border: 1px solid #696989; }

:

Example . The flex-end value

In this example, the axis is also directed from left to right, but the blocks are pressed to the right side, since justify-content is set to flex-end:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: flex; justify-content: flex-end; flex-direction: row; border: 1px solid #696989; } #parent > div { width: 100px; height: 50px; border: 1px solid #696989; }

:

Example . The center value

Now the blocks will be centered regardless of the direction of the main axis:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: flex; justify-content: center; flex-direction: row; border: 1px solid #696989; } #parent > div { width: 100px; height: 50px; border: 1px solid #696989; }

:

Example . The space-between value

The blocks are distributed along the main axis, with the first element pressed against the start of the axis and the last to the end:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: flex; justify-content: space-between; flex-direction: row; border: 1px solid #696989; } #parent > div { width: 100px; height: 50px; border: 1px solid #696989; }

:

Example . The space-around value

The blocks are distributed along the main axis, with the same gap between the first block and the start of the axis, the last block and the end of the axis as between the other blocks. However, the gaps add up and the distance between two blocks is twice as large as between the block and the start/end of the axis:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: flex; justify-content: space-around; flex-direction: row; border: 1px solid #696989; } #parent > div { width: 100px; height: 50px; border: 1px solid #696989; }

:

Example . The center value. Axis downwards

Let's change the direction of the main axis by specifying flex-direction in the column value:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: flex; justify-content: center; flex-direction: column; height: 400px; border: 1px solid #696989; } #parent > div { width: 100px; height: 50px; border: 1px solid #696989; }

:

Example . The space-between value. Axis downwards

Now the blocks will be distributed evenly vertically:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: flex; justify-content: space-between; flex-direction: column; height: 400px; border: 1px solid #696989; } #parent > div { width: 100px; height: 50px; border: 1px solid #696989; }

:

Example . Alignment to the start of the inline (row) axis in grid

Let's set the alignment of our elements to the start of the inline axis:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: grid; justify-content: start; grid-template-columns: 100px 100px; grid-template-rows: repeat(3, 1fr); gap: 10px; padding: 10px; border: 2px solid #696989; height: 200px; width: 400px; } #parent > div { gap: 10px; padding: 10px; box-sizing: border-box; border: 1px solid #696989; }

:

Example . Aligning to the center of the inline axis in grid

Now let’s set the alignment for our elements to the center of the inline axis:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: grid; justify-content: center; grid-template-columns: 100px 100px; grid-template-rows: repeat(3, 1fr); gap: 10px; padding: 10px; border: 2px solid #696989; height: 200px; width: 400px; } #parent > div { gap: 10px; padding: 10px; box-sizing: border-box; border: 1px solid #696989; }

:

Example . Aligning to the end of the inline axis in grid

Let's set the alignment of our elements to the end of the inline axis:

<div id="parent"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> #parent { display: grid; justify-content: end; grid-template-columns: 100px 100px; grid-template-rows: repeat(3, 1fr); gap: 10px; padding: 10px; border: 2px solid #696989; height: 200px; width: 400px; } #parent > div { gap: 10px; padding: 10px; box-sizing: border-box; border: 1px solid #696989; }

:

See also

  • the flex-direction property
    that specifies a direction of the flex block axes
  • the align-items property
    that sets an alignment along the cross axis
  • the flex-wrap property
    that sets the multi-line nature of flex blocks
  • the flex-flow property
    a shorthand for flex-direction and flex-wrap
  • the order property
    that specifies an order of flex blocks
  • the align-self property
    that specifies an alignment of a single block
  • the flex-basis property
    that sets a size of a certain flex block
  • the flex-grow property
    that sets a greediness of flex blocks
  • the flex-shrink property
    that sets a shrink of flex blocks
  • the flex property
    a shorthand for flex-grow, flex-shrink and flex-basis
enru