27.01.2015 Views

Lesson 3: XHTML Coding Objectives: Demonstrate knowledge of ...

Lesson 3: XHTML Coding Objectives: Demonstrate knowledge of ...

Lesson 3: XHTML Coding Objectives: Demonstrate knowledge of ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Lesson</strong> 3: <strong>XHTML</strong> <strong>Coding</strong><br />

<strong>Objectives</strong>:<br />

<strong>Demonstrate</strong> <strong>knowledge</strong> <strong>of</strong> basic <strong>XHTML</strong> document structure.<br />

Identify <strong>XHTML</strong> document structure tags, including the tag and the<br />

tag.<br />

Create <strong>XHTML</strong> that validates properly.<br />

Format paragraphs and text with <strong>XHTML</strong> tags.<br />

Use comments and good coding practice.<br />

Markup Tags<br />

A markup tag is simply an element name enclosed in angle brackets or wickets.<br />

A tag embeds the markup information in the document. For this reason, tags are the<br />

building blocks <strong>of</strong> markup files.<br />

The combination <strong>of</strong> markup tags and text is <strong>of</strong>ten referred to as code or markup.<br />

Types <strong>of</strong> tags<br />

There are two types <strong>of</strong> tags:<br />

o Container tags – use opening and closing tags. Container tags are also<br />

know as non-empty tags. Below is an example <strong>of</strong> a container tag.<br />

o Empty tags – stands alone and is not closed with a slash character. Below<br />

is an example <strong>of</strong> an empty tag.<br />

This is an example <strong>of</strong> a container tag.<br />

About Me <br />

Opening Tag<br />

Closing Tag<br />

o <strong>XHTML</strong> 1.0 allows you to use an alternative method for closing tags. In<br />

this method a non-empty tag is as follows .<br />

This is an example <strong>of</strong> an empty tag.<br />

This is a list item.<br />

This is a list item.<br />

Do not use empty tags!<br />

Empty Tags<br />

They are not valid in<br />

<strong>XHTML</strong>.<br />

o Use <strong>of</strong> empty tags in <strong>XHTML</strong> will cause your page not to validate. Older<br />

versions <strong>of</strong> HTML allowed for the use <strong>of</strong> empty tags.<br />

What makes up a tag<br />

The following three things put inside angle brackets can be a tag.


o An element – provides the main instruction <strong>of</strong> the tag. An element is<br />

required in every tag. Examples: , ,,, and<br />

many others.<br />

o An attribute – describes a certain aspect <strong>of</strong> the element. Some elements<br />

require attributes, however some do not. An example <strong>of</strong> an element is the<br />

align attribute <strong>of</strong> the element.<br />

o A value – gives value to the element and its attribute. Example: Values are optional unless a tag requires it.<br />

Document structure tags<br />

All <strong>XHTML</strong> documents must have the following structure tags:<br />

o A tag – this tag tells the interpret the markup used in the<br />

document. This is an SGML statement. If you declare an HTML 4.01<br />

DTD, then the interpreter will read it according to HTML 4.01 rules. If<br />

you declare an <strong>XHTML</strong> DTD, then the interpreter will read the document<br />

as an <strong>XHTML</strong> document.<br />

o An tag – Any code before the HTML tag is not HTML, but rather<br />

SGML.<br />

o A tag – The section allows you to insert tags,<br />

links to style sheets, and the tags.<br />

o Any tags – This tag specifies data about the data or metadata.<br />

Metadata can include a document description, keywords to help a search<br />

engines index the page, and specification <strong>of</strong> a character set. The <br />

tag is placed between the tags.<br />

o A tag that references a style sheet – This tag is recommended for<br />

<strong>XHTML</strong> Transitional and is required for <strong>XHTML</strong> Strict. Most sheets have<br />

a .css extension. The tag is placed between the tags.<br />

o A tag – This tag gives your document a title. This tag is placed<br />

between the tags.<br />

o A tag – This tag begins the body <strong>of</strong> the document.<br />

If your <strong>XHTML</strong> document does not have these tags the document will fail validation.<br />

HTML and <strong>XHTML</strong> coding<br />

<strong>XHTML</strong><br />

o Tags are case sensitive and should always be typed in lower case.<br />

o All tags must be properly closed<br />

o Use <strong>of</strong> empty tags will cause your page to fail validation.<br />

o Your page must begin with a DTD or Document Type Declaration.<br />

HTML<br />

o Tags are not case sensitive and can be typed in upper and/or lower case.<br />

o HTML coding is more forgiving that <strong>XHTML</strong>.<br />

o Use <strong>of</strong> empty tags is permitted.<br />

Document Type Declaration (DOCTYPE)<br />

The Document Type Declaration or tag describes the nature <strong>of</strong> your<br />

code and is placed at the very beginning <strong>of</strong> your document.


The DTD specifies the page’s primary language and markup version.<br />

