24 Lesser-Known HTML Attributes You May Want to Use ✨📚

Madza - Feb 1 '22 - - Dev Community

A while ago I created an article where I covered useful HTML tags and their types. This week I decided to make a sequel, reviewing some of the HTML attributes you might want to use.

All of the attributes are easy to set up and can help you to achieve common tasks, which you would otherwise get by using some complex external libraries.

In this article, I will review each attribute and include the code snippets, so it is easier for you to understand the use cases and syntax of the attributes.


1. Accept

Describes which input file types are allowed.

<input type="file" accept=".jpg, .png">
Enter fullscreen mode Exit fullscreen mode

Only used with file type of the<input> tag. Takes in a comma-separated list of one or more file types. To allow all files of specific media type, use accept="image/*".

2. Autofocus

Indicates that the particular element should be focused on page load.

<input type="text" autofocus>
Enter fullscreen mode Exit fullscreen mode

Only one element in the document or dialog may have the autofocus attribute. If applied to multiple elements the first one will receive focus.

3. Inputmode

Hints at the type of data that might be entered by the user while editing the element or its contents.

<input type="text" inputmode="url" />
<input type="text" inputmode="email" />
<input type="text" inputmode="numeric" />
Enter fullscreen mode Exit fullscreen mode

This allows a browser to display an appropriate virtual keyboard.

4. Pattern

Specifies a regular expression that the <input> value is checked against on form submission.

<input name="username" id="username" pattern="[A-Za-z0-9]+">
Enter fullscreen mode Exit fullscreen mode

5. Required

Ensures that the element must be filled out before submitting the form.

<form action="/send_form.js">  
Username: <input type="text" name="username" required>  
<input type="submit">  
</form>
Enter fullscreen mode Exit fullscreen mode

6. Autocomplete

Specifies whether the browser has permission to provide assistance to fill out form fields like email, phone numbers, country, etc.

<input name="credit-card-number" id="credit-card-number" autocomplete="off">
Enter fullscreen mode Exit fullscreen mode

For the full list of available autocomplete values, see MDN reference.

7. Multiple

This attribute allows the user to select multiple values.

<input type="file" multiple>
Enter fullscreen mode Exit fullscreen mode

You can use it with file and email types of <input> and the <select> tag.

8. Download

Specifies that the target will be downloaded when the user clicks on the hyperlink.

<a href="document.pdf" download>Download PDF</a>
Enter fullscreen mode Exit fullscreen mode

9. Contenteditable

This attribute allows the user to edit the content of the element.

<div contenteditable="true">
  This text can be edited by the user.
</div>
Enter fullscreen mode Exit fullscreen mode

10. Readonly

Specifies that an input field is read-only.

<input type="text" id="sports" name="sports" value="golf" readonly>
Enter fullscreen mode Exit fullscreen mode

A user can still tab to it, highlight it, and copy the text from it. To forbid those actions, use the disabled attribute, instead.

11. Hidden

Specifies whether or not the element is visible.

<p hidden>This text is hidden</p>
Enter fullscreen mode Exit fullscreen mode

12. Spellcheck

Defines whether the element is checked for spelling errors.

<p contenteditable="true" spellcheck="true">Myy spellinng is checkd</p>
Enter fullscreen mode Exit fullscreen mode

Typically, all the non-editable elements are not checked, even if the spellcheck attribute is set to true and the browser supports spellchecking.

13. Translate

Specifies whether the element should be translated when the page is localized.

<footer><p translate="no">Printing Works, Inc</p></footer>
Enter fullscreen mode Exit fullscreen mode

An example use case would be your company name, book titles, locations, etc.

14. Loading

Specifies whether a browser should load an image immediately or to defer loading of off-screen images until, for example, the user scrolls near them.

<img src="https://cdn.mysite.com/media/image.jpg" loading="lazy">
Enter fullscreen mode Exit fullscreen mode

eager is the default behavior, lazy is used to defer (aka lazy loading).

15. Onerror

Allows adding a fallback image if the original is not loaded.

<img src="imageafound.png" onerror="this.onerror=null;this.src='imagenotfound.png';"/>
Enter fullscreen mode Exit fullscreen mode

The this.onerror=null is used to prevent the loop if the fallback image itself is not available.

16. Poster

Allows adding an image to be shown while the video is downloading.

<video 
src="https://cdn.mysite.com/media/video.mp4"
poster="image.png">
</video>
Enter fullscreen mode Exit fullscreen mode

If not specified, nothing is displayed until the first frame is available, then the first frame is shown as the poster frame.

17. Controls

Specifies whether or not the audio/video controls should be displayed on the player.

<audio controls
<source src="track11.mp3"  type="audio/mpeg">
</audio>
Enter fullscreen mode Exit fullscreen mode

18. Autoplay

Ensures that the audio/video will automatically start playing as soon as it is loaded.

<video autoplay
src="https://cdn.mysite.com/media/myvideo.mp4"
poster="image.png">
</video>
Enter fullscreen mode Exit fullscreen mode

19. Loop

Specifies that the audio/video will start over again, every time it is finished.

<audio loop
<source src="track323.mp3"  type="audio/mpeg">
</audio>
Enter fullscreen mode Exit fullscreen mode

20. Cite

Points to where the content is taken from, or change or deletion is referred.

<blockquote cite="https://mysite.com/original-source-url">
  <p>Some awesome quote</p>
</blockquote>
Enter fullscreen mode Exit fullscreen mode

21. Datetime

Specifies the date and time when the text was deleted/inserted.

<p>
  My plans for 2021 include visiting Thailand,
  <del datetime="2021-01-01T18:21">creating 6 courses,</del> 
  <ins datetime="2021-02-02T14:07">writing 12 articles.</ins>
</p>
<p>I will evaluate the completion on <time datetime="2021-12-31"></time>.</p>
Enter fullscreen mode Exit fullscreen mode

When used with the <time> element, it represents a date and/or time in the machine-readable format.

22. Async

Ensures the script is executed asynchronously with the rest of the page.

<script src="script.js" async></script>
Enter fullscreen mode Exit fullscreen mode

The async attribute only has an effect on external scripts (src attribute must be present).

23. Defer

Ensures the script is executed when the page has finished parsing.

<script src="script.js" defer></script>
Enter fullscreen mode Exit fullscreen mode

The defer attribute only has an effect on external scripts (src attribute must be present).

24. Draggable

Specifies whether an element is draggable or not.

<script>
const allowDrop = (e) => e.preventDefault();
const drag = (e) => e.dataTransfer.setData("text", e.target.id);
const drop = (e) => {
  var data = e.dataTransfer.getData("text");
  e.target.appendChild(document.getElementById(data));
}
</script>
<div ondrop="drop(event)" ondragover="allowDrop(event)" style="width:150px; height:50px; padding: 10px; border:1px solid black"></div>
<p id="drag" draggable="true" ondragstart="drag(event)">Drag me into box</p>
Enter fullscreen mode Exit fullscreen mode

Writing has always been my passion and it gives me pleasure to help and inspire people. If you have any questions, feel free to reach out!

Connect me on Twitter, LinkedIn and GitHub!

Visit my Blog for more articles like this.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player