Ordered list
■■
Option group
■■
Selection choice
■■
Output control
■■
Paragraph
■■
Plug-in parameter
■■
Preformatted text
■■
Progress of a task
■■
Inline quotation
■■
Ruby parenthesis
■■
Ruby text
CHAPTER 2
Getting started with HTML5
■■
Ruby annotation
■■
Sample output
■■
Linked or embedded script
■■
Document section
■■
Selection control
■■
Small print
■■
Media resource
■■
Generic inline container
■■
Strong importance
■■
Embedded style sheet
■■
Subscript
■■
Superscript
■■
Table
■■
Table body
■■
Table cell
■■
Multiline text control
■■
Table footer
■■
Table header cell
■■
Table head
■■
Date and/or time
■■
Document title
■■
Table row
■■
Unordered list
■■
Variable
■■
Video or movie
■■
Optionally break up a large word at this element
Many of these elements are discussed in more detail later in this book.
Adding attributes to elements Key Terms
The begin tag can contain additional data in the form of an attribute. An attribute is a name=”value” pair in which name is unique within the tag and value is always enclosed within either single quotes or double quotes. You can add many attributes to the begin tag. You can also alternate using single quotes and double quotes, which is especially beneficial when you need to embed single or double quotes within the value of the attribute. You can also have Boolean attributes that contain the attribute name but no value.
Lesson 1: Introducing HTML5
CHAPTER 2
35
Here is an example of an element that has attributes.
In this example, id and class are attributes. The id attribute uniquely identifies an element within an HTML document. The class attribute specifies a named CSS style that should be applied to the element.
Working with Boolean attributes Key Terms
Some attributes are Boolean attributes, which means that the mere presence of the attribute indicates that an option is set. Some examples of Boolean attributes are as follows. ■■
checked Used with the check box and option button to indicate selection
■■
selected Used to indicate which option is selected in a drop-down or select list
■■
disabled Used to disable input, text area, button, select, option, or opt group
■■
readonly Used to prevent the user from typing data into a text box, password, or text area
There are different ways to indicate a Boolean attribute. One way is to use the minimized form, by which you just add the attribute name into the starting tag but don’t provide a value. Here is an example of minimized form when setting a check box to selected.
Another way to indicate a Boolean attribute is to use quoted form, in which you provide either an empty value or the name of the attribute as its value. Here are examples of both.
The latter seems redundant but is usually considered to be the preferred way to represent the Boolean attribute. If you use jQuery, which is a third-party JavaScript toolset, you’ll find that it works best with that redundant example.
Quick check ■■
You are using a element, and you want it to be disabled until some criteria is met. What is the best way to disable the element when the page is loaded?
Quick check answer ■■
Write the element using quoted syntax and assign the attribute name to the attribute as follows.
Button
36
CHAPTER 2
Getting started with HTML5
HTML5 global attribute reference HTML5 defines a set of named attributes that can be applied to any HTML5 element. These elements are called global attributes, and each has a very specific meaning, as follows. ■■
accesskey Enables you to either specify a shortcut key to which to jump or to set focus to an element. As a rule, you shouldn’t use this because it can cause problems with other technologies.
■■
class Used with CSS to specify one or more class names for an element.
■■
contenteditable Specifies that the content within the tag can be edited.
■■
■■
■■ ■■
contextmenu User can right-click an element to display a menu. At the time of this writing, no browser supports this attribute. dir Enables you to specify left-to-right or right-to-left text direction for the content in an element. draggable Specifies whether an element is draggable. dropzone Enables you to specify the behavior of the dragged data when it’s dropped. Data can be copied, moved, or linked.
■■
hidden Specifies that an element is not relevant.
■■
id Specifies a unique id for an element.
■■
■■
lang Specifies the language (English, French, German, and so on) of the element’s content. spellcheck Used with the lang attribute to enable you to indicate whether the element is to have its spelling and grammar checked.
■■
style Specifies an inline CSS style for the element.
■■
tabindex Sets the tabbing order of the element.
■■
title Provides extra information about the element.
You’ll see many examples of these global attributes in this book.
Working with self-closing tags Key Terms
You can represent any element that contains no content as a self-closing tag. A self-closing tag is a beginning tag and an ending tag in one. You end the starting tag with a space, slash, and greater-than symbol. For example, the element cannot have any content, so here is the beginning and ending tag in one: . In XML, any empty element can be written with a self-closing tag, but in HTML5, this can cause problems in different browsers. The rule of thumb is to use self-closing tags for tags that cannot have content, such as the tag. Empty elements that are capable of having content but currently don’t have content should have separate end tags. An example is
; there is no content, but the beginning and ending tags still exist.
Lesson 1: Introducing HTML5
CHAPTER 2
37
NOTE USE THE ELEMENT CAREFULLY
When using the element to reference an external JavaScript file, the element will not have content when used in this context, but you must always include a separate end tag as follows.
A symptom of incorrect usage of the element is when the browser renders a blank screen, but you can view the source, and all its content is there.
Quick Check ■■
You want to use the element to include a JavaScript file named MyCode. js in the scripts folder. What is the proper syntax?
Quick Check Answer: ■■
Working with void elements
Key Terms
Most but not all elements can have content, and the content can include elements with content. Elements are not required to have content, but some elements cannot have content. These are called void elements. For example, the tag represents a line break and cannot have any content. The following is a list of void elements in HTML5. ■■ ■■
Specifies the document’s base URL or target for all relative URLs in the document
■■
Represents a line break
■■
Defines the properties of one or more columns within a element
■■
Defines a command that can be invoked by a user
■■
Specifies a thematic change in content
■■
Defines an image
■■
Defines a typed data field that allows the user to edit the data
■■
■■
■■
38
Defines a hyperlink area with some text in an image map
Defines a relationship between a document and an external resource such as a cascading style sheet Defines a key-pair generator control for forms that is used to encrypt data that will be passed to the server Defines metadata that describes the HTML document
CHAPTER 2
Getting started with HTML5
■■
Defines a parameter for an object
■■
Defines a multimedia resource for a or element
■■
Optionally breaks up a large word at this element
In earlier versions of HTML, you just used the tag with no ending tag to indicate that you wanted to start a new line on the webpage. With XHTML, this was a problem because all beginning tags are required to have matching end tags. HTML5 allows you to use a beginning tag with no end tag, but a better solution is to use self-closing tags.
Adding expando attributes Key Terms
Expando attributes are attributes that you define. Expando attributes are also known as author-defined attributes or simply as custom attributes. Any time you want to attach data to an HTML tag, you can just create an attribute with the name of your choice and assign the data. However, the name you create might conflict with either an existing W3C-defined attribute name or a future W3C-defined attribute name. To ensure that you have no existing or future naming conflict, assign a name that is prefixed with “data-“.
Quick check ■■
You have a webpage with a element that contains the customer’s name. Along with the name, you want to include the customer number on the element, but you don’t want to display the customer number. How can you write the element for a customer called Contoso Ltd with customer number 123?
Quick check answer ■■
Use an expando attribute to hold the customer number as follows. Contoso Ltd
Adding comments You can add comments to your HTML source by using the following syntax.
Comments are not displayed on the rendered browser page but are sent to the browser. Comments can help document your source. No spaces are allowed between the character at the end of the comment tag. This seemingly weird behavior means that you cannot have backto-back dashes (--) in your comment because this combination causes HTML syntax errors. In addition, you cannot end a comment with three dashes, such as because this also generates a syntax error.
Lesson 1: Introducing HTML5
CHAPTER 2
39
Adding conditional comments Key Terms
Only Internet Explorer recognizes conditional comments, which enable you to add a browserspecific source that executes if the browser is Internet Explorer but is treated as a comment by other browsers. You can add conditional comments to your HTML document by using the following syntax.
This is not Internet Explorer!
The first conditional comment checks whether the browser is Internet Explorer and the version is earlier than or equal to 7. The next conditional comment checks whether the browser is Internet Explorer and the version is earlier than 7. The next conditional comment checks whether the browser is Internet Explorer and the version is 8. The next conditional comment checks whether the browser is Internet Explorer and the version is later than 8, followed by a check to see whether the browser is Internet Explorer and the version is later than or equal to 9. The last line checks whether the browser is not Internet Explorer. Note that the syntax of the last line is different from the others.
Creating an HTML document Now that you’ve seen the various elements and attributes, it’s time to group them in a meaningful way to create an HTML document. The HTML document contains an outer structure, metadata, and some content.
Basic document structure
Key Terms
Every HTML document should have a basic structure that consists of a declaration, which historically has indicated the version of HTML to the browser. In HTML5, this indicates to the browser that it should be in no-quirks mode. No-quirks mode causes the browser to operate in an HTML5-compliant manner. Next is the root element, which contains the element and the element. The element contains hidden information such as metadata that describes the HTML document and instructions. The following is an example of metadata in the element. title here content here
40
CHAPTER 2
Getting started with HTML5
Key Terms
In this example, the element describes the character set as utf-8, which is an efficient form of unicode in which the English language characters (ASCII) require only a single byte to be represented, and in which other languages can have characters that are represented with up to 4 bytes. This is the most common character set used in HTML and XML documents. This example also contains a element, which is important because it serves the following purposes. ■■
Displays in the browser toolbar
■■
Provides the default name for the page when it is added to favorites
■■
Displays the title when a search engine displays the page in the search results
The tag contains the displayable contents.
Using special characters (HTML entities) You might want to display the < and > characters on your webpage, but you’ve seen that the less-than and greater-than characters define tags. These characters can be displayed in your content by using either the entity name or entity number as follows. &entity_name; or entity_number; There are many HTML entities, but Table 1-1 lists the most common HTML entities you will use in your HTML document. TABLE 1-1 Reference to common entities
Display
Entity Name
Entity Number
Description
&
&
&
Ampersand
>
>
>
Greater-than sign
Contact Joe in Sales Contact Sales and Service Contact Sales cc Service Contact Sales Contact Sales with call me message Contact Sales with multi line message Contact Sales with hi subject and call me message
Adding images to your HTML document When you want to embed an image in your HTML document, use the element. The element does not have an ending tag; it’s a void element. The element has required attributes of src (abbreviation for source) and alt (abbreviation for alternate). Use the
Lesson 2: Embedding content
CHAPTER 2
47
src attribute to provide an absolute or relative URL reference to the image that is to be displayed. Use the alt attribute to provide alternate text to be displayed when the image is not available due to slow connection or other mishap. The following is an example of the element.
It’s important to understand that the image is not embedded in the HTML document. Instead, you provide a reference to the image file. When the browser reads your HTML document, the browser will reach your element and retrieve the image based on the src attribute. When the image is retrieved, the browser will merge the image into the final rendering that is displayed into the browser window. If the browser cannot display the image, it will display the alternate text.
Image file types When using the element, you can supply JPEG (.jpg or .jpeg), GIF (.gif), PNG (.png), or SVG (.svg) files. The following is a brief description of each file type that should help you decide which file type to use for your application. ■■
■■
■■
■■
48
JPG Also known as JPEG, this is best for photographs because it offers high compression and up to 16.8 million color combinations, but the compression algorithm is lossy, meaning that you lose detail every time you save the file. GIF GIF is great to use on small images that have a fixed number of colors. GIF also supports transparent color. GIF uses lossless compression and is best for logos and worst for photos. GIF also supports the ability to encapsulate multiple images in one file, which is commonly used to provide animated GIFs. PNG PNG is a great all-around file type due to its lossless high compression. PNG files can be 48-bit true color or 16-bit grayscale. PNG not only supports transparent color but also offers variable transparency. Photos aren’t compressed to be as small as JPG photos, but being lossless makes it worth the extra size in many scenarios. You might use PNG as your storage type for photos that you want to edit, but when displaying them on the web, you might want to save the PNG as JPG to achieve the best compression. SVG SVG is Scalable Vector Graphics and is great for drawings but not for photos. SVG images can be scaled up or down without losing detail because the file contains the instructions to draw the image, as opposed to the other file types that contain a raster-based image. Raster-based images are composed of color dots that make up the image. If you need to scale a raster-based image up or down, you will see that in color, dots are re-sampled, and the image typically ends up looking blocky.
CHAPTER 2
Getting started with HTML5
Quick check ■■
You are creating several small icons that will be displayed on your webpage. These icons will render as different shapes and will use transparent color. Each icon uses a small number of colors. Which would be the best image file type for this application?
Quick check answer ■■
Use the GIF format because it has transparent color support and because it requires small numbers of colors.
Creating image links If you create a hyperlink and the hyperlink’s content is an element, you have created an image that can be clicked—an image link. Here is an example of an image link.
Creating an image map You can create a clickable image map on your HTML document by using the element. It contains elements that define clickable regions on the image. The element has a name attribute that must be set. On an element, set the usemap attribute to the element’s name to create a relationship between the image and the map. In the element, you define elements by using the shape, coords, href, and alt attributes. The element is a void element, so you use a self-closing tag. The shape attribute is set to rect, circle, poly, or default, where default is an element whose size is the same as the image and is triggered if no specific is defined for coordinates where you clicked. The coords attribute will contain x and y coordinates where 0, 0 is the top-left corner of the image. The coords attribute is set according to the shape as follows. ■■
rect x1, y1, x2, and y2 specify the coordinates of the left, top, right, and bottom.
■■
circle x, y, and radius specify the coordinates of the center and the radius.
■■
poly x1, y1, x2, y2,.., xn, and yn specify the coordinates of the edges. The first and last coordinate pairs should be the same to close the polygon, but if they aren’t the same, the browser will add a closing pair.
The href attribute is the same as the href attribute on the element and can be set to an absolute or relative URL. The alt attribute is set to alternate text to be displayed and is required when the href attribute is set.
Lesson 2: Embedding content
CHAPTER 2
49
The following is an example of creating an image map with its areas and assigning the image map to an image.
Embedding plug-in content You can use the and elements to embed content from plugins. Why are there two tags for the same purpose? The reason is the differences in browsers over the years. Originally, Netscape created the tag for Netscape-style plug-ins, and Internet Explorer added support for the tag. The tag was added to HTML 4.0 with the benefit of supporting Internet component downloads so that the plug-in could be automatically downloaded and installed.
The tag Although the HTML 4.01 specification did not support the tag, most browsers continued support due to the installed base and the ease of use. In HTML5, both tags exist. The tag provides more functionality, whereas the tag is easier to use. For example, if you want to play a Flash file, you can use the tag as follows.
The tag has the following attributes. ■■
height Specifies the height in pixels of the embedded content
■■
src Specifies the URL of the external file to embed
■■
type Specifies the MIME type of the embedded content
■■
width Specifies the width in pixels of the embedded content
For browsers that don’t support the tag, you can add fallback content into the element as follows.
50
CHAPTER 2
Getting started with HTML5
The tag The tag embeds an object within an HTML document. You can embed multimedia such as audio, video, Java applets, ActiveX, PDF, and Flash in your webpages. The tag contains the following attributes. ■■
data Supplies the URL of the resource to be used by the object
■■
form Indicates one or more form ids to which the object belongs
■■
height Specifies the height in pixels of the object
■■
name Defines the name of the object
■■
type Defines the MIME type of data specified in the data attribute
■■
usemap Indicates the name of a client-side image map to be used with the object
■■
width Specifies the width in pixels of the object
IMPORTANT AT A MINIMUM
At a minimum, you must define either the data or type attribute.
The tag can be used within the element only. You might find examples that implement many more attributes than are defined in this list because older versions of HTML supported other attributes, but HTML5 supports only the attributes that are listed plus the global attributes. You can also use the tag to embed another webpage in your HTML document. The following is an example of using the element to embed a webpage in your HTML document.
As a rule, consider using the tag when embedding a webpage from another domain into your HTML document by using sandboxing. It’s made for that purpose, whereas the tag is a more general-purpose tag. The tag behaves differently with different browsers, and you might find that features such as tooltips work only with the tag and not with the tag. It is possible to embed images and image maps in your HTML document by using the tag, but you should avoid doing so. It’s best to use the tag to embed images. The text within the element is alternate text to be displayed for browsers that do not support the tag.
Lesson 2: Embedding content
CHAPTER 2
51
Passing parameters to an object Because the tag can represent any object, the tag enables you to pass data to the object. You can use one or more tags to pass parameters to plug-ins that have been embedded with the tag. The tags must be before any alternate text that is within the element. The tag has a name and a value attribute, and both are required for each tag. The following is an example of using a tag with an audio file to keep the audio file from automatically playing when the page is loaded.
Using the object tag to create a browser context In addition to using the tag to create a nested browser context, you can use the tag, but this is not supported on all browsers, so you should use the tag.
Lesson summary ■■
■■
■■
■■
■■
■■
Use the sandbox attribute on the tag to help prevent malware and annoyances such as pop-ups from being introduced when the content is embedded in your HTML page. Use the seamless attribute on the tag to indicate that the source content is to be rendered to appear as though it’s part of the containing document. The tag creates a hyperlink to either an external HTML document or an internal location in the current document. The tag can also be used to send email messages. The element is a void element and is used to add an image reference to your HTML document. JPG is best for displaying photos on HTML pages due to its compression, GIF is best for small images with transparency and embedded animations, PNG is best for storage due to lossless compression during editing sessions, and SVG is best for drawings due to its vector drawing scalability.
■■
You can create a clickable image map by using the and elements.
■■
You can use the tag to provide simple content embedding.
■■
52
You can use the element to provide reuse of HTML by embedding an HTML document in your current HTML document.
You can use the tag to provide content embedding with greater flexibility because it can have nested elements.
CHAPTER 2
Getting started with HTML5
Lesson review Answer the following questions to test your knowledge of the information in this lesson. You can find the answers to these questions and explanations of why each answer choice is correct or incorrect in the “Answers” section at the end of this chapter. 1. You want to embed a Flash file called myFlash.swf in your HTML document. Which is
the most appropriate code? A. B. C. D. 2. You want to create a drawing of a machine that Contoso, Ltd., will be selling on its
website. The drawing will be embedded in your HTML document, and you want it to maintain its quality when resized. Which is the most appropriate file type to use? A. SVG B. GIF C. JPG D. PNG
Practice exercises You’ve learned a bit about HTML elements and attributes, and it’s time to create a website. In Exercise 1, you create a website for a fictitious company, Contoso, Ltd., and add a home page. In Exercise 2, you add the expense reports, human resources, and main content pages. If you encounter a problem completing an exercise, the completed projects can be installed from the Practice Exercises folder that is provided with the companion content.
Exercise 1: Create a simple website by using Visual Studio Express for Web In this practice, you create a simple website by using Visual Studio Express 2012 for Web. The quality of the webpages produced will be less than desirable because CSS hasn’t been discussed yet. The goal of this practice is to use many of the tags that have been described in this lesson. You start by creating an ASP.NET website by using Visual Studio Express 2012 for Web, and then you add to and modify the home page. 1. If you haven’t installed Visual Studio Express 2012 for Web, do so now. You can down-
load this from the Microsoft website.
Practice exercises
CHAPTER 2
53
2. Start Visual Studio Express 2012 for Web. Navigate to file and choose New Project.
Navigate to Installed | Templates | Visual Basic | Web and select ASP.NET Empty Web Application. 3. Set the name of your application to ContosoWebSite. 4. Select a location for your application. 5. Set the solution name to ContosoWebSiteSolution. 6. Be sure to keep the Create Directory For Solution check box selected. 7. Click OK to create your new project. 8. When the application is created, click Debug and select Start Debugging. (The shortcut
key is usually F5 but can vary based on your installation settings.) The ASP.NET Empty Web Application doesn’t include a home page, so an error page is displayed, showing an HTTP Error 403.14 - Forbidden error. The error page indicates (in a rather indirect way) that you don’t have a default page on your website, so the web server tries to display a list of all files in the directory. However, the security settings on the website will not permit directory browsing to display the directory contents. 9. Close the error page, which should automatically stop debugging. If you need to, you
can stop debugging by clicking Debug and choosing Stop Debugging (or pressing Shift+F5). 10. Add a home page. In the Solution Explorer window, right-click the ContosoWebSite
project, choose Add, and select HTML Page. Set the name to default.html and click OK. The home page is added to your website and contains the following HTML.
11. In the element, set the title to Contoso Ltd. Home Page. 12. In the Solution Explorer window, right-click the project, choose Add, select New Folder,
and name the folder Images. 13. In the Solution Explorer window, right-click the Images folder that you just added,
choose Add, select Existing Item, and select the ContosoLogo.jpg file that is located in the Chapter02 Resources folder. 14. In the element, add a comment and set the text to Add with Contoso
logo. The element is covered in Lesson 2.
54
CHAPTER 2
Getting started with HTML5
15. Using the