Lecture

HTML Attributes

Attributes are used in HTML elements to provide additional information about a tag's behavior, style, data, or to control the element's actions.

HTML attributes are included within the start tag and are written in the format name="value".

Structure of HTML Attributes
<elementName attributeName="attributeValue"> ... </elementName>

Attribute Usage Example

img Tag with src, alt Attributes
<img src="imagePath.jpg" alt="Image description" />

The src attribute is a mandatory attribute for the <img> tag, specifying the path of the image to be displayed.

An img tag without a defined src attribute will not display an image.


Key Attribute Examples

  1. Common to HTML Tags: class, id Attributes

    The id attribute is used to assign a unique identifier to a specific element, whereas the class attribute is used to apply the same style to multiple elements.

    • class: Used when applying the same style to several elements. Multiple elements can share the same class name.
      Example of class Attribute
      <div class="highlight">Highlight class applied</div>
    • id: Used to differentiate specific elements or to select particular elements with JavaScript. The id value must be unique and not duplicated across HTML elements.
      Example of id Attribute
      <div id="main-content">HTML element with main-content ID</div>

  1. Attributes Unique to img Tag: src, alt

    In img elements, src specifies the path to the image, while alt provides alternative text to display when the image cannot be shown.

    img Tag with src and alt Attributes
    <img src="https://picsum.photos/id/237/300" alt="Dog" />

  1. Attributes Unique to a Tag: href

    The href attribute in an a element, which creates a hyperlink used for navigating to other pages, specifies the URL of the page to navigate to.

    a Tag with href Attribute
    <a href="https://www.example.com">Visit Website</a>

Follow the highlighted parts of the code and try entering it yourself.

Mission
0 / 1

Where are attributes located in an HTML element?

Both the start tag and the end tag

Start tag

End tag

Content

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help