![]() |
VOOZH | about |
HTML Attributes are special words used within the opening tag of an HTML element. They provide additional information about HTML elements. HTML attributes are used to configure and adjust the element's behaviour, appearance, or functionality in a variety of ways.
<img> src "https://media.geeksforgeeks.org/wp-content/cdn-uploads/Geek_logi_-low_res.png" <img> tag is used for embedding images in an HTML page. The src attribute within the <img> tag specifies the path to the image file you wish to display. This attribute is crucial as it directs the browser to the image’s location on the internet or a local directory. <tagname attribute_name = "attribute_value"> content... </tagname>
An HTML attribute consists of two primary components:
1. attribute_name: This is the name of the attribute, which specifies what kind of additional information or property you are defining for the element. Common attribute names include href, src, class, id, etc.
2. attribute_value: The value is assigned to the attribute to define the specific setting or behavior. It is always placed in quotes.
HTML attributes can be broadly categorized based on their function and the type of elements they modify. For example -
These attributes can be used with any HTML element (though their effects might vary based on the element):
| Attribute | Description |
|---|---|
| class | Groups elements and allows styling. |
| style | Inline CSS styles. |
| contenteditable | Determines whether the content within the element is editable. |
| role | Specifies the element’s accessibility role. |
| tabindex | Determines the order of focus during keyboard navigation. |
| id | Assigns a unique identifier to an element, allowing targeting with CSS or JavaScript. |
| title | Creates a tooltip that appears when a user hovers over the element. |
| lang | Specifies the language of the element’s content, aiding with translation and accessibility. |
Some other main types of HTML attributes are:
charset. Let's take look at some of the most commonly used HTML attributes:
The alt attribute in HTML provides alternative text for an image if the image cannot be displayed. It improves accessibility and provides context for screen readers.
The width and height Attribute is used to adjust the width and height of an image(in pixels).
The id attribute in HTML assigns a unique identifier to an element, allowing it to be targeted by CSS and JavaScript for styling and manipulation purposes.
The title attribute is used to explain an element by hovering the mouse over it. The behavior differs with various elements but generally, the value is displayed while loading or hovering the mouse pointer over it.
The href attribute in HTML, used with the <a> tag, specifies a link destination. Clicking the linked text navigates to this address. Adding `target="_blank"` opens it in a new tab.
The style attribute is used to provide various CSS effects to the HTML elements such as increasing font-size, changing font-family, coloring, etc.
The language is declared with the lang attribute. Declaring a language can be important for accessibility applications and search engines.
1. Always Use Lowercase Attributes:
2. Always Quote Attribute Values:
3. Declare Quote as an Attribute Value:
<input type="text" placeholder='Enter your "username" here'>In this example, the attribute value itself contains double quotes ("username"), so the entire value is enclosed within single quotes to avoid confusion.
4. Boolean Attributes Should Be Written Without Values:
Example:
<input type="checkbox" checked>Writing checked="checked" also works, but it is redundant. Simply including the attribute is enough to represent a true state.
5. Proper Attribute Order for Readability:
id, class, other global attributes, specific attributes, and finally, event attributes.<button id="btn1" class="button-class" type="submit" onclick="handleClick()">Submit</button>6. Avoid Deprecated Attributes:
align, bgcolor, and border, are considered deprecated. <p style="text-align: center;">This text is centered.</p>Instead of using the align attribute, use the style attribute or a CSS class to achieve the same effect.