The DTD can include a Web address that contains the proper Document Type<br />

Definition for the markup version used.<br />

Two problems may arise if you do not specify a DOCTYPE:<br />

o You will not be able to control how your code renders in the future.<br />

o You will not be able to validate your code.<br />

<strong>XHTML</strong> approximates the HTML 4.01 tags.<br />

Example: <strong>XHTML</strong> Transitional –<br />

<br />

The tag<br />

The opening and closing tags encompass all markup for the entire<br />

page.<br />

The tag can have many attributes, including:<br />

o xmlns – abbreviation for XML namespace. This attribute is required in the<br />

tag, however, most browsers will automatically add it, if you omit<br />

it.<br />

o lang – configures the page to use a particular language. Example: for a<br />

document written in English, you would use .<br />

o There are many other attributes for this tag. For a complete list go to<br />

http://www.w3schools.com or http://www.w3.org<br />

The tag<br />

The tag encompasses several page elements including:<br />

o The tag – this tag specifies various information about the page.<br />

This tag has several attributes including:<br />

Content-type – associates the page to HTTP<br />

http-equiv – contains values that will allow you to automatically<br />

refresh or redirect your page.<br />

Name – values include keywords, description, and author. The<br />

keywords value allows you to specify single words that allow<br />

search engines to match the pages to the keywords.<br />

Content – this attribute can specify character sets. When this<br />

attribute is paired with the name attribute it can provide values that<br />

supply keywords, a description, author name, and may more.<br />

o Then tag that references a style sheet, if present. – this tag usually<br />

comes before the title tag. Style sheets are recommended for <strong>XHTML</strong><br />

Transitional and are required for <strong>XHTML</strong> Strict.<br />

o The tag – this is the first tag that will allow you to specify content<br />

that will appear on the page. Any text between the opening and closing<br />

tags will appear in the page title box at the top <strong>of</strong> the browser.<br />

The tag<br />

All text that you want to appear on the page should be place between the open and<br />

closing tags.


The tag has several attributes including:<br />

o bgcolor – specifies the background color <strong>of</strong> the entire page.<br />

o background – specifies a background image for the page.<br />

o link – specifies the color <strong>of</strong> hypertext links on the page.<br />

Attribute values must be placed within quotation marks.<br />

Web Site File Structure<br />

At some point your page (html and images) will be uploaded to your site. For this<br />

reason it is a good idea to organize your files.<br />

<strong>XHTML</strong> pages are usually placed in a directory with images in a subfolder <strong>of</strong> the<br />

same directory. Below is an example <strong>of</strong> this structure.<br />

My Website<br />

images<br />

logo.jpg<br />

button.jpg<br />

index.html<br />

about.html<br />

services.html<br />

Before you create a web page there three things that you should do:<br />

o Obtain a text editor – Most operating systems have their own editors. In<br />

Windows this editor is Notepad. If you do not want to use the editor that<br />

comes with your operating system, you can download one. Common text<br />

editors include Notepad, WordPad, Vi, and Pico.<br />

o Install multiple browsers - There are several browsers on the market today.<br />

You will want to install two or three browser on your computer so that you<br />

can test your page in multiple environments.<br />

o Set file preferences – Windows does not show file extension by default.<br />

So you will need to change your folder options to allow you to see file<br />

extensions.<br />

Style Sheets<br />

A style sheet is a text file that contains the instructions on how the page will be<br />

displayed.<br />

A style sheet will allow you to change one thing, for example the color <strong>of</strong> the heading<br />

one style, without having to search each and every page.


HTML 4.01 and <strong>XHTML</strong> 1.0 both require that you use CSS to create the layout and<br />

formatting <strong>of</strong> your page.<br />

Before using a CSS you should learn the terminology.<br />

o There are four basic parts <strong>of</strong> a CSS rule:<br />

Selector – any element that you want to effect.<br />

Declaration – this part <strong>of</strong> a CSS rule is made up <strong>of</strong> the property and<br />

value enclosed in curly brackets.<br />

Property – this is what are you going to change about the selector.<br />

Value - this is the color or size that you are changing the particular<br />

selector to.<br />

Example rule: body {background: tan}<br />

Proper CSS Structure<br />

o A properly coded CSS rule should look similar to the one below.<br />

body {<br />

font-family: arial, verdana, helvetica; color: gray:<br />

}<br />

o Notice that the curly brackets are on separate lines and that the declaration<br />

is indented. This is considered good coding practice.<br />

o To ignore a rule in a style sheet you can comment it out. To do this place a<br />

/* before the rule and a */ after the rule.<br />

Example: /*body {background: tan}*/<br />

Inheritance<br />

o Inheritance is an essential concept <strong>of</strong> Cascading Style Sheets.<br />

o The term cascading refers to inheritance.<br />

o The styles for your CSS will cascade to the pages that are linked to it.<br />

