Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

The capture attribute on file inputs: user and environment

The `capture` attribute on a file input hints how to create a new media file. For photos and videos, `user` prefers a camera facing the user, while `environment` prefers one facing away.

Version: W3C HTML Media Capture Recommendation (2018); implementation support varies

Syntax

<input type="file" accept="image/*" capture="user">
<input type="file" accept="image/*" capture="environment">

The curriculum phrase capture na input type=file means using the HTML Media Capture attribute capture on an input with type="file". Use accept to indicate the expected file type: image/* for images, video/* for video, or audio/* for audio. It is still a file input: the attribute neither gives the page a live stream nor uploads the file.

For photos and video, capture="user" prefers a camera facing the user, usually the front camera. capture="environment" prefers a camera facing away from the user, usually the rear camera. Write the value explicitly: a bare capture does not select either of those directions.

This is a hint, not a requirement. If the preferred direction is unavailable, the browser may use its default. Depending on the device and browser support, it may instead show a menu or a regular file picker. If choosing an existing file matters, offer a separate input without capture. The accept attribute does not validate file contents: if a server receives uploads, validate them there.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Photo capture preferences</title>
</head>
<body>
  <p>
    <label for="front-photo">Take a selfie (front camera preferred)</label><br>
    <input id="front-photo" type="file" accept="image/*" capture="user">
  </p>
  <p>
    <label for="rear-photo">Take a photo (rear camera preferred)</label><br>
    <input id="rear-photo" type="file" accept="image/*" capture="environment">
  </p>
  <p>
    <label for="image-file">Choose an image file</label><br>
    <input id="image-file" type="file" accept="image/*">
  </p>
  <script>
    const controls = [...document.querySelectorAll('input[type="file"]')];
    const descriptions = new Map([
      ['user', 'requests a front-facing camera'],
      ['environment', 'requests an outward-facing camera'],
      [null, 'makes no capture request']
    ]);
    const expectedPreferences = [...descriptions.keys()];
    const isVisible = element =>
      element.getClientRects().length > 0 &&
      getComputedStyle(element).visibility === 'visible';

    // Check the controls and their markup, not which picker or camera opens.
    if (controls.length !== expectedPreferences.length ||
        !controls.every((control, index) =>
          isVisible(control) &&
          [...control.labels].some(label => isVisible(label) && label.innerText.trim()) &&
          control.accept === 'image/*' &&
          control.getAttribute('capture') === expectedPreferences[index] &&
          !control.form)) {
      throw new Error('Expected three visible, labeled image file controls with the specified capture preferences and no upload form.');
    }

    const [first, second, third] = controls.map(control =>
      descriptions.get(control.getAttribute('capture'))
    );
    console.log(
      'The page displays three labeled image file controls. ' +
      `The first ${first}, the second ${second}, and the third ${third}. ` +
      'The browser and device determine what opens when each control is activated. ' +
      'The page does not upload anything.'
    );
  </script>
</body>
</html>

Result

The page displays three labeled image file controls. The first requests a front-facing camera, the second requests an outward-facing camera, and the third makes no capture request. The browser and device determine what opens when each control is activated. The page does not upload anything.

The first control prefers the front camera, the second prefers the rear camera, and the third does not request capture. Compare what each opens on mobile and desktop. The example does not upload anything.

Related lessons

  • HTML · Lesson 7 · Intermediate · Version: 2

Sources