goer.org | HTML Tutorial : Introduction : Attributes

Table of Contents

About
Introduction
Diving In
Structure
Tinkering
Tags
Attributes
Browser Tools
Basic
Paragraph Breaks
Headings
Font Styles
Colors
Links
Images
Lists
Intermediate
Style Sheets
Classes and IDs
Div and Span
Font Control
Borders
Margins and Padding
Align and Indent
Advanced
Tables
Miscellany
SHTML

Attributes

Attributes modify the behavior of a tag. Attributes allow you to change sizes, colors, specify link locations, and much more.

Attribute Components

Attributes have two main components:

<tagname attribute1="value1" attribute2="value2"...>
...(text or more tags go here)...
</tagname>

Tags may have multiple attributes, and the order of the attributes is irrelevant.

Attributes are almost always optional. They add some sort of effect if you put them in, but you usually won't get in trouble if you leave them out. Always remember: if you forget to add something or make a mistake, the browser is supposed to do nothing. That's usually a good thing.

Note that each tag has a list of permitted attributes. You cannot freely add any attribute to any tag and expect it to work.

We provided an example of an attribute in the Tags section. In that example, the style attribute controlled the background color of the entire webpage. Here's another example:

Link to Yahoo (source)

<a href="http://www.yahoo.com">link to Yahoo</a>

The <a> tag (or, "anchor" tag) allows us to create an HTML link to another web page. We specify the location with the href attribute, which we set to the value, "http://www.yahoo.com". The results look like this:

Link to Yahoo (Results)



Link to Yahoo

If you select the link, you will indeed go to Yahoo.

Similarities to Tags

As with tags:

Quote Marks

You may quote attribute values if you like. That is, href=http://www.yahoo.com is the same as href="http://www.yahoo.com". You must use a quote mark if the value contains a special character such as a space or an angle bracket (< >). If you don't use quotes for special characters, the browser will interpret the special character as the end of the attribute or tag. The resulting garbled HTML will not be pretty.

When you do use quote marks, you must be sure to close them. Otherwise your attribute will "continue" until the next quote mark... which could be a long while. Again, you'll notice the garbled results immediately, although tracking down the source of the problem might take some work.