HTML Attributes




HTML Attributes


HTML attributes provide additional information about HTML elements.

HTML Attributes

  1. All HTML elements can have attributes
  2. Attributes provide additional information about elements
  3. Attributes are always specified in the start tag
  4. Attributes usually come in name/value pairs like: name="value"

The href Attribute

The <a>(Anchor Tag) tag defines a hyperlink. The href attribute specifies the url of the page and the link goes to:

Example

<a href="https://codeingers.blogspot.com">Visit Codeingers</a>



The src Attribute

The <img> tag is used to embed an image in an HTML page. The src attribute specifies the path to the image to be displayed:

Example

<img src="img_girl.jpg">



The width and height Attributes

The <img> tag should also contain the width and height attributes, which specifies the width and height of the image (in pixels):

Example

<img src="img_girl.jpg" width="500" height="600">



The alt Attribute

The required alt attribute for the <img> tag specifies an alternate text for an image, if the image for some reason cannot be displayed. This can be due to slow connection, or an error in the src attribute, or if the user uses a screen reader.

Example

<img src="img_girl.jpg" alt="Boy with a jacket">

See what happen if we try to display an image this show does not exist:



The style Attribute

The style attribute is used to add styles to an element, such as color, font, size,font width and many more.

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 is meant to assist the search engines and browsers.

The following example specifies English as the language:

Example

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



Chapter Summary

  • All HTML elements can have attributes
  • The href attribute of <a> specifies the url of the page the link goes to
  • The src attribute of <img> specifies the path of the image to be displayed
  • The width and height attributes of <img> provide size information for images
  • The alt attribute of <img> provides an alternate text for an image
  • The style attribute is used to add styles to an element, such as color, font, size, font width and many more
  • The lang attribute of the <html> tag declares the language of the Web page
  • The title attribute defines some extra information about an element

Post a Comment

1 Comments

Please do not enter any spam link in the comment box