HTML - type Attribute



The type is an HTML attribute that specifies the type of element. This is used to specify the type of button for the <button> tag, and it is also used in the <input> tag to specify the type of input to display.

The type attribute determines the internet media type (often referred to as MIME type) for the <embed>, <link>, <object>, <script>, <source>, and <style> elements.

Following are the elements where the type attribute can be used−

S.No Elements
1 <a>
2 <button>
3 <embed>
4 <input>
5 <link>
6 <menu>
7 <object>
8 <script>
9 <source>
10 <style>

Syntax

Following is the syntax of the type attribute −

<element type="value">

Example

In the following example, we are going to use type attribute with the input element.

<!DOCTYPE html>
<html>
<body>
   <h1>The button type attribute</h1>
   <form action="" method="post">
      <label for="fname">First name:</label>
      <input type="text" id="fname" name="fname">
      <br>
      <br>
      <label for="lname">Last name:</label>
      <input type="text" id="lname" name="lname">
      <br>
      <br>
      <button type="submit" value="Submit">Submit</button>
      <button type="reset" value="Reset">Reset</button>
   </form>
</body>
</html>

On running the above code, the output window will pop up displaying the input field along with a submit and reset button on the webpage.

Example

Considering the another scenario, where we are going to use the type attribute with the embed element.

<!DOCTYPE html>
<html>
<body>
   <h1>The embed type attribute</h1>
   <embed type="image/jpg" src="https://www.tutorialspoint.com/images/logo.png?v2" />
</body>
</html>

When we run the above code, it will generate an output consisting of the image displayed on the webpage.

Example

Let's look into the following example, where we are going to use the type attribute with the link element.

style.css

body{
   background-color: purple;
}
h2{
   color: green;
}
span{
   color: black;
}

type.html

<!DOCTYPE html>
<html>
<body>
   <head>
      <link rel="stylesheet" type="text/css" href="style.css">
   </head>
   <body>
      <h2> tutorials <span>point</span>
      </h2>
      <h3> link element with type attribute </h3>
      <p> this is external CSS </p>
   </body>
</html>

On running the above code, the output window will pop up displaying the text applied with CSS on the webpage.

html_attributes_reference.htm
Advertisements