Author: saqibkhan

  • Head Elements

    HTML head elements define metadata like the title, character set, links to external stylesheets, and other details. This information does not display on the webpage but is helpful for the search engines and browsers. The head elements are placed inside the <head> tag.

    The following are the commonly used head elements:

    • <title> Element
    • <meta> Element
    • <base> Element
    • <link> Element
    • <style> Element
    • <script> Element

    Let’s understand each element in detail with the help of examples.

    HTML <title> Element

    The HTML <title> tag is used for specifying the title of the HTML document. The title must describe the content of the web page, and its format should be text only. It appears in the title bar of the browser’s tab.

    Example

    Following is an example that shows how to give a title to an HTML document using the <title> tag:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Title Tag Example</title></head><body><p>Describing the use of title tag</p></body></html>

    HTML <meta> Element

    The HTML <meta> tag is used to provide metadata about an HTML document. The metadata is nothing but additional information about the web page, including page expiry, page author, list of keywords, page description, and so forth. This information is further used for the purpose of search engine optimization. Remember, the metadata specified by the <meta> tag is not displayed on the web page, but it is machine-readable. Its most commonly used attributes are namecontentcharset, and http-equiv.

    Read More: HTML Meta Tags

    Example

    The following example describes a few of the important usages of the <meta> tag inside an HTML document:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Meta Tag Example</title><!-- Provide list of keywords --><meta name="keywords" content="C, C++, Java, PHP, Perl, Python"><!-- Provide description of the page --><meta name="description" content="Simply Easy Learning by Tutorials Point"><!-- Author information --><meta name="author" content="Tutorials Point"><!-- Page content type --><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!-- Page refreshing delay --><meta http-equiv="refresh" content="30"><!-- Page expiry --><meta http-equiv="expires" content="Wed, 21 June 2006 14:25:27 GMT"><!-- Tag to tell robots not to index the content of a page --><meta name="robots" content="noindex, nofollow"></head><body><p>Describing the use of HTML meta tag</p></body></html>

    HTML <base> Element

    The HTML <base> tag is used for specifying the base URL for all relative URLs in a page, which means all the other URLs will be concatenated into the base URL while locating the given item. We are allowed to use only one base element in our HTML document. The most frequently used attributes of the tag are hrefandtarget.

    Example

    In this example, all the given pages and images will be searched after prefixing the given URLs with the base URL http://www.tutorialspoint.com/ directory:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Base Tag Example</title><base href = "index.htm/" /></head><body><img src="/images/logo.png" alt="Logo Image"/><a href="/html/index.htm" title="HTML Tutorial"/>HTML Tutorial</a></body></html>

    On running the above code, the tutorialspoint logo along with a link will be displayed. If we click on that link, it will redirect us to the index page of the HTML tutorial.

    In HTML, the <link> tag is used to specify relationships between the current webpage and another external resource. The source of external resources is placed inside thehref attribute. The other attributes of the tag are reltype, and media. Its most common use is to embed stylesheets into the HTML document.

    Example

    Following is an example of linking an external style sheet file available in the “css” subdirectory within the web root:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML link Tag Example</title><link rel="stylesheet" type="text/css" href="/css/style.css"></head><body><p>It is an example of linking stysheet to the current HTML document.</p></body></html>

    HTML <style> Element

    The HTML <style> tag is used to specify styles either for the whole HTML document or for a particular element. Its most common attributes are title and media.

    Example

    In the following example, we are defining a style for the <p> tag using the <style> tag.

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML style Tag Example</title><base href="http://www.wisdompro.com/" /><style>
    .myclass{
    background-color: #aaa;
    padding: 10px;
    }
    </style></head><body><p class="myclass">Hello, World!</p></body></html>

    Note − To learn about how Cascading Style Sheets work, kindly check a separate tutorial available at CSS Tutorial.

    HTML <script> Element

    The HTML <script> tag is used to include either an external script file or to define an internal script for the HTML document. The script is an executable code that performs some action.

    Example

    Following is an example where we are using a script tag to define a simple JavaScript function. When the user clicks on the OK button, an alert box will pop up with a Hello, Worldmessage.

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML script Tag Example</title><base href="http://www.tutorialspoint.com/" /><script type="text/JavaScript">
    
      function Hello(){
         alert("Hello, World");
      }
    </script></head><body><input type="button" onclick="Hello();" name="ok" value="OK" /></body></html>
  • Embed Multimedia

    In the previous two chapters, we have used the <audio> and <video> elements to add music and videos into our web page. There are other alternative ways to add videos, sounds, images, or any other external content to the website by using HTML tags <embed>and<object>. These tags cause the browser itself to include controls for the multimedia automatically:

    • HTML <embed> tag is used to embed external content such as images, videos, and web applications. It is often used for embedding content like Flash movies or audio/video players.
    • HTML <object> tag is used to embed various types of external resources, including images, videos, PDFs, and other web resources. It can render multiple types of files.

    Syntax

    Here is the syntax of embedding multimedia in the webpage:Embed tag:

    <embed src = "url_of_resource">

    Object tag:

    <object data="url_of_resource" type="typeOfResource">

    Attributes of <embed> Tag

    The following are the attributes used with the <embed> tag:

    AttributeDescription
    widthWidth attribute is used describe width of the embedded file in pixels.
    heightHeight of the embedded file in pixels.
    titleIt is used to label the content. The title should describe the content.
    srcURL of the file to be embedded.
    typeIt indicates the type of input like mp4 and mp3.

    Attributes of <object> Tag

    The following are the attributes used with the <object> tag:

    AttributesDescription
    dataThe location or path of the resource is passed into data attribute.
    typeIt indicates the type of resource.
    heightIt signifies the height of the resource display area.
    widthIt signifies the width of the resource display area.
    formIts value is the id of a form. The form attribute shows which object is associated with the form.
    nameIt specify a unique name for the object.
    usemapSpecifies a URL of a client-side image map to be used with the object.

    Examples of HTML Multimedia Embedding

    Here are a few examples that illustrate how to render multimedia content in a webpage using the <embed> and <object> tags:

    • Embedding a Video Using <embed> Element
    • Embedding an Audio Using <embed> Element
    • Render a PDF Using <object> Element
    • Render an HTML Document Inside Webpage

    Embedding a Video Using <embed> Element

    To embed an external video file inside the web page, we can pass the path of the video file into the src attribute of the <embed> element. The supported video formats are MP4, WebM, and Ogg. We dont need to use the controls attribute, as the <embed> tag provides the controls automatically depending on the type of media. The controls attribute allows users to manage the video playback functions.

    The following example demonstrates how to embed a video file using the embed element:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML embed Tag</title></head><body><h1>Playing video using embed tag</h1><embed src="/html/media/video/sampleTP.mp4" 
    
          type="video/mp4" 
          width="450" 
          height="250"&gt;&lt;/embed&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Embedding an Audio Using <embed> Element

    To add a soundtrack to the webpage, we can pass the path of the audio file into the src attribute by mentioning the type of audio. The supported audio formats are ogg, mp3, and wav.

    The following example demonstrates how to embed an audio file using the <embed> element:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML embed Tag</title></head><body><h1>Playing audio using embed tag</h1><embed src = "/html/media/audio/sample_3sec_audio.mp3" 
    
          type = "audio/mp3" 
          width="450" 
          height="250"&gt;&lt;/embed&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Render a PDF Using <object> Element

    HTML 4 introduces the <object> element, which offers an all-purpose solution to generic object inclusion. The <object> element allows HTML authors to specify everything required by an object for its presentation by a user agent.

    We can embed a PDF document in an HTML document as follows:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>PDF Embed Example</title></head><body><h1>Embedding a PDF Document</h1><p>Here is an embedded PDF document:</p><object data="html/pdf/index.pdf" 
    
              type="application/pdf" 
              width="300" 
              height="200"&gt;&lt;/object&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Render an HTML Document Inside Webpage

    We can embed an HTML document in an HTML document itself as follows:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>HTML Embed Example</title></head><body><h1>Embedding an HTML Document</h1><p>Here is an embedded HTML document:</p><object data="html/index.htm" 
    
            type="text/html" 
            width="500" 
            height="400"&gt;
      alt : &lt;a href="html/index.htm"&gt;
         test.htm
      &lt;/a&gt;&lt;/object&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Comparison between <object> and <embed> Tags

    Comparison between two similar tags will help to choose the right tag on the right place.

    Feature<object><embed>
    HTML Tag<object><embed>
    UsageEmbeds multimedia like audio, video, Java applets, ActiveX, PDF, and Flash.Primarily used to embed multimedia content
    AttributesSupports various attributes like data, type, width, and height.Supports various attributes like src, type, width, and height.
    HTML5SupportedSupported
    Fallback ContentCan include fallback content within the tagCannot include fallback content within the tag

    The object tag supports fallback content, which means if a version of a browser does not support the object tag, then the content written inside the object tag is displayed instead of the object tag.

  • HTML Audio

    The HTML <audio> element embeds an audio file to the webpage. You can add an audio player inside a webpage using the <audio> element.

    HTML <audio> Element

    The <audio> element is used to enable the support of audio files within a web page. We can include multiple sources of audio; however, the browser will choose the most appropriate file automatically. Most of the attributes of <video> element is also compatible with the <audio> element. The most frequently used attributes of the HTML audio element are controlsautoplayloopmuted, and src.

    Attributes of <audio> Elements

    The following are the attributes used with the <audio> tag:

    AttributeDescription
    controlsThis attribute adds built-in audio controls for play, pause, and volume.
    autoplayThis attribute allows playing the audio automatically when the page is loaded.
    loopThis attribute allows looping of the audio.
    mutedThis attribute mutes the audio by default when the page is loaded.
    preloadThis attribute specifies how the audio should be preloaded by the browser.
    srcThis attribute specifies the path to the audio file.File URL

    Embedding an Audio in HTML

    You can embed an audio player using the <audio> tag by specifying the audio file path. The audio file path can be defined either by setting the src attribute or by including the <source> tag.

    The current HTML5 draft specification does not specify which audio formats browsers should support in the audio tag. But the most commonly used audio formats are oggmp3, and wav. Therefore, it is also possible to supply all these formats by using multiple <source> tags within the <audio> element. Let the browser decide which format is more suitable for audio playback.

    It is necessary to use the controls attribute so that users can manage audio playback functions such as volume adjustment, forward or backward navigation, and play or pause operations.

    Syntax

    Here is the syntax to embed an audio file:

    <audio controls><source src="file_path" type="audio/mpeg">
      Your browser does not support the audio element.
    </audio>

    Here,

    • <audio>: The main element to embed an audio player.
    • controls: Controls to add play, pause, and volume functionalities.
    • <source>: It specifies the audio file name (along with its path) and the audio file’s format.
    • Fallback text: The text to be displayed if the browser doesn’t support the <audio> element. In the above syntax, it will display:
      “Your browser does not support the audio element.”

    Example of HTML Audio Element

    Here is the complete example you can use to embed an audio player on the webpage:

    Open Compiler

    <!DOCTYPE html><html><body><p>Working with audio element</p><audio controls><source src= "/html/media/audio/sample_3sec_audio.mp3" type = "audio/mp3" /><source src= "/html/media/audio/sample_3sec_audio.wav" type = "audio/wav" /><source src= "/html/media/audio/sample_3sec_audio.ogg" type = "audio/ogg" />
    
      Your browser does not support the &lt;audio&gt; element.
    </audio></body></html>

    Using autoplay, muted, and loop Attributes in Audio Player

    We can also configure the audio to play automatically in a loop by using the autoplay and loop attributes. Remember, the Chrome browser does not support the autoplay feature unless the muted attribute is used. Therefore, it is recommended to use autoplay and muted attributes together.

    The autoplay is a Boolean attribute that is used to play the audio automatically once the page is loaded. If the loop attribute is present on the <audio> element, the audio sound will automatically rewind to the beginning once the end of the audio is reached.

    Example

    The following example illustrates the autoplay and looping of audio −

    Open Compiler

    <!DOCTYPE html><html><body><p>Working with audio element</p><audio controls autoplay muted loop><source src= "/html/media/audio/sample_3sec_audio.mp3" type = "audio/mp3" /><source src= "/html/media/audio/sample_3sec_audio.wav" type = "audio/wav" /><source src= "/html/media/audio/sample_3sec_audio.ogg" type = "audio/ogg" />
    
      Your browser does not support the &lt;audio&gt; element.
    </audio></body></html>

    Browser Support for Different Audio Formats

    The table below illustrates the various audio formats that are supported by different browsers:

    BrowserOGGWAVMP4
    ChromeYesYesYes
    EdgeYesYesYes
    SafariNoYesYes
    FirefoxYesYesYes
    OperaYesYesYes

  • HTML Video

    The HTML <video> element embeds and shows a video on the webpage. You can embed any type of video content on the webpage by using the <video> element.

    HTML <video> Element

    The <video> element is used to enable video playback support within a web page. It works very similarly to the <img> element, as it also requires adding the path or URL of the video within the src attribute. The HTML supports only MP4, WebM, and Ogg video formats. The <video> element also supports audio; however, the <audio> element is more suitable for that purpose.

    Embedding Videos in HTML

    To embed a video inside a web page, you need to set the src attribute inside the <video> tag that specifies the path or URL for the video. A web page serves content to a wide variety of users with various types of browsers. Each browser supports different video formats (MP4, WebM, and Ogg) as mentioned earlier. Therefore, we can supply all the formats that HTML supports by including multiple <source> tags. Let the browser decide which format is more suitable for video playback.

    Syntax

    Below is the syntax to embed a video on the webpage:

    Open Compiler

    <video width="640" height="360" controls><source src="video-file.mp4" type="video/mp4"><source src="video-file.ogg" type="video/ogg">
      Your browser does not support the video tag.
    </video>

    The controls Attribute

    You can also enable the built-in controls for controlling audio and video playback for the users (if needed) with the help of the controls attribute. It provides an interface that enables users to manage video playback functions such as volume adjustment, video navigation (forward and backward), and play or pause operations.

    Example to Embed a Video

    The following example shows how to insert the path or URL of a video inside the video element:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Video Element</title></head><body><p>Playing video using video element</p><p>The browser is responsible for determining the appropriate format to use.</p><video width="450" height="250" controls><source src="/html/media/video/sampleTP.webm" type="video/webm"><source src="/html/media/video/sampleTP.mp4" type="video/mp4"><source src="/html/media/video/sampleTP.ogv" type="video/ogg"><p>Sorry, video element is not supported!</p></video></body></html>

    Customizing Video Display Size

    To set (adjust) the dimensions of the video display area, also known as the viewport, you can use the height and width attributes of the <video> element. These attributes define the height and width of the video viewport in pixels.

    Note that the video will adjust its aspect ratio (e.g., 4:3 and 16:9) to align with the specified height and width. Therefore, it is advisable to match the dimensions of the viewport for a better user experience.

    Example

    Let’s look at an example code below to understand better:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Video Element</title></head><body><p>Playing video using video element</p><video width="450" height="250" controls><source src="/html/media/video/sampleTP.mp4" type="video/mp4"></video></body></html>

    HTML Video autoplay and loop Attributes

    You can configure the video to play automatically in a loop by using the autoplay and loop attributes.

    Remember, a few browsers like Chrome 70.0 do not support the autoplay feature unless the muted attribute is used. Therefore, it is recommended to use autoplay and muted attributes together.

    Example

    The following example illustrates the autoplay and looping of video:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Video Element</title></head><body><p>Playing video using video element</p><video width="450" height="250" autoplay muted loop><source src="/html/media/video/sampleTP.mp4" type="video/mp4"></video></body></html>

    Setting a Video Thumbnail

    You can pass a URL of an image that works as a thumbnail for the video within the poster attribute of the <video> element. It will display the specified image until the video starts playing.

    Example

    In the following example, we are illustrating the use of poster attribute:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Video Element</title></head><body><p>Playing video using video element</p><video width="450" height="250" controls muted poster ="tutorials_point.jpg" ><source src="/html/media/video/sampleTP.mp4" type="video/mp4"></video></body></html>

    Browser Support for Various Video Formats

    The table below illustrates the various video formats that are supported by different browsers:

    BrowserOGGWebMMP4
    ChromeYesYesYes
    EdgeYesYesYes
    SafariNoYesYes
    FirefoxYesYesYes
    OperaYesYesYes

  • Input Attributes

    Input Attributes

    The HTML input attributes define the characteristics and behavior of the <input> element. These input attributes are used with the different types of input fields, such as text, email, password, date, number, and so forth. Note that the input element is used to create interactive controls for the web-based forms so that it can accept data from the user.

    The <input> element requires only an opening tag, and it will work only if we add it in between the <form> tags. In this tutorial, we are going to explore the attributes that are used with the <input> element.

    The attributes of the <input> element are as follows −

    • type and name
    • value
    • size
    • maxlength
    • readonly
    • disabled
    • min and max
    • accept and multiple
    • placeholder
    • required
    • autofocus
    • list

    The ‘type’ and ‘name’ Attributes

    The type attribute indicates the type of input control, like text, password, email, and so on. The name attribute of an input element assigns an identifier to the form control that enables the server to recognize and retrieve the value.

    Example

    The following HTML code illustrates the use of type and name attributes:

    Open Compiler

    <!DOCTYPE html><html><head><title>The type and name Attributes</title></head><body><form >
    
      First name: &lt;input type = "text" name = "first_name" /&gt;&lt;br&gt;&lt;br&gt;
      Last name: &lt;input type = "text" name = "last_name" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'value' Attribute

    The value attribute is used to provide an initial value inside the input control.

    Example

    In the following example, we are creating two input fields with initial value as " first name..." and " last name...":

    Open Compiler

    <!DOCTYPE html><html><head><title>The value Attribute</title></head><body><form >
    
      First name: &lt;input type = "text" name = "first_name" value = "first name..." /&gt;&lt;br&gt;&lt;br&gt;
      Last name: &lt;input type = "text" name = "last_name" value = "last name..."/&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'size' Attribute

    The size attribute allows you to specify the width of the text-input control in terms of characters. The default size is 20 characters.

    Example

    In this example, the size of the text-input control is set to 40:

    Open Compiler

    <!DOCTYPE html><html><head><title>The size Attribute</title></head><body><form >
    
      First name: &lt;input type = "text" name = "first_name" size = "40" /&gt;&lt;br&gt;&lt;br&gt;
      Last name: &lt;input type = "text" name = "last_name" size = "40"/&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'maxlength' Attribute

    The maxlength attribute allows you to specify the maximum number of characters a user can enter into the text box.

    Example

    The following example demonstrates how to set the maxlength of an input field:

    Open Compiler

    <!DOCTYPE html><html><head><title>The maxlength Attribute</title></head><body><form >
    
      First name: &lt;input type = "text" name = "first_name" /&gt;&lt;br&gt;&lt;br&gt;
      Last name: &lt;input type = "text" name = "last_name" /&gt;&lt;br&gt;&lt;br&gt;
      Contact: &lt;input type = "text" name = "phone" maxlength = "10"/&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'readonly' Attribute

    The readonly attribute of an input field indicates the field as read-only. Although the content of a read-only field cannot be altered, users can still select it and copy the text. Also, the value of a read-only field is included when the form is submitted.

    Example

    The following example shows the use of the readonly attribute of the <input> element:

    Open Compiler

    <!DOCTYPE html><html><head><title>The readonly Attribute</title></head><body><form >
    
      Emp. Name: &lt;input type = "text" name = " your_name" value = "your name..."/&gt;&lt;br&gt;&lt;br&gt;
      Emp. Email: &lt;input type = "text" name = "mail" value = "your email..."/&gt;&lt;br&gt;&lt;br&gt;
      Organization: &lt;input type = "text" name = "organization" value = "Tutorialspoint" readonly/&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'disabled' Attribute

    The disabled attribute of an input field indicates the field as disabled. Unlike readonly, the value of a disabled field will not be included when the form is submitted.

    Example

    In this example, the field containing the organization name is marked as disabled:

    Open Compiler

    <!DOCTYPE html><html><head><title>The disabled Attribute</title></head><body><form >
    
      Emp. Name: &lt;input type = "text" name = "your_name" value = "your name..."/&gt;&lt;br&gt;&lt;br&gt;
      Emp. Email: &lt;input type = "email" name = "mail" value = "your email..."/&gt;&lt;br&gt;&lt;br&gt;
      Organization: &lt;input type = "text" name = "organization" value = "Tutorialspoint" disabled/&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'min' and 'max' Attributes

    The min and max attributes determine the minimum and maximum values, respectively, of an input field like number, date, week, and so on. If we use them together, they will allow users to enter an input within a predefined range.

    Example

    In the following example, we are mentioning the minimum working hours as 3 and maximum as 8 by using the min and max attributes:

    Open Compiler

    <!DOCTYPE html><html><head><title>The min and max Attribute</title></head><body><form >
    
      Emp. Name: &lt;input type = "text" name = "your_name" value = "your name..."/&gt;&lt;br&gt;&lt;br&gt;
      Emp. Email: &lt;input type = "email" name = "mail" value = "your email..."/&gt;&lt;br&gt;&lt;br&gt;
      Organization: &lt;input type = "text" name = "organization" value = "Tutorialspoint" readonly/&gt;&lt;br&gt;&lt;br&gt;
      Working Hrs: &lt;input type = "number" name = "working_hours" min="3" max="8"/&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'accept' and 'multiple' Attributes

    The accept attribute specifies the types of files that the server will take in. If we use the multiple attribute, it will allow the users to upload more than one file.

    Example

    The following HTML code can accept multiple image files:

    Open Compiler

    <!DOCTYPE html><html><head><title>The accept and multiple Attributes</title></head><body><form><input type = "file" name = "fileupload" accept = "image/*" multiple /></form></body></html>

    The 'placeholder' Attribute

    The placeholder attribute of an input field, like text, search, and email, briefly outlines the desired value of the field. Its predefined value is displayed in the input field until the user begins to enter their own value.

    Example

    In the following example, we are using the placeholder attribute for the email input field:

    Open Compiler

    <!DOCTYPE html><html><head><title>The placeholder Attribute</title></head><body><form>
    
      Emp. Name: &lt;input type = "text" name = "your_name"/&gt;&lt;br&gt;&lt;br&gt;
      Emp. Email: &lt;input type = "email" name = "mail" placeholder = "[email protected]"/&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'required' Attribute

    The required attribute in an input field like text, search, password, and email signifies that the field must contain some values for the form to be successfully submitted. In other words, it indicates the mandatory field.

    Example

    The following example illustrates the use of the required attribute. Without filling the mandatory fields, users will not be able to submit the form:

    Open Compiler

    <!DOCTYPE html><html><head><title>The required Attribute</title></head><body><form ><p>The * Star represents mandatory field</p>
    
      Emp. Name: &lt;input type = "text" name = "your_name" required/&gt;*
      &lt;br&gt;&lt;br&gt;
      Emp. Email: &lt;input type = "email" name = "mail" placeholder = "[email protected]" required/&gt;*
      &lt;br&gt;&lt;br&gt;&lt;input type = "submit"&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'autofocus' Attribute

    The autofocus attribute in an input field ensures that the field must be selected automatically once the webpage loads completely. It implies that the cursor will be positioned to the specified input field. In cases where multiple elements use the autofocus attribute, only the first element will acquire the focus.

    Example

    Following is the example of the autofocus attribute:

    Open Compiler

    <!DOCTYPE html><html><head><title>The autofocus Attribute</title></head><body><form >
    
      Emp. Name: &lt;input type = "text" name = "your_name" autofocus/&gt;&lt;br&gt;&lt;br&gt;
      Emp. Email: &lt;input type = "email" name = "mail" placeholder = "[email protected]" /&gt;&lt;br&gt;&lt;br&gt;&lt;input type = "submit"&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The 'list' Attribute

    The list attribute defines a set of predefined options for an <input> element, which are defined within a <datalist> element. The <input> element uses a specific string as an ID to create a link to the corresponding <datalist> element.

    Example

    In this example, we are creating a list of cities with the help of the list attribute:

    Open Compiler

    <!DOCTYPE html><html><head><title>The list Attribute</title></head><body><form >
    
      Emp. Name: &lt;input type = "text" name = "your_name" autofocus/&gt;&lt;br&gt;&lt;br&gt;
      Emp. Email: &lt;input type = "email" name = "mail" placeholder = "[email protected]" /&gt;&lt;br&gt;&lt;br&gt;
      Location −
      &lt;input list="location" name="cities"&gt;&lt;datalist id = "location"&gt;&lt;option value="Banglore"&gt;&lt;option value="Hyderabad"&gt;&lt;option value="Patna"&gt;&lt;option value="Delhi"&gt;&lt;/datalist&gt;&lt;input type = "submit"&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Form Controls

    HTML form controls (elements) are the elements used within the <form> element to collect the user information.

    Form Controls (Elements)

    The form elements create controls for the user interaction within the webpage; these elements are also termed as form controls. The form elements enable users to enter information for the server-side processing. The nature of interaction with the server can vary depending on the type of control used while creating the form. For example, radio buttons are typically used to accept gender information.

    We have used several common form controls in previous discussions; we will now dive into a more detailed exploration of these elements.

    There are different types of form controls that we can use to collect data using HTML form:

    • Text Input Controls
    • Checkboxes Control
    • Radio Buttons Control
    • Select Box Control
    • File Select Box
    • Button Control
    • Hidden Form Control
    • Datetime Controls
    • Date Control
    • Month Control
    • Week Control
    • Time Control
    • Number Control
    • Range Control
    • Email Control
    • URL Control

    Let us look at these controls one by one −

    Text Input Controls

    The text input controls are further divided into three main categories −

    • Single-line Text Input Control
    • Password Input Control
    • Multi-line Text Input Control

    Single-line Text Input Control

    The single-line text input control is used for items that require only one line of user input, such as search boxes or names. They are created using the <input> tag.

    Example

    The following example illustrates how to take a single-line text input:

    Open Compiler

    <!DOCTYPE html><html><head><title>Text Input Control</title></head><body><form >
    
      First name: &lt;input type = "text" name = "first_name" /&gt;&lt;br&gt;&lt;br&gt;
      Last name: &lt;input type = "text" name = "last_name" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    On running the above code, two single-line text input fields will be displayed.

    Password Input Control

    The password input control is also a single-line text input, but it masks the character as soon as a user enters it. They are also created using the HTML <input> tag, but the type attribute is set to password:

    Example

    In the following example, we will demonstrate how to take password input from users.

    Open Compiler

    <!DOCTYPE html><html><head><title>Password Input Control</title></head><body><form >
    
      User ID : &lt;input type = "text" name = "user_id" /&gt;&lt;br&gt;&lt;br&gt;
      Password: &lt;input type = "password" name = "password" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The above HTML code will display one text input field and one password input field.

    Multiple-line Text Input Control

    The multiple-line text input control is used when the user is required to give details that may be longer than a single sentence. Multi-line input controls are created using the HTML <textarea> tag.

    Example

    The following example demonstrates how to use multi-line text input to take item descriptions:

    Open Compiler

    <!DOCTYPE html><html><head><title>Multiple-Line Input Control</title></head><body><form>
    
      Description : &lt;br /&gt;&lt;textarea rows = "5" cols = "50" name = "description"&gt;
         Enter description here...
      &lt;/textarea&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The above code will produce a text area where users can provide multiple lines of text.

    Checkboxes Control

    Checkboxes are used when more than one option is required to be selected. They are also created using the <input> tag, but the type attribute is set to checkbox.

    Example

    In this HTML code, we are creating a form with two checkboxes:

    Open Compiler

    <!DOCTYPE html><html><head><title>Checkbox Control</title></head><body><form><input type = "checkbox" name = "maths" value = "on"> Maths
    
      &lt;input type = "checkbox" name = "physics" value = "on"&gt; Physics
    </form></body></html>

    On executing the above code, it will create two checkboxes.

    Radio Buttons Control

    Radio buttons are used when out of many options, just one option is required to be selected. They are also created using the <input> tag, but the type attribute is set to radio.

    Example

    In this example code, we are creating a form with two radio buttons:

    Open Compiler

    <!DOCTYPE html><html><head><title>Radio Box Control</title></head><body><form><input type = "radio" name = "subject" value = "maths"> Maths
    
      &lt;input type = "radio" name = "subject" value = "physics"&gt; Physics
    </form></body></html>

    On running the above HTML code, it will produce two radio buttons.

    Select Box Control

    select box provides features to list down various options in the form of drop-down list, from where a user can select one or more options.

    Example

    The following HTML code illustrates how to create a form with a drop down box:

    Open Compiler

    <!DOCTYPE html><html><head><title>Select Box Control</title></head><body><form><select name = "dropdown"><option value = "Maths" selected>Maths</option><option value = "Physics">Physics</option><option value = "Chemistry">Chemistry</option></select></form></body></html>

    The above HTML code will create a dropdown menu with three values.

    File Select Box

    If we want to allow a user to upload a file to our website, we will need to use a file upload box, also known as a file select box. This is also created using the <input> element, but the type attribute is set to file.

    Example

    In the following example, we will create a form with one file upload box that accepts only images:

    Open Compiler

    <!DOCTYPE html><html><head><title>File Upload Box</title></head><body><form><input type = "file" name = "fileupload" accept = "image/*" /></form></body></html>

    Button Control

    There are various ways in HTML to create clickable buttons. We can create a clickable button using the <input> tag by setting its type attribute to button.

    Example

    In this HTML code, we are creating a form with three different types of buttons:

    Open Compiler

    <!DOCTYPE html><html><head><title>File Upload Box</title></head><body><form><input type = "submit" name = "submit" value = "Submit" /><input type = "reset" name = "reset" value = "Reset" /><input type = "button" name = "ok" value = "OK" /><input type = "image" name = "imagebutton" src = "/html/images/logo.png" /></form></body></html>

    Hidden Form Control

    Thehidden form controls are used to hide data inside the page, which later on can be pushed to the server. This control hides inside the code and does not appear on the actual page. For example, the following hidden form is being used to keep the current page number. When a user clicks next page, then the value of the hidden control will be sent to the web server, and there it will decide which page will be displayed next based on the passed current page.

    Example

    The following example shows the usage of hidden control:

    Open Compiler

    <!DOCTYPE html><html><head><title>File Upload Box</title></head><body><form><p>This is page 10</p><input type = "hidden" name = "pagename" value = "10" /><input type = "submit" name = "submit" value = "Submit" /><input type = "reset" name = "reset" value = "Reset" /></form></body></html>

    Datetime Controls

    In HTML, the datetime control represents date and time (year, month, day, hour, minute, second, and fractions of a second) together, encoded according to ISO 8601 with the time zone set to UTC. If we use the datetime-local, it will display date and time with no time zone information.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><form action = "/cgi-bin/html5.cgi" method = "get">
    
      Date and Time : &lt;input type = "datetime" name = "newinput" /&gt;&lt;input type = "submit" value = "submit" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Date Control

    The HTML date control is used to specify a date (year, month, day) encoded according to ISO 8601.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><form action = "/cgi-bin/html5.cgi" method = "get">
    
      Date : &lt;input type = "date" name = "newinput" /&gt;&lt;input type = "submit" value = "submit" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Month Control

    In HTML, the month control is used to display a date consisting of only a year and a month encoded according to ISO 8601.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><form action = "/cgi-bin/html5.cgi" method = "get">
    
      Month : &lt;input type = "month" name = "newinput" /&gt;&lt;input type = "submit" value = "submit" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Week Control

    As the name suggests, the week control displays a date consisting of only a year and a week number encoded according to ISO 8601.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><form action = "/cgi-bin/html5.cgi" method = "get">
    
      Week : &lt;input type = "week" name = "newinput" /&gt;&lt;input type = "submit" value = "submit" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Time Control

    The HTML time control specifies the hours, minutes, seconds, and fractional seconds encoded according to ISO 8601.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><form action = "/cgi-bin/html5.cgi" method = "get">
    
      Time : &lt;input type = "time" name = "newinput" /&gt;&lt;input type = "submit" value = "submit" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Number Control

    The number control accepts only numerical values. The step attribute specifies the precision, and its default value is 1.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><form action = "/cgi-bin/html5.cgi" method = "get">
    
      Select Number : &lt;input type = "number" min = "0" max = "10" step "1"
         value = "5" name = "newinput" /&gt;&lt;input type = "submit" value = "submit" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Range Control

    The range type is used for input fields that should contain a value from a range of numbers.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><form action = "/cgi-bin/html5.cgi" method = "get">
    
      Select Range : &lt;input type = "range" min = "0" max = "10" step "1"
         value = "5" name = "newinput" /&gt;&lt;input type = "submit" value = "submit" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Email Control

    The email control accepts only email value. This type is used for input fields that should contain an e-mail address. If you try to submit a simple text, it forces you to enter only an email address in [email protected] format.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><form action = "/cgi-bin/html5.cgi" method = "get">
    
      Enter email : &lt;input type = "email" name = "newinput" /&gt;&lt;input type = "submit" value = "submit" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    URL Control

    The HTML URL control accepts only URL values. This type is used for input fields that should contain a URL address. If you try to submit a simple text, it forces you to enter only a URL address, either in the http://www.example.com format or in the http://example.com format.

    Example

    Open Compiler

    <!DOCTYPE html><html><body><form action = "/cgi-bin/html5.cgi" method = "get">
    
      Enter URL : &lt;input type = "url" name = "newinput" /&gt;&lt;input type = "submit" value = "submit" /&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Print Page

  • Form Attributes

    In HTML, each element has its own attributes that are used to define the characteristics of that particular HTML element and are placed inside the element’s opening tag. The <form> element also has attributes that provide different functionalities like redirection on other web pages and auto-completion of text.

    Form Attributes

    HTML form attributes provide different functionalities, such as redirection to other web pages, auto-completion of text, specifying data validation rules, controlling the behavior of form submissions, etc.

    Following is a list of the most frequently used form attributes −

    • action
    • method
    • target
    • autocomplete
    • enctype
    • novalidate

    The action Attribute

    The action attribute of the <form> element transmits the user’s input to a backend script for processing. A form is of no use unless it processes the information provided by the user. Therefore, it is important to pass the URL of a program to the action attribute. Note that the formaction attribute can override the value of the action attribute.

    Example

    The following example illustrates the use of the action attribute. When we click the submit button, the form will redirect us to the home page of Tutorialspoint:

    Open Compiler

    <!DOCTYPE html><html><head><meta charset="utf-8"><title> The action Attribute </title></head><body><!-- Start of the form element --><form action = "https://www.tutorialspoint.com"><!-- to take input -->
    
      Name: &lt;input type = "text" name = "your_name" required/&gt;&lt;br&gt;&lt;br&gt;
      Email: &lt;input type = "email" name = "mail" required/&gt;&lt;br&gt;&lt;br&gt;&lt;!-- to submit the data --&gt;&lt;input type = "submit"&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The method Attribute

    The method attribute determines which HTTP method should be used by the browser while uploading the form information. The most commonly used methods are as follows:

    S.NoValues & Description
    1GETIt is the default method for form submission, which means if we don't specify the method name explicitly, the form will use the GET method to send data.
    2POSTIt is used to send form data inside HTTP request body. It is safer than GET method.

    It is not recommended to use the GET method while sending sensitive information like credit/debit card numbers and passwords because it exposes the submitted data in the URL.

    Example

    The following example demonstrates how to use the method attribute of the <form> element.

    Open Compiler

    <!DOCTYPE html><html><head><meta charset="utf-8"><title> The method Attribute </title></head><body><!-- Start of the form element --><form action = "https://www.tutorialspoint.com" method = "post"><!-- to take input -->
    
      Name: &lt;input type = "text" name = "your_name" required/&gt;&lt;br&gt;&lt;br&gt;
      Email: &lt;input type = "email" name = "mail" required/&gt;&lt;br&gt;&lt;br&gt;&lt;!-- to submit the data --&gt;&lt;input type = "submit"&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    On clicking the submit button, the user will be redirected to the home page of Tutorialspoint.

    The target Attribute

    The target attribute determines the target window or frame where the result of the script will be displayed after submitting the form. The default target is the current window. The target attribute accepts the following values:

    S.No.Values & Description
    1_selfIt opens the response in the same frame as it was clicked.
    2_blankIt opens the response in the new window or tab.
    3_parentIt opens the response in the parent frame.
    4_topIt opens the response in the full body of window.
    5framenameIt opens the response in the named iframe.

    Example

    In the following example, we will use the target attribute with the value _self. The response will be open in the current window:

    Open Compiler

    <!DOCTYPE html><html><head><meta charset="utf-8"><title> The target Attribute </title></head><body><!-- Start of the form element --><form action = "https://www.tutorialspoint.com" target = "_self"><!-- to take input -->
    
      Name: &lt;input type = "text" name = "your_name" required/&gt;&lt;br&gt;&lt;br&gt;
      Email: &lt;input type = "email" name = "mail" required/&gt;&lt;br&gt;&lt;br&gt;&lt;!-- to submit the data --&gt;&lt;input type = "submit"&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The novalidate Attribute

    The novalidate is a Boolean attribute that indicates the form does not need any kind of validation. The term validation refers to the process of verifying the correctness of user input based on predefined conditions. This attribute, when applied, exempts the form from such checks, allowing user inputs to bypass these conditions.

    If Boolean attributes like novalidate are present on an HTML element, it specifies true, and in the case of absence, false is assumed. They do not accept any values.

    Example

    In the previous examples, the form redirected us to a new web page when we entered our name and email. For this example, we will use the novalidate attribute, which will allow the redirection without entering any information:

    Open Compiler

    <!DOCTYPE html><html><head><meta charset="utf-8"><title> The novalidate Attribute </title></head><body><!-- Start of the form element --><form action = "https://www.tutorialspoint.com" target = "_blank" autocomplete="off" method = "get" novalidate><!-- to take input -->
    
      Name: &lt;input type = "text" name = "your_name" required/&gt;&lt;br&gt;&lt;br&gt;
      Email: &lt;input type = "email" name = "mail" required/&gt;&lt;br&gt;&lt;br&gt;&lt;!-- to submit the data --&gt;&lt;input type = "submit"&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The autocomplete Attribute

    The autocomplete attribute of HTML predicts and suggests the subsequent input based on the initial characters entered in the input field. This attribute primarily has two states, namely on and off.

    S.No.Values & Description
    1onBy default, the autocomplete attribute is set to on, enabling the autocomplete functionality.
    2offThe autocomplete attribute can be toggled to off to disable this feature as per the requirements of the web application.

    Instance

    <form action = "https://www.tutorialspoint.com" target = "_blank" autocomplete="off" method = "get">

    The enctype Attribute

    We use the enctype attribute to specify how the browser encodes the data before it sends it to the server. Its possible values are −

    S.No.Values & Description
    1application/x-www-form-urlencodedThis is the standard method most forms use in simple scenarios.
    2mutlipart/form-dataThis is used when you want to upload binary data in the form of files like images, Word files etc.
    3text/plainIt only encodes the spaces into + symbol.

    Instance

    <form action = "https://www.tutorialspoint.com" target = "_blank" autocomplete="off" method = "get" enctype = "text/plain">

    Print Page

  • Forms

    An HTML form is a webpage section usually used for collecting data from the users and then sent to a server for further processing.

    HTML Forms

    HTML forms are collections of interactive controls and various input types, such as text, numbers, email, password, radio buttons, checkboxes, buttons, etc., that collect user information. HTML forms are created by using the HTML <form> tag. All user input-related tags are placed inside the <form> tag.

    The <form> Element

    The HTML <form> element defines a form to collect user information in the HTML document.

    Syntax

    The <form> element has the following syntax; you can place any input-related element inside it:

    <form><!-- Form elements--></form>

    The following syntax contains all necessary elements:

    <form action="url" method="method_type" target="target_value" enctype="enctype_value"><!-- Form elements--></form>

    Why Use HTML Forms?

    HTML forms are used to collect user information from the webpage and send it to the server. The common uses for HTML forms are:

    • Creating registration forms so that users can sign up with their information and authenticate further to access the functionalities of the websites/web applications.
    • Collect data through the different types of surveys, feedback, etc.
    • Uploading the images, resumes, or any other type of files.

    Creating an HTML Form

    To create an HTML form, use the <form> element along with the other required elements based on the information you want to collect, such as input boxes, buttons, radio buttons, checkboxes, etc. These elements are known as form controls (form elements).

    Example

    The following is an example of an HTML form having some required elements:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Form Example</title></head><body><h1>HTML Form Example</h1><form><!-- Text Input --><label for="name"><strong>Name:</strong></label><input type="text" id="name" name="name" placeholder="Enter your name" required><br/><br/><!-- Radio Buttons --><label><strong>Gender:</strong></label><input type="radio" id="male" name="gender" value="male"><label for="male">Male</label><input type="radio" id="female" name="gender" value="female"><label for="female">Female</label><br/><br/><!-- Checkboxes --><label><strong>Hobbies:</strong></label><input type="checkbox" id="reading" name="hobbies" value="reading"><label for="reading">Reading</label><input type="checkbox" id="traveling" name="hobbies" value="traveling"><label for="traveling">Traveling</label><input type="checkbox" id="sports" name="hobbies" value="sports"><label for="sports">Sports</label><br/><br/><!-- Submit Button --><button type="submit">Submit</button></form></body></html>

    HTML Form with Redirection

    In the previous example, we designed a form that accepts user input but doesn’t process the data. In this example, users will be redirected to Tutorialspoint’s HTML Tutorial upon form submission. The redirection only happens if both the first name and last name fields are filled out; otherwise, the form prompts the user to provide the required information.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><meta charset="utf-8"><title>Sample HTML Form</title></head><body><!-- Start of the form element --><form action="" method="post"><!-- Form controls --><label for="first_name">First Name:</label><input type="text" id="first_name" name="first_name" required /><br><br><label for="last_name">Last Name:</label><input type="text" id="last_name" name="last_name" required /><br><br><input type="submit" value="Submit"></form></body></html>

    Note:

    The HTML form tag’s action attribute specifies the URL to which form data will be submitted or where the user will be redirected after submitting the form. You can specify the file name that is accepting the user information for the further processing. For an example, the file name is process.php; use the action attribute like this:

    <form action="/process.php" method="post">

    Form Elements

    There is a list of elements that can be used within the form element. All the elements are briefly described below:

    1. The <form> Element

    HTML <form> tag is used to create the <form> element. This element is the container for all other form elements. The form element does not create the form; it’s the container that holds the other form elements.

    Example

    <form>.....</form>

    2. The <input> Element

    HTML <input> tag is an essential element of form control for gathering user input from websites. We can use this tag to create an input element.

    Example

    <input type = ".."/>

    3. The <label> Element

    HTML <label> tag is used to create a label element that represents a caption for an item in a UI (user interface), or to add labels to a form control like text, textarea, checkbox, radio button, etc.

    Example

    <label>.......</label>

    4. The <legend> Element

    HTML <legend> tag is the element’s first child and specifies the caption or title for the <fieldset> tag.

    Example

    <legend>.......</legend>

    5. HTML <select> Element

    HTML <select> tag is used to create the dropdown in HTML forms. We can use this tag to create a dropdown anywhere we want.

    Example

    <select>....</select>

    6. The <button> Element

    HTML <button> tag is an interactive element used to create a button in HTML.

    Example

    <button>Button</button>

    7. The <fieldset> Element

    HTML <fieldset> tag is used to group several controls within a web form. By using the <fieldset> tag and <legend> tag, a form can be much easier for users to understand.

    Example

    <fieldset>....</fieldset>

    8. The <datalist> Element

    HTML <datalist> tag contains a set of <option> elements that represent recommended options available to choose from among others.

    Example

    <datalist>....</datalist>

    9. The <output> Element

    HTML <output> tag is a flexible and underused component that enables programmers to dynamically show the outcomes of calculations or scripts inside the content.

    Example

    <output> Results... </output>

    10. The <option> Element

    HTML <option> tag defines either the elements of the data list for autocomplete, specified by the <datalist> tag, or the items of a drop-down list, defined by the <select> tag.

    Example

    <option>.....</option>

    11. The <optgroup> Element

    HTML <optgroup> tag is used in the <select> element to group together relevant <option> elements.

    Example

    <optgroup><option>..</option>
    
     .
     .
    </optgroup>

    12. The <textarea> Element

    HTML <textarea> tag is used to represent a multiline plain-text editing control.

    Example

    <textarea>.......</textarea>

    Form Attributes

    HTML form attributes provide specific functionalities, such as redirection to other web pages, auto-completion of text, etc.

    The below table lists out some of the common form attributes:

    AttributeDescription
    actionIt is used to specify a URL that processes the form submission.
    methodIt is used to define which HTTP method to use when submitting the form.
    targetIt is used to specify where to open the linked document.
    autocompleteIt allows you to set whether the autocomplete for the form should be on or off.
    enctypeIt is used to specify how the form input data should be encoded before sending it to the server.
    novalidateIt defines that while submitting the form, the form data should not be validated in an HTML document.

    Styling HTML Forms

    You can customize the appearance of HTML forms and their elements by using the CSS to match your website theme or to make it more appealing.

    Example

    The following is an example of an HTML form with CSS:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Form</title><style>
    
            body {
                font-family: 'Segoe UI', Arial, sans-serif;
            }
            form {
                width: 100%;
                max-width: 400px;
                background-color: #e8f5e9;
                padding: 20px;
                border: 1px solid #ccc;
                border-radius: 5px;
            }
            legend {
                font-size: 1.2rem;
                font-weight: bold;
                margin-bottom: 10px;
            }
            label {
                display: block;
                margin-bottom: 5px;
                font-size: 0.9rem;
            }
            input[type="text"],
            input[type="email"],
            input[type="password"],
            textarea {
                width: 100%;
                padding: 8px;
                margin-bottom: 15px;
                border: 1px solid #ccc;
                border-radius: 4px;
                box-sizing: border-box;
            }
            textarea {
                resize: none;
            }
            input[type="submit"] {
                width: 100%;
                padding: 10px;
                font-size: 1rem;
                color: #fff;
                background-color: #04af2f;
                border: none;
                border-radius: 4px;
                cursor: pointer;
            }
            input[type="submit"]:hover {
                background-color: #039325;
            }
        &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;form&gt;&lt;fieldset&gt;&lt;legend&gt;Registration Form&lt;/legend&gt;&lt;label for="firstName"&gt;First Name&lt;/label&gt;&lt;input type="text" id="firstName" name="FirstName" /&gt;&lt;label for="lastName"&gt;Last Name&lt;/label&gt;&lt;input type="text" id="lastName" name="LastName" /&gt;&lt;label for="email"&gt;Email ID&lt;/label&gt;&lt;input type="email" id="email" name="email" /&gt;&lt;label for="password"&gt;Enter Your Password&lt;/label&gt;&lt;input type="password" id="password" name="password" /&gt;&lt;label for="confirmPass"&gt;Confirm Your Password&lt;/label&gt;&lt;input type="password" id="confirmPass" name="confirmPass" /&gt;&lt;label for="address"&gt;Address&lt;/label&gt;&lt;textarea id="address" name="address"&gt;&lt;/textarea&gt;&lt;input type="submit" value="Submit" /&gt;&lt;/fieldset&gt;&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Print Page

  • HSL Color Picker

    HSL color values define colors using three parameters: hue (color type), saturation (color intensity), and lightness (brightness). HSLA extends HSL by adding an alpha parameter, which specifies the opacity level of the color.

    HSL Color Picker

    Use this HSL Color Picker to create your desired color by adjusting the hue, saturation, and lightness levels:

    Input

    Adjust the hue, saturation, and lightness to create your desired color.

    Hue
    Saturation
    Lightness

    Result (HSL Color)

    HSL Color Code Copy & Use

    Color: hsl(0, 0%, 0%)Copy

  • HSL and HSLA Colors

    HSL and HSLA Colors

    HSL color values define colors using three parameters: hue (color type)saturation (color intensity), and lightness (brightness). HSLA extends HSL by adding an alpha parameter, which specifies the opacity level of the color.

    HSL Color Codes

    HTML supports the HSL color model, which stands for Hue, Saturation, and Lightness. It provides a flexible and intuitive way to define colors. The HSL representation allows developers to specify hues, adjust saturation, and control lightness, offering a wider range of color choices.

    • Hue: It is a degree on the color wheel from 0 to 360, where 0 is red, 120 is green, and 240 is blue.
    • Saturation: It is a percentage value that indicates how intense or vivid the color is, where 0% means a shade of gray, and 100% is the full color.
    • Lightness: This is also a percentage value that indicates how bright or dark the color is, where 0% is black, 50% is neither light nor dark, and 100% is white.

    Creating HSL Color

    To create HSL color, use the hsl() function and pass the values for hue, saturation, and lightness. Following is the syntax to use the hsl() function:

    hsl(hue, saturation%, lightness%)
    

    Example

    Here’s an example demonstrating the use of HSL color in HTML:

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><title>HTML HSL Color Example</title><style>
    
      body {
         font-family: Arial, sans-serif;
         text-align: center;
         padding: 50px;
      }
      .hsl-color-box {
         width: 200px;
         height: 200px;
         margin: 0 auto;
         background-color: hsl(120, 50%, 50%);
         /* HSL representation */
         color: white;
         display: flex;
         align-items: center;
         justify-content: center;
      }
    </style></head><body><div class="hsl-color-box"><p>
         This box has an HSL color background 
      &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    In this example, the background-color property of the .hsl-color-box class is set using the HSL color representation. The values are as follows:

    • Hue (H): 120 degrees (green)
    • Saturation (S): 50%
    • Lightness (L): 50%

    Adjust these values to experiment with different colors. The HSL model offers a more flexible way to work with colors, making it easier to fine-tune and control the appearance of elements on a webpage.

    HSLA Color Codes

    In HTML, HSLA stands for hue, saturation, lightness, and alpha. It is an extension of the HSL color code with an optional alpha parameter for transparency. This alpha channel specifies how transparent or opaque a color is with a number between 0.0 and 1.0. Here, 0.0 means fully transparent and 1.0 means no transparency.

    Creating HSLA Color

    To create HSLA colors, use the hsla() function by passing values for hue, saturation, lightness, and alpha for transparency. The hsla() function can be used in CSS files or inside the style attribute in HTML. The following is the syntax to use the hsla() function to create HSLA color:

    hsla(hue, saturation%, lightness%, alpha)
    

    Example

    In this example, we have set the background color and text color using HSLA color code:

    Open Compiler

    <!DOCTYPE html><html><head><title>HTML Colors by HSLA code</title></head><body style = "width:300px; height:100px;"><h2 style = "background-color: hsla(0, 0%, 40%, 0.5);">
    
      Setting the Background using hsla()
    </h2><p style = "color: hsla(0, 0%, 30%, 1.0);">
      The text color of the paragraph is 
      styled using hsla()
    </p></body></html>

    HSL Color Picker

    You can use this HSL Color Picker to create your desired color by adjusting the hue, saturation, and lightness levels:

    Hue
    Saturation
    Lightness

    Color: hsl(0, 0%, 0%)Copy