Basic HTML
Loi Ngoc Nguyen, Duy-Ky Nguyen, PhDWe adopt XHTML requirements
- XHTML elements must be properly nested
- XHTML elements must always be closed, using <p />, <br />, <hr /> instead of pair
- XHTML elements must be in lowercase
- XHTML documents must have one root element
- !DOCTYPE declaration
Basic HTML Tags
Example 1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html><head><title></title></head> <body> Hello! This is my very first HTML file. </body> </html> |
and the output
Hello! This is my very first HTML file. |
The display is in only one single line, rather than in multi-line with al lot of blank spaces as seen above.
This is because there's no markup tags required by HTML
Note
We will omit the header (<!DOCTYPE ... boby>) and the footer (</body></html>) for clarityExample 2
Now we use the markup of line break <br /> to get multi-line displayHello!<br /> This is my very first HTML file. |
and the output
Hello! This is my very first HTML file |
Example 3
The markup of paragraph <p /> create more blank gap than the <br />Hello!<p /> This is my very first HTML file. |
and the output
Hello! This is my very first HTML file |
Example 4
The markup blank space (ended with semicolon) is used to get multiple blank spaceHello!<br /> This is my very first HTML file. |
and the output
Hello! This is my very first HTML file. |
Example 5
The markup horizontal ruler <hr /> is used to create a horizontal lineThe hr tag defines a horizontal rule: <hr /> This is 1st paragraph <hr /> This is 2nd paragraph <hr /> |
and the output
The hr tag defines a horizontal rule:
This is 1st paragraph This is 2nd paragraph |
Example 6
The comment <!--. . .
-->
is hidden
<!-- This is multi-line comment --> Comment line is hidden. |
and the output
Comment line is hidden. |
Example 6
Some HTML headers<h1>Header 1</h1><p /> <h2>Header 2</h2><p /> <h3>Header 3</h3><p /> <h4>Header 4</h4><p /> <h5>Header 5</h5><p /> |
and the output
Header 1Header 2Header 3Header 4Header 5 |
HTML Tag Attributes
Example 1Center the header
<h1 align="center">Header 1</h1><p/> The heading above is aligned to the center of this page. The heading above is aligned to the center of this page. The heading above is aligned to the center of this page.<p/> |
and the output
Header 1The heading above is aligned to the center of this page. The heading above is aligned to the center of this page. The heading above is aligned to the center of this page |
Example 2
Color the background
<body bgcolor="cyan"> <h1 align="center">Header 1</h1><p/> The heading above is aligned to the center of this page. The heading above is aligned to the center of this page. The heading above is aligned to the center of this page.<p/> </body> |
and the output
Header 1The heading above is aligned to the center of this page. The heading above is aligned to the center of this page. The heading above is aligned to the center of this page |
Note
The attribute value beige is quoted.HTML Text Formatting
Example 01<b>This text is bold</b><p/> <i>This text is italic</i><p/> This text contains <sub>subscript</sub><p/> This text contains <sup>superscript</sup> |
and the output
This text is bold This text is italic This text contains subscript This text contains superscript |
Example 2
Preformatted text preserves both spaces and line breaks, WYSWYG, ideal for display code
<pre> This is preformatted text. It preserves both spaces and line breaks. </pre> <p>The pre tag is good for displaying computer code:</p> <pre> for i = 1 to 10 print i next i </pre> |
and the output
This is The pre tag is good for displaying computer code: for i = 1 to 10 |
HTML Character Entities
Some characters have a special meaning in HTML, like the less than sign (<) that defines the start of an HTML tag. If we want the browser to actually display these characters we must insert character entities in the HTML source.
A character entity has three parts: an ampersand (&), an entity name or a # and an entity number, and finally a semicolon (;).
To display a less than sign in an HTML document we must write: < or <
The advantage of using a name instead of a number is that a name is easier to remember. The disadvantage is that not all browsers support the newest entity names, while the support for entity numbers is very good in almost all browsers.
Note that the entities are case sensitive and ended with semicolon ";"The Most Common Character Entities:
Result | Description | Entity Name | Entity Number |
---|---|---|---|
non-breaking space | |   | |
< | less than | < | < |
> | greater than | > | > |
& | ampersand | & | & |
" | quotation mark | " | " |
' | apostrophe | ' (does not work in IE) | ' |
Some Other Commonly Used Character Entities:
Result | Description | Entity Name | Entity Number |
---|---|---|---|
¢ | cent | ¢ | ¢ |
£ | pound | £ | £ |
¥ | yen | ¥ | ¥ |
€ | euro | € | € |
§ | section | § | § |
© | copyright | © | © |
® | registered trademark | ® | ® |
× | multiplication | × | × |
÷ | division | ÷ | ÷ |
HTML Links
HTML uses the <a href='...'> (anchor) tag to create a link to another document.
An anchor can point to any resource on the Web: an HTML page, an image, a sound file, a movie, etc.
Example 1=link to local file <a href='../dir/file.htm'><a href="apage.htm"> This text</a> is a link to a page on this Web site. <p/> <a href="http://www.microsoft.com/"> This text</a> is a link to a page on the World Wide Web. <p/> |
and the output
This text is a link to a page on this Web site. This text is a link to a page on the World Wide Web |
Example 2= link to part in same fileusing the pair<a href='#here>and <a id='here'/> placed at wehere to jum in the file
Using "#here" at the end of the link to jump to somewhere with <a href="#here"> within the page
<a href="http://www.w3schools.com/apage.htm#here"> Go here for explanation</a> |
and within that file, there's something like
<a href="#here"> Go here for explanation</a> |
Example 2
Tp jump to somewhere within the same file, just drop out the address
<a href="apage.htm#here"> Go here for explanation</a> |
HTML Lists
Unordered Lists
An unordered list is a list of items. The list items are marked with bullets (typically small black circles).
An unordered list starts with the <ul> tag. Each list item starts with the <li> tag.
Example 1
<ul> |
and the output
|
Example 2
Different type of unordered list
<h4>Disc bullets list:</h4> |
and the output
Disc bullets list:
Circle bullets list:
Square bullets list:
|
Ordered Lists
An ordered list is also a list of items. The list items are marked with numbers.
An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.
Example 3
<ol> |
and the output
|
Example 4
Different type of order list
<h4>Numbered list:</h4> |
and the output
Numbered list:
Letters list:
Lowercase letters list:
Roman numbers list:
Lowercase Roman numbers list:
|
Definition Lists
A definition list is not a list of items. This is a list of terms and explanation of the terms.
A definition list starts with the <dl> tag. Each definition-list term starts with the <dt> tag. Each definition-list definition starts with the <dd> tag.
Example 5
<dl> |
and the output
|
Example 6
Nested list
<h4>A nested List:</h4> |
and the output
A nested List:
|
HTML Tables
Nested tables are used intensively in HTML file to layout presentation. It's more powerful and more flexible than using HTML frame. Using HTML frame has limitation in some browser and unable going down to small area like table cell.Example 1
<table border="1"> |
and the output
|
Example 2
Using border attribute to do layout without border, drop out attribute or set to "0"
<!--table--> |
and the output
|
Example 3
Headings in a table with extra row
<table border="1"> |
and the output
Heading 1 | Heading 2 |
---|---|
row 1, cell 1 | row 1, cell 2 |
row 2, cell 1 | row 2, cell 2 |
Example 4
Empty cells in a table
<table border="1"> |
and the output
row 1, cell 1 | row 1, cell 2 |
row 2, cell 1 |
Note : The $nbsp; is used in case some browser display no border for an empty cell
Example 5
Table with caption
<h4> |
and the output
This table has a caption, and a thick border:
|
Example 6
Table cells that span more than one row/column using rowspan /colspan
<<h4>Cell that spans two columns:</h4> |
and the output
Cell that spans two columns:
Cell that spans two rows:
|
Example 7
Layout presentation using nested tables
<table border="1"> |
and the output
|
Example 8
Cell padding, to fill blank space around a cell
<h4>Without cellpadding:</h4> |
and the output
Without cellpadding:
With cellpadding:
|
Example 9
Cell spacing, to fill spcae around a cell
h4>Without cellspacing:</h4> |
and the output
Without cellspacing:
With cellspacing:
|
Example 10
Add background color or image to table
<h4>A background color:</h4> |
and the output
A background color:
A background image:
|
Example 11
Add background color or image to cell
<h4>Cell backgrounds:</h4> <table border="1"> <tr> <td bgcolor="red">First</td> <td>Row</td> </tr> <tr> <td background="src/bgdesert.jpg"> Second</td> <td>Row</td> </tr> </table> |
and the output
Cell backgrounds:
|
Example 12
Align content in table
<table width="400" border="1"> |
and the output
|
HTML Forms
Example 1Text and password type
<form> |
and the output
Note that when you type characters in a password field, the browser displays asterisks or bullets instead of the characters. |
Example 2
Radio button
<form> |
and the output
Male Female |
Example 3
Check box
<form> |
and the output
I have a bike: I have a car: I have an airplane: |
Example 4
Form action - Server Side Script run on Server, eg CGI, PHP, ...
<form name="input" action="html_form_action.cgi" method="get"> |
and the output
Username: |
If you type some characters in the text field above, and click the "Submit" button, you will send your input to a page called "html_form_action.asp". That page will show you the received input
Example 5
Form action - Client Side Script run on Client, eg JavaScript
<script type="text/javascript"> |
and the output
Username: |
If you type some characters in the text field above, and click the "Submit" button, you will send your input to a page called "html_form_action.asp". That page will show you the received input
Example 6
Drop down box
<form action=""> |
and the output
Example 7
Username and Passwd
<form> |
and the output
Username: Password: |
HTML Images
Example 1Insert image
An image: <img src="constr4.gif" width="144" height="50"><p/> A moving image:<img src="hackanm.gif" width="48" height="48"><p/> Note that the syntax of inserting a moving image is no different from that of a non-moving image.<p/> |
and the output
An image: A moving image: Note that the syntax of inserting a moving image is no different from that of a non-moving image. |
Example 2
Insert image from different location
An image from another folder: <img src="/images/netscape.gif" width="33" height="32"><p/> |
and the output
An image from another folder: An image from W3Schools: |
Example 3
Background image
<body background="background.jpg"> |
and the output