Lesser-Known HTML Attributes: Examples and Use Cases Part 2

saurabhkurve

Saurabh Kurve

Posted on August 28, 2024

Lesser-Known HTML Attributes: Examples and Use Cases Part 2

In the previous blog, we explored some hidden but powerful HTML attributes that can enhance your website’s functionality. Now, let’s dive deeper and uncover more lesser-known HTML attributes that can make your web development more efficient and user-friendly.


1. <input> with min and max Attributes

The min and max attributes are used in <input> fields to set a range of valid values, such as for numbers or dates.

Syntax:

<input type="number" min="1" max="100">
Enter fullscreen mode Exit fullscreen mode

Use Case:

When creating forms that require numerical inputs, you can restrict the values to a specific range, such as a quantity selector or age input.

<form>
    <label for="age">Enter your age (between 18 and 60):</label>
    <input type="number" id="age" name="age" min="18" max="60">
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: Limiting a user's input for age selection between 18 and 60.

2. <textarea> with maxlength Attribute

The maxlength attribute in the <textarea> tag limits the number of characters a user can input.

Syntax:

<textarea maxlength="250"></textarea>
Enter fullscreen mode Exit fullscreen mode

Use Case:

This is ideal for forms where users need to submit text, but you want to ensure the content stays within a certain limit (e.g., comments, reviews).

<form>
    <label for="feedback">Your feedback (max 250 characters):</label>
    <textarea id="feedback" name="feedback" maxlength="250"></textarea>
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: Restricting a feedback form to 250 characters for concise responses.

3. <select> with multiple Attribute

The multiple attribute allows users to select more than one option from a <select> dropdown.

Syntax:

<select multiple>
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
</select>
Enter fullscreen mode Exit fullscreen mode

Use Case:

Use this attribute when users need to choose multiple options, such as selecting skills in a job application form.

<form>
    <label for="skills">Choose your skills:</label>
    <select id="skills" name="skills" multiple>
        <option value="html">HTML</option>
        <option value="css">CSS</option>
        <option value="js">JavaScript</option>
    </select>
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: A multi-select dropdown for selecting multiple programming languages in a survey.

4. <button> with type="button" Attribute

The type="button" attribute makes a button perform a custom action without submitting the form.

Syntax:

<button type="button">Click Me</button>
Enter fullscreen mode Exit fullscreen mode

Use Case:

Great for interactive elements like triggering a modal or calling a JavaScript function without form submission.

<button type="button" onclick="showAlert()">Show Alert</button>
<script>
    function showAlert() {
        alert('Hello, world!');
    }
</script>
Enter fullscreen mode Exit fullscreen mode
  • Example: Creating a button that triggers a JavaScript alert without submitting a form.

5. <input> with step Attribute

The step attribute defines the intervals for numeric or date inputs, allowing users to increment or decrement values by a specified amount.

Syntax:

<input type="number" step="0.01">
Enter fullscreen mode Exit fullscreen mode

Use Case:

Use this attribute when you need precise control over user input, such as setting increments for price, volume, or time.

<form>
    <label for="price">Set price (in dollars):</label>
    <input type="number" id="price" name="price" step="0.01">
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: Allowing users to input a price with two decimal places in a payment form.

6. <video> with muted Attribute

The muted attribute in the <video> tag ensures that the video starts playing without sound.

Syntax:

<video muted autoplay>
    <source src="video.mp4" type="video/mp4">
</video>
Enter fullscreen mode Exit fullscreen mode

Use Case:

This is helpful when autoplaying videos, especially in advertisements or background videos, where audio might not be desired immediately.

<video muted autoplay loop>
    <source src="background.mp4" type="video/mp4">
</video>
Enter fullscreen mode Exit fullscreen mode
  • Example: Autoplaying a video on a website's background without sound.

7. <input> with list Attribute

The list attribute ties an <input> field to a <datalist> element, creating a dropdown list of pre-defined options while still allowing custom input.

Syntax:

<input type="text" list="browsers">
<datalist id="browsers">
    <option value="Chrome">
    <option value="Firefox">
    <option value="Safari">
</datalist>
Enter fullscreen mode Exit fullscreen mode

Use Case:

Perfect for cases where you want to offer suggestions to users but still allow for flexibility if their input doesn't match any option, such as in search boxes.

<form>
    <label for="browser">Choose your browser:</label>
    <input type="text" id="browser" list="browser-options">
    <datalist id="browser-options">
        <option value="Chrome">
        <option value="Firefox">
        <option value="Safari">
    </datalist>
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: Suggesting web browsers in a field while allowing custom input.

8. <progress> with value and max Attributes

The value and max attributes in the <progress> tag show the completion of a task, such as file uploading, in a visual progress bar.

Syntax:

<progress value="70" max="100"></progress>
Enter fullscreen mode Exit fullscreen mode

Use Case:

Useful for visualizing the progress of tasks, such as uploading files, installing software, or completing forms.

<progress id="fileProgress" value="30" max="100">30%</progress>
Enter fullscreen mode Exit fullscreen mode
  • Example: Displaying the upload progress of a file in a form.

9. <fieldset> with disabled Attribute

The disabled attribute in the <fieldset> tag disables all form elements within the fieldset.

Syntax:

<fieldset disabled>
    <input type="text" value="Disabled field">
</fieldset>
Enter fullscreen mode Exit fullscreen mode

Use Case:

This is helpful when you need to temporarily prevent users from interacting with certain fields until specific conditions are met.

<form>
    <fieldset disabled>
        <legend>Account Info</legend>
        <label for="username">Username:</label>
        <input type="text" id="username" name="username">
    </fieldset>
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: Disabling form fields until a user logs in or completes a previous step.

10. <input> with autofocus Attribute

The autofocus attribute sets focus on the input field automatically when the page loads, without requiring user interaction.

Syntax:

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

Use Case:

Useful for improving user experience by guiding users to start interacting with the form immediately, like in a login or search box.

<form>
    <label for="search">Search:</label>
    <input type="text" id="search" name="search" autofocus>
</form>
Enter fullscreen mode Exit fullscreen mode
  • Example: Automatically focusing on a search bar when the page loads.

These additional lesser-known HTML attributes can significantly improve the functionality and interactivity of your website. By mastering these attributes, you can create forms that are more user-friendly, websites that load faster, and content that is more engaging. Keep exploring, and you’ll find even more ways to enhance your web development skills!

💖 💪 🙅 🚩
saurabhkurve
Saurabh Kurve

Posted on August 28, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related