24 Lesser-Known HTML Attributes You May Want to Use ✨📚
Tags: webdev, html, productivity, beginners
Published: 2022-02-01 12:06:47

---
title: 24 Lesser-Known HTML Attributes You May Want to Use ✨📚
published: true
description: 
tags: webdev, html, productivity, beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbxm3f4jgk1bdh9gwj9k.png
canonical_url: https://madza.hashnode.dev/24-lesser-known-html-attributes-you-may-want-to-use
---
A while ago I created [an article](https://dev.to/madza/12-simple-html-snippets-to-avoid-complex-libraries-3na8) 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.

<hr>

### 1. Accept

Describes which input file types are allowed.  

```html
<input type="file" accept=".jpg, .png">
```

 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.

```html
<input type="text" autofocus>
```

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.

```html
<input type="text" inputmode="url" />
<input type="text" inputmode="email" />
<input type="text" inputmode="numeric" />
```

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.

```html
<input name="username" id="username" pattern="[A-Za-z0-9]+">
```

### 5. Required

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

```html
<form action="/send_form.js">  
Username: <input type="text" name="username" required>  
<input type="submit">  
</form>
```

### 6. Autocomplete

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

```html
<input name="credit-card-number" id="credit-card-number" autocomplete="off">
```

For the full list of available `autocomplete` values, see [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete).

### 7. Multiple

This attribute allows the user to select multiple values.

```html
<input type="file" multiple>
```

 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.

```html
<a href="document.pdf" download>Download PDF</a>
```

### 9. Contenteditable

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

```html
<div contenteditable="true">
  This text can be edited by the user.
</div>
```

### 10. Readonly

Specifies that an input field is read-only.

```html
<input type="text" id="sports" name="sports" value="golf" readonly>
```

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.

```html
<p hidden>This text is hidden</p>
```

### 12. Spellcheck

Defines whether the element is checked for spelling errors.

```html
<p contenteditable="true" spellcheck="true">Myy spellinng is checkd</p>
```

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.

```html
<footer><p translate="no">Printing Works, Inc</p></footer>
```
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.

```html
<img src="https://cdn.mysite.com/media/image.jpg" loading="lazy">
```

`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.

```html
<img src="imageafound.png" onerror="this.onerror=null;this.src='imagenotfound.png';"/>
```

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.

```html
<video 
src="https://cdn.mysite.com/media/video.mp4"
poster="image.png">
</video>
```

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.

```html
<audio controls
<source src="track11.mp3"  type="audio/mpeg">
</audio>
```

### 18. Autoplay

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

```html
<video autoplay
src="https://cdn.mysite.com/media/myvideo.mp4"
poster="image.png">
</video>
```

### 19. Loop

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

```html
<audio loop
<source src="track323.mp3"  type="audio/mpeg">
</audio>
```

### 20. Cite

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

```html
<blockquote cite="https://mysite.com/original-source-url">
  <p>Some awesome quote</p>
</blockquote>
```

### 21. Datetime

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

```html
<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>
```

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.

```html
<script src="script.js" async></script>
```

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.

```html
<script src="script.js" defer></script>
```

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.

```html
<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>
```

<hr>

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](https://twitter.com/madzadev), [LinkedIn](https://www.linkedin.com/in/madzadev/) and [GitHub](https://github.com/madzadev)!

Visit my [Blog](https://madza.dev/blog) for more articles like this.