Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Right-align flex item?
The CSS flexbox layout provides several methods to right-align flex items within a container. This is a common requirement in web design for creating navigation bars, button groups, and other interface elements where items need to be positioned on the right side.
Syntax
/* Method 1: Using justify-content */
.container {
display: flex;
justify-content: flex-end;
}
/* Method 2: Using margin-left auto */
.container {
display: flex;
}
.right-item {
margin-left: auto;
}
/* Method 3: Using align-self (for column direction) */
.container {
display: flex;
flex-direction: column;
}
.right-item {
align-self: flex-end;
}
Method 1: Using justify-content Property
The justify-content property aligns all flex items along the main axis. Setting it to flex-end moves all items to the right
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
justify-content: flex-end;
background-color: #f0f0f0;
padding: 10px;
gap: 10px;
}
.item {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
</body>
</html>
Three green items are aligned to the right side of the gray container with spacing between them.
Method 2: Using margin-left auto
To right-align only specific items while keeping others on the left, use margin-left: auto on the target item
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
background-color: #f0f0f0;
padding: 10px;
gap: 10px;
}
.item {
background-color: #2196F3;
color: white;
padding: 10px 15px;
border-radius: 4px;
}
.right-align {
margin-left: auto;
background-color: #FF5722;
}
</style>
</head>
<body>
<div class="container">
<div class="item">Left Item</div>
<div class="item">Another Left</div>
<div class="item right-align">Right Item</div>
</div>
</body>
</html>
Two blue items appear on the left side, while one orange item is pushed to the right side of the container.
Method 3: Using align-self Property
When using flex-direction: column, use align-self: flex-end to right-align individual items
<!DOCTYPE html>
<html>
<head>
<style>
.container {
display: flex;
flex-direction: column;
background-color: #f0f0f0;
padding: 10px;
gap: 10px;
width: 300px;
}
.item {
background-color: #9C27B0;
color: white;
padding: 10px 15px;
border-radius: 4px;
width: fit-content;
}
.right-align {
align-self: flex-end;
background-color: #E91E63;
}
</style>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item right-align">Right Item</div>
</div>
</body>
</html>
Items are stacked vertically. The first two purple items align to the left, while the pink "Right Item" aligns to the right side of the container.
Conclusion
Right-aligning flex items can be achieved using justify-content: flex-end for all items, margin-left: auto for specific items, or align-self: flex-end in column layouts. Choose the method based on whether you want to align all items or just specific ones.
