HTML Input Types and Attributes You Must Know As A Beginner

Introduction

In the world of web development, creating user-friendly and interactive interfaces is crucial. HTML input forms are the backbone of user interaction whether it’s a simple contact form or a complex registration process. The <input> tag empowers developers to create versatile and dynamic web forms, enabling users to input various types of data and interact with web applications. In this blog post, we will explore HTML Input Types and Attributes.

What is <input> element?

The <input> HTML element is used to display an input field where the user can enter data, such as text, numbers, dates, checkboxes, radio buttons, etc., and interact with the web applications. As an empty element, it does not have a closing tag.

The <input> element include a range of attributes that controls its behaviour and appearance.

The <label> element is used with the <input> element to define the title for it. <label> element’s “for” attribute and <input> element’s “id” attribute links each other.

Types of input

Let’s explore the types of input element:

  • <input type="text"> defines a single-line text field and allows users to enter single-line text such as names, email addresses, etc.
  • <input type="number"> defines a numeric field and allows users to enter numeric values such as age, weight etc. It also includes up and down arrows to increase or decrease the value.
  • <input type="button" /> defines a button that can be used to trigger javascript functions or any other actions without submitting the form.
  • <input type="email" /> defines an input field that allows users to enter their email addresses.
  • <input type="password" /> defines a password input field that allows users to enter the password. It hides the characters entered by the user, to protect the user’s privacy.
  • <input type="checkbox" /> defines a checkbox that allows users to select one or more options from a list.
  • <input type="radio" /> defines a radio button that allows users to select only one single option from a list.
  • <input type="tel" /> defines an input field that allows the user to enter a telephone number.
  • <input type="search" /> defines a single-line text field that allows users to enter their search queries.
  • <input type="file" /> defines an input field that allows users to select a file to upload. It can be used to upload files such as images, videos, documents, audio, etc.
  • <input type="submit" /> defines a button that allows the user to submit the form.
  • <input type="color" /> defines a field that allows the user to select a colour.
  • <input type="date" /> defines a field that allows users to select a date.
  • <input type="datetime-local" /> defines a field that allows the user to select a date and time, without time zone information.
  • <input type="hidden" /> defines an input field that is not visible to the user. It serves as a storage mechanism for sensitive information like session tokens or other data that must be included in form submissions but remain invisible and unmodifiable to the user.
  • <input type="image" /> defines an image as a submit button.
  • <input type="month" /> defines an input field that allows the user to select month and year.
  • <input type="range" /> defines a field that allows the user to select a value within a range of values using a slider. The default range is 0 to 100, but it can be changed using the minmax, and step attributes.
  • <input type="reset" /> defines a reset button that resets all form values to their default values.
  • <input type="time" /> defines a field that allows users to select the time.
  • <input type="url" /> defines a field that allows the user to enter a URL.
  • <input type="week" /> defines a field that allows the user to select a week and year.

Attributes for <input> element

Let’s explore the attributes of the input element:

  • accept attribute specifies a filter for what file type the user can select to upload.
  • alt attribute specifies an alternate text for the user if for some the image is not available.
  • autocomplete attribute enables or disables the browser’s autocomplete feature for the input field.
  • autofocus attribute specifies that an <input> element should automatically get focused when the page loads. It is a boolean attribute.
  • checked attribute specifies that an <input> element must be checked when the page loads. It is a boolean attribute.
    This can be used only on the <input type="checkbox" /> and <input type="radio" />.
  • dirname attribute enables the submission of the text direction of the input field.
  • disabled attribute disables the input field, preventing user interaction.
  • formaction attribute determines the destination URL where the input control will be processed upon form submission. This attribute overrides the action attribute of the <form> element.

    The formaction attribute is used with <input type="submit" /> and <input type="image" />.
  • formmethod attribute defines the HTTP method for sending form-data. This attribute overrides the method attribute of the <form> element.
    The formmethod attribute is used with <input type="submit" /> and <input type="image" />.
  • formnovalidate attribute specifies that the form is not to be validated during submission. It is a boolean attribute. The formnovalidate attribute overrides the novalidate attribute of the <form> element.
    The formnovalidate attribute is used with <input type="submit" />.
  • height attribute specifies the height of the <input> element.
  • max attribute specifies the maximum value for an <input> element. This is used with the following input types: numberrangedatedatetime-localmonthtime and week.
  • maxlength attribute specifies the maximum number of characters to allow in the <input> element.
  • min attribute specifies the minimum value for an <input> element. This is used with the following input types: numberrangedatedatetime-localmonthtime and week.
  • minlength attribute specifies the minimum number of characters to allow in the <input> element.
  • multiple attribute is a boolean attribute and specifies that the user is allowed to enter more than one value in the <input> element.
    The multiple attribute is used with <input type="email" /> and <input type="file" />.
  • name attribute specifies the name of an <input>element.
  • pattern attribute specifies a pattern for the input value.
    The pattern attribute works with the following input types: textdatesearchurltelemail, and password.
  • placeholder attribute provides a sample value to guide users on what to input.
  • readonly attribute is a boolean attribute and specifies that an input field is read-only.
  • required attribute ensures that the input field must be filled out before submitting the form.
  • size attribute specifies the visible width, in characters, of an <input> element.
    The size attribute works with the following input types: textsearchtelurlemail, and password.
  • src attribute specifies the URL of the image. This is used only with <input type="image" /> .
  • step attribute specifies the interval between numbers in an <input> element. This attribute works with the following input types: numberrangedatedatetime-localmonthtime and week.
  • type attribute specifies the type of an <input> element.
  • value attribute specifies the value of an <input> element.
  • width attribute specifies the width of the <input> element.

Conclusion

HTML input elements are essential components of web forms and play a vital role in facilitating user interaction. By understanding the different input types, attributes, and styling options, you can create compelling and user-friendly forms that capture accurate and relevant data. So go ahead, experiment with HTML input elements, and unleash the power of interactive web forms in your projects.

Keep coding!

Click here to get a comprehensive introduction to CSS flexbox!

Leave a Comment