CHAPTER 4 CREATING A SIMPLE PAGE

(HTML Overview)

IN THIS CHAPTER

An introduction to (X)HTML elements and attributes

A step-by-step demonstration of marking up a simple web page

The elements that provide document structure

Basic text and image elements

A simple style sheet

Troubleshooting broken web pages

Part I provided a general overview of the web design environment. Now that we’ve covered the big concepts, it’s time to roll up our sleeves and start creating a real web page. It will be a simple page, but even the most complicated pages are based on the principles described here.

In this chapter, we’ll create a simple web page step by step so you can get a feel for what it’s like to mark up a document with (X)HTML tags. The exercises allow you to work along.

This is what I want you to get out of this chapter:

• Get a feel for how (X)HTML markup works, including an understanding of elements and attributes.

• See how browsers interpret (X)HTML documents.

• Learn the basic structure of an (X)HTML document.

• Get a first glimpse of a style sheet in action.

Don’t worry about learning the specific text elements or style sheet rules at this point; we’ll get to those in the following chapters. For now, just pay attention to the process, the overall structure of the document, and the new terminology.

A Web Page, Step by Step

You got a look at an (X)HTML document in Chapter 2, How the Web Works, but now you’ll get to create one yourself and play around with it in the browser. The demonstration in this chapter has five steps that cover the basics of page production.

Step 1: Start with content. As a starting point, we’ll add raw text content and see what browsers do with it.

Step 2: Give the document structure. You’ll learn about (X)HTML elements and the elements that give a document its structure.

Step 3: Identify text elements. You’ll describe the content using the appropriate text elements and learn about the proper way to use (X)HTML.

Step 4: Add an image. By adding an image to the page, you’ll learn about attributes and empty elements.

Step 5: Change the look with a style sheet. This exercise gives you a taste of formatting content with Cascading Style Sheets.

(X)HTML

(X)HTML is a shorthand way to refer to both HTML and its latest version, XHTML. Authors may write documents in either version. For a detailed explanation of the differences, see Chapter 10, Understanding the Standards.

By the time we’re finished, you will have written the source document for the page shown in Figure 4-1. It’s not very fancy, but you have to start somewhere.

We’ll be checking our work in a browser frequently throughout this demonstration—probably more than you would in real life—but because this is an introduction to (X)HTML, it is helpful to see the cause and effect of each small change to the source file along the way.

Image

Figure 4-1. In this chapter, we’ll write the source document for this web page step by step.

HTML the Hard Way

With all the wonderful web-authoring tools out there today, chances are you will be using one to create your pages.

You may be asking, “If the tools are so great, do I need to learn HTML at all?” The answer is yes, you do. You may not need to have every element memorized, but some familiarity is essential for everyone who wants to make web pages. If you apply for a “web designer” position, employers will expect that you know your way around an (X)HTML document.

I stand by my method of teaching (X)HTML the old-fashioned way—by hand. There’s no better way to truly understand how markup works than typing it out, one tag at a time, then opening your page in a browser. It doesn’t take long to develop a feel for marking up documents properly.

Understanding (X)HTML will make using your authoring tools easier and more efficient. In addition, you will be glad that you can look at a source file and understand what you’re seeing. It is also crucial for troubleshooting broken pages or fine-tuning the default formatting that web tools produce.

Before We Begin, Launch a Text Editor

In this chapter and throughout the book, we’ll be writing out (X)HTML documents by hand, so the first thing we need to do is launch a text editor. The text editor that is provided with your operating system, such as Notepad (Windows) or TextEdit (Macintosh), will do for these purposes. Other text editors are fine as long as you can save plain text (ASCII) files with the .html extension. If you have a WYSIWYG web authoring tool such as Dreamweaver or FrontPage, set it aside for now. I want you to get a feel for marking up a document manually (see the sidebar, (X)HTML the Hard Way).

This section shows how to open new documents in Notepad and TextEdit. Even if you’ve used these programs before, skim through for some special settings that will make the exercises go more smoothly. We’ll start with Notepad; Mac users can jump ahead.

Creating a new document in Notepad (Windows users)

These are the steps to creating a new document in Notepad on Windows XP (Figure 4-2).

