Web-Module II - Chapter1
Web-Module II - Chapter1
Web-Module II - Chapter1
Module II
HTML Tables and Forms & Advanced CSS
• A table in HTML is created using the <table> element and can be used to represent
information that exists in a two-dimensional grid.
• A table is a matrix of cells. The cells in the top row often contain column labels, those in the
leftmost column often contain row labels, and most of the rest of the cells contain the data of
the table.
• The content of a cell can be almost any document element, including text, a heading, a
horizontal rule, an image, and a nested table.
• A table that does not include the border attribute will be a matrix of cells with neither a
border nor rules.
• The browser has default widths for table borders and rules, which are used if the border
attribute is assigned the value “border.” Otherwise, a number can be given as border’s value,
which specifies the border width in pixels. For example, border = “3” specifies a border 3
pixels wide.
• A border value of “0” specifies no border and no rules. The rules are set at 1 pixel when any
nonzero border value is specified. The border attribute is the most common attribute for the
<table> tag.
• In most cases, a displayed table is preceded by a title, given as the content of a <caption> tag,
which can immediately follow the opening <table> tag.
• The cells of a table are specified one row at a time. Each row of a table is specified with a
row tag, <tr>.
• Within each row, the row label is specified by the table heading tag, <th> . Each data cell of
a row is specified with a table data tag, <td>
The first row of a table usually has the table’s column labels. For example, if a table has three
data columns and their column labels are, Apple, Orange, and Screwdriver respectively, the first
row can be specified by the following:
<tr>
<th> Apple </th>
<th> Orange </th>
<th> Screwdriver </th>
</tr>
Each data row of a table is specified with a heading tag and one data tag for each data column.
For example, the first data row for our work-in-progress table might be as follows:
<tr>
<th> Breakfast </th>
<td> 0 </td>
<td> 1 </td>
<td> 0 </td>
</tr>
The following document describes the whole table:
Eg:
<html>
<body>
<br />
<table border="1">
<caption> Diet </caption>
<tr>
<th>Breakfast</th>
<th>Lunch</th>
<th>Dinner</th>
</tr>
<tr>
<td>Apple</td>
<td>Rice</td>
<td>Cucumber</td>
</tr>
<tr>
<td>Watermelon</td>
<td>Rice</td>
<td>Papaya</td>
</tr>
</table>
</body>
</html>
<tr>
<th colspan="3">Diet to be followed</th>
</tr>
The colspan attribute specification in a table header or table data tag tells the browser to make
the cell as wide as the specified number of rows.
Eg:
<html>
<body>
<table border="1">
<caption> Diet </caption>
<tr>
<th rowspan="2" ></th>
<th colspan="3">Fruit Juice Drinks</th>
</tr>
<tr>
<th>Apple</th>
<th>Orange</th>
<th>Strawberry</th>
</tr>
<tr>
<th>Breakfast</th>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<th> Lunch</th>
<td>1</td>
<td>1</td>
Dept. of ISE, SVIT Page 3
Module II – HTML Tables and Forms & Advanced CSS
<td>1</td>
</tr>
<tr>
<th>Dinner</th>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
</table>
</body>
</html>
<table>
<tr>
<td>
<img src="images/959.jpg" alt="Castle"/>
</td>
<td>
<h2>Castle</h2>
<p>Lewes, UK</p>
<p>Photo by: Michele Brooks</p>
Dept. of ISE, SVIT Page 4
Module II – HTML Tables and Forms & Advanced CSS
Table Borders
Table borders can be assigned to both the <table> and the <td> element (or <th>). Borders
cannot be assigned to the <tr>, <thead>, <tfoot>, and
<tbody> elements.
tbody tr:hover {
background-color: #9e9e9e;
color: black;
}
tbody tr:nth-child(odd) {
background-color: white;
}
• Forms provide the user with an alternative way to interact with a web server. Another way is
by using hyperlinks.
• Using a form, the user can enter text, choose items from lists, and click buttons. Programs
running on the server will take the input from HTML forms and processes it, or save it.
• A form is defined by a <form> element, which is a container for other elements that represent
the various input elements within the form as well as plain text and almost any other HTML
element.
Example: Suppose the user a request for an HTML page from the server (1) that contains some
type of form on it. This could be something as complex as a user registration form or as simple
as a search box. After the user fills out the form, there needs to be some mechanism for
submitting the form data back to the server. This is typically achieved via a submit button. As
interaction between the browser and the web server is governed by the HTTP protocol, the form
data must be sent to the server via a standard HTTP request. This request is typically some type
of server-side program that will process the form data in some way; this could include checking
it for validity, storing it in a database, or sending it in an email.
The use of GET or POST method decides where the browser locates the user input in the HTTP
request. Using GET, the browser locates the data in the URL of the request; with POST, the
form data is located in the HTTP header after the HTTP variables.
Generally, form data is sent using the POST method. However, the GET method is useful when
you are testing or developing a system, since you can examine the query string directly in the
browser’s address bar.
Type Description
text Creates a single-line text entry box.
<input type="text" name="title" />
textarea Creates a multiline text entry box.
<textarea rows="3" cols=”50” />
password Creates a single-line text entry box for a password (which masks the user
entry as bullets or some other character)
<input type="password" ... />
email Creates a single-line text entry box suitable for entering an email address.
This is an HTML5 element. Some browsers will perform validation when
form is submitted.
<input type="email" ... />
tel Creates a single-line text entry box suitable for entering a telephone. This
is an HTML5 element. Since telephone numbers have different formats in
different parts of the world, current browsers do not perform any special
Choice Controls
Forms often need the user to select an option from a group of choices. HTML provides
several ways to do this.
Select Lists
• The <select> element is used to create a multiline box for selecting one or more items.
• The options (defined using the <option> element) can be hidden in a dropdown list or
multiple rows of the list can be visible.
• Option items can be grouped together via the <optgroup> element. The selected attribute in
the <option> makes it a default value.
• The value attribute of the <option> element is used to specify what value will be sent back to
the server in the query string when that option is selected.
• The value attribute is optional; if it is not specified, then the text within the container is sent.
The new <datalist> element is a new addition to HTML5. This element allows to define a list of
values that can appear in a drop-down autocomplete style list for a text element. This can be
helpful for situations in which the user must have the ability to enter anything, but are often
entering one of a handful of common elements.
Radio Buttons
Radio buttons are used when the user has to select a single item from a small list of choices and
all the choices have to be visible. They are added by using the <input type="radio"> element.
The buttons are made mutually exclusive (i.e., only one can be chosen) by sharing the same
name attribute. The checked attribute is used to indicate the default choice, while the value
entered in value attribute is sent to the server when submit button is clicked.
Eg:
<input type="radio" name="where" value="1">North America<br/>
<input type="radio" name="where" value="2" checked>South America<br/>
<input type="radio" name="where" value="3">Asia
Checkboxes
Checkboxes are used for getting yes/no or on/off responses from the user. Checkboxes are added
by using the <input type="checkbox"> element. You can also group checkboxes together by
sharing the same name attribute. Each checked checkbox will have its value sent to the server.
Button Controls
HTML defines several different types of buttons.
Type Description
<input type="submit"> Creates a button that submits the form data to the server
<input type="reset"> Creates a button that clears the user’s already entered form data.
<input type="button"> Creates a custom button. This button may requires a script for it to
actually perform any action
<input type="image"> Creates a custom submit button that uses an image for its display
Type Description
date Creates a general date input control. The format for the date is “mm-dd-
yyyy”.
time Creates a time input control. The format for the time is “HH:MM
AM/PM,” for hours:minutes
datetime Creates a control in which the user can enter a date and time.
datetime-local Creates a control in which the user can enter a date and time without
specifying a time zone
month Creates a control in which the user can enter a month in a year. The
format is “mm- yyyy.”
week Creates a control in which the user can specify a week in a year. The
format is “W##- yyyy.”
The term web accessibility refers to the assistive technologies, various features of HTML that
work with those technologies, and different coding and design practices that can make a site
more usable for people with visual, mobility, auditory, and cognitive disabilities.
In order to improve the accessibility of websites, the W3C created the Web Accessibility
Initiative (WAI) in 1997. The WAI produces guidelines and recommendations, as well as
organizing different working groups on different accessibility issues.
Dept. of ISE, SVIT Page 14
Module II – HTML Tables and Forms & Advanced CSS
Accessible Tables
HTML tables can be quite frustrating, for people with visual disability. One important way to
improve the accessibility is to only use tables for tabular data, not for layout. Using the following
accessibility features for tables in HTML can improve the using of tables for those users:
1. Describe the table’s content using the <caption> element. This provides the user with the
ability to discover what the table is about before having to listen to the content of each and every
cell in the table.
2. Connect the cells with a textual description in the header. It is quite revealing to listen to
reader software recite the contents of a table that has not made these connections. It sounds like
this: “row 3, cell 4: 45.56; row 3, cell 5: Canada; row 3, cell 6: 25,000; etc.” However, if these
connections have been made, it sounds instead like this: “row 3, Average: 45.56; row 3, Country:
Canada; row 3, City Count: 25,000; etc.,”.
Accessible Forms
HTML forms are also potentially problematic with respect to accessibility. The use of the
<fieldset>, <legend>, and <label> elements, provide a connection between the input elements in
the form.
The main purpose of <fieldset> and <legend> is to logically group related form input elements
together with the <legend> providing a type of caption for those elements.
Each <label> element should be associated with a single input element, by using the ‘for’
attribute. So that if the user clicks on or taps the <label> text, that control will receive the form’s
focus (i.e., it becomes the current input element and any keyboard input will affect that control).
2.6 Microformats
The web has millions of pages and there are many similar information from site to site. Most
sites have Contact Us page, in which addresses and other information are displayed; calendar of
upcoming events or information about products or news. These types of common information
can be tagged in a similar way, and automated tools can be used to gather and transform the
information.