Beginning HTML, XHTML, CSS, and JavaScript

Beginning HTML, XHTML, CSS, and JavaScript

4.3 3
by Jon Duckett
     
 

View All Available Formats & Editions

An indispensable introductory guide to creating web pages using the most up-to-date standards

This beginner guide shows you how to use XHTML, CSS, and JavaScript to create compelling Web sites. While learning these technologies, you will discover coding practices such as writing code that works on multiple browsers including mobile devices, how to use AJAX

See more details below

Overview

An indispensable introductory guide to creating web pages using the most up-to-date standards

This beginner guide shows you how to use XHTML, CSS, and JavaScript to create compelling Web sites. While learning these technologies, you will discover coding practices such as writing code that works on multiple browsers including mobile devices, how to use AJAX frameworks to add interactivity to your pages, and how to ensure your pages meet accessible requirements.

Packed with real-world examples, the book not only teaches you how to write Web sites using XHTML, CSS and JavaScript, but it also teaches you design principles that help you create attractive web sites and practical advice on how to make web pages more usable. In addition, special checklists and appendices review key topics and provide helpful references that re-enforce the basics you've learned.

  • Serves as an ideal beginners guide to writing web pages using XHTML
  • Explains how to use CSS to make pages more appealing and add interactivity to pages using JavaScript and AJAX frameworks
  • Share advice on design principles and how to make pages more attractive and offers practical help with usability and accessibility
  • Features checklists and appendices that review key topics

This introductory guide is essential reading for getting started with using XHTML, CSS and JavaScript to create exciting and compelling Web sites.

Note: CD-ROM/DVD and other supplementary materials are not included as part of eBook file.

Read More

Product Details

ISBN-13:
9781118057322
Publisher:
Wiley
Publication date:
02/17/2011
Sold by:
Barnes & Noble
Format:
NOOK Book
Pages:
864
Sales rank:
582,486
File size:
16 MB
Note:
This product may take a few minutes to download.

Read an Excerpt

Beginning HTML, XHTML, CSS, and JavaScript


By Jon Duckett

John Wiley & Sons

Copyright © 2010 John Wiley & Sons, Ltd
All right reserved.

ISBN: 978-0-470-54070-1


Chapter One

Structuring Documents for the Web

In this chapter, you learn the key concept of creating any web page: how to give it structure. You need to add structure to a document so that web browsers can present the page to people who visit your site in a way they will understand. For example, imagine a news article that contains a headline (or title) and several paragraphs of text; if you wanted to put this article on the Web, you would need to add structure to the words in the document so that the browser knows which words are the headline, and where each paragraph starts and ends. To give a document structure, you'll need to learn how to create web pages using HTML. Or, to be a little more precise, this book focuses on a type of HTML known as XHTML.

In this chapter you will:

Create several example web pages in XHTML.

See how a web page describes its structure to a web browser.

Discover the meaning of some key terms used by web designers, such as elements, attributes, tags, and markup.

By the end of the chapter, you will have learned the basic building blocks needed to build a web page, and will have put thisinto practice with several examples.

A Web of Structured Documents

Before we create our first web page, let's just take a moment to look at the printed information we see every day, and how it compares to what we see on the Web. Every day, you come across all kinds of printed documents - newspapers, train timetables, insurance forms. You can think of the Web as being a sea of documents that all link together, and bear a strong similarity to the printed documents that you meet in everyday life.

Every morning I used to read a newspaper. A newspaper is made up of several stories or articles (and probably a fair smattering of advertisements, too). Each story has a headline and then some paragraphs, perhaps a subheading, and then some more paragraphs; it may also include a picture or two.

I don't buy a daily paper anymore, as I tend to look at news online, but the structure of articles on news web sites is very similar to the structure of articles in newspapers. Each article is made up of headings, paragraphs of text, and some pictures (sometimes the pictures might be replaced by a video). The parallel is quite clear; the only real difference is that in a newspaper you may have several stories on a single page, whereas on the Web each story tends to get its own page. The news web sites also often use homepages that display the headline and a brief summary of the stories.

Consider another example: Say I'm catching a train to see a friend, so I check the schedule or timetable to see what time the trains go that way. The main part of the schedule is a table telling me what times trains arrive and when they depart from different stations. You can probably think of several types of documents that use tables. From the listings in the financial supplement of your paper to the TV schedule, you come across tables of information every day - and often when this information is put on the Web, these tables are recreated.

Another common type of printed document is a form. For example, on my desk at the moment, I have a form (which I really must mail) from an insurance company. This form contains fields for me to write my name, address, and the amount of coverage I want, along with checkboxes to indicate the number of rooms in the house and what type of lock I have on my front door. There are lots of forms on the Web, from simple search boxes that ask what you are looking for to the registration forms you are required to go through before you can place an online order for books or CDs.