1. Open the Start menu and navigate to Notepad (in Accessories). Image

2. Clicking on Notepad will open a new document window, and you’re ready to start typing. Image

3. Next, we’ll make the extensions visible. This step is not required to make (X)HTML documents, but it will help make the file types more clear at a glance. In any Explorer window, select “Folder Options...” from the Tools menu Image and select the “View” tab. Image Find “Hide extensions for known file types” and uncheck that option. Image Click OK to save the preference and the file extensions will now be visible.

Image

Figure 4-2. Creating a new document in Notepad.

Creating a new document in TextEdit (Macintosh users)

By default, TextEdit creates “rich text” documents, that is, documents that have hidden style formatting instructions for making text bold, setting font size, and so on. (X)HTML documents need to be plain text documents, so we’ll need to change the Format, as shown in this example (Figure 4-3).

1. Use the Finder to look in the Applications folder for TextEdit. When you’ve found it, double-click the name or icon to launch the application.

2. TextEdit opens a new document. You can tell from the text formatting menu at the top that you are in Rich Text mode Image. Here’s how you change it.

3. Open the Preferences dialog box from the TextEdit menu.

4. There are three settings you need to adjust:

Select “Plain text”. Image

Select “Ignore rich text commands in HTML files”. Image

Turn off “Append ‘.txt’ extensions to plain text files”. Image

5. When you are done, click the red button in the top-left corner. Image

6. Quit TextEdit and restart it to open a new document with the new Plain Text settings. The formatting menu will no longer be on the new document.Image

Image

Figure 4-3. Launching TextEdit and choosing Plain Text settings in the Preferences.

Step 1: Start with Content

Now that we’ve got our new document, it’s time to get typing. A web page always starts with content, so that’s where we begin our demonstration. Exercise 4-1 walks you through entering the raw text content and saving the document in a new folder.

exercise 4-1 | Entering content

  1. Type the content for the home page into the new document in your text editor. Just copy it as you see it here. Keep the line breaks the same for the sake of playing along.

    Black Goose Bistro

    The Restaurant
    The Black Goose Bistro offers casual lunch and dinner fare in
    a hip atmosphere. The menu changes regularly to highlight the
    freshest ingredients.

    Catering
    You have fun... we’ll handle the cooking. Black Goose Catering
    can handle events from snacks for bridge club to elegant corporate
    fundraisers.

    Location and Hours
    Seekonk, Massachusetts;
    Monday through Thursday 11am to 9pm, Friday and Saturday, 11am to
    midnight

  2. Select “Save” or “Save as” from the File menu to get the Save As dialog box (Figure 4-4). The first thing you need to do is create a new folder that will contain all of the files for the site (in other words, it’s the local root folder).

    Windows: Click the folder icon at the top to create the new folder. Image

    Mac: Click the “New Folder” button. Image

    Image

    Figure 4-4. Saving index.html in a new folder called “bistro”.

    Name the new folder bistro, and save the text file as index.html in it. Windows users, you will also need to choose “All Files” after “Save as type” to prevent Notepad from adding a “.txt” extension to your filename. The filename needs to end in .html to be recognized by the browser as a web document. See the sidebar, Naming Conventions, for more tips on naming files.

  3. Just for kicks, let’s take a look at index.html in a browser. Launch your favorite browser (I’m using Firefox) and choose “Open” or “Open File” from the File menu. Navigate to index.html and select the document to open it in the browser. You should see something like the page shown in Figure 4-5. We’ll talk about the results in the following section.
    Image

    Figure 4-5. The home page content in a browser.

Naming Conventions

It is important that you follow these rules and conventions when naming your files:

Use proper suffixes for your files. (X)HTML files must end with .html (some servers allow .htm). Web graphics must be labeled according to their file format: .gif , .png , or .jpg (.jpeg is also acceptable).

Never use character spaces within filenames. It is common to use an underline character or dash to visually separate words within filenames, such as robbins_bio.html or robbins-bio.html.

Avoid special characters  such as ?, %, #, /, :, ;, , etc. Limit filenames to letters, numbers, underscores, hyphens, and periods.

Filenames may be case-sensitive,  depending on your server configuration. Consistently using all lowercase letters in filenames, while not necessary, makes your filenames easier to manage.

Keep filenames short.  Short names keep the character count and file size of your (X)HTML file in check. If you really must give the file a long, multiword name, you can separate words with capital letters, such as ALongDocumentTitle.html, or with underscores, such as a_long_document_title.html, to improve readability.

Self-imposed conventions.  It is helpful to develop a consistent naming scheme for huge sites. For instance, always using lowercase with underscores between words. This takes some of the guesswork out of remembering what you named a file when you go to link to it later.

NOTE

The raw text file for this exercise is available online at www.learningwebdesign.com/materials/.

What Browsers Ignore

Some information in the source document will be ignored when it is viewed in a browser, including:

Line breaks (carriage returns). Line breaks are ignored. Text and elements will wrap continuously until a new block element, such as a heading (h1) or paragraph (p), or the line break (br) element is encountered in the flow of the document text.

Tabs and multiple spaces. When a browser encounters a tab or more than one consecutive blank character space, it displays a single space. So if the document contains:

   long,     long      ago

the browser displays:

   long, long ago

Unrecognized markup. A browser simply ignores any tag it doesn’t understand or that was incorrectly specified. Depending on the element and the browser, this can have varied results. The browser may display nothing at all, or it may display the contents of the tag as though it were normal text.

Text in comments. Browsers will not display text between the special <!-- and --> tags used to denote a comment. See the (X)HTML Comments sidebar later in this chapter.

Learning from step 1

Our content isn’t looking so good (Figure 4-5). The text is all run together—that’s not how it looked in the original document. There are a couple of things to be learned here. The first thing that is apparent is that the browser ignores line breaks in the source document. (The sidebar, What Browsers Ignore, lists other information in the source that are not displayed in the browser window.)

Second, we see that simply typing in some content and naming the document .html is not enough. While the browser can display the text from the file, we haven’t indicated the structure of the content. That’s where (X)HTML comes in. We’ll use markup to add structure: first to the (X)HTML document itself (coming up in Step 2), then to the page’s content (Step 3). Once the browser knows the structure of the content, it can display the page in a more meaningful way.

Step 2: Give the Document Structure

We’ve got our content saved in an .html document—now we’re ready to start marking it up.

Introducing... the HTML element

Back in Chapter 2, How the Web Works, you saw examples of (X)HTML elements with an opening tag (<p> for a paragraph, for example) and closing tag (</p>). Before we start adding tags to our document, let’s look at the structure of an HTML element and firm up some important terminology. A generic (X)HTML element is labeled in Figure 4-6.

An element consists of both the content and its markup.

Image

Figure 4-6. The parts of an (X)HTML element.

Elements are identified by tags in the text source. A tag consists of the element name (usually an abbreviation of a longer descriptive name) within angle brackets (< >). The browser knows that any text within brackets is hidden and not displayed in the browser window.

The element name appears in the opening tag (also called a start tag) and again in the closing (or end) tag preceded by a slash (/). The closing tag works something like an “off” switch for the element. Be careful not to use the similar backslash character in end tags (see the tip, Slash vs. Backslash).

The tags added around content are referred to as the markup. It is important to note that an element consists of both the content and its markup (the start and end tags). Not all elements have content, however. Some are empty by definition, such as the img element used to add an image to the page. We’ll talk about empty elements a little later in this chapter.

One last thing...capitalization. In this book, all elements are lowercase, and I recommend that you follow the same convention. Even though it isn’t strictly required for HTML documents, it is required for XHTML documents, so keeping all your markup lowercase brings you one step closer to being compatible with future web standards. See the sidebar, Do As I Say, Not As They Do, for details.

Basic document structure

Much like you and me, (X)HTML documents have a head and a body. The head of the document (also sometimes called the header) contains descriptive information about the document itself, such as its title, the style sheet it uses, scripts, and other types of “meta” information. The body contains the actual content that displays in the browser window.

Figure 4-7 shows the minimal skeleton of an (X)HTML document*. First, the entire document is contained within an html element. The html element is called the root element because it contains all the elements in the document, and it may not be contained within any other element. It is used for both HTML and XHTML documents.

The head comes next and contains the title element. According to the (X)HTML specifications, every document must contain a descriptive title. The body element comes after the head and contains everything that we want to show up in the browser window.  The document structure elements do not affect how the content looks in the browser (as you’ll see in a moment), but they are required to make the document valid (that is, to properly abide by the (X)HTML standards).

Image

Figure 4-7. The minimal structure of an (X)HTML document.

Do As I Say, Not As They Do

If you view the source of a few web pages, you are likely to see markup that looks different from the examples in this book. That’s because this book teaches contemporary authoring methods that are in keeping with the stricter requirements of XHTML. If you’re learning markup for the first time, you might as well learn to do it like the pros do it.

Lax markup practices are partly due to the fact that the rules of HTML are less stringent than XHTML. In addition, browsers have been forgiving of incorrect markup, so designers have gotten away with bad habits for years.

I recommend following these guidelines even for documents written with HTML.

Capitalization. In HTML, element names are not case sensitive, so you could write <IMG>, <Img>, or <img>. Most professionals, however, keep all elements and attributes in lowercase for consistency and to be in line with future (X)HTML standards.

Quotation marks. All attribute values should be in quotation marks, even though in HTML, certain values were okay without them.

Closing elements. In HTML, it is okay to omit the closing tag for certain block elements (such as a paragraph or list item), however, it is safer to close every element in the document.

Complex tables for layout. Old-school web design is well-known for its use of complex nested tables for page layout. Now that style sheets can handle the same thing, the table-based approach is obsolete.

Are you ready to add some structure to the Black Goose Bistro home page? Open the index.html document and move on to Exercise 4-2.

exercise 4-2 | Adding basic structure

  1. Open the newly created document, index.html, if it isn’t open already.
  2. Put the entire document in an HTML root element by adding an <html> start tag at the very beginning and an end </html> tag at the end of the text. This identifies the document as marked up in HTML (although XHTML uses html as well in order to be backwards compatible). Throughout the exercises in this chapter, we’ll be writing markup according to the rules of XHTML.
  3. Next, create the document head that contains the title for the page. Insert <head> and </head> tags before the content. Within the head element, add the title, “Black Goose Bistro”, surrounded by opening and closing <title> tags.
  4. Finally, define the body of the document by wrapping the content in <body> and </body> tags. When you are done, the source document should look like this (the markup is shown in color to make it stand out):

    <html>

    <head>
       <title>Black Goose Bistro</title>
    </head>

    <body>
    Black Goose Bistro

    The Restaurant
    The Black Goose Bistro offers casual lunch and dinner fare in a hip
    atmosphere. The menu changes regularly to highlight the freshest
    ingredients.

    Catering Services
    You have fun... we’ll do the cooking. Black Goose Catering can handle
    events from snacks for bridge club to elegant corporate fundraisers.

    Location and Hours
    Bakers Corner in Seekonk, Massachusetts;
    Monday through Thursday 11am to 9pm, Friday and Saturday, 11am to
    midnight
    </body>

    </html>

  5. Save the document in the bistro directory, so that it overwrites the old version. Open the file in the browser or hit “refresh” or “reload” if it is open already. Figure 4-8 shows how it should look now.
Image

Figure 4-8. The home page in a browser after the document structure elements have been defined.

NOTE

The correct terminology is to say that the title element is nested within the head element. We’ll talk about nesting more in later chapters.

Don’t Forget a Good Title

Not only is a title element required for every document, it is quite useful as well. The title is what is displayed in a user’s Bookmarks or Favorites list. Descriptive titles are also a key tool for improving accessibility, as they are the first thing a person hears when using a screen reader. Search engines rely heavily on document titles as well. For these reasons, it’s important to provide thoughtful and descriptive titles for all your documents and avoid vague titles, such as “Welcome” or “My Page.” You may also want to keep the length of your titles in check so they are able to display in the browser’s title area.

Not much has changed after structuring the document, except that the browser now displays the title of the document in the top bar. If someone were to bookmark this page, that title would be added to their Bookmarks or Favorites list as well (see the sidebar, Don’t Forget a Good Title). But the content still runs together because we haven’t given the browser any indication of how it is broken up. We’ll take care of that next.

Step 3: Identify Text Elements

