You are on page 1of 87

2.

Web Pages
Contents
• Webpage
• Headings
• Linking
• Images
• Tables
• Forms
• Internal Linking
• Meta Elements
HTML
• Tables, which are particularly useful for structuring information from
databases (i.e., software that stores structured sets of data)

• Forms for collecting information from web-page visitors

• Internal linking for easier page navigation

• Meta elements for specifying information about a document


Editing HTML Web Page
• We’ll create HTML5 documents by typing HTML5 markup text in a
text editor (such as Notepad, TextEdit, vi, emacs) and saving it with
the .html or .htm filename extension.
Editing HTML Web Page
• DOCTYPE
• The document type declaration (DOCTYPE) in line 1 is required in HTML5
documents so that browsers render the page in standards mode, according to
the HTML and CSS specifications.
• Comments
• Lines 3–4 are HTML5 comments. You’ll insert comments in your HTML5
markup to
• improve readability and describe the content of a document. The browser
ignores comments when your document is rendered. HTML5 comments start
with <!-- and end with
• We include in our examples comments that specify the figure number and file
name and state the example’s purpose.
HTML Head and Body Elements
• HTML5 markup contains text (and images, graphics, animations, audios and
videos) that represents the content of a document and elements that specify a
document’s structure and meaning.
• Some important elements are the html element (which starts in line 5 and ends in
line 14), the head element (lines 6–9) and the body element (lines 11–13).
• The head section contains information about the HTML5 document, such as
the character set (UTF-8, the most popular character-encoding scheme for the
web) that the page uses (line 7)—which helps the browser determine how to
render the content—and the title (line 8).
• The head section also can contain special document-formatting instructions
called CSS3 style sheets and client-side programs called scripts for creating
dynamic web pages. The body section contains the page’s content, which the
browser displays when the user visits the web page.
Start Tags and End Tags
• HTML5 documents delimit most elements with a start tag and an end
tag. A start tag consists of the element name in angle brackets (for
example, <html> in line 5).
• An end tag consists of the element name preceded by a forward slash
(/) in angle brackets (for example, </html> in line 14).
Title Element
• Line 8 specifies a title element. This is called a nested element,
because it’s enclosed in the head element’s start and end tags. The
head element is also a nested element, because it’s enclosed in the
html element’s start and end tags. The title element describes the
web page.
• Titles usually appear in the title bar at the top of the browser window,
in the browser tab on which the page is displayed, and also as the text
identifying a page when users add the page to their list of Favorites or
Bookmarks, enabling them to return to their favorite sites.
• Search engines use the title for indexing purposes and when
displaying results.
Paragraph Element (<p>...</p>)
• Line 11 begins the document’s body element, which specifies the
document’s content, which may include text, images, audios and
videos.
• Some elements, such as the paragraph element denoted with <p>
and </p> in line 12, help define the structure of a document. All the
text placed between the <p> and </p> tags forms one paragraph.
• When a browser renders a paragraph, it places extra space above and
below the paragraph text. The key line in the program is line 12,
which tells the browser to display Welcome to HTML5!.
• End Tags
• This document ends with two end tags (lines 13–14), which close the
body and html elements, respectively. The </html> tag informs the
browser that the HTML5 markup is complete.

• Opening an HTML5 File in Your Default Web Browser


• To open an HTML5 example from this chapter, open the folder where
you saved the book’s examples, browse to the folder and click and
open it in your default web browser.
Validator
• You must use proper HTML5 syntax to ensure that browsers process your
documents properly.
• The World Wide Web Consortium (W3C) provides a validation service (at
validator.w3.org) for checking a document’s syntax. Documents can be
validated by providing the URL of an online web page
• uploading a file to the validator
• pasting code directly into a text area provided on the validator
• Pasting code directly into a text area provided on the validator site.