As you can see, there are many parallels between the structure of printed documents you come across every day and pages you see on the Web. When it comes to writing web pages, it is the XHTML code you start learning in this chapter that tells the web browser how the information you want to display is structured - what text to put in a heading, or in a paragraph, or in a table, and so on - so that the browser can present it properly to the user.

Introducing HTML and XHTML

Even if you have never seen any HTML (Hypertext Markup Language) code, you may be familiar with the fact that it is used to create most web pages. There have been several versions of HTML since the Web began, and the development of the language is overseen by an organization called the W3C (World Wide Web Consortium).

The last major version of HTML was HTML 4.01 in December 1999. In January 2000, some stricter rules were added to HTML 4.01, creating what is known as XHTML (Extensible Hypertext Markup Language), and it is this version of the language that we will be focusing on throughout this book. Generally, you will see the term XHTML used in the rest of this book. Unless otherwise mentioned, the same rules apply to HTML.

I use this stricter XHTML syntax because it can be processed by more applications than HTML 4.01. If you are interested, there is a summary of the differences between HTML 4 and XHTML 1.0 in Chapter 14. There is also a brief look forward to what will be the next version of HTML, HTML 5, in another section of Chapter 14.

As its name suggests, XHTML is a markup language, which may sound complicated, until you realize that you come across markup every day. When creating a document in a word processor, you can add styles to the text to explain the document's structure. For example, you can distinguish headings from the main body of the text using a heading style (usually with a larger font). You can use the Return (or Enter) key to start a new paragraph. You can insert tables into your document to hold data, or create bulleted lists for a series of related points, and so on. While this does affect the presentation of the document, the key purpose of this kind of markup is to provide a structure that makes the document easier to understand.

When marking up documents for the Web, you are performing a very similar process, except you do it by adding things called tags to the text. With XHTML, the key thing to remember is that you are adding the tags to indicate the structure of the document (not how you want it to be presented); for example, which part of the document is a heading, which parts are paragraphs, what belongs in a table, and so on. Browsers such as Internet Explorer, Firefox, and Safari all use this markup to help present the text in a familiar fashion, similar to that of a word processor - main headings are bigger than the text in paragraphs, there is space above and below each paragraph, lists of bullet points have a circle in front of them. But the XHTML specification does not specify which font should be used or what size that font should be.

While earlier versions of HTML allowed you to control the presentation of a document - such as which typefaces and colors a document should use - XHTML markup is not supposed to be used to style the document; that is the job of CSS, which you meet in Chapter 7.

Let's have a look at a very simple web page. As I mentioned in the Introduction, you don't need any special programs to write web pages - you can simply use a text editor such as Notepad on Windows or TextEdit on a Mac, and save your files with the .html file extension. You can download this example along with all the code for this book from the Wrox web site at www.wrox.com; the example is in the Chapter 1 folder (along with other examples from this chapter) and is called ch01_eg01.html.

<html> <head> <title>Popular Websites: Google</title> </head> <body> <H1>About Google</H1>

Google is best known for its search engine, although Google now offers a number of other services.

Google's mission is to organize the world's information and make it universally accessible and useful.

Its founders Larry Page and Sergey Brin started Google at Stanford University.

</body> </html>

This may look a bit confusing at first, but it will all make sense soon. As you can see, there are several sets of angle brackets with words or letters between them, such as <html>, <head>, </title>, and </body>. These angle brackets and the words inside them are known as tags, and these are the markup I have been talking about. Figure 1-1 illustrates what this page would look like in a web browser.

As you can see, this document contains the heading "About Google" and a paragraph of text to introduce the company. Note also that it says "Popular Websites: Google" in the very top-left of the browser window; this is known as the title of the page (to the right it says Mozilla Firefox, which is the browser this page was opened in).

To understand the markup in this first example, you need to look at what is written between the angle brackets and compare that with what you see in the figure, which is what you will do next.

Tags and Elements

If you look at the first and last lines of the code for the last example, you will see pairs of angle brackets containing the letters "html" Starting on the first line, the first angled bracket looks like a less-than sign, then there are the letters "html," followed by a second angled bracket, which looks like a greater-than sign. The two brackets and all of the characters between them are known as a tag.

In this example, there are lots of tags and they are all in pairs; there are opening tags and closing tags. The closing tag is always slightly different from the opening tag in that it has a forward slash after the first angled bracket: </html>.

A pair of tags and the content these include are known as an element. In Figure 1-2, you can see the heading for the page of the last example.