With a little markup experience under your belt, it should be a no-brainer to add the markup that identifies headings and subheads (h1 and h2), paragraphs (p), and emphasized text (em) to our content, as we’ll do in Exercise 4-3. However, before we begin, I want to take a moment to talk about what we’re doing and not doing when marking up content with (X)HTML.

(X)HTML Comments

You can leave notes in the source document for yourself and others by marking them up as comments. Anything you put between comment tags (<!-- -->) will not display in the browser and will not have any effect on the rest of the source.

<!-- This is a comment -->
<!-- This is a
  multiple-line comment
  that ends here. -->

Comments are useful for labeling and organizing long (X)HTML documents, particularly when they are shared by a team of developers. In this example, comments are used to point out the section of the source that contains the navigation.

<!-- start global nav -->
<ul>
 ...
</ul>
<!-- end global nav -->

Bear in mind that although the browser will not display comments in the web page, readers can see them if they “view source,” so be sure that the comments you leave are appropriate for everyone.

Introducing...semantic markup

The purpose of (X)HTML is to provide meaning and structure to the content. It is not intended to provide instructions for how the content should look (its presentation).

Your job when marking up content is to choose the (X)HTML element that provides the most meaningful description of the content at hand. In the biz, we call this semantic markup. For example, the first heading level on the page should be marked up as an h1 because it is the most important heading on the page. Don’t worry about what that looks like in the browser...you can easily change that with a style sheet. The important thing is that you choose elements based on what makes the most sense for the content.

In addition to adding meaning to content, the markup gives the document structure. The way elements follow each other or nest within one another creates relationships between the elements. This document structure is the foundation upon which we can add presentation instructions with style sheets, and behaviors with JavaScript. We’ll talk about document structure more in Part III, when we discuss Cascading Style Sheets.

Although HTML was intended to be used strictly for meaning and structure since its creation, that mission was somewhat thwarted in the early years of the Web. With no style sheet system in place, HTML was extended to give authors ways to change the appearance of fonts, colors, and alignment. Those presentational extras are still out there, so you may run across them when you “view source.” In this book, however, we’ll focus on using HTML and XHTML the right way, in keeping with the new standards-based approach of contemporary web design.

Okay, enough lecturing. It’s time to get to work on that content in Exercise 4-3.

exercise 4-3 | Defining text elements

  1. Open the document index.html in your text editor, if it isn’t open already.
  2. The first line of text, “Black Goose Bistro,” is the main heading for the page, so we’ll mark it up as a Heading Level 1 (h1) element. Put the opening tag, <h1>, at the beginning of the line and the closing tag, </h1>, after it, like this.

    <h1>Black Goose Bistro</h1>

  3. Our page also has three subheads. Mark them up as Heading Level 2 (h2) elements in a similar manner. I’ll do the first one here; you do the same for “Catering” and “Location and Hours”.

    <h2>The Restaurant</h2>

  4. Each h2 element is followed by a brief paragraph of text, so let’s mark those up as paragraph (p) elements in a similar manner. Here’s the first one; you do the rest.

    <p>The Black Goose Bistro offers casual lunch and
    dinner fare in a hip atmosphere. The menu changes
    regularly to highlight the freshest ingredients.
    </p>

  5. Finally, in the Catering section, I want to emphasize that visitors should just leave the cooking to us. To make text emphasized, mark it up in an emphasis element (em) element, as shown here.

    <p>You have fun... <em>we’ll handle the cooking.
    </em> Black Goose Catering can handle events
    from snacks for bridge club to elegant corporate
    fundraisers.</p>

  6. Now that we’ve marked up the document, let’s save it as we did before, and open (or refresh) the page in the browser. You should see a page that looks much like the one in Figure 4-9. If it doesn’t, check your markup to be sure that you aren’t missing any angle brackets or a slash in a closing tag.
Image

Figure 4-9. The home page after the content has been marked up in (X)HTML elements.

Now we’re getting somewhere. With the elements properly identified, the browser can now display the text in a more meaningful manner. There are a few significant things to note about what’s happening in Figure 4-9.

Block and inline elements

While it may seem like stating the obvious, it is worth pointing out that the heading and paragraph elements start on new lines and do not run together as they did before. That is because they are examples of block-level elements. Browsers treat block-level elements as though they are in little rectangular boxes, stacked up in the page. Each block-level element begins on a new line, and some space is also usually added above and below the entire element by default. In Figure 4-10, the edges of the block elements are outlined in red.

