HTML - <output> Tag



HTML <output> tag is a flexible and under used component that enables programmers to dynamically show the outcomes of calculations or scripts inside the content. It improves the overall user experience by offering a quick and meaningful way to display the results of user activities or computations.

It can be used in forms, math-related material, or any other part of the document where dynamic output needs to be given because it is a self-closing tag, which means it doesn't need a closing tag.

Syntax

<output>
</output>

Attribute

HTML output tag supports Global and Event attributes of HTML. And some specific attributes as well which are listed bellow.

Attribute Value Description
for element_id List of IDs of other elements, i.e it indicates the elements who have contributed input value to the calculation.
form form_id Enables to place output elements anywhere within a document.
name name It is the name of the element.

Examples of HTML output Tag

Bellow examples will illustrate the usage of output tag. Where, when and how to use it to create output section and how we can style that output using CSS.

Creating Output Element

Following is the example, where we are providing the range slider along with a inputfield and considering the sum of the user given values.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML output tag</title>
</head>
<body>
   <!--create output element-->
   <form oninput="add.value = parseInt(n1.value) + parseInt(n2.value)">
      <input type="range" min="0" max="100" name='n1' value="10">
      <input type="number" name='n2' value="20">
      <br> Output: <output name='add'></output>
   </form>
</body>
</html>

Output based on two input field

Considering the another scenario, where we are going to take two input fields with predefined values and retrieving the output as the combination of the both the values.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML output tag</title>
</head>
<body>
   <!--create output element-->
   <form oninput="add.value = (txt1.value).concat(txt2.value)">
      <input type="text" name='txt1' value="HTML">
      <input type="text" name='txt2' value="CSS">
      <br> Output: <output name='add'></output>
   </form>
</body>
</html>

Output of Arithmetic Operation

In the following example, we are going to perform the arthimetic operations.

<!DOCTYPE html>
<html lang="en">

<head>
    <title>HTML output tag</title>
</head>

<body>
    <!--create output element-->
    <form oninput="sum.value = parseInt(n1.value) 
   + parseInt(n1.value) + parseInt(n3.value)">
        <input type="range" value="10" name='n1'> +
        <input type="number" value="5" name='n2'> +
        <input type="number" value="10" name='n3'> =
        <output name='sum'></output>
    </form>
</body>

</html>

Styling Output Element

Let's look at the following example, where we are going to apply CSS to the output tag.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML output tag</title>
   <style>
      output {
         width: 200px;
         height: 50px;
         color: blue;
         padding: 10px;
         background-color: cadetblue;
         font-size: 20px;
      }
   </style>
</head>
<body>
   <!--create output element-->
   <form oninput="sum.value = parseInt(n1.value) + parseInt(n1.value)">
      <input type="range" value="5" name='n1'> 
      + 
      <input type="number" value="2" name='n2'>
      <br>
      <br> Output: <output name='sum' for="n1 n2"></output>
   </form>
</body>
</html

Supported Browsers

Tag Chrome Edge Firefox Safari Opera
ouput Yes 10.0 Yes 13.0 Yes 4.0 Yes 5.1 Yes 11.0
html_tags_reference.htm
Advertisements