HTML Notes
HTML Notes
HTML
HTML is a language for describing web pages. HTML stands for Hyper Text Markup Language.
HTML is not a programming language, it is a markup language. A markup language is a set of
markup tags. The purpose of the tags are to describe page content.
HTML Tags
HTML markup tags are usually called HTML tags. HTML tags are keywords (tag names)
surrounded by angle brackets like <html>. HTML tags normally come in pairs like <b> and
</b>. The first tag in a pair is the start tag, the second tag is the end tag. The end tag is written
like the start tag, with a forward slash before the tag name. Start and end tags are also called
opening tags and closing tags. "HTML tags" and "HTML elements" are often used to describe
the same thing.
<tagname>content</tagname>
• HTML document begins and ends with HTML tag i.e. <HTML></HTML>
Here <HTML> indicates the browser that it is a HTML document and </HTML> tells the
browser that HTML document is completed.
• Header Tag i.e. <HEAD></HEAD>
Header Tag does not contain any text, it only contains the Title Tag in it.
• Title tag i.e. <TITLE></TITLE>
anything written between this tag is not displayed on the screen but it is used to identify the
Webpage.
• Body tag i.e.<BODY></BODY>
This is the main part of HTML document. The content which is to be displayed on screen as
webpage should be written here. Body Tag contains the text as well as various tags but only the
text will be displayed on Webpage.
A simple example
<html>
<head>
<title> My First WEB PAGE </title>
<body>
HTML is Hyper Text Markup Language
</body>
1
</html>
<!DOCTYPE html>
< html>
< head>
< title>Title of the document</title>
< /head>
< body>
The content of the document......
< /body>
< /html>
<head>
< base href="[Link] />
2
< base target="_blank" />
< /head>
Example
<a href="[Link] College</a>
Example
A named anchor inside an HTML document:
<a name="tips">Useful Tips Section</a>
Create a link to the "Useful Tips Section" inside the same document:
3
<a href="#tips">Visit the Useful Tips Section</a>
Or, create a link to the "Useful Tips Section" from another page:
<a href="[Link]
Visit the Useful Tips Section</a>
4
<html> <body>
<script type="text/javascript">
[Link]("Hello World!")
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
<p>A browser without support for JavaScript will show the text in the noscript element.</p>
</body> </html>
Header - <h?></h?>
HTML uses six levels of “heading level” tag pairs; the smaller the heading level number, the
larger is the print size:
A line space automatically is inserted before and after a heading (that is, an entire line is skipped
between a heading and any text before and after it).
No matter what the font size is, headings with the same level number are always the same size.
Headings are always in bold print.
The <H#> tag may contain within it an alignment parameter (recognized by most browsers) as
part of the command:
"LEFT" (the default) aligns the heading with the left margin of the page.
"CENTER" centers the heading horizontally on the page.
"RIGHT" aligns the heading with the right margin of the page.
5
In most cases, it is preferable for a heading to be centered horizontally on the page. However,
since some browsers so not recognize the ALIGN parameter for headings, it is a good idea to
center a heading by using <CENTER>&</CENTER> tags.
Bold Text
Anything that appears within <b>...</b> element, is displayed in bold as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Bold Text Example</title>
</head>
<body>
<p>The following word uses a <b>bold</b> typeface.</p>
</body>
</html>
Italic Text
Anything that appears within <i>...</i> element is displayed in italicized as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Italic Text Example</title>
</head>
<body>
<p>The following word uses a <i>italicized</i> typeface.</p>
</body>
</html>
Underlined Text
Anything that appears within <u>...</u> element, is displayed with underline as shown below:
6
Example
<!DOCTYPE html>
<html>
<head>
<title>Underlined Text Example</title>
</head>
<body>
<p>The following word uses a <u>underlined</u> typeface.</p>
</body>
</html>
Strike Text
Anything that appears within <strike>...</strike> element is displayed with strikethrough, which
is a thin line through the text as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Strike Text Example</title>
</head>
<body>
<p>The following word uses a <strike>strikethrough</strike> typeface.</p>
</body>
</html>
Monospaced Font
The content of a <tt>...</tt> element is written in monospaced font. Most of the fonts are known
as variable-width fonts because different letters are of different widths (for example, the letter 'm'
is wider than the letter 'i'). In a monospaced font, however, each letter has the same width.
Example
<!DOCTYPE html>
<html>
<head>
<title>Monospaced Font Example</title>
</head>
<body>
<p>The following word uses a <tt>monospaced</tt> typeface.</p>
</body>
7
</html>
Superscript Text
The content of a <sup>...</sup> element is written in superscript; the font size used is the same
size as the characters surrounding it but is displayed half a character's height above the other
characters.
Example
<!DOCTYPE html>
<html>
<head>
<title>Superscript Text Example</title>
</head>
<body>
<p>The following word uses a <sup>superscript</sup> typeface.</p>
</body>
</html>
Subscript Text
The content of a <sub>...</sub> element is written in subscript; the font size used is the same as
the characters surrounding it, but is displayed half a character's height beneath the other
characters.
Example
<!DOCTYPE html>
<html>
<head>
<title>Subscript Text Example</title>
</head>
<body>
<p>The following word uses a <sub>subscript</sub> typeface.</p>
</body>
</html>
8
Inserted Text
Example
<!DOCTYPE html>
<html>
<head>
<title>Inserted Text Example</title>
</head>
<body>
<p>I want to drink <del>cola</del><ins>wine</ins></p>
</body>
</html>
Deleted Text
Example
<!DOCTYPE html>
<html>
<head>
<title>Deleted Text Example</title>
</head>
<body>
<p>I want to drink <del>cola</del><ins>wine</ins></p>
</body>
</html>
Larger Text
The content of the <big>...</big> element is displayed one font size larger than the rest of the
text surrounding it as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Larger Text Example</title>
</head>
<body>
9
<p>The following word uses a <big>big</big> typeface.</p>
</body>
</html>
Smaller Text
The content of the <small>...</small> element is displayed one font size smaller than the rest of
the text surrounding it as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Smaller Text Example</title>
</head>
<body>
<p>The following word uses a <small>small</small> typeface.</p>
</body>
</html>
Grouping Content
The <div> and <span> elements allow you to group together several elements to create sections
or subsections of a page.
For example, you might want to put all of the footnotes on a page within a <div> element to
indicate that all of the elements within that <div> element relate to the footnotes. You might then
attach a style to this <div> element so that they appear using a special set of style rules.
Example
<!DOCTYPE html>
<html>
<head>
<title>Div Tag Example</title>
</head>
<body>
<divid="menu"align="middle">
<ahref="/[Link]">HOME</a> |
<ahref="/about/contact_us.htm">CONTACT</a> |
<ahref="/about/[Link]">ABOUT</a>
</div>
<divid="content"align="left"bgcolor="white">
<h5>Content Articles</h5>
10
<p>Actual content goes here.....</p>
</div>
</body>
</html>
The <span> element, on the other hand, can be used to group inline elements only. So, if you
have a part of a sentence or paragraph which you want to group together, you could use the
<span> element as follows
Example
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
Font
The <font> tag provides no real functionality by itself, but with the help of a few attributes, this
tag is used to change the style, size, and color of HTML text elements. The size, color, and face
attributes can be used all at once or individually, providing users with the ability to create
dynamic font styles for any HTML element.
Font Size
Set the size of your font with size. The range of accepted values goes from 1 -- the smallest, to 7
-- the largest. The default size of a font is 3.
Font Face
Choose a different font face by specifying any font you have installed. Font face is synonymous
with font type. As a web designer, be aware that if you specify a custom font type and users
viewing the page don't have the exact same font installed, they will not be able to see it. Instead
the chosen font will default to Times New Roman. To reduce the risk of running into this
situation, you may provide a list of several fonts with the face attribute, such as outlined below.
Centre - <center></center>
A useful tag, as it says, it makes everything in between the tags centered (in the middle
of the page).
Emphasis - <em></em>
Used to emphasize text, which usually appears in italics, but can vary according to your
browser.
Colors
Colors are displayed combining RED, GREEN, and BLUE light.
Color Values
12
HTML colors are defined using a hexadecimal notation (HEX) for the combination of Red, Green, and
Blue color values (RGB).
The lowest value that can be given to one of the light sources is 0 (in HEX: 00). The highest value is
255 (in HEX: FF).
HEX values are specified as 3 pairs of two-digit numbers, starting with a # sign. 147 color names are
defined in the HTML and CSS color specification (16 basic color names plus 130 more). The table
below lists basic colors, along with their hexadecimal values. The 16 basic color names are: aqua,
black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow.
Color Values
Color Name Color Hex Color RGB
Black #000000 rgb(0,0,0)
Yellow #FFFF00 rgb(255,255,0)
Red #FF0000 rgb(255,0,0)
Maroon #800000 rgb(128,0,0)
Gray #808080 rgb(128,128,128)
Lime #00FF00 rgb(0,255,0)
Green #008000 rgb(0,128,0)
Olive #808000 rgb(128,128,0)
Silver #C0C0C0 rgb(192,192,192)
Aqua #00FFFF rgb(0,255,255)
Blue #0000FF rgb(0,0,255)
Navy #000080 rgb(0,0,128)
White #FFFFFF rgb(255,255,255)
Fuchsia #FF00FF rgb(255,0,255)
Purple #800080 rgb(128,0,128)
Teal #008080 rgb(0,128,128)
The combination of Red, Green, and Blue values from 0 to 255, gives more than 16 million different
colors (256 x 256 x 256).
The start attribute accepts only integer values, even though ordered lists may use different
numbering systems, such as roman numerals.
<ol start="30">
<li>Head north </li>
<li>Turn right </li>
<li>Turn left </li>
</ol>
Output:
30. Head north
31. Turn right
32. Turn left
14
The reversed attribute, when used on the <ol> element, allows a list to appear in reverse order.
An ordered list of five items numbered 1 to 5 may be reversed and ordered from 5 to 1.
The value attribute may be used on an individual <li> element within an ordered list to change
its value within the list. The number of any list item appearing below a list item with a value
attribute will be recalculated accordingly.
A type attribute can be added to an ordered list, to define the type of the marker:Type
Description
type="1" The list items will be numbered with numbers (default)
type="A" The list items will be numbered with uppercase letters
type="a" The list items will be numbered with lowercase letters
type="I" The list items will be numbered with uppercase roman numbers
type="i" The list items will be numbered with lowercase roman numbers
For example:
Upper case List:
<ol type="A">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Output:
This is a definition term
And this is the definition
15
term 2
definition 2
term 3
definition 3
Use definition lists anywhere you have a list that has two parts to each item. The most common
use is with a glossary of terms, but you can also use it for an address book (name is the term
and the address is the definition), or an HTML form (field name is the term, input box is the
definition), or for any list of name/value pairs.
4.3.3 Links
Links are found in nearly all web pages. Links allow users to click their way from page to page.
HTML links are hyperlinks.
A hyperlink is an element, a text, or an image that you can click on, and jump to another
document.
In HTML, links are defined with the <a> tag:
<a href="url">link text</a>
Example:
<a href="[Link] our HTML tutorial</a>
The href attribute specifies the destination address ([Link]
The link text is the visible part (Visit our HTML tutorial). Clicking on the link text, will send you to
the specified address.
When you move the mouse cursor over a link, two things will normally happen, the mouse
arrow will turn into a little hand and the color of the link element will change.
The id attribute can be used to create bookmarks inside HTML documents. Bookmarks are not
displayed in any special way. They are invisible to the reader. These bookmarks can be used to
link the hyperlink to specific content within the same page.
Add an id attribute to any <a> element:
<a id="tips">Useful Tips Section</a>
Then create a link to the <a> element (Useful Tips Section):
<a href="#tips">Visit the Useful Tips Section</a>
Or, create a link to the <a> element (Useful Tips Section) from another page:
<a href="[Link] the Useful Tips Section</a>
4.3.4 Images
The <img> tag defines an image in an HTML page. The <img> tag has two required attributes:
src and alt. Note that Images are not technically inserted into an HTML page, images are linked
to HTML pages. The <img> tag creates a holding space for the referenced image. Also, to link an
image to another document, simply nest the <img> tag inside <a> tags.
16
Example: How to insert an image:
<imgsrc="[Link]" alt="Smiley face" height="42" width="42">
4.3.5 Sounds
Using Plug-ins
A browser Plug-in is a small computer program that extends the standard functionality of the
browser. Plug-ins can be used for many purposes: play music, display maps, verify your bank id,
control your input, and much more. Plug-ins can be added to HTML pages using the <object>
tag or the <embed> tag. These tags defines containers for resources (normally non-HTML
resources), which, depending on the type, will either be displayed by the browsers, or by an
external plug-in.
The following code fragment can display an MP3 file embedded in a web page:
<!DOCTYPE html>
<html> <body>
<embed height="100" width="100" src="horse.mp3"></embed>
17
<p>If you cannot hear the sound, your computer or browser doesn't support the
sound format.</p>
<p>Or, you have your speakers turned off.</p>
</body> </html>
<!DOCTYPE html>
<html>
<body>
<object height="100" width="100" data="horse.mp3"></object>
<p>If you cannot hear the sound, your computer or browser doesn't support the
sound format.</p>
<p>Or, you have your speakers turned off.</p>
</body>
</html>
18
4.3.6 Videos
Videos can be played in HTML by many different methods.
<!DOCTYPE html>
<html>
<body>
<h2>Playing the Object</h2>
<embed src="C:\Users\Public\Videos\Sample Videos\[Link]" width="200"
height="200"><p>If you cannot see this, your computer doesn't support the
format</p>
</body>
</html>
19
The <embed> Tag's Attributes
Attribute Description Possible Values
<!DOCTYPE html>
<html>
<body>
<object height="400" width="400" data="C:\Users\Public\Videos\Sample
Videos\[Link]""> </object>
</body>
</html>
Video Formats
Some of the more common video formats are:
20
• .swf - Shockwave/Flash File (by Macromedia/Adobe)
• .wmv - Windows Media Player video format (by Microsoft)
• .mov - Quicktime video format (by Apple)
• .mpeg - Video compression format (specified by the Moving Pictures Experts Group)
• .ogg - Open standard container format maintained by the [Link] Foundation. Also includes
.ogv, .oga, .ogx, and .spx extensions
• .webM - royalty-free, high-quality open video compression format for use with HTML5 video.
4.3.7 Background
The <body> tag defines the document's body. The <body> element contains all the contents of
an HTML document, such as text, hyperlinks, images, tables, lists, etc.
Attributes of <body> tag:
Attribute Value Description
alink color Specifies the color of an active link in a document
background URL Specifies a background image for a document
rgb(x,x,x)
bgcolor Specifies the background color of a document
#xxxxxxcolorname
link color Specifies the color of unvisited links in a document
text color Specifies the color of the text in a document
vlink color Specifies the color of visited links in a document
21
4.3.8 Tables
The <table> tag defines an HTML [Link] HTML table consists of the <table> element and one
or more <tr>, <th>, and <td> elements. The <tr> element defines a table row, the <th> element
defines a table header, and the <td> element defines a table cell.
Attributes:
Example:
A simple HTML table, containing two columns and two rows:
<table>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
</table>
To make a cell span more than one column, use the colspan attribute:
22
Example:
<table border=1>
<tr>
<th>Name</th>
<thcolspan="2">Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>
Output:
Name Telephone
To make a cell span more than one row, use the rowspan attribute:
Example:
<table border=1>
<tr>
<th>Name:</th>
<td>Bill Gates</td>
</tr>
<tr>
<throwspan="2">Telephone:</th>
<td>555 77 854</td>
</tr>
<tr>
<td>555 77 855</td>
</tr>
</table>
Output:
Name: Bill Gates
555 77 854
Telephone:
555 77 855
23
4.3.9 Forms and Input
HTML Forms are used to select different kinds of user input.
HTML Forms
HTML forms are used to pass data to a server. A form can contain input elements like text fields,
checkboxes, radio-buttons, submit buttons and more. A form can also contain select lists, textarea,
fieldset, legend, and label elements.
<form>
.
input elements
.
< /form>
Text Fields
<input type="text" /> defines a one-line input field that a user can enter text into:
<form>
First name:< input type="text" name="firstname" /><br />
Last name:< input type="text" name="lastname" />
< /form>
First name:
Last name:
Note: The form itself is not visible. Also note that the default width of a text field is 20 characters.
Password Field
<input type="password" /> defines a password field:
<form>
Password:< input type="password" name="pwd" />
< /form>
Password:
24
Note: The characters in a password field are masked (shown as asterisks or circles).
Radio Buttons
<input type="radio" /> defines a radio button. Radio buttons let a user select ONLY ONE of a limited
number of choices:
<form>
< input type="radio" name="sex" value="male" /> Male<br />
< input type="radio" name="sex" value="female" /> Female
< /form>
Male
Female
Checkboxes
<input type="checkbox" /> defines a checkbox. Checkboxes let a user select ZERO or MORE options
of a limited number of choices.
<form>
< input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
< input type="checkbox" name="vehicle" value="Car" /> I have a car
< /form>
I have a bike
I have a car
Submit Button
<input type="submit" /> defines a submit button.
A submit button is used to send form data to a server. The data is sent to the page specified in the
form's action attribute. The file defined in the action attribute usually does something with the
received input:
Submit
Username:
25
If you type some characters in the text field above, and click the "Submit" button, the browser will
send your input to a page called "html_form_action.asp". The page will show you the received input.
HTML frames allow you to split the whole window up into different frames. For example, you
can have a frame to display your left menu, another frame to display the top menu, and another frame
to display the main content area. With frames, your HTML document can be made up of many different
pages.
Frames are achieved by creating a frameset page, and defining each frame from within that
page. This frameset page doesn't actually contain any content - just a reference to each frame. The
HTML <frame> tag is used to specify each frame within the frameset. All frame tags are nested with a
<frameset> tag.
So, in other words, if you want to create a web page with 2 frames, you would need to create 3
files - 1 file for each frame, and 1 file to specify how they fit together.
Creating Frames
HTML Code:
<html>
<head>
<title>Frameset page<title>
</head>
<frameset cols = "25%, *">
<frame src ="frame_example_left.html" />
<frame src ="frame_example_right.html" />
</frameset>
</html>
26
The left frame (frame_example_left.html):
<html>
<body style="background-color:green">
<p>This is the left frame (frame_example_left.html).</p>
</body>
</html>
<html>
<body style="background-color:yellow">
<p>This is the right frame (frame_example_right.html).</p>
</body>
</html>
Standard Attributes
The <frame> tag supports the following standard attributes in HTML 4.01:
Optional Attributes
Attribute Value Description
27
4.4 XHTML
XHTML is HTML written as XML.
XHTML means:
• XHTML stands for EXtensibleHyperText Markup Language
• XHTML is almost identical to HTML
• XHTML is stricter than HTML
• XHTML is HTML defined as an XML application
• XHTML is supported by all major browsers
Today's market consists of different browser technologies. Some browsers run on computers,
and some browsers run on mobile phones or other small devices. Smaller devices often lack the
resources or power to interpret "bad" markup.
XML is a markup language where documents must be marked up correctly (be "well-
formed").By combining the strengths of HTML and XML, XHTML was [Link] is HTML
redesigned as XML.
The Most Important Differences from HTML:
❖ Document Structure
• XHTML DOCTYPE is mandatory
• The xmlns attribute in <html> is mandatory
• <html>, <head>, <title>, and <body> are mandatory
❖ XHTML Elements
• XHTML elements must be properly nested
• XHTML elements must always be closed
• XHTML elements must be in lowercase
• XHTML documents must have one root element
❖ XHTML Attributes
• Attribute names must be in lower case
• Attribute values must be quoted
• Attribute minimization is forbidden
❖ <!DOCTYPE ....> Is Mandatory
An XHTML document must have an XHTML DOCTYPE [Link] <html>, <head>,
<title>, and <body> elements must also be present, and the xmlns attribute in <html>
must specify the xml namespace for the document.
This example shows an XHTML document with a minimum of required tags:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"[Link]
<html xmlns="[Link]
<head>
<title>Title of document</title>
</head>
<body>
some content
28
</body>
</html>
❖ XHTML Elements Must Be Properly Nested
In HTML, some elements can be improperly nested within each other, like this:
<b><i>This text is bold and italic</b></i>
In XHTML, all elements must be properly nested within each other, like this:
<b><i>This text is bold and italic</i></b>
❖ XHTML Elements Must Always Be Closed
This is wrong:
<p>This is a paragraph
<p>This is another paragraph
This is correct:
<p>This is a paragraph</p>
<p>This is another paragraph</p>
❖ Empty Elements Must Also Be Closed
This is wrong:
A break: <br>
A horizontal rule: <hr>
An image: <imgsrc="[Link]" alt="Happy face">
This is correct:
A break: <br />
A horizontal rule: <hr />
An image: <imgsrc="[Link]" alt="Happy face" />
❖ XHTML Elements Must Be In Lower Case
This is wrong:
<BODY>
<P>This is a paragraph</P>
</BODY>
This is correct:
<body>
<p>This is a paragraph</p>
</body>
❖ XHTML Attribute Names Must Be In Lower Case
This is wrong:
<table WIDTH="100%">
This is correct:
<table width="100%">
29
❖ Attribute Minimization Is Forbidden
Wrong:
<input type="checkbox" name="vehicle" value="car" checked
/>
Correct:
<input type="checkbox" name="vehicle" value="car"
checked="checked" />
Wrong:
<input type="text" name="lastname" disabled />
Correct:
<input type="text" name="lastname" disabled="disabled"
/>
When thinking of dynamic HTML, you need to remember the qualities of standard HTML,
especially that once a page is loaded from the server, it will not change until another
request comes to the server. Dynamic HTML give you more control over the HTML elements
and allows them to change at any time, without returning to the Web server.
Features of DHTML
30
Changing the tags and properties
This is one of the most common uses of DHTML. It allows you to change the qualities of an
HTML tag depending on an event outside of the browser (such as a mouse click, time, or
date, and so on). You can use this to preload information onto a page, and not display it
unless the reader clicks on a specific link.
Real-time postioning
When most people think of DHTML this is what they expect. Objects, images, and text
moving around the Web page. This can allow you to play interactive games with your
readers or animate portions of your screen.
Dynamic Fonts
This is a Netscape only feature. Netscape developed this to get around the problem
designers had with not knowing what fonts would be on a reader's system. With dynamic
fonts, the fonts are encoded and downloaded with the page, so that the page always looks
how the designer intended it to.
Data binding
This is an IE only feature. Microsoft developed this to allow easier access to databases from
Web sites. It is very similar to using a CGI to access a database, but uses an ActiveX control
to function. This feature is very advanced and difficult to use for the beginning DHTML
writer.
CSS is used in DHTML to control the look and feel of the Web page. Style sheets define the
colors and fonts of text, the background colors and images, and the placement of objects on
the page. Using scripting and the DOM, you can change the style of various elements.
CSS Syntax
A CSS rule has two main parts: a selector, and one or more declarations:
The property is the style attribute you want to change. Each property has a value.
CSS Example
A CSS declaration always ends with a semicolon, and declaration groups are surrounded by
curly brackets:
31
p {color:red;text-align:center;}
To make the CSS more readable, you can put one declaration on each line, like this:
Example
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
p
{
color:red;
text-align:center;
}
</style>
</head>
<body>
<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
</body>
</html>
CSS Comments
Comments are used to explain your code, and may help you when you edit the source code
at a later date. Comments are ignored by browsers.
A CSS comment begins with "/*", and ends with "*/", like this:
/*This is a comment*/
p
{
text-align:center;
/*This is another comment*/
color:black;
font-family:arial;
}
The id Selector
The id selector is used to specify a style for a single, unique element.
The id selector uses the id attribute of the HTML element, and is defined with a "#".
32
The style rule below will be applied to the element with id="para1":
Example:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#para1
{
text-align:center;
color:red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
This allows you to set a particular style for many HTML elements with the same class.
The class selector uses the HTML class attribute, and is defined with a "."
In the example below, all HTML elements with class="center" will be center-aligned:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.center
{
text-align:center;
}
</style>
</head>
<body>
<h1 class="center">Center-aligned heading</h1>
<p class="center">Center-aligned paragraph.</p>
</body>
</html>
You can also specify that only specific HTML elements should be affected by a class.
33
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
[Link]
{
text-align:center;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be center-aligned.</p>
</body>
</html>
< head>
< link rel="stylesheet" type="text/css" href="[Link]" />
< /head>
An external style sheet can be written in any text editor. The file should not contain any
html tags. Your style sheet should be saved with a .css extension. An example of a style
sheet file is shown below:
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/[Link]");}
34
< head>
< style type="text/css">
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/[Link]");}
< /style>
< /head>
To use inline styles you use the style attribute in the relevant tag. The style attribute can
contain any CSS property. The example shows how to change the color and the left margin
of a paragraph:
Attribute definitions
In CSS, property declarations have the form "name : value" and are separated by a
semi-colon.
CSS properties:
Font Properties
font-family, font-style, font-variant, font-weight, font-size and font
Color and Background Properties
Color, background- color, background-image, background- repeat, background-
attachment, background- position andbackground
Text Properties
word-spacing, letter-spacing, text-decoration, vertical- alignment, text-
transformation
text-alignment, text-indentation and line-height,
Box Properties
top-margin, right- margin, bottom-margin, left-margin, margin, top- padding,
right- padding, bottom-padding, left-padding, padding, top- border-width, right-
border- width, bottom-border-width, left border width, border- width, border-
color, border-style, top-border, right- border, bottom- border, left-border,
border, width, height, float and clear
Classification Properties
Display, whitespace, list-style- type, list-style-image, list-style-positionand list-
style,
35
4.5.6 User style sheet
User style sheets allow you to have more control over the Web pages you are visiting. They allow you
to define styles for usability as well as for your own comfort. To use a user style sheet in Internet
Explorer for Windows.
1. Create a style sheet and save it on a drive that can be seen by the user.
4. Click the check box for "Format documents using my style sheet".
5. Browse to your style sheet and choose it. Then click "OK" twice to get out of the Internet
Options menu.
6. Close your browser and re-open it. Your styles will be applied to all pages you visit after that.
Elements can be positioned using the top, bottom, left, and right properties. However, these
properties will not work unless the position property is set first. They also work differently depending
on the positioning method.
Static Positioning
HTML elements are positioned static by default. A static positioned element is always positioned
according to the normal flow of the [Link] positioned elements are not affected by the top,
bottom, left, and right properties.
Fixed Positioning
An element with fixed position is positioned relative to the browser [Link] will not move even if the
window is scrolled:
Note: IE7 and IE8 support the fixed value only if a !DOCTYPE is specified.
Fixed positioned elements are removed from the normal flow. The document and other elements
behave like the fixed positioned element does not [Link] positioned elements can overlap other
elements.
Relative Positioning
A relative positioned element is positioned relative to its normal position.
<!DOCTYPE html>
<html>
<head>
<style>
h2.pos_left
{
position:relative;
left:-20px;
}
h2.pos_right
37
{
position:relative;
left:20px;
}
</style>
</head>
<body>
<h2>This is a heading with no position</h2>
<h2 class="pos_left">This heading is moved left according to its normal position</h2>
<h2 class="pos_right">This heading is moved right according to its normal position</h2>
<p>Relative positioning moves an element RELATIVE to its original position.</p>
<p>The style "left:-20px" subtracts 20 pixels from the element's original left position.</p>
<p>The style "left:20px" adds 20 pixels to the element's original left position.</p>
</body>
</html>
The content of relatively positioned elements can be moved and overlap other elements, but the
reserved space for the element is still preserved in the normal flow.
Absolute Positioning
An absolute position element is positioned relative to the first parent element that has a position other
than static. If no such element is found, the containing block is <html>:
<!DOCTYPE html>
<html>
<head>
<style>
h2
{
position:absolute;
left:100px;
top:150px;
38
}
</style>
</head>
<body>
<h2>This is a heading with an absolute position</h2>
<p>With absolute positioning, an element can be placed anywhere on a page. The heading below is
placed 100px from the left of the page and 150px from the top of the page.</p>
</body>
</html>
Absolutely positioned elements are removed from the normal flow. The document and other elements
behave like the absolutely positioned element does not [Link] positioned elements can
overlap other elements.
Overlapping Elements
When elements are positioned outside the normal flow, they can overlap other [Link] z-index
property specifies the stack order of an element (which element should be placed in front of, or
behind, the others).An element can have a positive or negative stack order:
<!DOCTYPE html>
<html>
<head>
<style>
img
{
position:absolute;
left:0px;
top:0px;
39
z-index:-1;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<imgsrc="C:\Users\Public\Pictures\Sample Pictures\[Link]" width="100" height="140" />
<p>Because the image has a z-index of -1, it will be placed behind the text.</p>
</body>
</html>
An element with greater stack order is always in front of an element with a lower stack order.
Note: If two positioned elements overlap, without a z-index specified, the element positioned last in
the HTML code will be shown on top.
DHTML is not a technology in and of itself; rather, it is the product of three related and
complementary technologies: HTML, Cascading Style Sheets (CSS), and JavaScript. To
allow scripts and components to access features of HTML and CSS, the contents of the
40
document are represented as objects in a programming model known as the Document
Object Model (DOM).
With the DOM, all nodes in the tree can be accessed by JavaScript. All HTML elements (nodes) can be
modified, and nodes can be created or deleted.
The terms parent, child, and sibling are used to describe the relationships. Parent nodes have children.
Children on the same level are called siblings (brothers or sisters).
The following image illustrates a part of the node tree and the relationship between the nodes:
41
Look at the following HTML fragment:
<html>
<head>
<title>DOM Tutorial</title>
</head>
<body>
<h1>DOM Lesson one</h1>
<p>Hello world!</p>
</body>
< /html>
and:
• The <html> node has two child nodes: <head> and <body>
• The <head> node has one child node: the <title> node
• The <title> node also has one child node: the text node "DOM Tutorial"
• The <h1> and <p> nodes are siblings and child nodes of <body>
and:
DOM Methods
Methods are actions you can perform on nodes (HTML Elements)
42
Programming Interface
The DOM can be accessed with JavaScript (and other programming languages).
All HTML elements are defined as objects, and the programming interface is the object methods and
object properties.
A property is a value that you can get or set (like the name or content of a node).
Example:
<!DOCTYPE html>
<html>
<body>
<script>
x=[Link]("intro");
[Link]("<p>The text from the intro paragraph: " + [Link] + "</p>");
</script>
</body>
</html>
Output:
Hello World!
43
DOM Properties
Properties are values of nodes (HTML Elements) that you can get or set
The innerHTML property is useful for getting or replacing the content of HTML elements.
Example
The following code gets the innerHTML from the <p> element with id="intro":
Example
<html>
< body>
< p id="intro">Hello World!</p>
< script>
var txt=[Link]("intro").innerHTML;
[Link](txt);
< /script>
< /body>
< /html>
4.6 XML
Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding
documents in a format which is both human-readable and machine-readable.
• XML stands for EXtensible Markup Language.
• XML was designed to describe data.
• XML is a software- and hardware-independent tool for carrying information.
• XML is easy to learn.
• XML is a markup language much like HTML
• XML was designed to describe data, not to display data
• XML tags are not predefined. You must define your own tags
• XML is designed to be self-descriptive
• XML is a W3C Recommendation
XML Document Example:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
44
</note>
XML Advantages
XML is used in many aspects of web development, often to simplify data storage and sharing.
• XML Separates Data from HTML
If you need to display dynamic data in your HTML document, it will take a lot of work to
edit the HTML each time the data [Link] XML, data can be stored in separate
XML files. This way you can concentrate on using HTML/CSS for display and layout, and
be sure that changes in the underlying data will not require any changes to the
[Link] a few lines of JavaScript code, you can read an external XML file and update
the data content of your web page.
• XML Simplifies Data Sharing
In the real world, computer systems and databases contain data in incompatible
formats.
XML data is stored in plain text format. This provides a software- and hardware-
independent way of storing [Link] makes it much easier to create data that can be
shared by different applications.
• XML Simplifies Data Transport
One of the most time-consuming challenges for developers is to exchange data between
incompatible systems over the [Link] data as XML greatly reduces this
complexity, since the data can be read by different incompatible applications.
45
<body>Don't forget me this weekend!</body>
</note>
The first line is the XML declaration. It defines the XML version (1.0).The next line describes the
root element of the document (like saying: "this document is a note"):<note>
The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
And finally the last line defines the end of the root element:</note>
You can assume, from this example, that the XML document contains a note to Tove from Jani.
XML Documents Form a Tree Structure. XML documents must contain a root element. This
element is "the parent" of all other [Link] elements in an XML document form a
document tree. The tree starts at the root and branches to the lowest level of the [Link]
elements can have sub elements (child elements):
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
46
<width>80</width>
<length>120</length>
</table>
If these XML fragments were added together, there would be a name conflict. Both contain a
<table> element, but the elements have different content and meaning.A user or an XML
application will not know how to handle these [Link] conflicts in XML can easily be
avoided using a name prefix.
This XML carries information about an HTML table, and a piece of furniture:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two <table> elements have
different names.
47
The content of the file is shown below:
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
The purpose of a DTD is to define the structure of an XML document. It defines the structure
with a list of legal elements. With a DTD, your XML files can carry a description of its own
format, independent groups of people can agree on a standard for interchanging [Link] , you
can verify that the data you receive from the outside world is valid.
48
The Schema above is interpreted like this:
• <xs:element name="note"> defines the element called "note"
• <xs:complexType> the "note" element is a complex type
• <xs:sequence> the complex type is a sequence of elements
• <xs:element name="to" type="xs:string"> the element "to" is of type string (text)
• <xs:element name="from" type="xs:string"> the element "from" is of type string
• <xs:element name="heading" type="xs:string"> the element "heading" is of type string
• <xs:element name="body" type="xs:string"> the element "body" is of type string
XML Schemas are More Powerful than DTD. XML Schemas are written in XML, are extensible to
additions, support data types and namespaces.
Syntax:
<xsl:variable name=”name” select=”expression”>
<!--conten:template -- >
</xsl:variable>
Attributes
Attribute Value Description
name name required. Specifies the name of the [Link] expression Optional. Defines the value
of the variable.
Example 1
If the select attribute is present, the <xsl:variable> element cannot contain any content. If the select
attribute contains a literal string, the string, must be within quotes. The following example assigns the
value “red” to the variable “color”:
Example 2
If the <xsl:variable> element only contains a name attribute , and there is no content, then the value
of the variable is an empty string:
49
Example 3
The following example adds a value to the variable “header” by the content of the <xsl: variable>
element:
1. XML-based formats have become the default for many office-productivity tools, including
Microsoft Office (Office Open XML), [Link] and LibreOffice (Open Document), and
Apple's iWork.
2. XML has also been employed as the base language for communication protocols.
3. Applications for the Microsoft .NET Framework use XML files for configuration.
4. XML has come into common use for the interchange of data over the Internet.
50
4.6.5 DTD
A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines
the document structure with a list of legal elements and attributes.
• !DOCTYPE note defines that the root element of this document is note
• !ELEMENT note defines that the note element contains four elements:
"to,from,heading,body"
• !ELEMENT to defines the to element to be of type "#PCDATA"
• !ELEMENT from defines the from element to be of type "#PCDATA"
• !ELEMENT heading defines the heading element to be of type "#PCDATA"
• !ELEMENT body defines the body element to be of type "#PCDATA"
This is the same XML document as above, but with an external DTD (Open it, and select view source):
51
< heading>Reminder</heading>
< body>Don't forget me this weekend!</body>
< /note>
DTD – Elements
In a DTD, elements are declared with an ELEMENT declaration.
Declaring Elements
In a DTD, XML elements are declared with an element declaration with the following syntax:
Empty Elements
Empty elements are declared with the category keyword EMPTY:
Example:
XML example:
<br />
Example:
< !ELEMENT from (#PCDATA)>
Example:
52
< !ELEMENT note ANY>
Example:
When children are declared in a sequence separated by commas, the children must appear in the
same sequence in the document. In a full declaration, the children must also be declared, and the
children can also have children. The full declaration of the "note" element is:
Example:
The example above declares that the child element "message" must occur once, and only once inside
the "note" element.
Example:
The + sign in the example above declares that the child element "message" must occur one or more
times inside the "note" element.
Example:
The * sign in the example above declares that the child element "message" can occur zero or more
times inside the "note" element.
53
Declaring Zero or One Occurrences of an Element
< !ELEMENT element-name (child-name?)>
Example:
The ?sign in the example above declares that the child element "message" can occur zero or one time
inside the "note" element.
The example above declares that the "note" element must contain a "to" element, a "from" element, a
"header" element, and either a "message" or a "body" element.
The example above declares that the "note" element can contain zero or more occurrences of parsed
character data, "to", "from", "header", or "message" elements.
54