Chapter 2
Chapter 2
Hyper Text Markup Language (HTML) is the language used to write Web documents.
rules (grammar) and codes (words and phrases) necessary for the creation of a usable
document
o It uses standard ASCII characters and contains formatting codes, called commands or tags,
describe the structure of a document, provide font and graphics information and contain
hyperlinks to other web pages and internet resources.
HTML: A Structured Language
HTML is not a programming language, it is a markup language, which is used to design web
pages.
A markup language is a set of markup tags
A markup language such as HTML is simply a collection of codes (elements in this case) that
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.
Eg. <tagname>content</tagname>
HTML Elements
"HTML tags" and "HTML elements" are often used to describe the same thing.
HTML element is everything between the start tag and the end tag, including the tags:
The markup tags tell the web browser how to display the page.
<body>
<p>This is my first paragraph.</p>
</body>
</html>
The example above contains 3 HTML elements.
HTML Example Explained
The <p> element:
<p>This is my first paragraph.</p>
The <p> element defines a paragraph in the HTML document.
The element has a start tag <p> and an end tag </p>.
The element content is: This is my first paragraph.
The <body> element:
<body>
<p>This is my first paragraph.</p>
</body>
The <body> element defines the body of the HTML document.
The element has a start tag <body> and an end tag </body>.
The element content is another HTML element (a p element).
The <html> element:
<html>
<body>
<p>This is my first paragraph.</p>
</body>
</html>
The <html> element defines the whole HTML document.
The element has a start tag <html> and an end tag </html>.
The element content is another HTML element (the body element).
Don't Forget the End Tag
Some HTML elements might display correctly even if you forget the end tag:
<p>This is a paragraph
<p>This is a paragraph
The example above works in most browsers, because the closing tag is considered
optional.
Never rely on this. Many HTML elements will produce unexpected results and/or errors
if you forget the end tag .
Empty HTML Elements
<br> is an empty element without a closing tag (the <br> tag defines a line break).
<hr> element is also an empty element which is used to create a horizontal line in the document
Tip: In XHTML, all elements must be closed. Adding a slash inside the start tag, like <br />, is
HTML tags are not case sensitive: <P> means the same as <p>. Many web sites use uppercase
HTML tags.
W3Schools use lowercase tags because the World Wide Web Consortium (W3C) recommends
Tag Description
<code> Defines computer code text
<kbd> Defines keyboard text
<samp> Defines sample computer code
<var> Defines a variable
<pre> Defines preformatted text
Code, sample, and keyboard elements typically render
in a constant-width (also called monospace)
font such as Courier by default.
Variables usually render in italics.
<html>
<body>
<p> Table 3.8 student grade report
<pre>
name idno result
Solomon 334 78
Girma 454 84
Buzunesh 343 90
</pre>
</p>
</body>
</html>
Abbreviation and Acronyms
Marking up shorthand terms as acronyms and abbreviations
provides useful information for search engines, screen readers,
and other devices.
Abbreviations, indicated by the abbr element, are shortened
versions of a word ending in a period (Conn. for Connection, for
example).
<acronym title="American Type Founders">ATF</acronym>
Acronyms, indicated by the acronym element, are abbreviations
formed by the first letters of the words in a phrase (such as WWW
or USA).
<abbr title="Points">pts.</abbr>
Both elements use the title attribute to provide the long version
of the shortened term.
Note: when you put the cursor on, it will display the
abbreviated word in full version.
Citation
The cite element is used to identify a reference to
another document, such as a book, magazine, article
title, and so on.
Defining terms
Used when defining instance of a word
<p><dfn>Script typefaces</dfn>
are based on handwriting.</p>
Inserted and deleted text
The ins and del elements are used to mark up changes to the text and indicate
parts of a document that have been inserted or deleted
<p><The scientific director of KIOT is:
<del title="retired">Mr Yichalewal</del>
<ins>Dr Ahmedin</ins></p
HTML Links
Links are found in nearly all Web pages. Links allow users to click their way from page
to page.
These pages often include links to
other Web sites or
other pages in the same site.
A link is specified using the anchor <a> element.
Anything between the opening <a> tag and the closing </a> tag becomes part of the
<a href="mailto:[email protected]?subject=XHTML">
<a href="mailto:[email protected]?
subject=XHTML&[email protected]"></a>
Tables
Are used
displaying tabular data
to format whole pages and create visually attractive layouts ( recently CSS is
becoming the standard for layout and tables are not recommended )
We use <table> . . . </table>
to create a table in XHTML use the <table> element.
A row is contained inside a <tr> element— table row
each cell is then written inside the row element using a <td> element for “table data”
and <th> for “table headers,”
<table border="1">
<tr>
<td>row 1, column 1 </td>
<td>row 1, column 2 </td>
</tr>
<tr>
<td>row 2, column 1 </td>
<td>row 2, column 2 </td>
</tr>
Table Headers
The text marked up as headers (th elements) are displayed differently from the
other cells in the table (td elements).
Bold and
Centric
Are important because they provide information or context about the cells in the
row or column
In the code fragment below how many columns and rows are there?
<table>
<table border="1">
<tr>
<td>Sufjan Stevens</td>
<td>Illinoise</td>
<td>Asthmatic Kitty Records</td>
</tr>
<tr>
<td>The Shins</td>
<td>Oh Inverted World</td>
<td>Sub-pop Records</td>
</tr>
</table>
Spanning Cells
is the stretching of a cell to cover several rows or columns
You make a header or data cell span by adding the colspan or rowspan
attributes
Column spans, created with the colspan attribute in the td or th element,
stretch a cell to the right to span over the subsequent columns
Example :
<table border="1">
<tr>
<th colspan="2">Fat</th>
</tr>
<tr>
<td> Saturated Fat (g) </td>
<td> Unsaturated Fat (g) </td>
</tr>
</table>
Exercise
Write HTML code to construct the following table
Row spans
Row spans, created with the rowspan attribute
cause the cell to span downward over several rows.
Example:
<table>
<tr>
<th rowspan="3">Serving Size</th>
<td>Small (8oz.)</td>
</tr>
<tr>
<td>Medium (16oz.)</td>
</tr>
<tr>
<td>Large (24oz.)</td>
</tr>
</table>
Exercise
write the markup for the table shown
The align attribute
alignment of the table within the containing body-text
flow
accepts a value of either left, right, or center,
element.
The other component of a web form is an application or
script on the server that processes the information
collected by the form and returns an appropriate
response.(CGI, PHP, ASP etc )
The HTML <form> element defines a form that is used to
collect user input:
In HTML form contains form elements.
Form elements are different types of input elements, like text
fields, checkboxes, radio buttons, submit buttons, and more.
The <input> Element
The <input> element is the most important form element.
The <input> element can be displayed in several ways,
depending on the type attribute.
Text Input type
<input type="text"> defines a one-line input field for text
input:
Example
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
Radio Button Input type
<input type="radio"> defines a radio button.
Radio buttons let a user select ONE of a limited number of
choices:
Example
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
The Name Attribute
Each input field must have a name attribute to be submitted.
If the name attribute is omitted, the data of that input field
will not be sent at all.
This example will only submit the "Last name" input field:
Example
<form action="/action_page.php">
First name:<br>
<input type="text" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>
HTML Form Elements
The <select> Element
The <select> element defines a drop-down list:
Example
<select name = "cars" >
<option value = "Volvo"> Volvo </option>
<option value = "Toyota"> Toyota </option>
<option value = "Lorry"> Lorry </option>
<option value = "Cobra"> Cobra </option>
</select>
The <option> elements define an option that can be selected.
By default, the first item in the drop-down list is selected.
To define a pre-selected option, add the selected attribute to the
option:
<option value = "Cobra” selected> Cobra </option>
The <textarea> Element
The <textarea> element defines a multi-line input field (a
text area):
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
The <button> Element
The <button> element defines a clickable button:
<button type="button" onclick="alert('Hello
World!')">Click Me!</button>
Labels
is used to associate descriptive text with its respective form field. This
provides important context for users with speech-based browsers.
Each label element is associated with exactly one form control.
There are two ways to use it.
1) implicit association, nests the control and its description within a label
element:
<label>Male: <input type="radio" name="gender" value="M" /></label>
<label>Female: <input type="radio" name="gender" value="F" /></label>
2) explicit association, matches the label with the control's id reference,
The for attribute tells which control the label is for.
useful when the control is not directly next to its descriptive text in the
source
and when we use CSS to align them
<label for="form-login-username">Login account:</label>
<input type="text" name="login" id="form-login-username" />
<label for="form-login-password">Password:</label>
<input type="password" name="password" id="form-login-password"/>
Fieldset and legend
Fieldset element is used to indicate a logical group of form controls.
A Fieldset may also include a legend element that provides a caption
for the enclosed fields.
<fieldset>
<legend>Customer Information</legend>
<ol>
<li><label>Full name: <input type="text" name="name" /></label></li>
<li><label>Email: <input type="text" name="email" /></label></li>
<li><label>State: <input type="text" name="state" /></label></li>
</ol>
</fieldset>
<fieldset>
<legend>Mailing List Sign-up</legend>
<ul>
<li><label>Add me to your mailing list <input type="radio"
name="list" value="yes" checked="checked" /></label></li>
<li><label>No thanks <input type="radio" name="list" value="no" />
Single-line text field
used for entering a single word or line of text
syntax : <input type="text" />
Example:
<li><label for="form-city">City:</label>
<input type="text" name="city" value="Your Hometown" size="25" maxlength="50"
id="form-city" /></li>
The value attribute specifies default text that appears in the field when the form is
loaded. When you reset a form, it returns to this value.
Size
By default, browsers display a text-entry box that is 20 characters wide, but
you can change the number of characters using the size attribute. eg. size="25"
maxlength
By default, users can type an unlimited number of characters in a text field
maxlength="50”
Multiline text entry field
Syntax: <textarea>...</textarea>
Example:
<li><label for="form-entry">Official contest entry:</label> <br/><textarea
name="contest_entry" rows="5" cols="100" id="form-entry">Tell us
why you love the band in 50 words or less. Five winners will get backstage
passes!</textarea></li>
rows
Specifies the number of lines of text the area should
of characters
Input Type Password
<input type="password"> defines a password field:
<form>
User name:<br>
<input type="text" name="username"><br>
User password:<br>
<input type="password" name="psw">
</form>
Input Type Submit and reset
<input type="submit"> defines a button
for submitting form data to a form-handler.
The form-handler is typically a server page with a script for
processing input data.
The form-handler is specified in the form's action attribute:
Input Type Reset
<input type="reset"> defines a reset button that will reset
all form values to their default values:
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
<input type="reset">
</form>
Input Type Checkbox
<input type="checkbox"> defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited
number of choices.
Example
<form>
<input type="checkbox" name="vehicle1" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle2" value="Car"> I have a car
</form>
<form>
Select your favorite color:
<input type="color" name="favcolor">
</form>
Input Type Date
The <input type="date"> is used for input fields that should
contain a date.
<form>
Birthday:
<input type="date" name="bday">
</form>
You can also add restrictions to dates:
Example
<form>
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31"><br>
Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02"><br>
</form>
Input Type Datetime-local
The <input type="datetime-local"> specifies a date and
time input field, with no time zone.
<form>
Birthday (date and time):
<input type="datetime-local" name="bdaytime">
</form>
Input Type Email
The <input type="email"> is used for input fields that
should contain an e-mail address.
<form>
E-mail:
<input type="email" name="email">
</form>
Input Type Month
The <input type="month"> allows the user to select a month and year.
<form>
Birthday (month and year):
<input type="month" name="bdaymonth">
</form>
Input Type Number
The <input type="number"> defines a numeric input field.
You can also set restrictions on what numbers are accepted.
The following example displays a numeric input field, where you can
enter a value from 1 to 5:
<form>
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">
</form>
Input Type Range
The <input type="range"> defines a control for entering a number whose exact
value is not important (like a slider control). Default range is 0 to 100. However,
you can set restrictions on what numbers are accepted with the min, max, and step
attributes:
<form>
Quantity:
<input type="number" name="points" min="0" max="100" step="10" valu
e="30">
</form>
Input Type Search
The <input type="search"> is used for search fields (a search field behaves like
a regular text field).
Example
<form>
Search Google:
<input type="search" name="google search">
</form>
Input Type Tel
The <input type="tel"> is used for input fields that should contain a telephone number.
<form>
Telephone:
<input type="tel" name="usrtel">
</form>
Input Type Time
The <input type="time"> allows the user to select a time (no time zone).
<form>
Select a time:
<input type="time" name="usr_time">
</form>
Input Type Url
The <input type="url"> is used for input fields that should contain a URL address.
<form>
Add your homepage:
<input type="url" name="homepage">
</form>
Input Type Week
The <input type="week"> allows the user to select a week
and year.
Depending on browser support, a date picker can show up in
the input field.
Example
<form>
Select a week:
<input type="week" name="week_year">
</form>
HTML Input Attributes
The value Attribute
The value attribute specifies the initial value for an input
field:
<form action="">
First name:<br>
<input type="text" name="firstname" value="John">
</form>
The readonly Attribute
The readonly attribute specifies that the input field is read
only (cannot be changed):
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" readonly>
</form>
The disabled Attribute
The disabled attribute specifies that the input field is disabled.
A disabled input field is unusable and un-clickable, and its
value will not be sent when submitting the form:
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" disabled>
</form>
The size Attribute
The size attribute specifies the size (in characters) for the input
field:
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" size="40">
</form>
The maxlength Attribute
The maxlength attribute specifies the maximum allowed length for the input
field:
Example
<form action="">
First name:<br>
<input type="text" name="firstname" maxlength="10">
</form>
In maxlength attribute, the input field will not accept more than the
allowed number of characters.
The maxlength attribute does not provide any feedback. If you want to
alert the user, you must write JavaScript code.
The height and width Attributes
The height and width attributes specify the height and width of an
<input type="image"> element.
<input type="image" src="img_submit.gif" alt="Submit" width="48" height="48">
The list Attribute
The list attribute refers to a <datalist> element that contains
pre-defined options for an <input> element.
An <input> element with pre-defined values in a <datalist>:
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
The min and max Attributes
The min and max attributes specify the minimum and
maximum values for an <input> element.
The min and max attributes work with the following input
types: number, range, date, datetime-local, month, time and
week.
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31">
Menu control
<select>...</select>
An option within a menu
<option>...</option>
A logical grouping of options within a menu
<optgroup>...</optgroup>
Pull-down menus
only one item may be selected.
select element displays as a pull-down menu by default when
no size is specified or
if the size attribute is set to 1
Example:
<label for="form-fave">What is your favorite 80s band?<label><br />
<select name="EightiesFave" id="form-fave">
<option>The Cure</option>
<option>Cocteau Twins</option>
<option>Tears for Fears</option>
<option>Thompson Twins</option>
<option value="EBTG">Everything But the Girl</option>
</select>