Skip to content

HTML Attributes

HTML Attributes provide additional information for HTML elements. They are key to enhancing element functionality and semantics.

Core Characteristics of Attributes

  • All HTML elements can have attributes.
  • Attributes are always specified in the opening tag.
  • Attributes usually come in name/value pairs, e.g., name="value".
  • Attribute values provide specific configuration for the attribute.

Common Attribute Examples

1. The href Attribute

The <a> tag is used to define a hyperlink. The href attribute specifies the URL the link points to.

html
<a href="https://www.google.com">Visit Google</a>

In this example, href is the attribute name, and https://www.google.com is the attribute value.

2. The src Attribute

The <img> tag is used to embed an image on the page. The src (source) attribute specifies the path or URL of the image.

html
<img src="images/logo.png" />

3. The width and height Attributes

The <img> tag can also use width and height attributes to specify the image's width and height (in pixels).

html
<img src="logo.png" width="250" height="100" />

Note: Although you can set dimensions directly with attributes, modern web development recommends using CSS to control styling.

4. The alt Attribute

The alt (alternative text) attribute provides alternative text for images. When an image cannot be displayed for some reason (e.g., network issues, incorrect path), the browser will display the text in the alt attribute. Additionally, screen readers will read this text aloud, which is crucial for visually impaired users accessing web pages.

html
<img src="logo.png" alt="Website Logo" width="250" height="100" />

If the image fails to load, users will see the text "Website Logo".

5. The style Attribute

The style attribute is used to add inline styles to an element, meaning CSS code is written directly within the tag.

html
<p style="color:red; font-size:16px;">This is a red paragraph.</p>

Note: Inline styles are typically used for quick testing or specific situations. For larger projects, separating CSS into <style> tags or external .css files is a better practice.

6. The lang Attribute

The lang attribute in the <html> tag declares the language of the entire page. This is important for search engines and browsers.

html
<!DOCTYPE html>
<html lang="en">
<body>
    ...
</body>
</html>

en indicates English.

7. The title Attribute

The title attribute can provide additional explanatory information for elements. When the mouse hovers over an element, this information usually appears as a tooltip.

html
<p title="I am a tooltip">Hover over me.</p>

Quotation Marks for Attribute Values

Attribute values should always be enclosed in quotation marks. Both single quotes (') and double quotes (") can be used.

In most cases, double quotes are more common. However, in special cases where the attribute value itself contains double quotes, you can use single quotes to wrap it, and vice versa.

html
<p title='This is a "quote"'>Single quotes wrapper</p>
<p title="This is a 'quote'">Double quotes wrapper</p>

Best Practices Summary

  • Use lowercase: HTML5 standard doesn't require it, but it's recommended to always use lowercase for attribute names.
  • Use quotation marks: Always wrap attribute values in quotes. This avoids potential errors, especially when attribute values contain spaces.

Content is for learning and research only.