Open In App

How to Right-align flex item?

Last Updated : 10 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Flexbox is a CSS layout module that provides a way to easily align and distribute space among items in a container. The concept revolves around two main axes:

  1. Main-Axis: The primary axis along which the flex items are laid out.
  2. Cross-Axis: The perpendicular axis to the main axis.

The direction and alignment of flex items can be controlled using various Flexbox properties. Here’s how you can align flex items to the right using different techniques.

Representation of flex-box

Right Aligning Flex Items Using Flexbox Properties

Below are two common methods to right-align flex items in a flex container.

Example 1: Right Aligning Using flex-direction Property

When the flex-direction is set to "row-reverse", the flex items are aligned to the right, and the order of the items is reversed. In other words, the items will be laid out from right to left.

html
<!DOCTYPE html>
<html>
<head>
<title>Right Alignment</title>
<style>
  #container 
       { 
            width:600px;
            height: 300px; 
            border: 5px solid black; 
            display: flex; 
            flex-direction: row-reverse;
        } 
  #container div 
        { 
            width: 100px; 
            height: 50px; 
        } 
</style>  
</head>
<body>
<div id="container"> 
        <div style="background-color:#389900;">one</div> 
        <div style="background-color:#08ebb2;">two</div> 
        <div style="background-color:#dd2289;">three</div> 
        <div style="background-color:#fcff66;">four</div>     
 </div> 
</body>
</html>

When flex-direction is set as "row-reverse" it not only right aligns the flex items but reverses the order of the items also. In simple words, flex-items now expand from right to left as shown in the given figure.

Output:

Figure-1

Example 2: Right Aligning Using justify-content Property

When justify-content is set to "flex-end", all flex items are shifted to the right end of the container along the main axis. Unlike the previous example, the items retain their original order and are laid out from left to right.

html
<!DOCTYPE html>
<html>
<head>
<title>Right alignment</title>
<style>
  #container 
       { 
            width:600px;
            height: 300px; 
            border: 5px solid black; 
            display: flex; 
            justify-content: flex-end;
        } 
  #container div 
       { 
            width: 100px; 
            height: 50px; 
        } 
</style>
</head>
<body>
<div id="container"> 
        <div style="background-color:#389900;">one</div> 
        <div style="background-color:#08ebb2;">two</div> 
        <div style="background-color:#dd2289;">three</div> 
        <div style="background-color:#fcff66;">four</div>
 </div>
</body>
</html>

When justify-content is set to "flex-end", it instantly shifts all the flex-items to the end of the flex-container along the main-axis, i.e flex items get right aligned. It is different from the above-used method in terms of direction only as in this, flex-items will expand from left to right only. It will be more clear with the given figure.

Output:

Figure-2

Article Tags :

Similar Reads