o The rules in the style sheet will override attributes like bgcolor.<br />

CSS Facts<br />

There are four ways to apply a CSS styles:<br />

o Declare an inline style<br />

o Create an embedded style sheet<br />

o Link to an external style sheet<br />

o Use an imported style sheet - imported style sheets have been known to<br />

cause problems… certain older browsers crash when rendering pages with<br />

imported style sheets.<br />

Benefits <strong>of</strong> using a CSS<br />

o Consistency – Using a CSS makes it easy to have a consistent look and<br />

feel to your site.<br />

o Easy change management – When you need to change the color <strong>of</strong> a<br />

heading style you only have to change it on the style sheet. If you didn’t<br />

use a style sheet you would have to search for every occurrence <strong>of</strong> the<br />

heading style and change them one by one.


Paragraph formatting and block-level elements<br />

There are two types <strong>of</strong> elements:<br />

o A block-level element is an element that affects one or more paragraphs.<br />

o Text-level elements can affect a single character or a word.<br />

There are two types <strong>of</strong> breaks:<br />

o Paragraph breaks – a paragraph break is the most basic block-level<br />

element. The opening and closing paragraph tags define the<br />

beginning and end <strong>of</strong> a paragraph.<br />

o Line breaks – a line break is a text-level element. The line break tag <br />

does not need a separate closing tag.<br />

Heading Levels<br />

There are six Heading levels, h1, h2, h3, h4, h5, and h6.<br />

H1 is the largest heading and H6 is the smallest.<br />

Heading sizes are relative. H4 is the size <strong>of</strong> normal paragraph text.<br />

Heading levels are block elements, so they are automatically preceded and followed<br />

by a paragraph break.<br />

You should not place heading tags between a set <strong>of</strong> paragraph tags.<br />

Tag nesting in markup<br />

Most <strong>of</strong>ten multiple sets <strong>of</strong> tags will be used to format test. To do this you will have<br />

to place one pair <strong>of</strong> tags inside <strong>of</strong> another pair <strong>of</strong> tags, this is called nesting.<br />

Proper nesting requires that open and close one tag within another.<br />

Proper nesting:<br />

o … … <br />

Alignment<br />

Primitive formatting with the tag<br />

o The tag allows you to display plain text files in their original<br />

format.<br />

o This is an easy way to display tabular data<br />

Indenting and Centering<br />

o Text is automatically left aligned in <strong>XHTML</strong>.<br />

o You can center or right align a paragraph using the tag.<br />

This text is centered <br />

o You can also use the align attribute in the paragraph tag to center a<br />

paragraph.<br />

o You can use the tag to indent a paragraph.<br />

Do not use the tag within the tag. Do not use<br />

tags within the tag. Doing either <strong>of</strong> these will<br />

cause your code not to validate.<br />

Text-Level Elements<br />

A text-level element can affect a single character or an entire page.<br />

Basic text-level elements<br />

o Bold tag and strong tag


• Both <strong>of</strong> these tags bold your text; however, you should use the<br />

strong tag because it is standard.<br />

o Italic tag and emphasis tag <br />

• Both <strong>of</strong> these tags italicize your text; however, you should use the<br />

emphasis tag because it is standard.<br />

o Underline tag <br />

• You should limit your use <strong>of</strong> the underline tag , because <strong>of</strong> the<br />

use <strong>of</strong> underlining hyperlinks.<br />

Lists<br />

There are two kinds <strong>of</strong> lists.<br />

o Ordered List<br />

• This type <strong>of</strong> list is numbered.<br />

• To create this list you would use the ordered list tag.<br />

• You place the list items between the opening and closing<br />

tags.<br />

• Coded ordered list:<br />

<br />

a<br />

b<br />

c<br />

<br />

o Unordered List<br />

• This is a bulleted list.<br />

• To create this list you would use the unordered list tag.<br />

• You place the list items between the opening and closing<br />

tags.<br />

• Coded ordered list:<br />

<br />

a<br />

b<br />

c<br />

<br />

A list item, is the tag that creates the parts <strong>of</strong> your list. It is recommended that<br />

you use an opening and a closing list item tag when creating your lists.<br />

Good <strong>Coding</strong> Practice and Comments<br />

Some coding techniques make it easier to find and make changes in code. Basically<br />

these techniques provide easier readability.


Good coding practice example:<br />

<br />

<br />

<br />

My first page<br />

<br />

<br />

<br />

This is my first web page.<br />

<br />

<br />

You can use comments within your coding to add notes to yourself that will not<br />

appear on the web page.<br />

Comment syntax:<br />

o <br />

When to use comments:<br />

o “Comment out” code to see how the page will appear without a particular<br />

element.<br />

o Inform others about important parts <strong>of</strong> the code.<br />

o Remind yourself why you used a particular piece <strong>of</strong> coding.<br />

o Insert programming code like JavaScript.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!