References

Beginner-friendly references for web development, with live, editable examples.

The HTML volume attribute

Attribute All modern browsers
In short

The volume property is a JavaScript property (not an HTML attribute) on <audio> and <video> elements. Read or set it from 0.0 (silent) to 1.0 (loudest), e.g. video.volume = 0.5.

Overview

The volume attribute gets or sets the playback volume. It applies to the <audio> and <video> elements (as a JavaScript property).

There is no volume attribute in markup. Set the initial volume from JavaScript. Use muted in HTML to start silent.

Syntax

video.volume = 0.5;  // half volume

Values

Value
A number from 0.0 (silent) to 1.0 (loudest).

Best practices

  • Before assigning volume, clamp a computed level to the 0 to 1 range. Out-of-range numbers throw an IndexSizeError instead of being capped, and NaN throws a TypeError. Even volume += 0.2 fails at 0.9.
  • Toggle muted for a mute button instead of setting the volume to 0. The browser stores the two separately, so unmuting brings back the level the user picked.
  • Initialize a custom slider from the element's current volume rather than 1. Browsers may carry the last level over from an earlier session, and volumechange reports later changes, including ones made in the built-in controls.
  • A volume of 0.5 may not sound half as loud. Loudness rises as volume goes from 0 to 1, but the scale need not be linear. Tune slider steps by ear.
  • Plan for Safari on iOS, where volume support is partial. The property is writable, but it reads back as 1 and a new value doesn't change the sound. Offer muted as the control there.

Frequently asked questions

What does the volume attribute do?
Gets/sets media volume (a JS property).
Can I set this as an HTML attribute?
No. It is a JavaScript property, not a markup attribute. Read or set it on the element in script.
How do I access it?
Through the element object in JavaScript, e.g. video.volume.
Which elements use the volume attribute?
It is an element-specific attribute, used on the <audio> and <video> elements, as a JavaScript property.