The opening tag says "This is the beginning of a heading" and the closing tag says "This is the end of a heading." Like most tags in XHTML, the text inside the angled brackets explains the purpose of the tag - here h1 indicates that it is a level 1 heading (or top-level heading). As you will see shortly, there are also tags for subheadings (<H2>, <H3>, <H4>, <H5>, and <H6>). If we had not put tags around the words "About Google," it would just be another bit of text; it would not be clear that these words formed the heading.

Now look at the three paragraphs of text about the company; each one is placed between an opening <p> tag and a closing </p> tag. And, you guessed it, the p stands for paragraph.

As you can see, the tags throughout this example actually describe what you will find between them, creating the structure of the document. The text between the <H1> and </H1> tags is a heading, and between the opening <p> and closing </p> tags make up paragraphs. Indeed, the whole document is contained between opening <html> and closing </html> tags.

You will often find that terms from a family tree are used to describe the relationships between elements. For example, an element that contains another element is known as the parent, while the element that's between the parent element's opening and closing tags is called a child of that element. So, the <title> element is a child of the <head> element, the <head> element is the parent of the <title> element, and so on. Furthermore, the <title> element can be thought of as a grandchild of the <html> element.

It is worth noting that the tags in this example are all in lowercase characters; you will sometimes see web pages written in HTML where tags are uppercase (or a mix of uppercase and lowercase letters). When XHTML was introduced, with its stricter rules, it stated that all tags were written in lowercase.

Separating Heads from Bodies

Whenever you write a web page in XHTML, the whole of the page is contained between the opening <html> and closing </html> tags, just as it was in the last example. Inside the <html> element, there are two main parts to the page:

The <head> element: Often referred to as the head of the page, this contains information about the page (this is not the main content of the page). For example, it might contain a title and a description of the page, or instructions on where a browser can find CSS rules that explain how the document should look. It consists of the opening <head> tag, the closing </head> tag, and everything in between.

The <body> element: Often referred to as the body of the page, this contains the information you actually see in the main browser window. It consists of the opening <body> tag, closing </body> tag, and everything in between.

Together, the <html>, <head>, and <body> elements make up the skeleton of an XHTML document - they are the foundation upon which every web page is built.

Inside the <head> element of the first example page, you can see a <title> element:

<head> <title>Popular Websites: Google</title> </head>

Between the opening <title> tag and the closing </title> tag are the words Popular Websites: Google, which is the title of this web page. When I introduced Figure 1-1, which showed the screenshot of this page, I called your attention to the words right at the top of the browser window. This is where browsers such as Internet Explorer, Firefox, and Safari display the title of a document; it is also the name they use when you save a page in your favorites, and it helps search engines understand what your page is about.

The real content of your page is held in the <body> element, which is what you want users to read, and this is shown in the main browser window.

You may have noticed that the tags in this example appear in a symmetrical order. If you want to have one element inside another, then both the element's opening and closing tags must be inside the containing element. For example, the following is allowed:

<p> This paragraph contains some <em>emphasized text.</em></p>

whereas the following is wrong because the closing </em> tag is not inside the paragraph element:

<p> This paragraph contains some <em>emphasized text. </p></em>

In other words, if an element is to contain another element, it must wholly contain that element. This is referred to as nesting your elements correctly.

Attributes Tell Us About Elements

What really differentiates web documents from standard documents are the links (or hyperlinks) that take you from one web page to another. Let's take a look at a link by adding one to the example you just looked at. Links are created using an <a> element (the a stands for anchor).

Here we will add a link from this page to Google in a new paragraph at the end of the document. There is just one new line in this example (code sample ch01_eg02.html) and that line is highlighted:

<html>
<head>
<title>Popular Websites: Google</title>
</head>
<body>
<H1>About Google</H1>

Google is best known for its search engine, although Google now offers a number of other services.

Google's mission is to organize the world's information and make it universally accessible and useful.

Its founders Larry Page and Sergey Brin started Google at Stanford University.

<a href="http://www.Google.com/">Click here to visit Google's Web site.</a>

</body>
</html>

Inside this new paragraph is the <a> element that creates the link. Between the opening <a> tag and the closing </a> tag is the text that you can click on, which says "Click here to visit Google's Web site." Figure 1-3 shows you what this page looks like in a browser.

If you look closely at the opening tag of the link, it carries something called an attribute. In this case, it's the href attribute; this is followed by an equal sign, and then a pair of quotation marks which contain the URL for Google's web site. In this case, the href attribute is telling you where the link should take you. You'll look at links in greater detail in the next chapter, but for the moment this illustrates the purpose of attributes.

(Continues...)



Excerpted from Beginning HTML, XHTML, CSS, and JavaScript by Jon Duckett Copyright © 2010 by John Wiley & Sons, Ltd. Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Read More

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >