HTML loop Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Media & Playback

Introduction

The loop attribute is used on <audio> and <video> elements to make media restart automatically when it reaches the end. By default, audio and video play once and stop. With loop, playback repeats indefinitely until the user pauses it or JavaScript changes the setting. This is ideal for background clips, ambient sound loops, and short animations—not for programming loops in JavaScript.

What You’ll Learn

01

Boolean Attr

Presence = repeat.

02

audio & video

Media only.

03

loop vs autoplay

Repeat vs start.

04

controls

Let users pause.

05

JS loop

Toggle at runtime.

06

A11y

Avoid annoyance.

Purpose of loop

The primary purpose of the loop attribute is to specify whether a media element should automatically restart once playback finishes. Without loop, the media stops at the last frame or sample. With loop, the browser seeks back to the start and plays again seamlessly.

Common uses include looping background videos on landing pages, repeating short UI sound effects, and ambient audio tracks. Always pair looping media with user controls or a clear way to stop playback.

📝 Syntax

loop.html
<video width="320" height="240" controls loop>
  <source src="clip.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

<audio src="ambient.mp3" controls loop></audio>

Syntax Rules

  • Valid on <video> and <audio> elements only.
  • Boolean attribute—write loop or loop=""; presence enables repeating playback.
  • Default when omitted: play once (loop is false).
  • Prefer bare loop over loop="true" (both work, but bare form is idiomatic HTML).
  • Combine with controls so users can pause looping media.
  • JavaScript property: mediaElement.loop = true on HTMLMediaElement.

💎 Values

The loop attribute is a boolean attribute:

  • loop present — Media repeats indefinitely after reaching the end (true).
  • Attribute absent — Media plays once and stops (false, default).
  • loop="" or loop="loop" — XHTML-style boolean; same as bare loop.
loop-js.html
document.getElementById("myVideo").loop = true;
document.querySelector("audio").loop = false;

⚡ Quick Reference

Use CaseMarkupNotes
Looping video<video loop controls>Add controls for UX
Looping audio<audio loop controls>Ambient sounds
Background cliploop muted playsinlineOften with autoplay
Disable loopOmit loopOr .loop = false
Custom repeat countJS ended eventNot built into loop attr

Applicable Elements

ElementSupported?Notes
<video>YesPrimary use case
<audio>YesMusic, sound effects
<ul>, <ol>NoDocument lists, not media loop
<marquee> (deprecated)NoDifferent legacy feature

loop vs autoplay

Featureloopautoplay
What it doesRepeats after endStarts on page load
Requires user gesture?No (for loop alone)Often blocked without mute
Typical comboloop muted playsinline autoplayBackground hero videos
AccessibilityCan annoy if no pauseCan surprise users

Examples Gallery

Looping video, looping audio, and toggling loop with JavaScript.

👀 Live Preview

Short looping video (plays until you pause):

The loop attribute makes this clip restart when it ends. muted helps autoplay policies in some browsers.

Example — Looping Video

loop.html
<video width="320" height="240" controls loop>
  <source src="example.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>
Try It Yourself

How It Works

The boolean loop attribute tells the browser to restart playback at the ended event instead of stopping.

Example — Looping Audio

loop-audio.html
<audio src="ambient.mp3" controls loop></audio>
Try It Yourself

How It Works

loop on <audio> uses the same media pipeline as video—restart on end until paused or loop is set to false.

Dynamic Values with JavaScript

dynamic-loop.html
<video id="dynamicVideo" controls src="clip.mp4"></video>

<script>
  document.getElementById("dynamicVideo").loop = true;
</script>
Try It Yourself

How It Works

The loop property on HTMLMediaElement mirrors the content attribute. Use it when looping should depend on user settings or app state.

♿ Accessibility

  • Provide controls — Let users pause or mute looping media, especially autoplay clips.
  • Avoid unexpected sound — Looping audio that starts automatically can disorient screen reader users.
  • Respect reduced motion — Consider disabling loop/autoplay when prefers-reduced-motion: reduce is set.
  • Do not rely on loop for essential info — Content should be available without watching a repeating animation.
  • Keep background loops subtle — Low contrast, muted video loops are less distracting.

🧠 How loop Works

1

Media plays to end

Normal playback.

Play
2

ended event fires

Track reaches last frame.

Event
3

loop is true

Browser seeks to start.

Repeat
=

Continuous playback

Until user pauses or loop=false.

Browser Support

The loop attribute is supported in all modern browsers on <audio> and <video>. Behavior is consistent across Chrome, Firefox, Safari, and Edge.

HTML5 · Fully supported

Reliable media looping

All major browsers repeat audio and video when loop is set.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
loop on media 99% supported

Bottom line: Safe to use on audio and video; test autoplay + loop combos on mobile Safari.

💡 Best Practices

✅ Do

  • Use loop for short background clips and ambient audio
  • Add controls or a visible pause button
  • Combine muted playsinline for mobile background video
  • Toggle loop in JS when user changes preferences
  • Test on mobile devices and with autoplay policies

❌ Don’t

  • Loop loud autoplay audio without user consent
  • Assume loop="true" is required (bare loop is standard)
  • Use loop on <ul> or list elements
  • Hide the only way to stop repeating media
  • Ignore prefers-reduced-motion for decorative loops

Conclusion

The loop attribute is a valuable addition to HTML for repeating audio and video playback. Whether you are incorporating background videos or ambient sound, boolean loop on media elements keeps content playing seamlessly.

Always consider user experience: provide controls, avoid surprise autoplay sound, and use JavaScript when looping should be optional. Combined with autoplay and muted, loop powers many modern hero sections—used responsibly.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about loop

Bookmark these before adding repeating media.

5
Core concepts
🎬 02

audio & video

Media only

Elements
▶️ 03

vs autoplay

Repeat vs start

Compare
📝 04

.loop JS

Toggle runtime

Dynamic
05

controls

Let users pause

A11y

❓ Frequently Asked Questions

It makes <audio> or <video> restart automatically when playback reaches the end, repeating until paused or disabled.
Use bare loop for idiomatic HTML. loop="true" works but is unnecessary for boolean attributes.
No. The attribute loops indefinitely. Use JavaScript and a counter on the ended event for a fixed repeat count.
Yes. They are independent attributes often combined for background videos. Autoplay may require muted on mobile browsers.
Set videoElement.loop = true or false on the media element.
No. The HTML loop attribute is only for media elements. Programming loops use for, while, etc. in JavaScript.

Repeat media playback with loop

Practice loop on <video> and <audio> and toggle it with JavaScript in the Try It editor.

Try looping video example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful