HomeHTMLHTML Attributes

HTML Attributes

Beginner7 min3 views3 of 6

50% through HTML tutorials

HTML Attributes

HTML attributes provide additional information about HTML elements.

What are Attributes?

  • All HTML elements can have attributes
  • Attributes provide additional information about elements
  • Attributes are always specified in the start tag
  • Attributes 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:

<a href="https://netgramlearn.com">Visit NetGram Learn</a>

The src Attribute

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

<img src="cat.jpg" alt="A cute cat">

The alt Attribute

The alt attribute specifies an alternate text for an image, if the image cannot be displayed:

<img src="logo.png" alt="Company Logo" width="300" height="100">

The style Attribute

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

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

The id Attribute

The id attribute specifies a unique id for an HTML element. The value must be unique within the HTML document:

<h1 id="main-heading">My Heading</h1>
<p id="intro-paragraph">Introduction text.</p>

The class Attribute

The class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class:

<p class="highlight">A highlighted paragraph.</p>
<p class="highlight">Another highlighted paragraph.</p>

Data Attributes

Custom data-* attributes allow storing extra information:

<button data-user-id="42" data-action="delete">Delete</button>

๐Ÿ’ก Tip: Always use lowercase attribute names and quote attribute values for consistency and compatibility.

Test Your Knowledge

2 questions ยท Pass with 70%+

1Which attribute specifies an alternate text for an image?

2Where are attributes placed?

0 / 2 answered