We have seen that tags are like verbs or commands;
<em> means “start emphasis”
</p> means “end a paragraph”, and
<br /> means “move to a new line.”
If you remember your high school English, you know that verbs are modified by adverbs, which tell a verb where, when, how, or to what extent it should perform its action. If HTML tags are like verbs, we should have something like adverbs to allow us to modify the way they work. These “adverbs” are called attributes.
Let’s start by adding an attribute to an element you already
know: the <abbr> element.
You are learning <abbr title="Hypertext Markup Language">HTML</abbr>.
title="Hypertext Markup Language" is the attribute.title is the attribute name.Hypertext Markup Language is the attribute value.Here is what it looks like. Hover the mouse over the HTML and see what happens.
In this course, you will always write the attribute name in lowercase to be compatible with the XHTML syntax, though it can be in either upper- or lowercase in HTML syntax. You will also always enclose the attribute value in either double quotes or single quote marks. In HTML syntax, the quote marks are sometimes optional. This was always a source of great confusion to beginning authors, who would constantly have to worry about whether or not they had one of the special cases that didn’t need them.
You may put whitespace around the equal sign if you think it improves readability. You must put whitespace between the tag name and the attribute name.
Here is another example using a new element:
the <hr /> element. This
element draws a
horizontal rule, or line. It’s called a
“rule” because that’s the typographical term for a line.
You’ll notice that a lot of HTML terminology is borrowed from the
world of typesetting and typography. The <hr />
element is an empty element, which means it uses the XHTML shortcut
for opening-and-closing-tag-combined-into-one. Here’s
what it looks like:
As you see, it draws a horizontal rule all the way across the browser window. But let’s say you want it to have a width that stretches only 70% of the way across the window. Write this tag:
<hr style="width: 70%" />
and it draws this:
As before, the attribute name is all lowercase, there is whitespace between the tag name and the attribute name, and the attribute value is enclosed in quote marks. By the way, we have just sneaked in the first look at using styles to affect how an element is presented.
Well, as long as we have started using styles, here is a way to
use the style attribute to change the position of text on your page.
Try these and see what they do.
<p style="text-align: center">Centered Text</p> <p style="text-align: right">Right-aligned text</p> <p style="text-align: left">Left-aligned text (the default)</p>
You can also do text alignment on headers <h1> through
<h6> (among many other elements).
Try it and find out.
It is possible to have more than one attribute on a tag, just as you can have more than one adverb modifying a verb. However, at this point, there are no really great examples of this. We will return to this subject at a later point.
For the moment, remember
that your attribute names must be unique (you can’t put
two title attributes on a <abbr>),
and you must have whitespace between attributes.
Important: Attributes go on the
opening tag only. You never put attributes on a closing tag;
it’s unnecessary, and it’s not valid HTML.
You’ve learned quite a bit of HTML so far, and you may be tempted to write something like this:
<p>Wow! Now I know about the <dfn> and <q> elements!</p>
Of course, we all know what you intend, but that’s certainly not
what you’ll get. Try it and find out.
The problem is that the <b> and
<i> are real tags. You need some way to tell
the browser, “No, no, no. That less than sign is not the
beginning of a tag; I really want a plain old less than sign.”
Whenever you need a real less than sign, you must use
<. That’s an ampersand symbol (&) followed
by the letters lt, which stand for less
than, followed by a semicolon. In fact, this is what we’ve been
doing all along to show you these examples of HTML.
An ampersand followed by an abbreviation or a numeric code and a semicolon is called a character entity. These are listed in Appendix D of the book.
The other character entity which is absolutely required is the
&, which produces an ampersand. Thus, if you want
to say “x & y are < 15”, you must do it
this way:
x & y are < 15
Although browsers are very forgiving, and will almost always let you get away with
using a normal & or < rather than the
character entity, the key word here is almost.
Even if you do get away with it, it’s still not valid HTML.
You never need a character entity for a greater than sign, since
there’s no question whether a > belongs to a tag or
not. However, for the sake of symmetry, HTML provides the
> for a greater than symbol.
You could write the first example above in either of these two ways. They both work (try it and find out) but, for someone who’s used to HTML, the second one looks better.
<p>Wow! Now I know about the <dfn> and <q> elements!</p> <p>Wow! Now I know about the <dfn> and <q> elements!</p>
As the book says, you can also use numeric codes for character
entities. A less than sign can also be written as
< and an ampersand as &. The
# is required in valid HTML.
These character entities also let you put “foreign” characters into your documents. The word foreign is in quotemarks because it’s only foreign to people who speak English. Let’s say we want to display this:
<p>España y México</p>
If you type it exactly that way, and you’re using a PC, it will look
great on a PC. If you display the same page on a Macintosh, it won’t
look right, because the character codes for ñ and
é aren’t the same on the two systems. If you use
the character entities (either abbreviations or numeric), however, the
page will display correctly on any system:
<p>España y México</p> <p>España y México</p>
Did you see those quote marks around the word “Foreign” in the preceding section? Those look much better than using straight up-and-down quotes like this: "Foreign"—especially when they are in a heading. You should always use these curly quotes and a curly apostrophe to give your documents a more professional look.
| To get | Type this | or this |
|---|---|---|
| “ | “ |
“ |
| ” | ” |
” |
| ’ | ’ |
’ |
In these entity names,
“ stands for left double quote,
” stands for right double quote, and
’ stands for right single quote.
Is there a ‘?
Try it and find out!
In older HTML documents, you will see curly quotes
produced using
character entities “ and
”. Please don’t do this. Those character
codes are a Windows-only standard. The world of HTML and other
technologies from the World Wide Web consortium is based on the
Unicode standard, which you may read about at
http://www.unicode.org.
Instead, always use the Unicode numbers “
for opening curly quotes and ” for closing
curly quotes.
As a matter of record, all the character entities with numbers
€ through Ÿ are
Windows-standard, not Unicode-standard.
Sometimes there are phrases or sections of text that you don’t want word-wrapped. For example, you might want a company name like “Rodriguez and Sons” to always stay together on one line. Or, you might want a mathematical equation like “x = y + z” to not be broken across a line. In this paragraph, they will break if you resize the browser window. If you’re viewing this in the browser, try it..
To tell the browser that you want a space, but you don’t want
it to word wrap, you use the character entity
which stands for non-breaking space, or its
numeric equivalent,  .
This paragraph has the phrase “Rodriguez and Sons” and the mathematical equation “x = y + z” written with non-breaking spaces. If you resize the browser window, you will see that the phrase and the equation always stay together; they are never broken onto separate lines. They are written this way:
Rodriguez and Sons x = y + z
Yes, this is incredibly ugly, and it will give you fits when you are editing your documents. Your readers, however, will see text that works the way it ought to, and they will thank you for it.