• http://validator.w3.org/#validate-by-upload
• If it contains syntax errors, the validation service displays error messages describing
the errors. Since the HTML5 validator is still considered experimental, you’ll receive a
warning each time you validate an HTML5 document.
Headings
• Some text in an HTML5 document may be more important than other
text. HTML5 provides six heading elements (h1 through h6) for
specifying the relative importance of information (Fig. 2.2).
• Heading element h1 (line 12) is considered the most significant one
and is typically rendered in a larger font than the other five (lines 13–
17).
• Each successive heading element (h2, h3, etc.) is typically rendered in
a progressively smaller font.
• One of the most important HTML5 features is the hyperlink, which
references (or links to) other resources, such as HTML5 documents
and images.
• When a user clicks a hyperlink, the browser tries to execute an action
associated with it (for example, navigate to a URL or open an e-mail
client). Any displayed element can act as a hyperlink.
• Web browsers typically underline text hyperlinks and color their text
blue by default so that users can distinguish hyperlinks from plain
text.
• Line 13 introduces the strong element, which indicates that its
content has high importance.
• Browsers typically render such text in a bold font. Links are created
using the a (anchor) element.
• Line 16 defines a hyperlink to the URL assigned to attribute href
(hypertext reference), which specifies a resource’s location, such as a
web page or location within a web page a file, an e-mail address
• The anchor element in line 16 links the text Facebook to a web page
located at http:// www.facebook.com. The browser changes the color of
any text link once you’ve clicked the link (in this case, the links are purple
rather than blue).
• When a URL does not indicate a specific document on the website, the web
server returns a default web page.
• This page is often called index.html, but most web servers can be
configured to use any file as the default web page for the site.
• If the web server cannot locate a requested document,
• it returns an error indication to the web browser (known as a 404 error),
and the browser displays a web page containing an error message.
Hyperlinking to an E-Mail Address
• Anchors can link to e-mail addresses using a mailto: URL. When the
user clicks this type of anchored link, most browsers launch the user’s
default e-mail program (for example, Mozilla Thunderbird, Microsoft
Outlook or Apple Mail) to enable the user to write an e-mail message
to the linked address.
• Lines 13–14 contain an e-mail link. The form of an e-mail anchor is <a
href = "mailto:emailAddress">…</a>.
• In this case, we link to the e-mail address deitel@deitel.com. Line 13
includes the e-mail address as it will appear in the message displayed
on the browser.
Images
• Web pages may also contain images, animations, graphics, audios
and even videos. The most popular image formats used by web
developers today are PNG and JPEG.
Images
• Lines 13–14 use an img element to include an image in the document.
The image file’s location is specified with the src (source) attribute.
This image is located in the same directory as the HTML5 document,
so only the image’s file name is required.
• This is known as a relative path—the image is stored relative to the
location of the document. Optional attributes width and height
specify the image’s dimensions. You can scale an image by increasing
or decreasing the values of the image width and height attributes.
• If these attributes are omitted, the browser uses the image’s actual
width and height. Images are measured in pixels (“picture elements”),
which represent dots of color on the screen
alt attribute
• A browser may not be able to render an image for several reasons. It
may not support images—as is the case with text-only browsers—or
the client may have disabled image viewing to reduce download time.
Every img element in an HTML5 document must have an alt attribute.
If a browser cannot render an image, the browser displays the alt
attribute’s value.
• Previous example shows the Internet Explorer browser rendering a
red X symbol and displaying the alt attribute’s value, signifying that
the image (jhtp.png) cannot be found.
Using Images as Hyperlinks
• By using images as hyperlinks, you can create graphical web pages
that link to other resources.
• We create five different image hyperlinks. Clicking an image in this
example takes the user to a corresponding web page
Using Images as Hyperlinks
Using Images as Hyperlinks
• Lines 13–16 create an image hyperlink by nesting an img element in an anchor element.
• The img element’s src attribute value specifies that this image (links.jpg) resides in a
directory named buttons. The buttons directory and the HTML5 document are in the
same directory.
• Images from other web documents also can be referenced by setting the src attribute to
the name and location of the image. If you refer to an image on another website, the
browser has to request the image resource from that site’s server. [Note: If you’re
hosting a publicly available web page that uses an image from another site, you should
get permission to use the image and host a copy of the image on your own website.The
image’s owner may require you to acknowledge their work.
• Clicking an image hyperlink takes a user to the web page specified by the surrounding
anchor element’s href attribute. When the mouse hovers over a link of any kind, the URL
that the link points to is displayed in the status bar at the bottom of the browser window.
Special Characters and Horizontal Rules
• When marking up text, certain characters or symbols may be difficult
to embed directly into an HTML5 document. Some keyboards do not
provide these symbols (such as ©), or their presence in the markup
may cause syntax errors (as with <).
• For example, the markup
• Results in a syntax error because it uses the less-than character (<),
which is reserved for start tags and end tags such as <p> and </p>.
HTML5 provides character entity references (in the form &code;) for
representing special characters.
• We could correct the previous line by writing
Some common HTML character entity
references.
• In addition to special characters, this document introduces a
horizontal rule, indicated by the <hr> tag in line 17.
• Most browsers render a horizontal rule as a horizontal line with extra
space above and below it. As a professional, you’ll see lots of older
code—known as legacy code.
• The horizontal rule element should be considered a legacy element
and you should avoid using it.
• As you’ll learn, CSS can be used to add horizontal rules and other
formatting to documents.
• In lines 28–30, we introduce four new elements. Most browsers
render the del element as strike-through text. With this format users
can indicate document revisions.
• To superscript text (i.e., raise text above the baseline and in a
decreased font size) or subscript text (i.e., lower text below the
baseline and in a decreased font size), use the sup or sub element,
respectively.
• We also use character entity reference &lt; for a less-than sign and
• &frac14; for the fraction 1/4 (line 31)
List
Nested List
Tables
• Tables are frequently used to organize data into rows and
columns. Our first example creates a table with six rows and two
columns to display price information for various fruits.
• Tables are defined with the table element (lines 13–58).
• Line 13 specifies the table element’s start tag. The border attribute
with the value "1" specifies that the browser should place borders
around the table and the table’s cells.
• The border attribute is a legacy attribute that you should avoid. When
we introduce CSS3, we’ll use CSS’s border property, which is the
preferred way to format a table’s borders.
Tables
• The caption element (lines 17–18) specifies a table’s title. Text in this
element is typically rendered above the table.
• A table has three distinct sections—head, body and foot. The head
section (or header cell) is defined with a thead element (lines 22–27),
which contains header information such as column names.
• Each tr element (lines 23–26) defines an individual table row. The
columns in the thead section are defined with th elements. Most
browsers center text formatted by th (table header column) elements
and display them in bold.
• Table header elements (lines 24–25) are nested inside table row
elements.
Tables
• The body section, or table body, contains the table’s primary data.
The table body (lines 40–57) is defined in a tbody element.
• In the table body, each tr element specifies one row. Data cells
contain individual pieces of data and are defined with td (table data)
elements in each row.
• In this example, we specified only the table’s data, not its formatting.
As you can see, in the browser’s default formatting each column is
only as wide as its largest element, and the table itself is not visually
appealing. We’ll use CSS to specify HTML5 elements’ formats.
Tables
• The tfoot section (lines 31–36) is defined with a tfoot (table foot) element.
The text placed in the footer commonly includes calculation results and
footnotes. Here, we manually entered the calculation total.
• In later chapters, we’ll show how to perform such calculations dynamically.
Like other sections, the tfoot section can contain table rows, and each row
can contain cells. As in the thead section, cells in the foot section are
created using th elements, instead of the td elements used in the table
body.
• Before HTML5, the tfoot section was required to appear above the tbody
section of the table. As of HTML5, the tfoot section can be above or below
the tbody section in the code.
Tables
Colspan, Rowspan
Forms
• When browsing websites, users often need to provide information
such as search queries, e-mail addresses and zip codes.
• HTML5 provides a mechanism, called a form, for collecting data from
a user.
• Data that users enter on a web page is normally sent to a web server
that provides access to a site’s resources (for example, HTML5
documents, images, animations, videos).
• These resources are located either on the same machine as the web
server or on a machine that the web server can access through the
Internet.
Forms
• When a browser requests a publicly available web page or file that’s
located on a server, the server processes the request and returns the
requested resource.
• A request contains the name and path of the desired resource and
the protocol (method of communication).
• HTML5 documents are requested and transferred via the Hypertext
Transfer Protocol (HTTP).
Forms
• We are seeing a simple form that sends data to the web server for
processing.
• The web server typically returns a web page back to the web browser—this
page often indicates whether or not the form’s data was processed
correctly.
• [Note: This example demonstrates only client-side functionality. If you
submit this form (by clicking Submit), the browser will simply display
www.deitel.com (the site specified in the form’s action), because we
haven’t yet specified how to process the form data on the server.
• After this, server-side programming (for example, in PHP, ASP.NET and
JavaServer Faces) necessary to process information entered into a form.]
Forms
Forms
• method Attribute of the form Element:
• The form is defined in lines 20–43 by a form element. Attribute
method (line 20) specifies how the form’s data is sent to the web
server.
• Using method = "post" appends form data to the browser request,
which contains the protocol (HTTP) and the requested resource’s
URL.
• This method of passing data to the server is transparent—the user
doesn’t see the data after the form is submitted.
action Attribute of the form Element
• The action attribute in the form element in line 20 specifies the URL
of a script on the web server that will be invoked to process the
form’s data. Since we haven’t seen server-side programming yet (PHP,
JSP), we set this attribute to http://www.deitel.com for now.
• Lines 24–43 define input elements that specify data to provide to
the script that processes the form. There are several types of input
elements.
• An input’s type is determined by its type attribute. This form uses a
text input, a submit input, a reset input and three hidden inputs.
Hidden Inputs
• Forms can contain visual and nonvisual components. Visual components
include clickable buttons and other graphical user interface components
with which users interact.
• Nonvisual components, called hidden inputs (lines 23–28), store any data
that you specify, such as e-mail addresses and HTML5 document file
names that act as links.
• The three hidden input elements in lines 23–28 have the type attribute
hidden, which allows you to send form data that’s not input by a user.
• The hidden inputs are an e-mail address to which the data will be sent, the
e-mail’s subject line and a URL for the browser to open after submission of
the form. Two other input attributes are name, which identifies the input
element, and value, which provides the value that will be sent (or posted)
to the web server.
Hidden Inputs
• text input Element
• The text input in lines 32–33 inserts a text field in the form. Users can type data in
text fields. The label element (lines 31–34) provides users with information about
the input element’s purpose.
• The input element’s size attribute specifies the number of characters visible in
the text field. Optional attribute maxlength limits the number of characters input
into the text field—in this case, the user is not permitted to type more than 30
characters.
• submit and reset input Elements
• Two input elements in lines 40–41 create two buttons. The submit input element
is a button. When the submit button is pressed, the form’s data is sent to the
location specified in the form’s action attribute. The value attribute sets the text
displayed on the button. The reset input element allows a user to reset all form
elements to their default values.
Additional Form Elements
• Introducing new elements and attributes for creating more complex
forms.
• The textarea element (lines 31–32) inserts a multiline text area into
the form. The number of rows is specified with the rows attribute,
and the number of columns (i.e., characters per line) with the cols
attribute.
• In this example, the textarea is four rows and 36 characters wide. To
display default text in the textarea, place the text between the
• <textarea> and </textarea> tags.
Form
• Textarea
• Checkbox
• Radio button
• Drop down
• Button

• Difference between
• Checkbox and radio
Form
• The password input in line 39 inserts a password box with the
specified size (maximum number of displayed characters).

• A password box allows users to enter sensitive information, such as


credit card numbers and passwords, by “masking” the information
input with asterisks (*). The actual value input is sent to the web
server, not the masking characters.
Form
• Lines 45–59 introduce the checkbox input element. Checkboxes
enable users to select an option.

• When a user selects a checkbox, a check mark appears in the


checkbox. Otherwise, the checkbox remains empty. Each checkbox
input creates a new checkbox.

• Check-boxes can be used individually or in groups. checkboxes that


belong to a group are assigned the same name (in this case,
"thingsliked").
Form
• After the checkboxes, we present two more ways to allow the user to make
choices. When your form has several checkboxes with the same name,
make sure that they have different values, or the web server scripts will
not be able to distinguish them.
• In this example, we introduce two new input types. The first is the radio
button (lines 69–83) specified with type radio.
• Radio buttons are similar to checkboxes, except that only one radio button
in a group of radio buttons may be selected at any time.

• The radio buttons in a group all have the same name attributes and are
distinguished by their different value attributes. The attribute checked (line
71) indicates which radio button, if any, is selected initially. The checked
attribute also applies to checkboxes.
Form
• The select element (lines 92–105) provides a drop-down list from which
the user can select an item.
• The name attribute identifies the drop-down list. The option elements
(lines 93–104) add items to the drop-down list.
• The option element’s selected attribute specifies which item initially is
displayed as the selected item in the select element.
• If no option element is marked as selected, the browser selects the first
option by default.
• Not setting the name attributes of the radio buttons in a group to the same
name is a logic error because it lets the user select all of the radio buttons
at the same time.
Internal Linking
• We discussed how to hyperlink one web page to another.mThe
internal linking—a mechanism that enables the user to jump between
locations in the same document.
• Internal linking is useful for long documents that contain many
sections.
• Clicking an internal link enables the user to find a section without
scrolling through the entire document.
Internal Linking
• Line 13 contains a tag with the id attribute (set to "features") for an
internal hyperlink.
• To link to a tag with this attribute inside the same web page, the href
attribute of an anchor element includes the id attribute value,
preceded by a pound sign (as in #features).
• Line 55 contains a hyperlink with the id features as its target. Clicking
this hyperlink in a web browser scrolls the browser window to the h1
tag in line 13.
• You may have to resize your browser to a small window and scroll
down before clicking the link to see the browser scroll to the h1
element.
• A hyperlink can also reference an internal link in another document
by specifying the document name followed by a pound sign and the
id value, as in:
href = "filename.html#id“
• For example, to link to a tag with the id attribute booklist in
books.html, href is assigned "books.html#booklist".
• You can send the browser to an internal link on another website by
appending the pound sign and id value of an element to any URL, as
in:
href = "URL/filename.html#id"
Meta Elements
• Search engines catalog sites by following links from page to page
(often known as spidering or crawling the site) and saving
identification and classification information for each page.
• One way that search engines catalog pages is by reading the content
in each page’s meta elements, which specify information about a
document.
• Using the meta element is one of many methods of search engine
optimization (SEO)—the process of designing and tuning your website
to maximize your findability and improve your rankings in organic
(nonpaid) search engine results.
Meta Elements
• Two important attributes of the meta element are name, which identifies the
type of meta element, and content, which provides the information search
engines use to catalog/index pages.
• Lines 12–14 demonstrate a "keywords" meta element. The content attribute of
such a meta element provides search engines with a list of words that describe
the page.
• These words are compared with words in search requests. Thus, including meta
elements and their content information can draw more viewers to your site.
• Lines 15–18 demonstrate a "description" meta element. The content attribute of
such a meta element provides a three- to four-line description of a site, written in
sentence form.
• Search engines also use this description to catalog your site and sometimes
display this information as part of the search results.
Static Web Pages
• Static Web pages are very simple. It is written in languages such as
HTML, JavaScript, CSS, etc.
• For static web pages when a server receives a request for a web page,
then the server sends the response to the client without doing any
additional process.
• And these web pages are seen through a web browser. In static web
pages, Pages will remain the same until someone changes it manually.
Dynamic Web Pages
• Dynamic Web Pages are written in languages such as AJAX, ASP,
ASP.NET, etc.
• In dynamic web pages, the Content of pages is different for different
visitors.
• It takes more time to load than the static web page. Dynamic web
pages are used where the information is changed frequently, for
example, stock prices, weather information, etc.
Dynamic Web Pages
Static and Dynamic Web Page
• Static Websites
• Common examples of static websites include resume websites, portfolio
websites, brochure websites and other informational or read-only sites.
• These websites are small (three to four pages or fewer), limited in content,
and don’t require personalized content or frequent updates.

• Dynamic Websites
1.X (Dynamic Social Media Website)
2.Netflix (Dynamic Entertainment Website)
HTML5
• new input element types for
• colors,
• dates,
• times,
• e-mail addresses,
• numbers,
• ranges of integer values,
• telephone numbers,
• URLs, search queries, months and weeks
• Autocompletion capabilities that help users quickly re-enter text that they’ve
previously entered in a form
input Type color
Date
• The displayed date format will differ from the actual value — the displayed
date is formatted based on the locale of the user's browser, but the parsed
value is always formatted yyyy-mm-dd.
• The <input> element with type=”date” is used to allow the user to choose a
date. By default, the date format displayed in the input field is determined
by the user’s browser and operating system settings.
• However, it is possible to change the format of the date displayed in the
input field using HTML.
• JavaScript can be employed to enforce a specific format as browsers may
not directly support changing the format.
• Include a placeholder attribute with the desired date format, like “dd-mm-
yyyy.”
Validation
• Traditionally it’s been difficult to validate user input, such as ensuring
that an e-mail address, URL, date or time is entered in the proper
format.
• The new HTML 5 input types are self validating on the client side,
eliminating the need to add complicated JavaScript code to your web
pages to validate user input, reducing the amount of invalid data sub-
mitted and consequently reducing Internet traffic between the server
and the client to correct invalid input.
• The server should still validate all user input.
Validation
• When a user enters data into a form then submits the form (in this
example, by clicking the Submit button), the browser immediately
checks the self-validating elements to ensure that the data is correct.
• For example, if a user enters an incorrect hexadecimal color value
when using a browser that renders the color elements as a text field
(e.g., Chrome), a callout pointing to the element will appear,
indicating that an invalid value was entered.
• Figure 3.5 lists each of the new HTML5 input types and provides
examples of the proper formats required for each type of data to be
valid.
Validation
Bypass Validation
• If you want to bypass validation, you can add the formnovalidate
attribute to input type submit:

• <input type = "submit" value = "Submit" formnovalidate />


Placeholder attribute
• The placeholder attribute (lines 44, 72 and 77) allows you to place
temporary text in a text field.
• Generally, placeholder text is light gray and provides an example of the text
and/or text format the user should enter.
• When the focus is placed in the text field (i.e., the cursor is in the text
field), the placeholder text disappears—it’s not “submitted” when the user
clicks the Submit button (unless the user types the same text).
• HTML5 supports placeholder text for only six input types—text, search, url,
email and password. Because the user’s browser might not support
placeholder text, we’ve added descriptive text to the right of each input
element.
Required attribute
• The required attribute (lines 45 and 78) forces the user to enter a
value before submitting the form. You can add required to any of the
input types. In this example, the user must enter an e-mail address
and a telephone number before being able to submit the form.
• For example, if the user fails to enter an e-mail address and clicks the
Submit button, a callout pointing to the empty element appears,
asking the user to enter the information
input Element autocomplete Attribute
• The autocomplete attribute (line 18) can be used on input types to
automatically fill in the user’s information based on previous input—
such as name, address or e-mail.
• You can enable autocomplete for an entire form or just for specific
elements.
• Oline order form might set automcomplete = "on" for the name and
address inputs and set autocomplete = "off" for the credit card and
password inputs for security purposes.
input Element autocomplete Attribute

You might also like