Day 4
Day 4
HTML provides a variety of input types for collecting different types of user data in forms. Here
is a list of the HTML input types:
button
• A push button with no default behavior. Often used with JavaScript for custom functions.
checkbox
• A checkbox that allows the user to select one or more options from a set.
color
date
datetime-local
• An input for entering an email address. Provides validation for email format.
file
• An input for selecting one or more files from the user's device.
hidden
• An input that is not displayed in the browser. Used to store data that needs to be
submitted with the form but not shown to the user.
image
• An image submit button. Acts like a submit button but allows the use of an image as the
button.
month
1
number
password
radio
• A radio button. Allows the user to select one option from a set.
range
reset
search
• An input for entering search queries. Often styled and behaves similarly to a text input
but with additional search-specific features.
submit
tel
text
time
url
2
week
• An input for selecting a week and year, with no specific day or time.
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>All Input Types Form</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
padding: 20px;
}
.container {
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
3
text-align: center;
color: #333333;
}
label {
display: block;
margin-bottom: 8px;
color: #555555;
}
input,
select,
textarea {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #cccccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type="submit"] {
background-color: #007BFF;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
4
font-size: 16px;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>All Input Types Form</h1>
<div class="container">
<form action="#">
<!-- Text input -->
<label for="text">Text:</label>
<input type="text" id="text" name="text" placeholder="Enter text">
6
<!-- Checkbox input -->
<label for="checkbox">Checkbox:</label>
<input type="checkbox" id="checkbox" name="checkbox"> Check me
</html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Form for Beginners</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Simple HTML Form</h1>
<div class="container">
<h2>Beginner's Form</h2>
<form action="#">
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Your name" required>
<label for="age">Age:</label>
<input type="number" id="age" name="age" placeholder="Your age" required>
8
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Your email" required>
<label for="message">Message:</label>
<textarea id="message" name="message" placeholder="Type your message here"
required></textarea>
</html>
CSS
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
padding: 20px;
}
.container {
max-width: 500px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
9
h1, h2 {
text-align: center;
color: #333333;
}
label {
display: block;
margin-bottom: 8px;
color: #555555;
}
input[type="text"],
input[type="number"],
input[type="email"],
textarea {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #cccccc;
border-radius: 4px;
box-sizing: border-box;
}
textarea {
height: 100px;
}
10
input[type="submit"] {
background-color: #007BFF;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
The id attribute in HTML is used to uniquely identify elements within the document. In the
provided code snippet, the id attribute plays a crucial role for several reasons:
o The for attribute of the <label> element is used to bind the label to a specific
form control. The value of the for attribute must match the id of the
corresponding <input> element. This association improves accessibility, making
it easier for screen readers to understand which label corresponds to which input
field.
JavaScript Interactions:
• The id attribute allows JavaScript to easily access and manipulate specific elements.
Using methods like document.getElementById("name"), you can dynamically change
the properties or behavior of the element with that id.
• Although typically classes are used for styling multiple elements, id can be used in CSS
to apply unique styles to a single element. The id selector in CSS is denoted with a #.
Form Submission:
11
• While the id itself is not required for form submission, having unique identifiers for
input fields can help in client-side form validation and handling the form data efficiently.
12