-
Hajipur, Bihar, 844101
Attributes provide additional information about HTML elements. They are always written inside the opening tag, in the form of:
<tagname attribute="value">Content</tagname>
<a href="https://www.google.com">Visit Google</a>
href
is the attribute
"https://www.google.com"
is the value
It tells the browser where to go when the link is clicked
Attribute | Description | Used With |
---|---|---|
href |
Specifies the link URL | <a> |
src |
Specifies the image source | <img> |
alt |
Specifies alternative text for an image | <img> |
style |
Adds inline CSS styles | Any tag |
title |
Tooltip text on hover | Any tag |
id |
Unique identifier for the element | Any tag |
class |
Groups elements for styling | Any tag |
width / height |
Sets the size of elements | <img> , <video> |
Always use name="value" format
Always use quotes around values
Attributes must be written in the opening tag
You can use multiple attributes in one element
<img src="flower.jpg" alt="Beautiful Flower" width="300" height="200">
style
Attribute<p style="color: red; font-size: 20px;">This is a red paragraph.</p>
title
<p title="This is a tooltip!">Hover over me to see tooltip.</p>
id
and class
Attributes<p id="main-text" class="highlight">This is important text.</p>
You can then style it using CSS like:
#main-text {
font-weight: bold;
}
.highlight {
background-color: yellow;
}
Q1. Create an anchor tag with an href
pointing to https://example.com
.
Q2. Insert an image using the src
and alt
attributes.
Q3. Use the style
attribute to change a paragraph's color to green.
Q4. Add a title
to a heading that shows “Main Title” when hovered.
Q5. Add a paragraph with id="intro"
and class="text"
.
Q6. Use the width
and height
attributes to resize an image.
Q7. Create a tooltip using the title
attribute in a <p>
tag.
Q8. Use multiple attributes (e.g., style
, title
) in one <div>
.
Q9. Create a styled button using style="background-color: blue;"
.
Q10. Add a class="warning"
to two different elements.
Q1: What is the purpose of HTML attributes?
Q2: Where are attributes placed in an HTML element?
Q3: Which attribute is used to define a hyperlink destination?
Q4: What does the alt attribute do in an image tag?
Q5: Which attribute adds inline CSS to an element?
Q6: What happens if you forget quotes around an attribute value?
Q7: Which attribute is used to show text when the user hovers over the element?
Q8: What does id in HTML do?
Q9: What is the difference between id and class?
Q10: Which is the correct way to use multiple attributes?