By contrast, look at the text we marked up as emphasized (em). It does not start a new line, but rather stays in the flow of the paragraph. That is because the em element is an inline element. Inline elements do not start new lines; they just go with the flow. In Figure 4-10, the inline em element is outlined in light blue.

Image

Figure 4-10. The outlines show the structure of the elements in the home page.

The distinction between block-level and inline elements is important. In (X)HTML markup, whether an element is block-level or inline restricts what other elements it may contain. For example, you can’t put a block-level element within an inline element (such as a paragraph within a link). Block-level and inline elements also behave differently when it comes to applying Cascading Style Sheets.

Browsers have built-in style sheets that describe the default rendering of (X)HTML elements.

Default styles

The other thing that you will notice about the marked-up page in Figures 4-9 and 4-10 is that the browser makes an attempt to give the page some visual hierarchy by making the first-level heading the biggest and boldest thing on the page, with the second-level headings slightly smaller, and so on.

How does the browser determine what an h1 should look like? It uses a style sheet! All browsers have their own built-in style sheets that describe the default rendering of (X)HTML elements. The default rendering is similar from browser to browser (for example, h1s are always big and bold), but there are some variations (block quotes may or may not be indented). The appearance is also affected by the user’s preferences, discussed in Chapter 3, The Nature of Web Design.

If you think the h1 is too big and clunky as the browser renders it, just change it with a style sheet rule. Resist the urge to mark up the heading with another  element just to get it to look better (for example, using an h3 instead of an h1 so it isn’t as large). In the days before ubiquitous style sheet support, elements were abused in just that way. Now that there are style sheets for controlling the design, you should always choose elements based on how accurately they describe the content, and don’t worry about the browser’s default rendering.

We’ll fix the presentation of the page with style sheets in a moment, but first, let’s add an image to the page.

Step 4: Add an Image

What fun is a web page with no image? In Exercise 4-4, we’ll add an image to the page using the img element. Images will be discussed in more detail in Chapter 7, Adding Images, but for now, it gives us an opportunity to introduce two more basic markup concepts: empty elements and attributes.

Empty elements

So far, all of the elements we’ve used in the Black Goose Bistro home page have followed the syntax shown in Figure 4-1: a bit of text content surrounded by start and end tags.

A handful of  elements, however, do not have text content because they are used to provide a simple directive. These elements are said to be empty. The image element (img) is an example of such an element; it tells the browser to get an graphic file from the server and insert it into the flow of the text at that spot in the document. Other empty elements include the line break (br), horizontal rule (hr), and elements that provide information about a document but don’t affect its displayed content, such as the meta element.

The syntax for empty elements is slightly different for HTML and XHTML. In HTML, empty elements don’t use closing tags—they are indicated by a single tag (<img>, <br>, or <hr>, for example) inserted into the text, as shown in this example that uses the br element to insert a line break.

<p>1005 Gravenstein Highway North <br>Sebastopol, CA 95472</p>

In XHTML, all elements, including empty elements, must be closed (or terminated, to use the proper term). Empty elements are terminated by adding a trailing slash preceded by a space before the closing bracket, like so: <img />, <br />, and <hr />. Here is that example again, this time using XHTML syntax.

<p>1005 Gravenstein Highway North <br />Sebastopol, CA 95472</p>

Attributes

Obviously, an <img /> tag is not very useful by itself... there’s no way to know  which image to use. That’s where attributes come in. Attributes are instructions that clarify or modify an element. For the img element, the src (short for “source”) attribute is required, and provides the location of the image file via its URL.

The syntax for attributes is as follows:

<element attribute-name="value">Content</element>

or for empty elements:

<element attribute-name="value" />

For another way to look at it, the attribute structure of an img element is labeled in Figure 4-11.

Image

Figure 4-11. An element with attributes.

Here’s what you need to know about attributes:

• Attributes go after the element name in the opening tag only, never in the end tag.

• There may be several attributes applied to an element, separated by spaces in the opening tag. Their order is not important.

• Attributes take values, which follow an equals sign (=).

