HTML Attributes
In the last session you learned that elements are built from tags and content. Attributes give those tags extra information. Without attributes, many elements cannot do their real job — for example, a link needs an address, and an image needs a file path.
What is an HTML attribute?
An HTML attribute is a name and value placed inside an element’s start tag. It tells the browser something about that element that is not the same as the visible text content between tags.
Direct answer
Attributes use a simple pattern: name="value". They belong in the opening tag, before the closing >. Common examples include href for links and src plus alt for images. The next sessions will use attributes often, so learn the pattern once here.
Attributes on link and image
html<a href="https://example.com">Visit Example</a>
<img src="cat.jpg" alt="A cat sitting on a chair">
Reading the example
- On the link,
hrefstores the destination URL - The visible link text is still the content between
<a>and</a> - On the image,
srcpoints to the image file altprovides alternative text when the image cannot load or for accessibility- Both attributes sit inside the start tag
Syntax rules to remember
- Put attributes in the start tag only — not in the end tag
- Separate the attribute name from its value with
= - Wrap most values in quotes:
href="https://example.com" - You can place more than one attribute in the same start tag, separated by spaces
- Attribute names are usually written in lowercase in modern HTML
Several attributes on one tag
html<a href="https://example.com" title="Example website">Visit Example</a>
Here href sets the address and title can provide extra advisory text (often shown as a tooltip in desktop browsers). You do not need every optional attribute on every tag — use the ones the element needs.
Attributes vs content
Content is what sits between a start tag and an end tag, such as the words inside a paragraph. Attributes live on the start tag and configure the element. Mixing them up causes bugs: putting a URL in the link text alone does not create a working link; the URL belongs in href.
A note on empty elements
Empty elements such as img still use attributes, even though they have no separate end tag. That is normal: attributes describe the element; they are not the same thing as wrapped content.
Try it yourself
- Open a basic HTML page in your editor
- Add a link with an
hrefattribute to a real or example URL - Add an
imgwithsrcand meaningfulalttext (use any practice image path you have, or keep a placeholder name for learning the syntax) - Save and open the page in your browser
- Click the link and check that the image markup is present in the page source even if the image file is missing
Common mistakes
- Writing attributes on the closing tag
- Forgetting quotes around values that need them
- Using the wrong attribute for the job (for example putting a URL in
srcon a link instead ofhref) - Leaving
altempty on informative images - Assuming attribute text always appears as visible page content — many attributes configure behavior or accessibility instead
Summary
- Attributes add extra information inside a start tag
- The usual form is
name="value" - Links need
href; images commonly needsrcandalt - Content is between tags; attributes configure the tag
- You can combine multiple attributes on one start tag
- Later lessons reuse this pattern for headings, forms, media, and more
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top