How to Align input forms in HTML?
Last Updated :
16 Oct, 2024
In HTML, input forms are typically displayed as block elements, taking up the full width of their container. While this is often desirable, there are times when you might want to align input forms horizontally or vertically within a container for aesthetic or layout purposes.
These are the following approaches to align input forms in HTML:
Using Block and Inline-Block Layouts
To align input forms using simple CSS with block and inline-block layouts, start by wrapping your input fields and labels in a container. Set the labels to display: block, which ensures they take up the full width, stacking the input fields below them. This layout is straightforward and works well for vertical forms.
For a horizontal alignment, you can set the labels to display: inline-block, allowing them to sit next to their corresponding input fields. Ensure that both labels and inputs have consistent widths or use margin to create space between them. Adjust padding for better readability and user experience.
Example: Below is an example of aligning input forms in HTML.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Align Input Forms</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
}
.form-container {
width: 400px;
margin: 50px auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
form {
display: flex;
flex-direction: column;
}
label {
font-size: 14px;
color: #333;
margin-bottom: 5px;
}
input[type="text"],
input[type="email"],
input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
font-size: 16px;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
input[type="submit"]:active {
background-color: #004494;
}
</style>
</head>
<body>
<div class="form-container">
<form>
<label for="username">Username</label>
<input type="text" id="username" name="username" required>
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
Output:
OutputAlign Using Flexbox
Flexbox is a powerful CSS layout model that provides a flexible and efficient way to arrange elements on a page. It's particularly useful for creating responsive and dynamic layouts.
To align input forms in HTML using Flexbox, wrap your input fields and labels in a <div> container and set its display to flex. Choose flex-direction to stack the elements vertically or horizontally, depending on your design. Use separate <div> elements for each label-input pair to maintain proper alignment and spacing. Adjust properties like align-items and justify-content to fine-tune the layout.
- Flex Container: The parent element that contains the flex items.
- Flex Items: The child elements within the flex container.
- Flex Axis: The main axis along which the flex items are arranged.
- Cross Axis: The axis perpendicular to the flex axis.
Example: Below is an example to align input forms in HTML using flexbox:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Align using Flexbox</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 20px;
}
form {
background-color: white;
padding: 20px;
max-width: 500px;
margin: 50px auto;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.form-group {
display: flex;
align-items: center;
margin-bottom: 15px;
}
label {
width: 100px;
font-size: 16px;
color: #333;
}
input[type="text"],
input[type="email"] {
flex-grow: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
input[type="submit"] {
width: 100%;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
input[type="submit"]:active {
background-color: #004494;
}
</style>
</head>
<body>
<form>
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<input type="text" id="phone" name="phone" required>
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
OutputAlign Using Grid Layout
Grid Layout is another powerful CSS layout model that provides a grid-based structure for arranging elements. It's particularly useful for creating complex and responsive layouts.
To align input forms in HTML using CSS Grid Layout, start by wrapping your input fields and labels in a <div> container. Set the container's display to grid and define the columns with grid-template-columns to arrange elements side by side. Use <label> elements to improve accessibility and style them for alignment. Incorporate padding and margins for better spacing, ensuring the form is visually appealing.
Example: Below is an example to align using Grid Layout.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Align Form Inputs with Grid</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 20px;
}
.form-container {
display: grid;
grid-template-columns: 100px 1fr;
gap: 15px 10px;
max-width: 400px;
margin: 50px auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
label {
text-align: right;
padding-right: 10px;
font-size: 14px;
color: #333;
}
input[type="text"], input[type="email"], input[type="password"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
input[type="submit"] {
grid-column: 1 / span 2;
padding: 10px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
input[type="submit"]:active {
background-color: #004494;
}
</style>
</head>
<body>
<div class="form-container">
<label for="username">Username</label>
<input type="text" id="username" name="username" required>
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Submit">
</div>
</body>
</html>
Output:
Output
Similar Reads
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
HTML Interview Questions and Answers
HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Learning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis
8 min read
HTML Introduction
HTML stands for Hyper Text Markup Language, which is the core language used to structure content on the web. It organizes text, images, links, and media using tags and elements that browsers can interpret. As of 2025, over 95% of websites rely on HTML alongside CSS and JavaScript, making it a fundam
6 min read
HTML Tags - A to Z List
HTML Tags are fundamental elements used to structure and format content on web pages. They provide instructions to web browsers on how to render text, images, links, and other media.HTML tags are enclosed in angle brackets < > and usually come in pairs: an opening tag and a closing tag. The cl
15+ min read
HTML DOCTYPE Declaration
HTML DOCTYPE (Document Type Declaration) is an instruction that appears at the beginning of an HTML document, before the <html> tag.Its primary role is to tell the web browser which version of HTML the page is written in, ensuring that the browser renders the content correctly. It is not an HT
4 min read
30+ Web Development Projects with Source Code [2025]
Web development is one of the most in-demand career paths in the IT industry, experiencing consistent growth of around 20â25% annually. Whether you're a student starting out or an experienced professional looking to switch or advance your career, it's essential to go beyond theory and demonstrate yo
4 min read
Frontend Developer Interview Questions and Answers
Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie
15+ min read
HTML Forms
HTML forms, defined using the <form> Tags are essential for collecting user input on web pages. They incorporate a variety of interactive controls such as text fields, numeric inputs, email fields, password fields, checkboxes, radio buttons, and submit buttons. Over 85% of websites rely on for
5 min read
HTML Tables
HTML (HyperText Markup Language) is the standard markup language used to create and structure web pages. It defines the layout of a webpage using elements and tags, allowing for the display of text, images, links, and multimedia content. As the foundation of nearly all websites, HTML is used in over
10 min read