Html Attribute

Attributes define additional characteristics or properties of the element such as width and height of an image. Attributes are always specified in the start tag (or opening tag) and usually consists of name/value pairs like name="value" . Attribute values should always be enclosed in quotation marks.

Some HTML Attributes are given below .

1. The href Attribute

The <a> tag defines a hyperlink. The href attribute specifies the URL of the page the link goes to:

Example:

<!DOCTYPE html>

<html>

<body>

<h2>The href Attribute</h2>

<p>HTML links are defined with the a tag. The link address is specified in the href attribute:</p>

<a href="https://www.w3fcoders.blogspot.com">Visit Code learner</a>

</body>

</html>

Output


2. 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

<!DOCTYPE html>

<html>

<body>

<h2>The src Attribute</h2>

<p>HTML images are defined with the img tag, and the filename of the image source is specified in the src attribute:</p>

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

</body>

</html>


Output





3. The width and height Attributes

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

Example 

<!DOCTYPE html>

<html>

<body>

<h2> Width and Height Attributes</h2>

<p>The width and height attributes of the img tag, defines the width and height of the image:</p>

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

</body>

</html>

 Output



4. 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

<!DOCTYPE html>

<html>

<body>

<h2>The alt Attribute</h2>

<p>The alt attribute should reflect the image content, so users who cannot see the image gets an understanding of what the image contains:</p>

<img src="img_girl.jpg" alt="Girl with a jacket" width="500" height="600">

</body>

</html>

Output


5. The style Attribute

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

Example

<!DOCTYPE html>

<html>

<body>

<h2>The style Attribute</h2>

<p>The style attribute is used to add styles to an element, such as color:</p>

<p style="color:red;">This is a red paragraph.</p>

</body>

</html>

Output



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 to 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, and 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.