In this chapter, you have learned the basics of adding images to your Web pages. Although there is more to learn, this will give you enough to practice with as you become comfortable with HTML. The following table lists some of the markup required to embed and position images on your Web page:
|
To Do This |
Use This |
|---|---|
|
Embed an image |
<img src="image.gif" /> |
|
Make an image a link |
<a href="webpage.htm"><img |
|
Make a combined image link and text link |
<a href="webpage.htm"><img |
|
Provide alternate text |
<img src="image.gif" alt="alternate |
|
Specify an image’s height and width |
<img src="image.gif" height="#" |
|
Align an image to the left or right, with text wrapping (Deprecated) |
<img src="image.gif" align="left" /> |
|
Position an image relative to text or another image (Deprecated) |
<img src="image.gif" align="top"> |
|
Add white space on the sides of an image (Deprecated) |
<img src="image.gif" hspace="#"> |
|
Add white space on an image’s top and bottom |
<img src="image.gif" vspace="#" /> |
|
Use CSS to add a background image (tiled) |
<style> body {background-image:
|
|
Use CSS to add a background watermark |
<style> body {background-image:
|
|
Use CSS to add a background image down the left side of your page |
<style> body {background-image:
|
|
Use CSS to add a red inset border around your images |
<style> img {
|
Understand XHTML
Create an XHTML document
Validate your XHTML
Plan, publish, and promote your Web site
In Chapters 1 through 6, you learned enough HTML basics to construct a simple Web site. You have also been introduced to XHTML and its somewhat stricter rules. By now you may be wondering if all you have to do to write in XHTML is to write your HTML markup more
As you recall, XHTML was developed because of HTML’s inability to grow and change with the ever-expanding technology of our day. After all, when HTML was first developed, the concept of accessing the Internet by means of your cellular phone was more the stuff of science
The acronym XML stands for the
Extensible Markup Language,
a powerful
meta
-language that is a subset of an even more powerful
If you were working in XML and wanted to use it to develop your own markup language, you would create a
DTD
(Document Type Description)
. This is
A document type declaration is found at the very beginning of an XML document and functions as a pointer, directing a browser (or other software) to the location of that document’s DTD. That way, the browser can look up the “blueprint,” compare your document to it, and then display your document properly. For example, the document type declaration for an HTML 4.01 document looks like this: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">. You don’t often see the HTML Doctype declaration used because Web browsers don’t need it for interpreting HTML pages. However, when a document type declaration is used, it is placed before the opening HTML tag, as in the following code listing:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> (and so on)
Although this
So how do you sort out the differences between SGML, HTML, XHTML, and XML without getting a
The current specification is XHTML 1.1, and it will be covered in Chapter 16. However, the original version of XHTML (and the one still most commonly used) is XHTML 1.0. This specification actually uses three different DTDs: Transitional, Strict, and Frameset.
The Transitional DTD still allows for the use of HTML’s deprecated elements and attributes.
The
Strict DTD
does not permit the use of any presentational (deprecated) elements or attributes. In other words, you may not use HTML elements and attributes to control the appearance (
The
Frameset DTD
is for use when you are designing
When you write an XHTML 1.0 page, you need to identify which of the
Transitional <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Strict <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Frameset <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
| Caution |
The <!DOCTYPE> command is case sensitive. Make sure that you type the commands in exactly as you see them here, or your page will not validate. See Chapter 16 for the XHTML 1.1 <!DOCTYPE> command. |
Another small change in how an XHTML document is written is found in the opening <html> tag. Although you still use <html> as your root element, you must add a reference that points to the XHTML
namespace
. If you’re
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
As you’ll see in Chapter 16, it is this ability to identify with a namespace that enables you as a Web designer to extend XHTML by adding your own markup. For now, just remember to add the namespace attribute to your opening <html> tags.
Another requirement for writing XHTML documents is that they must be well formed . This is in contrast to HTML, which can be (and often is) written rather haphazardly. Browsers sometimes ignore mistakes in HTML code. You don’t have this luxury with XHTML. Your document must be well formed, which simply means it must contain no errors in syntax. A simple guideline for writing well-formed XHTML is as follows:
All elements (except empty elements) must have opening and closing tags:
<element> </element>
All elements must be properly nested:
<a> <b> </b> </a>
Empty elements must be properly closed:
<empty-element />
All attribute values must be quoted:
<element attribute="value">
Attributes must always have values:
<input checked="checked">
Elements and attributes must be written in lowercase:
Incorrect: <HTML>, <Html> Correct: <html>
An XHTML document must not only be well formed, but must also be
valid
. What’s the difference? The term “well formed” applies to the document’s grammar or syntax. If an XHTML document is written correctly, according to the rules mentioned in the preceding section, then it is well formed. However, in order to be “valid,” the document must be checked or
How do you validate your pages? The first step is by including the proper <!DOCTYPE> declaration for the version of XHTML you are using. The second step is to write your pages according to the rules that apply for that DTD. For example, if you decide to use the Strict DTD, be sure that you don’t use any of the deprecated elements or attributes. Otherwise, your document will not validate. This is because those elements and attributes are not recognized in the Strict DTD.
Finally, you will be wise to validate (compare) your page against that DTD. How do you do this? Many HTML editors have built-in validation programs that will point out where your pages need to be changed if you want them to be valid. The W3C has a validation service (http://validator.w3.org/) that checks your pages free of charge. You simply go to the site, enter the URL of the page you want validated into the form, and in a few seconds you will have a generated report showing where your page measures up against the DTD you’ve
| Caution |
You are not required to validate your pages against a DTD in order to write XHTML. In fact, even if they are not valid, your XHTML pages will still display correctly—for now. However, there are at least two good reasons to take the time to validate your pages. First, it will speed up your learning process. These sites help you find mistakes in your code and learn why your pages are not doing what you want them to. Second, in the future as browsers begin to follow the stricter standards, invalid pages may fail to display at all. That is why it’s a good practice to validate all of your XHTML pages. It’s more work now, but you will save yourself a lot of work in the future. |
Although the idea of well-formed and valid XHTML documents is simple once you’ve had time to think about it, there still can be a rather large
Add the <!DOCTYPE> declaration for the XHTML Transitional DTD. This should be inserted just before the opening <html> tag.
Modify the opening <html> tag to include the XHTML namespace:
<html xmlns="http://www.w3.org/1999/xhtml" >
Because the W3C’s validator requires that your document provide character encoding (or Unicode) information, add the following <meta> element between the <head> tags:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7" />
Because of the explosive growth of the Internet, it’s a good idea to always provide character-encoding information in your pages. This lets browsers know what character set they need to use in displaying your text. In the preceding code, this is done by using the <meta /> (descriptive) element.
Now save your document and go online to http://validator.w3.org.
Under the Validate Files heading, select the Local File option by entering into the input window the location of the page you just saved, as shown here:
If you don’t want to type the information, click Choose. The Open dialog box pops up, and you can navigate to the proper file folder and click the file you want to validate, as in the illustration that follows:
When the location of the file has been entered into the input window, click Validate File. If your xhtml1.htm file is valid, you will see a window that looks like this:
| Note |
If your page
|
But what if your page is not valid? Then you will see a different window, informing you that the page has not validated and giving you the reasons why. To see what this looks like (
| Note |
You’ll discover that the page does not validate according to the Strict DTD. Why? Because the home page link near the bottom of the page is not nested inside another element. According to the Strict DTD, the < a > element cannot stand on its own. It must be nested in another element (such as < p > , < h1 > , and so on). To bring the page into conformance, just enclose the < a > element inside a set of < p > tags, as in the following: < p >< a href="index.htm" > Home < /a >< /p > . |
If you are having difficulty getting the page to validate, try working from the code listing that follows. It validated against the XHTML 1.0 Transitional DTD and, with the correction made in the above Note, against the XHTML 1.0 Strict DTD.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My HTML Reference Guide</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7" /> </head> <body> <h1>Pages I've Created</h1> <ul> <li><a href="headings.htm">Headings</a></li> <li><a href="text.htm">Text Elements</a></li> <li><a href="sup.htm">Superscript & Subscript</a></li> <li><a href="del.htm">Deleted Text</a></li> <li><a href="pre.htm">Preformatted Text</a></li> <li><a href="ulist.htm">Unordered List</a></li> <li><a href="ulist2.htm">Multi-level Unordered List</a></li> <li><a href="olist.htm">Ordered List</a></li> <li><a href="olist2.htm">Ordered List with Start Attribute</a></li> <li><a href="olist3.htm">Outline List</a></li> <li><a href="dlist.htm">Definition List</a></li> <li><a href="text-format.htm">Text Formatting</a></li> <li><a href="generic-fonts.htm">Generic Fonts</a></li> <li><a href="font-colors.htm">The Color Property</a></li> <li><a href="16colors.htm">The Sixteen Basic Colors</a></li> <li><a href="css-color.htm">CSS Color</a></li> </ul> <a href="index.htm">Home</a> </body> </html>