In this article, we will learn how to center the input field using three different methods in CSS, which are:
- Center an input field using “text-align”
- Center an input field using “margin”
- Center an input field using “grid”
In order to align the input field in the center of the HTML element; first, you must learn the method to create the input field and then style it.
Let’s begin!
How to Create Input Field in Div Using HTML?
In HTML, we will create a div and assign the class “div” to it and add a heading <h1> inside the <center> tag. After that, create a sub div and assign the class name as “input” and an input field in it using the <input> tag; set the type of input field as “text”, and in the placeholder, add the text as “Enter your name”. The added will disappear when a user types anything in the input field
HTML
In the output below, you can see that the input field is successfully created in the div:
After that, move to CSS for styling the HTML elements.
How to Style Div Using CSS?
In the CSS file, use “.div” to access the div created in HTML. Then, set the height of the div as “250px”, add a background color as “rgb(134, 240, 227)” and set the border width as “10px”, style as “ridge”, and color as “rgb(2, 141, 127)”.
CSS
height: 250px;
background: rgb(134, 240, 227);
border: 10px ridge rgb(2, 141, 127);
}
Output
Now, we will check out different methods to center align an input field using CSS.
Method 1: How to Center an Input Field Using “text-align”?
In order to center the input field, we will utilize the “text-align” property of CSS. It is utilized to set the horizontal alignment of text in the HTML elements such as left, right, center, and justify.
Syntax
The syntax of the text-align property is:
The description of the above-provided syntax is given below:
- left: It is the default value of the text-align property utilized to set the text on the left side of the HTML element.
- right: It is used to align text on the right side.
- center: It specifies the center alignment of the text.
- justify: By using it, the words are spread out into a full line.
Let’s continue the example and align the input field in the center of the div.
Example
Here, we will use the “.input” to access the sub div in which the input field is created. After that, we will set the value of the text-align property as “center” to center the input field:
text-align: center;
}
The following image shows that the input field is aligned at the center of the div:
Method 2: How to Center an Input Field Using “margin”?
To create a transparent area around an element, the “margin” property of the CSS is used. This property allows you to set the margin of an element from the left, right, top, and bottom sides. It is also a shorthand property of margin-bottom, margin-top, margin-right, and margin-left properties. In one line, you can set all four values.
Syntax
The syntax of the margin property is given below:
The “length” and “auto” are the values of the margin property used for:
- length: It is used to set the margin manually.
- auto: It allows the browser to set the margin around an element by itself.
Let’s continue the example and center the input field.
Example
Consider the previous example and change the style of the border using the border property. After that, inside the “input” class, use the margin property to center align the input field. Set the value of the margin as “auto” and display it as “block” to center the input field. Here, we have used the value of the display property as a block because it allows an element to display in a block and takes up the whole line width:
margin: auto;
display: block;
}
Using the code above, the following output is obtained:
After the successful implementation of the second method, we will move to the third method.
Method 3: How to Center an Input Field Using “grid”?
To center the input field, we will use the display property and set its values as “grid”. It allows an element to display in a grid container.
Syntax
The syntax of the display property is:
As a continuation of the previous example, let’s center align the input field.
Example
Use the “.input” to access the sub div in which the input field is created and set the value of the display property as “grid” to set it in a grid container. After that, set the grid container in the “center” of the div by using the justify-content property:
display: grid;
justify-content: center;
}
The given output signifies that the input field is aligned in the center of the div:
That’s it! We have explained the method of centering an input field using CSS.
Conclusion
Input fields are placed in the center of the div by using three different methods of CSS, which are the “grid” values of the display property, the “text-align”, and “margin” properties. These properties allow you to adjust the elements as per your requirements. In this guide, we have discussed three methods to adjust the input field in the center.