Chapter 2
Chapter 2
• A table is divided into rows (with the <tr> tag), and each row is divided
into data cells (with the <td> tag).
• td stands for "table data," and holds the content of a data cell.
• A <td> tag can contain text, links, images, lists, forms, other tables, etc.
• Header information in a table is defined with the <th> tag. The text in a
th element will be bold and centered.
9
Tables <table border="1">
<tr>
<th>Name</th>
• <table> main element <th>Course</th>
• <tr> table row <th>Year</th>
</tr>
• <th> table header
<tr>
• <td> table data (cells) <td>A B Morgan</td>
<td>Fishing</td>
<td>5</td>
</tr>
<tr>
<td>D P Jones</td>
<td>Sailing</td>
<td>8</td>
</tr>
</table>
</table>
10
Tables <<table border="1">
<tr>
<th>Name</th>
• <table> main element <td>A B Morgan</td>
• <tr> table row <td>D P Jones</td>
</tr>
• <th> table header
<tr>
• <td> table data <th> Course</th>
<td>Fishing</td>
<td>Sailing</td>
</tr>
<tr>
<th>Year</th>
<td>8</td>
<td>5</td>
</tr>
</table>
</table>
11
<table border="1">
Rows and Columns <tr>
<th colspan="2">Name</th>
<th>Course</th>
• Cells can span multiple <th>Year</th>
columns and multiple </tr>
<tr>
rows with the colspan <td>A B</td>
and rowspan attributes <td>Morgan</td>
<td rowspan="2">Fishing</td>
<td>5</td>
</tr>
<tr>
<td>D J</td>
<td>Jones</td>
<td>Sailing</td>
<td>8</td>
</tr>
<tr>
</table>
12
The align and width attributes
• The align attribute <table border="1" align="center">
<tr>
determines the <th colspan="2" width="60%">Name</th>
position of the text <th rowspan="2">Course</th>
<th rowspan="2">Year</th>
within a cell </tr>
<tr>
<th>Last</th>
<th>Init.</th>
• The width attribute </tr>
<tr>
determines the width <td>Morgan</td>
of the row relative to <td>AB</td>
<td>Fishing</td>
the table <td align="center">5</td>
</tr>
<!– etc -->
13
Table attributes
Table attributes
<table border="3" align="center" cellspacing="6"
cellpadding="6" bgcolor="cyan">
<caption>
<h2>Course Data</h2>
</caption>
<tr>
<th>Name</th>
<th>Course</th>
<th>Year</th>
</tr>
<tr>
<td>A B Morgan</td>
<td>Fishing</td>
<td>5</td>
</tr>
<!– etc -->
15
Table attributes of rows and cells
Page formatting </body>
<table border="0" cellspacing="10">
• Tables can be used to <tr>
organize the layout of <td>
the web page itself <img src="cat.gif" alt="cat">
<ul>
<li>cats</li>
<li>dogs</li>
<li>butterflies</li>
</ul>
</td>
<td>
This piece of text illustrates
the idea of placing two columns
of information in a web page...
Note also that there is no
border in this table.
</td>
</tr>
</table>
</body>
17
Frames and Framesets
• A frameset partitions a web browser window so that multiple web
documents can be displayed simultaneously.
• Example application: To maintain a permanently visible directory of
links within your site, while also displaying one or more selected
documents from the site.
• otherwise the directory of links must appear on every page, and this scrolls up
with the page
• Note: framesets can cause problems for bookmarking and for "screen
readers" (for visually-impaired users)
18
Framesets
<html>
<head><title>Frames 1</title></head>
<frameset cols="140,*">
<frame name="navF" src="navigation.html">
<frame name="mainF" src="intro.html">
</frameset>
</html>
19
Frame attributes
<frameset cols="140,*">
<frame name="navF" src="navigation.html">
<frame name="mainF" src="intro.html">
</frameset>
21
intro.html
• A simple document which is initially placed in the
"mainF" frame
<html>
<head><title>Internet Computing</title></head>
<body>
<h2>Welcome to the HTML Course</h2>
<p>
<h4>Please select the subject of...</h4>
</p>
</body>
</html>
22
Nested framesets
<html>
<head><title>Frames 2</title></head>
<frameset cols="140,*">
<frame name="navF" src="navigation.html">
<frameset rows="30%,70%">
<frame name="upperF" src="intro.html">
<frame name="lowerF" src="course.html">
</frameset>
</frameset>
</html>
23
Noframes
• The NOFRAMES element specifies content that should be displayed only
by user agents that do not support frames or are configured not to display
frames.
• User agents that support frames must only display the contents of a
NOFRAMES declaration when configured not to display frames
• a <body> tag should be found for browsers that do not support frames
Noframes example
<html>
<head><title>Frames 1</title></head>
<frameset cols="140,*">
<frame name="navF" src="navigation.html">
<frame name="mainF" src="intro.html">
</frameset>
<noframes>
<body>
Something here for browsers not supporting frames
</body>
</noframes>
</html>
25
Inline Frames (Iframes)
• The IFRAME element allows
authors to insert a frame within a
block of text.
• The <iframe> tag is not used
within a <frameset> tag.
• Instead, it appears anywhere in the
document.
• The <iframe> tag defines a
rectangular region within the
document in which the browser
displays a separate document,
including scrollbars and borders.
Forms
• Forms are user interfaces for data input
• To use in local processing which may output web page content that is displayed on the client
27
Example applications
• Questionnaires to provide feedback on a web site
• Run a database query and receive results (an important part of e-commerce)
28
Input types
• text
• checkbox
• radio (buttons)
• select (options)
• textarea
• password
• button
• submit
• reset
• hidden
• file
• image
29
The method and action attributes
• The method attribute specifies the way that form data is sent to the server
program
– GET appends the data to the URL
– POST sends the data separately
• The action attribute specifies a server program (e.g. a perl program .pl
extension) that processes the form data
• Can also send an email: action=“mailto:D.P.Cosker@..”
<body>
<form method="POST" action="comments.pl">
<h2>Tell us what you think</h2>
<!-- etc -->
</form>
</body>
30
The input element: type="text"
• The type attribute specifies the type of user
input
• The name attribute gives an identifier to the input
data
• The size attribute specifies the length of the
input field
• The value attribute specifies an initial value for
the text (optional)
31
The input element:type="checkbox"
• The name attribute is used to define a set of
checkboxes
• The value attribute identifies the individual
checkbox
• If the checked attribute is set the box is
initially checked
32
The input element: type="radio"
• Radio buttons are similar to checkboxes,
but only one can be selected
• To select a button by default, use the
checked attribute (for one button only)
33
The input element: type="button"
• The name attribute uniquely identifies
a button
• The value attribute gives a label to the
button
• Actions can be associated with buttons-
more later
34
The input element:
type="submit/reset"
• type="submit"
– clicking this button sends the form data
to the program (URL) specified in the
action attribute of the form
• type="reset"
– clicking this button clears all data
entered so far
Thank you<br>
<input type="submit" name="send" value="Send">
<input type="reset" name="clear" value="Clear"><br>
35
The input element:
type="password/file/hidden"
• type="password"
– similar to type="text" except that the input is echoed with asterisks (so not
visible)
• type="file"
– provides a file dialogue box to specify a file that is sent to the server
• type="hidden"
– similar to text input, but the value attribute is used to specify data that is to
be sent to the server. Nothing appears on the screen.
– The data might be set by a server program to keep track of the details of a
particular transaction.
36
The textarea element
• Used for multi-line text input
• The size of the input area is specified
with the cols and rows attributes
• Any text placed inside the element
appears in the input area (this can be
deleted).
37
The select element
• The select element provides a menu of
options
• An option can be selected by default using
the selected attribute (otherwise the first in
the list is initially selected)
38
Linking HTML Forms with PHP Scripts
• To create a link between PHP scripts and HTML forms, we need to use the
functionalities of buttons, and the attribute of the <form tag>, specifically “action”
and “method”.
• Action specifies the PHP script that will process the form data
• Method specifies the way form data will transferred to server which can be either
GET or POST method
• GET: information visible on URLs
• POST: information not visible in URLs
• Submit buttons: When activated, submits a form.
• Reset buttons: When activated, a reset button resets all controls to their initial
values.
Cascading Style Sheets (CSS)
CSS Saves a Lot of Work!
1) Selector
2) Property/value
3) Declaration
4) Declaration block
5) Curly braces
Syntax of CSS
Selector
Selectors are used to declare which part of the markup a
style applies to, a kind of match expression.
3 types of selectors
1) Tag selectors (body, p, div, a)
2) ID selectors (#wrapper, #sidebar)
3) Class selectors (.content, .menu)
The selector is normally the HTML element you want to
style
Selectors should never start with a number, nor should they
have spaces in them
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 "#".
The style rule below will be applied to the element with
id="wrapper":
#wrapper
{ For example, to identify a
text-align:center; paragraph as “head”, use the code:
border:1px solid red; <div id=“wrapper”>…</div>
Width: 200px;
Height: 100px
}
*Do NOT start an ID name with a number!
Class Selector
The class selector is used to specify a style for a
group of elements. Unlike the id selector, the class
selector is most often used on several elements.
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 "."
Class Selector Cont’…
1) Inline style
2) Internal style sheet
3) External style sheet
Inline style
An inline style loses many of the advantages of style sheets by
mixing content with presentation. Use this method sparingly!
To use inline styles you use the style attribute in the relevant tag.
The style attribute can contain any CSS property. The ff example
shows how to change the color
<p style=“text-align: center; font- weight: bold; color:
yellow;”>This is a paragraph.</p>
Internal style sheet
An internal style sheet should be used when a single document has a
unique style. You define internal styles in the head section of an HTML
page, by using the <style> tag, like this:
<head>
<style type=“text/css”>
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
</style>
</head>
External style sheet
An external style sheet is ideal when the style is applied to many pages.
With an external style sheet, you can change the look of an entire Web site by
Property Description
background Sets all the background properties in one
declaration
h1 {
color: #00ff00;
}
h2 {
color: rgb(255,0,0);
}
Text Alignment
h1 {
text-align: center;
}
p.date {
text-align: right;
}
p.main {
text-align: justify;
}
Text Decoration
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
Text Transformation
The text-transform property is used to specify uppercase and lowercase
letters in a text.
It can be used to turn everything into uppercase or lowercase letters, or
capitalize the first letter of each word.
Example
p.uppercase { text-transform: uppercase;}
p.lowercase { text-transform: lowercase;}
p.capitalize { text-transform: capitalize;}
Text Indentation
CSS font properties define the font family, boldness, size, and the style of
a text.
The font family of a text is set with the font-family property.
The font-family property should hold several font names as a "fallback"
system.
If the browser does not support the first font, it tries the next font.
Start with the font you want, and end with a generic family, to let the
browser pick a similar font in the generic family, if no other fonts are
available.
• If the name of a font family is more than one word, it must be in
quotation marks, like: "Times New Roman".
• More than one font family is specified in a comma-separated list:
• Example
p {
font-family: "Times New Roman", Times, serif;
}
Font Style
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
Font Size
• Setting the text size with pixels gives you full control
over the text size:
• Example
h1 {
font-size: 40px;
}
h2 { font-size: 30px; }
p { font-size: 14px; }
Set Font Size With Em
To allow users to resize the text (in the browser menu), many
developers use em instead of pixels.
The em size unit is recommended by the W3C.
1em is equal to the current font size. The default text size in
browsers is 16px. So, the default size of 1em is 16px.
The size can be calculated from pixels to em using this
formula: pixels/16=em
Example
h1 {
font-size: 2.5em; /* 40px/16=2.5em */
}
h2 {
font-size: 1.875em; /* 30px/16=1.875em */
}
p {
font-size: 0.875em; /* 14px/16=0.875em */
}
Use a Combination of Percent and Em
h1 { font-size: 2.5em; }
h2 { font-size: 1.875em; }
p { font-size: 0.875em; }
CSS Positioning
• The CSS positioning properties allow you to position an element.
• It can also place an element behind another, and specify what should
happen when an element's content is too big.
• 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.
• There are four different positioning methods.
Static Positioning
<html>
<head>
<style>
img { position: absolute; left: 0px; top: 0px; z-index: -1;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<img src="w3css.gif" width="100" height="140">
<p>Because the image has a z-index of -1, it will be placed
behind the text.</p>
</body>
</html>
CSS Box Model
• All HTML elements can be considered as boxes. In CSS, the
term "box model" is used when talking about design and
layout.
• The CSS box model is essentially a box that wraps around
HTML elements, and it consists of:
margins,
borders,
padding, and
the actual content.
• The box model allows us to add a border around elements,
and to define space between elements.
The image below illustrates the box model:
Explanation of the different parts of box model:
• The CSS border properties allow you to specify the style, size, and color
of an element's border
• The border-style property specifies what kind of border to display.
• Note: None of the border properties will have ANY effect unless
the border-style property is set!
border-style values:
• The border-color property is used to set the color of the border. The color
can be set by:
• You can also set the border color to "transparent".
• If the border color is not set it is inherited from the color property of the
element.
• Note: The "border-color" property does not work if it is used alone.
• Use the "border-style" property to set the borders first
example
p.one {
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: #98bf21;
}
Border - Individual sides
Value Description
• margin: 25px;
– all four margins are 25px