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"
.
<elementName attributeName="attributeValue"> ... </elementName>
Attribute Usage Example
<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
-
Common to HTML Tags:
class
,id
AttributesThe
id
attribute is used to assign a unique identifier to a specific element, whereas theclass
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. Theid
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>
-
Attributes Unique to img Tag:
src
,alt
In
img
elements,src
specifies the path to the image, whilealt
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" />
-
Attributes Unique to a Tag:
href
The
href
attribute in ana
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.
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