How to center an input field using CSS?

In HTML, input fields are used inside forms to collect user data. The input fields are actually the most commonly used form elements which can be used to collect various types of user information such as name, email, phone, password, address, etc.

When we are working with these input fields, one common problem that we all face is their centering inside their form or their parent element.

In this article, We will learn different ways to center an input field using CSS. These methods are as follows:


Method 1: Center an Input Field Using text-align Property

Originally, the text-align property is used to align the text of an element to the left, right or center. But, we can also use this property to center our input fields.

Now, you might be thinking if the text-align property aligns only the text of an element, how can it align the inputs?

Well, this works because the input elements(<input>) are by default inline-block type elements. If an element is of type inline-block, it does not take the full with of its parent. Instead, it takes only as much width as it requires. This is the reason, the text-align property works on inputs.

Let’s say, we have a div element, which contains an input text-box:

<div>
    <input type="text" placeholder="Enter your name"> 
</div>

To center align the input field inside the div element, we can simply set its text-align property to center in our CSS file:

div{
   border: 2px solid blue;
   text-align: center;   /* Center Align */
   height: 100px;
   background: lightgreen;
   padding: 20px;
}

Below is the outcome of the above code:

Center an input field using text-align property

When using this approach, you have to keep in mind that this will also center other elements of the div such as the texts, images, links, etc. if any. So use it wisely.


Method 2: Center an Input Field Using margin: auto

The margin property basically specifies a space around the boundaries(outside borders) of an element. But it can also be used to center align things within their container.

All you need to do is apply margin: auto; on the element that you want to center. But there is one important thing that is to be kept in mind, if the element is not a block-level element, applying margin: auto; will not center it.

In that case, you have to explicitly make the element a block-level element by explicitly setting its display property to block.

Let’s take the same example again:

<div>
    <input type="text" placeholder="Enter your name"> 
</div>

To center align the input field, set its margin to auto along with display: block;

input{
    display: block;  /* Make Block type */
    margin: auto;    /* Auto margin */
}
div{
   border: 2px solid blue;
   height: 100px;
   background: lightgreen;
   padding: 20px;
}

Below is the outcome of the above code:

Center an input using margin auto

As you can see, the input is centered inside the div. The main advantage of this method over the previous one is that it only centers those elements on which we apply the margin: auto;. The remaining elements remain untouched.


Method 3: Center an input field using CSS Grids

CSS Grids are a layout system that allows developers to create two-dimensional layouts with rows and columns. We can also use them for alignment purposes.

Let’s say we have a div element which contains an input field. We have also assigned a grid-container class to the div.

<div class="grid-container">
    <input type="text" placeholder="Enter your name"> 
</div>

To center the input element inside the div, we have to first make the div a grid container which can be done by applying display: grid; property on it.

When you set the display property of an element to grid, the element starts behaving like a grid container. This means that we can now use any grid property on this element.

Now, to horizontally center the input element inside this grid container, you can set the justify-content property to center.

But there is one problem with this method, by default, the grid items take up the full height of the grid container. To stop this, you have to set the align-items property to start. The align-items property is used to align items vertically inside the container.

.grid-container{
    display: grid;
    justify-content: center;  /* Align in the center */
    align-items: start;       /* Set vertical alignment */
    border: 2px solid blue;
    height: 100px;
    background: lightgreen;
    padding: 20px;
}

… And here is the result:

Center an input field using CSS grids

Method 4: Center Input Fields using CSS Flexbox(Modern Way)

Now a days, CSS flexbox has become the most preffered method when it comes to centering things. CSS flexbox is basically a flexible box layout module that lets you create flexible and responsive layouts without using the float and position properties.

You can also use this method to center input elements with ease. Centering is almost similar to the grids.

If you want to use CSS flexbox, you have to simply set the display property of the element to flex. Aftering applying the display: flex; property, the element starts behaving like a flex container.

Let’s say we have a div element with a class flex-container, which contains an input element:

<div class="flex-container">
    <input type="text" placeholder="Enter your name"> 
</div>

Now, if we want to center align the input element inside the flex container, we can set the justify-content property to center just like the grids.

Here also, we have to set the align-items property to start or flex-start, so that the flex items do not take the full height of the flex container.

.flex-container{
    display: flex;   /* Make Flex Container */
    justify-content: center;  /* Align in the center */
    align-items: flex-start;       /* Set vertical alignment */
    border: 2px solid blue;
    height: 100px;
    background: lightgreen;
    padding: 20px;
}

Here is the result of the above code:

Center input using CSS flexbox

If you also want to center the input box vertically, you can set the align-items property to center:

.flex-container{
    display: flex;   /* Make Flex Container */
    justify-content: center;  /* Center horizontally */
    align-items: center;       /* Center vertically */
    border: 2px solid blue;
    height: 100px;
    background: lightgreen;
    padding: 20px;
}

Here is the outcome of the above CSS:

Perfectly center input using CSS flexbox

Conclusion

In this article, we learned four different ways to center align input fields, the text-align: center;, the margin: auto, the grids module and the flexbox module of CSS.

You can use any method based on your requirements. However, the modern and simplest way to center things is the flexbox module.

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts