How to Align Flexbox Elements Left, Right, and Center Using CSS?

How to Align Flexbox Elements Left, Right, and Center Using CSS?

You may face difficulties while aligning the elements in the CSS Flexbox, but there are some techniques that make this process easy. In this blog, we will discuss the methods to center one element and move others to the left or right. 

Table of Contents:

Understanding Flexbox basics

It is important to know the Flexbox basics before learning the techniques for aligning the elements. You can set up the container using the display: flex, and the elements inside it turn into flex. This makes you control the arrangement of the elements.  

Properties:

  • justify-content: You can use it to control the element horizontally. 
  • align-items: You can use this for vertical alignment.
  • align-self: You can use it for the alignment of a single element.  
  • flex-grow: It determines the element’s expanding level. 

Methods to Centre One Item and Align with Others

You can create a layout with one element in the center and align others to the right and left. You can use justify-content and margin and flex-grow for this purpose. 

Explore the Best Online Programs to Learn HTML, CSS, JavaScript, and More
Top Web Development Courses to Kickstart Your Coding Journey
quiz-icon

Method 1: Using justify-content and margin

You can use justify-content: space-between to spread the elements and set the margin: auto to center the elements to make it stay in the middle.   

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox Alignment</title>
<style>
.container {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
background-color: lightblue;
}
.left {
margin-right: auto;
}
.center {
margin: auto;
}
.right {
margin-left: auto;
}
</style>
</head>
<body>
<div class="container">
<div class="left">Left</div>
<div class="center">Center</div>
<div class="right">Right</div>
</div>
</body>
</html>

Output:

Using justify-content and margin

Explanation: You can use this code to make the center element remain in the middle and make the left and right elements remain in their position. 

Method 2: Using flex-grow

You can also use the flex-grow property to push the element apart.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox Alignment</title>
<style>
.container {
display: flex;
align-items: center;
padding: 20px;
background-color: lightblue;
}
.left {
flex-grow: 1;
text-align: left;
}
.center {
flex-grow: 0;
text-align: center;
}
.right {
flex-grow: 1;
text-align: right;
}
</style>
</head>
<body>
<div class="container">
<div class="left">Left</div>
<div class="center">Center</div>
<div class="right">Right</div>
</div>
</body>
</html>

Output:

Using flex-grow

Explanation: You can see the left and right elements take equal space using flex-grow: 1 and make the center element stay in the middle by flex-grow: 0

Responsive Adjustments

The media queries can be used for handling different sizes of screens and aligning them by making some adjustments depending on the screen size. This will work perfectly. 

Example:

@media (max-width: 600px) {
.container {
flex-direction: column;
align-items: center;
}
.left, .right {
flex-grow: 0;
margin: 5px 0;
}
}

This way, the elements neatly stack on smaller screens, keeping the layout good and easy to read.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

You can use properties such as justify-content, margin: auto, and flex-grow for aligning the element in the Flexbox. The properties mentioned above are used for keeping other elements to the left and right. Therefore, you can get the webpage with a clean layout. 

How to Align Flexbox Elements Left, Right, and Center Using CSS?– FAQs

Q1. What is Flexbox?

It is the CSS layout, which is used to align and distribute the space between the elements.

Q2. How can I center one element while aligning others to the left or right?

You can use properties like justify-content, margin: auto, and flex-grow for aligning the element.

Q3. Can I align elements differently for various screen sizes?

Yes, media queries can be used for aligning the elements differently for various screen sizes.

Q4. How do I stack elements on smaller screens?

Use flex-direction: column within a media query for stacking elements on smaller screens.

Q5. What’s the difference between justify-content and align-items?

You can use justify-content for aligning elements in the main axis and align-items for aligning in the cross axis.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner