In HTML, an attribute is a property of an HTML element that provides additional information or instructions about how that element should behave or be rendered. Attributes are typically used to modify the default behavior or appearance of an element, and they are specified as name-value pairs within the opening tag of the element. This tutorial will teach you the best Top 10 useful advanced HTML attributes
Here are a few examples of how attributes are used in HTML:
The href attribute on an <a> element specifies the URL of the linked resource.
The src attribute on an <img> element specifies the URL of the image file to be displayed.
The class attribute on an element specifies one or more class names that can be used to style the element with CSS.
The id attribute on an element specifies a unique identifier for the element that can be used to style the element with CSS or manipulate it with JavaScript.
The disabled attribute on an <input> element specifies that the input should be disabled and not editable by the user.
The checked attribute on a <input type="checkbox"> element specifies that the checkbox should be checked when the page loads.
Some attributes are required for certain elements to function properly, for example, the src attribute is required for the <img> element to display an image, while other attributes are optional and can be used to modify the element's behavior or appearance. Also, some attributes are global attributes, which means they can be used on any element, such as id and class attributes, while others are specific to certain elements.
Top 10 useful advanced HTML attributes
Below are useful and advanced HTML attributes:
1. Aria-label
The aria-label attribute in HTML is used to provide a text description for an element, which is used for accessibility purposes. This attribute is used to convey the purpose of the element to assistive technologies, such as screen readers, which can then read the label to users who are visually impaired.
The aria-label attribute can be used on any HTML element, and it is particularly useful for elements that do not have a visible label, such as icons or buttons.
Here's an example of how the aria-label attribute can be used on a button element:
<button aria-label="Submit Form">
<i class="fas fa-check"> </i>
</button>
In this example, the button is an icon, and the aria-label attribute provides a label that the screen readers will read out loud to the user, letting them know that the button is used to submit the form.
It is worth noting that the aria-label attribute should be used in conjunction with other accessibility attributes, such as role and tabindex, to provide a complete experience for users with assistive technologies. Also, it's important to avoid using aria-label to hide the visual text content, because it may make the content inaccessible for some users.
2. Tabindex
The tabindex attribute in HTML is used to specify the order in which elements on a web page should be focused when the user navigates through them using the Tab key. This attribute can be used on most HTML elements, including links, buttons, and form controls.
The tabindex attribute takes an integer value, where the element with the lowest value will be focused first, and elements with higher values will be focused later. Elements without a tabindex attribute will be skipped when the user navigates through the page using the Tab key.
Here's an example of how the tabindex attribute can be used on a button element:
<button tabindex="1">First button</button>
<button tabindex="2">Second button</button>
In this example, the first button will be focused first when the user navigates through the page using the Tab key, followed by the second button.
Tabindex is used to include non-focusable elements in the tab order, and negative values can also be used to exclude elements from the tab order. Elements with tabindex of '0' will be included in the tab order, but in the order that they appear in the document, after elements that have positive values.
Tabindex attribute should be used with caution and only when necessary, as it can disrupt the natural tab order of the page, making it difficult for users to navigate, particularly users with assistive technologies.
3. Placeholder
The placeholder attribute in HTML is used to provide a short hint or example of the expected value for an input field, such as a text input or a text area. The placeholder text will be displayed inside the input field when it is empty, and it will disappear when the user starts typing.
Here's an example of how the placeholder attribute can be used on a text input:
<input type="text" placeholder="Enter your name">
In this example, the text "Enter your name" will be displayed inside the text input when it is empty, and it will disappear when the user starts typing.
It is not a replacement for a label, it's a hint, so it should not be used as the only means of labeling the input field, it's recommended to use it in conjunction with a label element that describes the input field.
The placeholder text is not a value, so it's not included when the form is submitted, and also it might be hard to read for some users, especially when the placeholder text is light gray and the background is also light. Also, it's not supported by all browsers, so it's a good practice to test it in multiple browsers.
4. Required
The required attribute in HTML is used to indicate that an input field must be filled out before the form can be submitted. When an input field has the required attribute, the browser will automatically check if the field is empty when the user submits the form, and if it is, it will prevent the form from being submitted and display an error message.
Here's an example of how the required attribute can be used on a text input:
<input type="text" required>
In this example, the text input field is required, and the browser will automatically check if it is empty when the user submits the form, and if it is, it will prevent the form from being submitted and display an error message.
The required attribute can be used on most input types, including text inputs, checkboxes, radio buttons, and select elements, it can also be used on textarea. Also, you can use the pattern attribute to set a specific pattern for the input, it will prevent the form from being submitted if the user's input doesn't match the pattern.
You can use :valid and :invalid CSS pseudo-classes to customize the styling of the input field based on whether the input is valid or invalid, you can use this to give feedback to the user in case they didn't fill out the field correctly.
The required attribute doesn't validate the input, it just indicates that the field must be filled out, you should use JavaScript or other validation techniques to ensure that the input is valid.
5. Autofocus
The autofocus attribute in HTML is used to specify that an input field should automatically be focused when a web page loads. This can be useful for forms where a specific field, such as a search box or a login field, should be the first thing the user sees and interacts with.
Here's an example of how the autofocus attribute can be used on a text input:
<input type="text" autofocus>
In this example, the text input field will automatically be focused when the web page loads and the user can start typing in it right away.
The autofocus attribute can be used on most input types, including text inputs, checkboxes, radio buttons, and select elements, as well as on the <button> element. However, only the first element with the autofocus attribute set will be focused, so if multiple elements have autofocus, only the first one will be focused.
You can also use JavaScript to set the focus on an element after the page is loaded, this gives you more control over the timing and conditions for focusing an element.
The autofocus attribute should be used with caution, as it can be disruptive for some users, especially for users with cognitive or motor impairments, that's why it's a good practice to use it only when necessary and to test it with users with different abilities.
6. Pattern
The pattern attribute in HTML is used to specify a regular expression that an input field's value must match in order to be considered valid. This can be useful for forms where you want to ensure that the user's input matches a specific format, such as an email address or a telephone number.
Here's an example of how the pattern attribute can be used on a text input:
<input type="text" pattern="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" >
In this example, the text input field will be considered valid only if the value matches the regular expression specified by the pattern attribute, in this case, it's a pattern for email validation.
The pattern attribute is used, the browser will automatically check if the input field's value matches the pattern when the form is submitted. If the input does not match the pattern, the form will not be submitted and an error message will be displayed to the user.
You can also use the title attribute to provide an additional message to the user to explain the expected pattern.
The pattern attribute only checks the input's format, it doesn't check if the input is valid or not, it's a good practice to use this attribute in conjunction with other validation techniques, such as JavaScript or server-side validation. Also, you should be careful when using patterns, as they can be complicated to understand and create, so it's a good practice to test them with different inputs.
7. Form
The <form> element in HTML is used to create an interactive form that can be submitted to a server for processing. A form can contain various form controls, such as text inputs, checkboxes, radio buttons, and select elements, as well as buttons for submitting and resetting the form.
Here's an example of a simple HTML form:
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Submit">
</form>
In this example, the form contains three text inputs for the name, email, and password, and a submit button. The action attribute specifies the URL of the server-side script that will process the form data when it's submitted. The method attribute specifies the HTTP method that will be used to submit the form data (in this case, "post").
The <form> element also has other attributes, such as name, id, and target, that can be used to control the behavior and appearance of the form. The <form> element can also be used with JavaScript to validate the form's input before submitting it to the server, as well as to handle the form's submissions.
All form controls must have a unique name attribute, this allows the form data to be correctly associated with each form control when it's submitted. Also, it's a good practice to use the label element to provide a clear and concise text description
8. Download
The download attribute in HTML is used to specify that a link should download the linked resource instead of navigating to it. This attribute can be used on the <a> and <area> elements.
Here's an example of how the download attribute can be used on an <a> element:
Copy code
<a href="path/to/file.pdf" download>Download PDF</a>
In this example, when the user clicks on the link, their browser will prompt them to download the file "file.pdf" instead of navigating to it.
The download attribute can be used with the href attribute to specify the location of the resource to be downloaded, it can also be used without the href attribute, in this case, the link will download the current page.
You can also use the download attribute to specify a custom name for the downloaded file, for example:
<a href="path/to/file.pdf" download="my_document.pdf">Download PDF</a>
The download attribute is not supported by all browsers, and it's not a secure method to protect your files from being downloaded, you should use other methods such as server-side authentication and access control. Also, the download attribute is not recommended for large files or streaming content, it's better to use other methods such as the <audio> and <video> elements.
9. Media
The media attribute in HTML is used to specify the type of device or screen on which a specific stylesheet or resource should be applied. This attribute can be used on the <link> and <style> elements to apply a specific stylesheet based on the media type, and it can also be used on the <source> element to specify different resources to be used based on the media type.
Here's an example of how the media attribute can be used on an <link> element to apply a specific stylesheet for print:
<link href="print.css" rel="stylesheet" media="print">
In this example, the browser will apply the styles defined in the "print.css" file only when the user wants to print the page, this stylesheet will not be applied when the user is viewing the page on the screen.
The media attribute can also be used to target specific screen sizes and devices, for example:
<link href="mobile.css" rel="stylesheet" media="screen and (max-width: 600px)">
In this example, the browser will apply the styles defined in the "mobile.css" file only when the screen width is less than 600px.
The media attribute can accept a wide variety of media types and media queries, such as "screen", "print", "speech", "all" and many others, you can also use logical operators such as "and" and "not" to create complex media queries.
The media attribute is not supported by all browsers and it's not a replacement for responsive design techniques, it's a good practice to use it in conjunction with other techniques such as media queries, flexible grid, and images
10. Async
The async attribute in HTML is used to specify that a script should be executed asynchronously, meaning that it will not block the loading of the page while it is being executed. This attribute can be used on the <script> element to specify that the script should be loaded and executed as soon as it is available, without delaying the loading of the rest of the page.
Here's an example of how the async attribute can be used on an <script> element:
<script src="script.js" async></script>
In this example, the browser will load the script as soon as it is available, and it will execute it as soon as it is loaded, without delaying the loading of the rest of the page.
The async attribute can only be used with external scripts, it cannot be used with inline scripts. It also might not work for some older browsers that do not support this attribute.
You can also use the defer attribute to indicate that a script should be executed after the page has finished loading. The defer attribute is similar to async, but it guarantees that the script will be executed in the order that they were loaded, while async doesn't.
The async attribute can improve the performance of your website by allowing the browser to load and execute scripts in parallel with the rest of the page, but it can also introduce some complexity in your code, as the execution order of the scripts is not guaranteed, so it
The Tech Platform
Komentar