DEV Community

Cover image for Lesser-Known HTML Attributes: Examples and Use Cases
Saurabh Kurve
Saurabh Kurve

Posted on • Edited on

31 3 5 1 2

Lesser-Known HTML Attributes: Examples and Use Cases

HTML tags and their attributes are the foundation of web development, creating structure, formatting content, and enabling interaction. Some attributes, however, are lesser-known yet powerful, providing additional control and functionality to your HTML elements. Let’s dive into some of these hidden gems with examples and use cases.


1. <input> with autocomplete Attribute

The <input> tag is frequently used to accept user input, and its autocomplete attribute enhances user experience by suggesting previously entered values.

Syntax:

<input type="text" name="city" autocomplete="on">
Enter fullscreen mode Exit fullscreen mode

Use Case:

When creating a form where users are likely to enter repeated data (e.g., shipping addresses, cities), the autocomplete attribute speeds up the process by offering suggestions.

<form>
    <label for="city">City:</label>
    <input type="text" id="city" name="city" autocomplete="on">
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: Autocompleting cities in an address form based on user history.

2. <img> with srcset Attribute

The srcset attribute in the <img> tag allows for responsive images. It specifies multiple image sources and their conditions (screen sizes or resolutions), ensuring the correct image is loaded based on the user's device.

Syntax:

<img src="image-small.jpg" srcset="image-large.jpg 1024w, image-medium.jpg 640w" alt="Sample image">
Enter fullscreen mode Exit fullscreen mode

Use Case:

Ideal for responsive websites where the image should change depending on screen size or pixel density. It optimizes page loading times and provides a better user experience.

<img src="default.jpg" srcset="small.jpg 400w, medium.jpg 800w, large.jpg 1200w" alt="A beautiful landscape">
Enter fullscreen mode Exit fullscreen mode
  • Example: Loading smaller images for mobile devices and higher resolution images for desktops.

3. <a> with download Attribute

The download attribute on an anchor (<a>) tag forces the linked file to be downloaded rather than opening it in the browser.

Syntax:

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

Use Case:

When offering downloadable resources (e.g., ebooks, reports, forms), this attribute gives users a smoother experience by automatically triggering a download instead of navigating to the file.

<a href="resume.pdf" download>Download My Resume</a>
Enter fullscreen mode Exit fullscreen mode
  • Example: A portfolio site offering a downloadable resume.

4. <button> with formaction Attribute

The formaction attribute in the <button> tag is used in forms, allowing different buttons to submit the form to different URLs.

Syntax:

<form method="post">
    <button type="submit" formaction="/submit1">Submit to URL 1</button>
    <button type="submit" formaction="/submit2">Submit to URL 2</button>
</form>
Enter fullscreen mode Exit fullscreen mode

Use Case:

Useful when you need multiple submission options in a single form, such as submitting to different services or endpoints based on the user’s choice.

<form method="post">
    <button type="submit" formaction="process-payment.php">Make Payment</button>
    <button type="submit" formaction="save-draft.php">Save as Draft</button>
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: A form with options for making a payment or saving as a draft, each triggering different actions.

5. <iframe> with sandbox Attribute

The sandbox attribute gives extra security to embedded content, restricting its ability to execute scripts, navigate the parent page, or submit forms.

Syntax:

<iframe src="https://example.com" sandbox></iframe>
Enter fullscreen mode Exit fullscreen mode

Use Case:

This attribute is often used when embedding third-party content to prevent security risks, such as cross-site scripting (XSS) or malicious redirection.

<iframe src="https://external-site.com" sandbox="allow-scripts"></iframe>
Enter fullscreen mode Exit fullscreen mode
  • Example: Embedding a video player while restricting potentially harmful scripts from running.

6. <details> with open Attribute

The <details> tag creates a collapsible content section, and the open attribute specifies whether the details should be visible by default.

Syntax:

<details open>
    <summary>More Info</summary>
    <p>This is additional information.</p>
</details>
Enter fullscreen mode Exit fullscreen mode

Use Case:

The <details> and <summary> combination is perfect for FAQ sections, product descriptions, or any expandable/collapsible content.

<details>
    <summary>See FAQ</summary>
    <p>Here’s the answer to a common question.</p>
</details>
Enter fullscreen mode Exit fullscreen mode
  • Example: An FAQ section that allows users to expand and collapse answers.

7. <input> with pattern Attribute

The pattern attribute in an <input> element sets a regular expression that the input’s value must match in order to be valid.

Syntax:

<input type="text" pattern="[A-Za-z]+" title="Only letters allowed">
Enter fullscreen mode Exit fullscreen mode

Use Case:

This is useful when you want to enforce a specific format for user input, such as phone numbers, email addresses, or other structured data.

<form>
    <label for="username">Username (letters only):</label>
    <input type="text" id="username" name="username" pattern="[A-Za-z]+" title="Please use only letters.">
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: Validating that a username contains only letters.

8. <meta> with http-equiv Attribute

The http-equiv attribute of a <meta> tag provides HTTP header-like information to the browser, such as redirecting or specifying content types.

Syntax:

<meta http-equiv="refresh" content="30">
Enter fullscreen mode Exit fullscreen mode

Use Case:

This is often used to automatically refresh a page after a set interval or to set cache control policies.

<meta http-equiv="refresh" content="10; url=https://newpage.com">
Enter fullscreen mode Exit fullscreen mode
  • Example: Automatically redirecting users to a new page after 10 seconds.

9. <link> with rel="preload" Attribute

The rel="preload" attribute on a <link> tag allows you to load resources (like scripts, fonts, or images) before they are actually needed, improving page performance.

Syntax:

<link rel="preload" href="main.css" as="style">
Enter fullscreen mode Exit fullscreen mode

Use Case:

Useful for optimizing performance by preloading critical resources such as stylesheets or fonts, ensuring they’re available as soon as required.

<link rel="preload" href="logo.svg" as="image">
Enter fullscreen mode Exit fullscreen mode
  • Example: Preloading an image for better perceived performance on a visually heavy website.

10. <form> with novalidate Attribute

The novalidate attribute disables the form’s built-in validation, allowing for custom validation techniques via JavaScript or server-side logic.

Syntax:

<form novalidate>
Enter fullscreen mode Exit fullscreen mode

Use Case:

This is helpful when custom validation is preferred over the default HTML5 validation, such as with complex validation logic that needs to run on the server.

<form action="/submit" method="post" novalidate>
    <input type="email" name="email">
    <button type="submit">Submit</button>
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: Skipping client-side validation for an email form, allowing more robust server-side checks.

HTML attributes may seem basic, but the right combination can significantly improve the user experience, security, and performance of your website. Understanding how these often-overlooked attributes work, with their specific use cases, empowers developers to create more efficient, responsive, and user-friendly web applications.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (5)

Collapse
 
rashid_ali_53b3c22f7d779f profile image
Rashid Ali

Great 👍

Collapse
 
valen20 profile image
Valentine Udo

Educative, thanks 👍

Collapse
 
capgo profile image
Capgo

Cool!

Collapse
 
suruaino profile image
Suru

Insightful

Collapse
 
saurabhkurve profile image
Saurabh Kurve

Keep reading :)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay