How to Right-align flex item?

Cover Image for How to Right-align flex item?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Right-align flex item?

You've probably come across a situation where you need to right-align a flex item in a flex container. It could be something like aligning a "Contact" button or any other element to the right side of the container. But the real question is, can we achieve this without using position: absolute? The answer is yes! In this blog post, I'll show you how to do it in a more "flexboxish" way.

The Problem

Let's start by understanding the problem. In the given code snippet, we have a flex container with three flex items (div.a, div.b, div.c). The div.b is the one we want to center, while div.c should be aligned to the right side of the container.

<h2>With title</h2>
<div class="main">
  <div class="a"><a href="#">Home</a></div>
  <div class="b"><a href="#">Some title centered</a></div>
  <div class="c"><a href="#">Contact</a></div>
</div>

<h2>Without title</h2>
<div class="main">
  <div class="a"><a href="#">Home</a></div>
  <!-- <div class="b"><a href="#">Some title centered</a></div> -->
  <div class="c"><a href="#">Contact</a></div>
</div>

The CSS applied to these elements is as follows:

.main {
  display: flex;
}

.a,
.b,
.c {
  background: #efefef;
  border: 1px solid #999;
}

.b {
  flex: 1;
  text-align: center;
}

.c {
  position: absolute;
  right: 0;
}

As you can see, the div.c is right-aligned using position: absolute, but this solution is not very flexible and might cause issues in some cases. So let's find a better way!

The Solution

To right-align the flex item without using position: absolute, we can make use of the margin-left: auto trick. Here's how you can modify the code:

.c {
  margin-left: auto;
}

With this simple addition to the div.c class, the item will be automatically right-aligned within the flex container.

Code Snippet

To see the modified code in action, you can check out this JSFiddle.

Conclusion

Right-aligning a flex item doesn't have to involve complex positioning techniques. By using the margin-left: auto trick, you can achieve the desired result in a more flexible and efficient way. Remember, flexbox is all about making our lives easier as developers!

So go ahead, give it a try, and let us know how it worked for you. Feel free to share your thoughts and any other tips you have in the comments section below. Happy coding!


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello