How to Right Align Flex Item Using CSS
Topic: HTML / CSSPrev|Next
Answer: Use the CSS margin-left
Property
You can simply use the CSS margin-left
property with the value auto
to right align flex item within a flex container. Please note that all child elements of flex container automatically become flex items.
The last item in the following example having the class .ml-auto
will be automatically aligned to the right side. Let's try it out to understand how it basically works:
Example
Try this code »<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Right-align Flex Item Using CSS</title>
<style>
.flex-container {
display: flex;
background: #eee;
}
.item {
padding: 10px;
}
.ml-auto {
margin-left: auto;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="item"><a href="#">Home</a></div>
<div class="item"><a href="#">About</a></div>
<div class="item"><a href="#">Services</a></div>
<div class="item ml-auto"><a href="#">Login</a></div>
</div>
</body>
</html>
Related FAQ
Here are some more FAQ related to this topic: