HTML accept attribute
HTML accept attribute
The accept attribute specifies the types of files a user can upload via a file input field (<input type="file">). It restricts file selection to specific MIME types or file extensions.
Supported Elements
- <input type="file">
- Legacy Note: In HTML 4.01, accept was also valid on
Syntax
<input type="file" accept=".jpg,.png,image/*,video/*">
Values
- MIME Types: image/png, audio/mp3, video/mp4, application/pdf, etc.
- File Extensions: .jpg, .docx, .pdf (prefixed with a dot).
- Wildcards: image/* (all image types), audio/*, video/*.
- Default: If omitted, all file types are allowed.
Key Notes
- The accept attribute is only valid on <input type="file"> in HTML5.
- Support for <form accept> was removed in HTML5.
- Browsers may filter file selection dialogs based on accept, but this is not enforced (users can override it).
- Server-side validation is always required for security.
1. HTML5 Compatibility:
2. Browser Behavior:
Examples
Example 1: Restricting to Image Files (HTML5)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>accept Attribute Example</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="avatar">Upload Profile Picture:</label>
<input
type="file"
id="avatar"
name="avatar"
accept="image/png, image/jpeg, .jpg, .png"
>
<button type="submit">Upload</button>
</form>
</body>
</html>
Result
![html accept attribute with form](https://www.w3resource.com/w3r_images/html-accept-attribute-with-form.png)
View this example in a separate browser window
Example of HTML accept attribute with form.
Example 2: Accepting PDFs and Word Documents
<input
type="file"
accept=".pdf,.doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
>
Result
![html accept attribute with input](https://www.w3resource.com/w3r_images/html-accept-attribute-with-input.png)
View this example in a separate browser window
Example of HTML accept attribute with input.
Browser Compatibility
- Supported in all modern browsers (Chrome, Firefox, Safari, Edge).
- Partial support for wildcards (e.g., image/*) in older browsers.
Best Practices
- Always validate uploaded files on the server.
- Use specific MIME types (e.g., image/jpeg) over wildcards for stricter control.
- Combine with client-side validation (e.g., JavaScript) for better UX.
Previous: HTML accept-charset attribute
Next: HTML accesskey attribute
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics