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
How to Align Text in HTML?
In earlier versions of HTML, the align attribute was commonly used to align text to the left, right, or center. However, this attribute is now deprecated in HTML5 and should no longer be used in modern web development. Instead, text alignment is handled more effectively using CSS with the text-align
2 min read
How to align Image in HTML?
Aligning images within a webpage is an essential part of web design. It allows you to control how images are positioned about surrounding content. In HTML, there are several ways to align images using both attributes and CSS techniques, ranging from simple alignment to more advanced responsive layou
4 min read
How to Center Form in HTML?
In HTML, forms are typically displayed as a block element, taking up the full width of their container. While this is often desirable, there are times when you might want to center a form within its container for aesthetic or layout purposes. We will discuss different Methods to center form in HTML:
3 min read
How to align Placeholder Text in HTML ?
The placeholder attribute specifies a short hint that describes the expected value of an input field/text area. The short hint is displayed in the field before the user enters a value. In most of the browsers, placeholder texts are usually aligned on the left. The selector uses the text-align proper
2 min read
How To Create Registeration Form in HTML
HTML Registration Form is a web-based form designed to collect user information such as name, email, password, and other details. It typically includes input fields, validation, and a submit button to register users on a website.HTML Registration FormHow to Create HTML Registration FormBasic Structu
2 min read
HTML <input> form Attribute
The HTML <input> form Attribute is used to specify interactive input fields for web-based forms. A form can contain multiple input fields to accept inputs from the users. It is the most powerful element in HTML. Syntax: <input form="form_id"> Attribute Value: This attribute contains a si
1 min read
HTML <input> align Attribute
The HTML <input> align attribute is used with <input type=â imageâ> to set the horizontal alignment of the image. It is not supported by HTML 5. Note: Since <input type=â imageâ> is not supported by HTML5 you can use CSS there instead of this attribute. The syntax for CSS <input
1 min read
How to define a form for user input using HTML5 ?
In this article, we will learn how to create a form in a webpage by using the <form> tag. This tag is used to create form for user input. Also there are many elements which are used within form tag. Syntax: <form> Form Content... </form> Example: The following code snippet demonstr
1 min read
How to Center Text in HTML?
To center text in HTML, we can use CSS text-align: center property. It aligns the text to center horizontally. The <center> tag was used in older versions to center the text horizontally, but it is depreciated in HTML 5.Center Text using center TagThe <center> tag is a block-level elemen
1 min read
How to Display Suggestions for Input Field in HTML ?
In most cases, there is a field on forms that allows for auto-completion of user input. This can be achieved using the HTML <datalist>. The <datalist> tag works with the input tag to enable users to quickly fill in data on forms by presenting them with a dropdown list of pre-defined opti
3 min read