🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEhtml

html Documentation

LOADING ENGINE...

loop

AI & DATA SCIENCE // loop

The loop attribute on <video> or <audio> makes the media automatically restart from the beginning every time it finishes playing.

Syntax

<video loop autoplay muted>
  <source src="ambient.mp4" type="video/mp4">
</video>

Deep Dive Course

**`loop`** is a boolean attribute — once playback reaches the end, the browser immediately restarts it from the beginning, repeating indefinitely until the user pauses it (if controls are present) or navigates away. It's commonly combined with `autoplay` and `muted` for background-style video loops, or used on short sound effect clips and ambient audio tracks that should play continuously.

1Understanding loop

`loop` is a boolean attribute — once playback reaches the end, the browser immediately restarts it from the beginning, repeating indefinitely until the user pauses it (if controls are present) or navigates away. It's commonly combined with autoplay and muted for background-style video loops, or used on short sound effect clips and ambient audio tracks that should play continuously.

💡

For a genuinely seamless loop, make sure the source file's first and last frames (or audio waveform) match closely — otherwise viewers will notice a visible or audible 'jump' at the repeat point, regardless of the loop attribute working correctly.

editor.html
<audio loop>
  <source src="/rain-ambience.mp3" type="audio/mpeg">
</audio>
localhost:3000

2Practical Example

Here is a real-world application of loop showing how it is used in production HTML.

editor.html
<!-- A seamlessly looping muted background video -->
<video autoplay muted loop playsinline>
  <source src="/waves.mp4" type="video/mp4">
</video>
localhost:3000

3Best Practices

Follow these guidelines when working with loop:

1. Use loop for background ambience video/audio or short repeating sound effects

2. Combine with autoplay and muted for silent, continuously-looping background video

3. Ensure the source media's start and end points blend smoothly for a seamless-feeling loop

⚠️

Tip: For a genuinely seamless loop, make sure the source file's first and last frames (or audio waveform) match closely — otherwise viewers will notice a visible or audible 'jump' at the repeat point, regardless of the loop attribute working correctly.

editor.html
<audio loop>
  <source src="/rain-ambience.mp3" type="audio/mpeg">
</audio>
localhost:3000

Examples

Example 01Basic Usage
<audio loop>
  <source src="/rain-ambience.mp3" type="audio/mpeg">
</audio>
Example 02Advanced Example
<!-- A seamlessly looping muted background video -->
<video autoplay muted loop playsinline>
  <source src="/waves.mp4" type="video/mp4">
</video>

Best Practices

  • Use loop for background ambience video/audio or short repeating sound effects
  • Combine with autoplay and muted for silent, continuously-looping background video
  • Ensure the source media's start and end points blend smoothly for a seamless-feeling loop

Interview Question

Does the loop attribute guarantee a visually/audibly seamless repeat with no noticeable jump?

Hint: Think about what loop actually controls versus what the source media file itself determines.

No — loop only controls the BEHAVIOR of restarting playback automatically at the end; it has no effect on how well the content actually blends at that restart point. Whether the loop feels seamless depends entirely on the source file itself — if its last frame/sound doesn't closely match its first frame/sound, there will be a noticeable jump or click every time it repeats, regardless of the loop attribute functioning correctly.

Exercises

EasyPractice using loop in a real scenario.
View Solution
<audio loop>
  <source src="/rain-ambience.mp3" type="audio/mpeg">
</audio>

Frequently Asked Questions

Does the loop attribute guarantee a visually/audibly seamless repeat with no noticeable jump?

No — loop only controls the BEHAVIOR of restarting playback automatically at the end; it has no effect on how well the content actually blends at that restart point. Whether the loop feels seamless depends entirely on the source file itself — if its last frame/sound doesn't closely match its first frame/sound, there will be a noticeable jump or click every time it repeats, regardless of the loop attribute functioning correctly.

Related Functions

video-tagaudio-tagautoplay