• A value might be a number, a word, a string of text, a URL, or a measurement depending on the purpose of the attribute.     

• Always put values within quotation marks. Although quotation marks aren’t required around all values in HTML, they are required in XHTML. You might as well do it the more future-compatible way from the start. Either single or double quotation marks are acceptable as long as they are used consistently, however, double quotation marks are the convention.

• Some attributes are required, such as the src and alt attributes in the img element.

• The attribute names available for each element are defined in the (X)HTML specifications; in other words, you can’t make up an attribute for an element.

Now you should be more than ready to try your hand at adding the img element with its attributes to the Black Goose Bistro page in the next exercise.

exercise 4-4 | Adding an image

  1. If you’re working along, the first thing you’ll need to do is get a copy of the image file on your hard drive so you can see it in place when you open the file locally. The image file is provided in the materials for this chapter. You can also get the image file by saving it right from the sample web page online at www.learningwebdesign.com/chapter4/bistro. Right-click (or Ctrl-click on a Mac) on the goose image and select “Save to disk” (or similar) from the pop-up menu as shown in Figure 4-12. Be sure to save it in the bistro folder with index.html.
    Image

    Figure 4-12. Saving an image file from a page on the Web.

  2. Once you’ve got the image, insert it at the beginning of the first-level heading  by typing in the img element and its attributes as shown here:

    <h1><img src="blackgoose.gif" alt="Black Goose
    logo" />
    Black Goose Bistro</h1>

    The src attribute provides the name of the image file that should be inserted, and the alt attribute provides text that should be displayed if the image is not available. Both of these attributes are required in every img element.

  3. Now save index.html and open or refresh it in the browser window. The page should look like the one shown in Figure 4-13. If it doesn’t, check to make sure that the image file, blackgoose.gif, is in the same directory as index.html. If it is, then check to make sure that you aren’t missing any characters, such as a closing quote or bracket, in the img element markup.
    Image

    Figure 4-13. The Black Goose Bistro home page with the Black Goose logo inline image.

Step 5: Change the Look with a Style Sheet

Depending on the content and purpose of your web site, you may decide that the browser’s default rendering of your document is perfectly adequate. However, I think I’d like to pretty up the Black Goose Bistro home page a bit to make a good first impression on potential patrons. “Prettying up” is just my way of saying that I’d like to change its presentation, which is the job of Cascading Style Sheets (CSS).

In Exercise 4-5, we’ll change the appearance of the text elements and the page background using some simple style sheet rules. Don’t worry about understanding them all right now—we’ll get into CSS in more detail in Part III. But I want to at least give you a taste of what it means to add a “layer” of presentation onto the structure we’ve created with our XHTML markup.

exercise 4-5 | Adding a style sheet

  1. Open index.html if it isn’t open already.
  2. We’re going to use the style element to apply an embedded style sheet to the page. (This is just one of the ways to add a style sheet; the others are covered in Chapter 11, Style Sheet Orientation.)

    The style element is placed inside the head of the document. It uses the required type attribute to tell the browser the type of information in the element (text/css is currently the only option). Start by adding the style element to the document as shown here:

    Image

    Figure 4-14. The Black Goose Bistro page after CSS style rules have been applied.

    <head>
       <title>Black Goose Bistro</title>
       <style type="text/css">
     
      </style>
    </head>

  3. Now, type the following style rules within the style element just as you see them here. Don’t worry if you don’t know exactly what is going on... you’ll learn all about style rules in Part III.

      <style type="text/css">
       body {
          background-color: #C2A7F2;
         font-family: sans-serif;
         }
       h1 {
          color: #2A1959;
         border-bottom: 2px solid #2A1959;
         }
       h2 {
          color: #474B94;
         font-size: 1.2em;
         }
       h2, p {
          margin-left: 120px;
         }
      </style>

  4. Now it’s time to save the file and take a look at it in the browser. It should look like the page in Figure 4-14. If it doesn’t, go over the style sheet code to make sure you didn’t miss a semi-colon or a curly bracket.

We’re finished with the Black Goose Bistro page. Not only have you written your first XHTML document, complete with a style sheet, but you’ve learned about elements, attributes, empty elements, block-level and inline elements, the basic structure of an (X)HTML document, and the correct use of markup along the way.

