HTML Attributes
HTML attributes provide additional information about HTML elements.
Understanding Attributes
- All HTML elements can have attributes.
- Attributes provide additional information about an element's behavior or properties.
- Attributes are always specified in the start tag.
- They usually come in name/value pairs like:
name="value".
The href Attribute
The <a> tag defines a hyperlink. The href attribute specifies the URL of the page the link goes to.
Example
<a href="https://www.smalseotool.com">Visit Smal SEO Tool</a>The src Attribute
The <img> tag embeds an image. The src attribute specifies the path to the image.
Example
<img src="https://picsum.photos/seed/girl/200/300" alt="A sample image">There are two ways to specify a URL in the src attribute:
- Absolute URL: A link to an image hosted on another website (e.g., `https://www.example.com/image.jpg`).
- Relative URL: A link to an image hosted within your own website. This is generally the best practice as it won't break if you change your domain name.
The width and height Attributes
The <img> tag should also include width and height attributes. These specify the size of the image in pixels, helping the browser reserve space for the image before it loads.
Example
<img src="https://picsum.photos/seed/girl/250/300" alt="Girl with a jacket" width="250" height="300">The alt Attribute
The required alt attribute for <img> provides alternative text for an image if it cannot be displayed. This is crucial for accessibility (for screen readers) and for SEO.
Example of Alt Text
<img src="img_girl.jpg" alt="Girl with a jacket">If the image path is broken, the alt text will be displayed instead:
Example of Broken Image
<img src="img_typo.jpg" alt="Girl with a jacket">The style Attribute
The style attribute is used to add inline CSS styles to an element, such as color, font size, etc.
Example
<p style="color:red;">This is a red paragraph.</p>The lang Attribute
You should always include the lang attribute inside the <html> tag to declare the language of the web page. This assists search engines and browsers.
<!DOCTYPE html>
<html lang="en">
<body>
...
</body>
</html>You can also add a country code to be more specific:
<!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>The title Attribute
The title attribute defines extra information that appears as a tooltip when you hover over the element.
Example
<p title="I'm a tooltip">Mouse over this paragraph to see the title.</p>Best Practices: Lowercase and Quotes
While HTML is not strict about it, the best practice recommended by the W3C is to always use lowercase for attribute names and to always wrap attribute values in quotes (double quotes are most common).
Good:
<a href="https://www.smalseotool.com/html/">Visit our HTML tutorial</a>Bad (Avoid):
<a href=https://www.smalseotool.com/html/>Visit our HTML tutorial</a>Chapter Summary
- All HTML elements can have attributes.
- The
hrefattribute of<a>specifies the link's URL. - The
srcattribute of<img>specifies the image's path. - The
widthandheightattributes define image size. - The
altattribute provides text for screen readers and when images don't load. - The
styleattribute adds inline CSS. - The
langattribute declares the page language. - The
titleattribute provides extra info as a tooltip.
Test Yourself with an Exercise
Which of the following is a correct syntax for using an HTML attribute?