CSS
How to Align a Textbox in HTML Using CSS?
Aligning a textbox properly is crucial for creating a clean and user-friendly form layout. Whether you want to center it, align it to the left or right, or position it within a form, CSS provides multiple techniques to achieve the desired alignment.
In this blog, we will explore different ways to align a textbox using CSS with practical examples.
1. Center a Textbox Horizontally
To center a textbox horizontally within its container, you can use the text-align
, margin
, or flexbox
properties.
Method 1: Using margin: auto;
(Recommended for Forms)
.center-box {
display: block;
margin: 20px auto; /* Centers horizontally */
width: 50%; /* Adjusts the width */
padding: 10px;
}
<input type="text" class="center-box" placeholder="Enter text">
✅ This method works best when the parent container has a defined width.
Method 2: Using text-align: center;
(For Inline Elements)
.center-container {
text-align: center;
}
<div class="center-container">
<input type="text" placeholder="Enter text">
</div>
✅ Use this when aligning textboxes inside a div
.
2. Center a Textbox Vertically and Horizontally
If you want to center a textbox both vertically and horizontally, use Flexbox.
.flex-container {
display: flex;
justify-content: center; /* Centers horizontally */
align-items: center; /* Centers vertically */
height: 100vh; /* Full height of viewport */
}
<div class="flex-container">
<input type="text" placeholder="Enter text">
</div>
✅ This ensures the textbox is perfectly centered on the page.
3. Align a Textbox to the Left or Right
To align a textbox to the left or right, use float
, text-align
, or flexbox
.
Method 1: Using float
(Old Method)
.left-box {
float: left;
}
.right-box {
float: right;
}
<input type="text" class="left-box" placeholder="Left aligned">
<input type="text" class="right-box" placeholder="Right aligned">
✅ Works but can cause layout issues with complex designs.
Method 2: Using text-align
(For Inline Textboxes)
.align-right {
text-align: right;
}
<div class="align-right">
<input type="text" placeholder="Right aligned">
</div>
✅ Use this when aligning textboxes within a div
.
4. Align a Textbox Inside a Form
To align textboxes within a form, use display: flex;
for modern and responsive layouts.
.form-container {
display: flex;
flex-direction: column;
align-items: center; /* Aligns input fields */
}
<form class="form-container">
<input type="text" placeholder="Your Name">
<input type="email" placeholder="Your Email">
<button type="submit">Submit</button>
</form>
✅ Keeps all input fields and buttons aligned properly.
Conclusion
Aligning a textbox in HTML using CSS is simple when using the right techniques:
✅ Use margin: auto;
for centering horizontally.
✅ Use flexbox
for vertical and horizontal centering.
✅ Use text-align
inside containers for inline alignment.
✅ Use float
or flexbox
for left and right alignment.
✅ Use display: flex;
for aligning textboxes in a form.
By applying these CSS methods, you can improve form layouts and enhance user experience on your website.