The Wayback Machine - https://web.archive.org/web/20250310002545/https://www.geeksforgeeks.org/html-loop-attribute/
Open In App

HTML loop Attribute

Last Updated : 29 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The HTML loop Attribute is used to restart the audio and video again and again after finishing it. It contains the Boolean value. 

Syntax:  

<element loop>

Applicable  

  • <audio>
  • <video>
  • <marquee>
  • <bgsound>


Example 1: Below Example illustrates the use of loop attribute in <audio> element.  

html
<!DOCTYPE html>
<html>

<head>
    <title>
        Audio loop Attribute
    </title>
</head>

<body style="text-align: center">

    <h1 style="color: green"> 
    GeeksforGeeks 
</h1>
    <h2 style="font-family: Impact"> 
    HTML Audio loop Attribute 
</h2>
    <br>

    <audio id="Test_Audio" 
           controls loop>
        <source src="beep.mp3" 
                type="audio/mpeg">
    </audio>
</body>

</html>

Output:  


Example 2: Below Example illustrates the use of loop attribute in <video> element.  

html
<!DOCTYPE html>
<html>

<head>
    <title>
      HTML video loop Attribute
  </title>
</head>

<body>
    <center>
        <h1 style="color:green;">
          GeeksforGeeks
      </h1>

        <h3>HTML video loop Attribute</h3>

        <video width="400" 
               height="200" 
               controls loop>
          
            <source src="Canvas.move_.mp4"
                    type="video/mp4">
            <source src="Canvas.move_.ogg" 
                    type="video/ogg">
        </video>
    </center>
</body>

</html>

Output:  


Supported Browsers: The browsers supported by loop Attribute are listed below:  

HTML loop Attribute – FAQs

Can you control the number of times a media file loops using the loop attribute?

No, the loop attribute only creates an infinite loop. To control the number of loops, you would need to use JavaScript.

Does the loop attribute work with animated images?

No, the loop attribute is specific to <audio> and <video> elements. Animated images like GIFs do not use this attribute.

How do you stop a looping media element?

To stop a looping media element, remove the loop attribute or use JavaScript to set the loop property to false.

Does the loop attribute require a closing tag?

No, the loop attribute is a boolean attribute and does not require a closing tag or a value.

How can you loop a video a specific number of times?

Use JavaScript to listen for the ended event and restart the video a specific number of times by incrementing a counter.


Next Article

Similar Reads

three90RightbarBannerImg