HTML attributes give extra information about HTML elements. They help you customize how things look and behave. Think of them like settings or options for each tag.
What Are HTML Attributes?
- All HTML elements can have attributes
- Attributes provide extra information about an element
- Attributes are always written inside the start tag (never the closing tag)
- Attributes usually come in name/value pairs like
name="value"
<tagname attributename="value">Content</tagname>
The href Attribute (for Links)
The <a> tag creates a link. The href attribute tells the browser where that link should go. Without it, a link does nothing.
<a href="https://www.google.com">Click here to go to Google</a>
Try it Yourself
The href can also link to other pages on your own site, like href="about.html" or href="/contact.html".
The src Attribute (for Images)
The <img> tag embeds an image. The src attribute tells the browser where the image file is located. Without it, you just get a broken icon.
<img src="my-picture.jpg">
There are two ways to write the URL in the src attribute:
Absolute URL – Points to an image on another website. Example: src="https://example.com/images/cat.jpg". But be careful. External images can disappear or change without warning. Also, some images are copyrighted so you might not have permission to use them.
Relative URL – Points to an image inside your own website. Example: src="images/cat.jpg". This is almost always better. Your images will not break if you change your domain name later.
The width and height Attributes
These attributes control how big your image appears on the page. The values are in pixels.
<img src="dog.jpg" width="400" height="300">
Try it Yourself
If you only set width, the height will adjust automatically to keep the image looking right. But setting both gives you more control.
The alt Attribute (for Images)
The alt attribute provides a text description of an image. It shows up if the image fails to load. It also helps people who use screen readers (software that reads web pages out loud).
Technically, the alt attribute is required. Always include it.
<img src="sunset.jpg" alt="Orange sun setting over the ocean">
Now watch what happens if the image cannot be found:
See how the alt text shows up instead? That is why it matters.
The style Attribute (Add Some Flavor)
The style attribute lets you add CSS styles directly to an element. You can change colors, fonts, sizes, and more.
<p style="color:red;">This paragraph is red.</p>
<p style="font-size:20px;">This text is bigger.</p>
<p style="background-color:lightblue;">This has a blue background.</p>
Try it Yourself
Later you will learn better ways to style pages, but the style attribute is great for quick experiments.
The lang Attribute (Set Your Language)
You should always add the lang attribute to your <html> tag. It tells browsers and search engines what language your page is written in. This helps with translation tools and screen readers.
English page example:
<!DOCTYPE html>
<html lang="en">
<body>
...
</body>
</html>
You can also add a country code:
<html lang="en-US">
<html lang="es-MX">
<html lang="fr-FR">
You can look up language codes online if you need something specific.
The title Attribute (Tooltips)
The title attribute adds a little tooltip. When someone hovers their mouse over the element, the text pops up.
<p title="I am a secret tooltip">Hover your mouse over this paragraph.</p>
Try it Yourself
Go ahead, hover over the text in the preview. See the tooltip appear? That is the title attribute.
Always Use Lowercase for Attribute Names
Technically, HTML does not care if you use uppercase or lowercase. HREF and href both work. But almost every developer uses lowercase. It is the standard. It looks cleaner. Stick with lowercase.
Good: <a href="page.html">
Not great: <a HREF="page.html">
Always Quote Your Attribute Values
HTML does not strictly require quotes, but you should use them anyway. Without quotes, things break if your value has a space.
Good (with quotes):
<a href="https://www.google.com">Google</a>
Bad (no quotes, will cause problems):
<a href=https://www.google.com>Google</a>
Double quotes are most common. But single quotes work too. Use double quotes unless you have a good reason not to.
Sometimes you need both. For example, if your attribute value has double quotes inside it, wrap everything in single quotes:
<p title='John "Shotgun" Nelson'>Some text</p>
Or the other way around:
<p title="John 'Shotgun' Nelson">Some text</p>
Quick Summary
- Attributes go in the start tag, not the closing tag
- The
hrefattribute tells links where to go - The
srcattribute tells images where to load from - The
widthandheightcontrol image size - The
altattribute provides a text backup for images - The
styleattribute adds quick CSS styles - The
langattribute declares your page language - The
titleattribute creates tooltips on hover - Always use lowercase attribute names
- Always put quotes around attribute values (double quotes preferred)