When Good Pages Go Bad

The previous demonstration went very smoothly, but it’s easy for small things to go wrong when typing out (X)HTML markup by hand. Unfortunately, one missed character can break a whole page. I’m going to break my page on purpose so we can see what happens.

What if I had forgotten to type the slash (/) in the closing emphasis tag (</em>)? With just one character out of place (Figure 4-15), the remainder of the document displays in emphasized (italic) text. That’s because without that slash, there’s nothing telling the browser to turn “off” the emphasized formatting, so it just keeps going.

Image

Figure 4-15. When a slash is omitted, the browser doesn’t know when the element ends, as is the case in this example.

NOTE

Omitting the slash in the closing tag (in effect, omitting the closing tag itself) for certain block elements, such as headings or paragraphs, may not be so dramatic. Browsers interpret the start of a new block element to mean that the previous block element is finished.

I’ve fixed the slash, but this time, let’s see what would have happened if I had accidentally omitted a bracket from the end of the first <h2> tag (Figure 4-16).

Image

Figure 4-16. A missing end bracket makes all the following content part of the tag, and therefore it doesn’t display.

Having Problems?

The following are some typical problems that crop up when creating web pages and viewing them in a browser:

I’ve changed my document, but when I reload the page in my browser, it looks exactly the same.

It could be you didn’t save your  document before reloading, or you may have saved it in a different directory.

Half my page disappeared.

This could happen if you are missing a closing bracket (>) or a quotation mark within a tag. This is a common error when writing (X)HTML by hand.

I put in a graphic using the img element, but all that shows up is a broken graphic icon.

The broken graphic could mean a couple of things. First, it might mean that the browser is not finding the graphic. Make sure that the URL to the image file is correct. (We’ll discuss URLs further in Chapter 6, Adding Links.) Make sure that the image file is actually in the directory you’ve specified. If the file is there, make sure it is in one of the formats that web browsers can display (GIF,  JPEG, or PNG) and that it is named with the proper suffix (.gif, .jpeg or .jpg, or .png, respectively).

See how the headline is missing? That’s because without the closing tag bracket, the browser assumes that all the following text—all the way up to the next closing bracket (>) it finds—is part of that <h2> tag. Browsers don’t display any text within a tag, so my heading disappeared. The browser just ignored the foreign-looking element name and moved on to the next element.

Making mistakes in your first (X)HTML documents and fixing them is a great way to learn. If you write your first pages perfectly, I’d recommend fiddling with the code as I have here to see how the browser reacts to various changes. This can be extremely useful in troubleshooting pages later. I’ve listed some common problems in the sidebar, Having Problems? Note that these problems are not specific to beginners. Little stuff like this goes wrong all the time, even for the pros.

Test Yourself

Now is a good time to make sure you’re understanding the basics of markup. Use what you’ve learned in this chapter to answer the following questions. Answers are in Appendix A.

1. What is the difference between a tag and an element?

2. Write out the minimal structure of an (X)HTML document.

3. Mark whether each of these file names is an acceptable name for a web document by circling “Yes” or “No.” If it is not acceptable, provide the reason why.

a. Sunflower.html

Yes

No ___________________

b. index.doc

Yes

No ___________________

c. cooking home page.html

Yes

No ___________________

d. Song_Lyrics.html

Yes

No ___________________

e. games/rubix.html

Yes

No ___________________

f. %whatever.html

Yes

No ___________________

4. All of the following markup examples are incorrect. Describe what is wrong with each one, then write it correctly.

a. <img "birthday.jpg">

b. <i>Congratulations!<i>

c. <a href="file.html">linked text</a href="file.html">

d. <p>This is a new paragraph<\p>

5. How would you mark up this comment in an (X)HTML document so that it doesn’t display in the browser window?

    product list begins here

(X)HTML Review: Document Structure Elements

This chapter introduced the elements that establish the structure of the document. The remaining elements introduced in the exercises will be treated in more depth in the following chapters.

Element

Description

html

The root element that identifies the document as (X)HTML

head

Identifies the head of the document

title

Gives the page a title

body

Identifies the body of the document that holds the content

Get Learning Web Design, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.