Chapter 2 Web Programming Best
Chapter 2 Web Programming Best
Chapter 2 Web Programming Best
Chapter Two
If you need forms in your pages to send information to server and return result pages
back to browser you will need to know html codes.
.
2.1.1.Tags, Elements and Attributes of the tags
HTML is basically a series of markup tags that identify the elements and content of a
web page.As we have already seen, a tag name is enclosed in angle brackets (<>) and most tags
are a pair – a starting tag and an end tag. The end tag includes a forward slash (/) after the
opening angle bracket (<). So, for example, the tag that denotes the beginning and end of a web
page is:
<HTML>
</HTML>
Tags are not case-sensitive i.e. <html> and <hTmL> are the same as <HTML>.
However, it is good practice to be consistent i.e. to always use either upper case (<HTML>)
or lower case (<html>) as this makes the HTML more readable.
It is also good practice to use white space and indentation, as again, this makes the HTML
more readable. Indentation is especially useful for showing tags that are nested inside other
tags – we will see nested tags later.
Examples of tags are <b>, </b> and <p> (for a paragraph break).
Tags can also contain one or more attributes that modify what the tag does. An attribute is a
name-value pair inside the opening tag. Attributes can provide additional information about the
HTML elements on your page. This tag defines the body element of your HTML page: <body>.
With an added bgcolor attribute, you can tell the browser that the background color of your page
should be red, like this: <body bgcolor="red">. For example, the tag that denotes a paragraph
is <P>. To centre a paragraph on the web page, you can specify the align attribute with a value of
„center‟:
Note: the „center‟ attribute value is the American spelling for „centre‟. The non-American
spelling will not work. This also applies when using colour attributes – the spelling is „color‟.
<HTML>
<HEAD>
HTML Headings
Headings are defined with the <h1> to <h6> tags.
<h1> defines the largest heading. <h6> defines the smallest heading.
Example
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
HTML Paragraphs
Paragraphs are defined with the <p> tag.
Example
<p>This is a paragraph</p>
<p>This is another paragraph</p>
The ALIGN attribute can be used to position the paragraph – possible values are center, right,
left.
e.g. <P ALIGN="left">
This is a paragraph
This is a paragraph
This is a paragraph
Attributes of <HR> tag
SIZE: height or thickness of the line, specifies the number of pixels
Page 7 of 49 KMU Compiled by:-Samuel A.
Internet Programming Chapter Two HTML and CSS
WIDTH: number of pixels for the width (length across the page) of the line; or can specify a
% of the total page width e.g. "50%"; default is 100% or the entire page width.
ALIGN: left, right or center
COLOR: specify the colour of the line - a colour name or HTML colour value
The <br /> element is an empty HTML element. It has no end tag.
HTML Comments
Comments can be inserted in the HTML code to make it more readable and understandable.
Comments are ignored by the browser and are not displayed.
Comments are written like this:
Example
<!-- This is a comment -->
This example demonstrates how to insert a hidden comment in the HTML source code.
<html>
<body>
<!--This comment will not be displayed-->
<p>This is a regular paragraph</p>
</body>
</html>
This is a regular paragraph
<FONT></FONT>: Use to change the font size, colour and face (family) of the text
between the tags, using these attributes:
SIZE: a value from 1 to 7; can also specify +/- value to change the size relative to the
base font of the web page e.g. <FONT SIZE=4>changes to size 4</FONT> or
<FONT SIZE=+2>changes the size to the base font size plus 2 e.g. to 5 if the default
base font is used.
COLOR: change the colour of the font (see section Error! Reference source not
found. about Using Colours)
FACE: set the font family e.g. Arial, Verdana. More than one face should be
specified – as if the first one is not installed on the client PC, then the second one will
be used and so on e.g. <FONT FACE="Verdana,Arial" COLOR ="red"> - if the PC
has Verdana installed, it will be used, otherwise Arial will be used.
Note: Verdana and Arial are commonly used fonts on web pages, as they are very
readable and look good. Fonts that look good on Word documents are not necessarily the
best to use for web pages.
<CENTER></CENTER>: Causes all the lines of text between the tags to be centered
on the page.
<MARQUEE></MARQUEE>: Causes the text between the tags to scroll across the
screen. Attributes that can be used are:
ALIGN: specifies how the text around the Marquee should align with the
Marquee; values are left, right, top, middle, bottom
BEHAVIOR: specifies how the text moves across the screen – the default,
scroll, causes the text to start at one side of the window, scroll all the way
across and off the screen, then start again; slide causes the text to stop when it
reaches the second side of the window; alternate causes the text to bounce
from side to side.
BGCOLOR: specifies a background colour for the Marquee (as per other
colour attributes).
DIRECTION="LEFT"|"RIGHT" |"UP"|"DOWN": indicates the
direction marquee text will move. "LEFT" (the default) causes marquee text
to move from right to left on the screen.
"RIGHT" causes marquee text to move from left to right on the screen. UP
and DOWN do the same as their name implies.
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.
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.
Non-breaking Space
Normally HTML will truncate spaces in your text. If you write 10 spaces in your text HTML will
remove 9 of them. To add spaces to your text, use the character entity.
HTML Lists
HTML supports ordered, unordered and definition lists.
<OL></OL>: Ordered List – used to display a list of items, where the items are numbered in
sequence.
Each item in the list should be preceded by an <LI> tag (it does not need a closing tag).
The following attributes can be specified:
TYPE: to specify what numbering system is used – possible values are:
(TYPE=A) -capital letters. e.g. A, B, C ...
(TYPE=a) - small letters. e.g. a, b, c ...
(TYPE=I) - large roman numerals. e.g. I, II, III ...
(TYPE=i) - small roman numerals. e.g. i, ii, iii ...
(TYPE=1) - the default numbers. e.g. 1, 2, 3 ...
START: to specify if the list should start at a value other than 1
e.g.
<OL TYPE=1 START=3>
<LI>this is the first list item text
</OL>
<UL></UL>: Unordered List – used to display a list of items; the items are usually bulleted (not
numbered); and separated by white space. Each item in the list should be preceded by an <LI>
tag.
An Unordered List can be nested inside another Unordered List. It uses type attribute and the
possible values are circle, square and disc.
<LI>: List Item - used to denote an item in an Ordered List or an Unordered List. The <LI>
should appear immediately after the opening <OL> or <UL> tag.
Definition Lists
A definition list is not a list of single items. It is a list of items (terms), with a description of each
item (term).
<dl>
<dt>Coffee</dt>
<dd>Black hot drink</dd>
<dt>Milk</dt>
<dd>White cold drink</dd>
</dl>
Coffee
Black hot drink
Milk
White cold drink
Inside the <dd> tag you can put paragraphs, line breaks, images, links, other lists, etc.
2.1.4.Hypertext links
In web terms, a hyperlink is a reference (an address) to a resource on the web. Hyperlinks can
point to any resource on the web: an HTML page, an image, a sound file, a movie, etc. An
anchor is a term used to define a hyperlink destination inside a document.
The HTML anchor element <a>, is used to define both hyperlinks and anchors. We will use
the term HTML link when the <a> element points to a resource, and the term HTML anchor
when the <a> elements defines an address inside a document..
An HTML Link
Link Tags
Tag Description
<a> Defines an anchor
Link syntax:
The start tag contains attributes about the link. The element content (Link text) defines the part to
be displayed.
Note: The element content doesn't have to be text. You can link from an image or any other
HTML element.
The href attribute defines the link "address".This <a> element defines a link to KMU:
Visit KMU!
The target attribute defines where the linked document will be opened. The code below
will open the document in a new browser window:
Example
<a href="http://www.kmu.edu.et/"
target="_blank">Visit KMU</a>
Example:
<a href="#tips">
Jump to the Useful Tips Section</a>
<a href="http://www.w3schools.com/html_tutorial.htm#tips">
Jump to the Useful Tips Section</a>
2.1.5.Graphical links
An image can be used as a hyperlink by surrounding it with the Anchor tag. For example, to link
the image of the CS to the intranet on KMU-server:
<A HREF="//kmu-server">
<IMG SRC="images/KMU_Faculty_Technology.jpg" WIDTH=500 HEIGHT=375
ALT="Kotebe Metropolitan University FBE" BORDER=0></A>
The border attribute is useful when an image is hyperlinked. If the border attribute is not set to 0,
or is not specified at all, the image will have a blue line around it, to indicate a hyperlink. Often,
it is preferable not to have the blue line, so the border attribute should be set to a value of 0.
2.1.6.E-mail links
Using html it is also possible to create E-mail links.
How to create an email link?
To create an e-mail link on your page, all you need to do is use the standard link tag. The trick is
in what you use as the address of the link. To force the browser to read it as an e-mail link, you
use "mailto:" rather than "http://" to begin the address. After the "mailto:", you will use your e-
mail address rather than a Web address, like this:
If you click on the link, your browser will bring up a window for you to send an e-mail, with the
e-mail address already filled in.
You can also create the subject of the message so the viewer doesn't have to fill in something in
the subject line. You do this by adding a "?" at the end of your e-mail address and then your
subject, like this:
The example link is below, notice that when you click it, the subject field of your email message
is already filled in with "Hey John".
2.1.7.Tables
There is a set of HTML tags that can be used to define tables on web pages.
Tables are a very useful way to control the layout of your pages. Various attributes can be used
to define the size and colour of the table.
Tag Attributes Description
<TABLE> This tag denotes the beginning and end of a table definition
</TABLE>
ALIGN "left", "right" or "center" – alignment on the page
Colour of the border (if the border is present i.e. BORDER attribute
has a value greater than 0) – a named colour or an RGB hex value e.g.
BORDER = "1" BORDERCOLOR ="red"
BORDERCOLORLIGHT Or
BORDERCOLORDARK BORDER = "1" BORDERCOLOR ="#ff0000"
These two attributes can be used to specify a light and a darker colour
to use in the border (if the border is present) - this gives the border a 3-
ALIGN Alignment of the text in this cell – to the LEFT, CENTER or RIGHT
of the cell.
VALIGN
Vertical alignment of the text in the cell – to the TOP, MIDDLE or
BOTTOM of the cell. Another possible value is BASELINE – this
specifies that all the text in the row should be vertically aligned to the
same baseline. If this is not specified, text is aligned to the centre of
ROWSPAN the cell by default.
BACKGROUND If nowrap is specifed in a <td> tag, it means that any text display in the
BGCOLOR cell will not be wrapped i.e. it will not go onto the next line. This can
BORDERCOLOR force the width of the cell to be big enough to accommodate all the
BORDERCOLORLIGHT text in it.
BORDERCOLORDARK
HEIGHT All as described for the TABLE tag.
2.1.8.Image maps
Graphics or images can be placed on a web page (as well as using them as the page or table
background). The tag to do this is the <IMG> (IMaGe) tag.
For example, to insert an image of the Faculty of Natural and Computational Science :
<IMG SRC="images/KMU.jpg" WIDTH=500 HEIGHT=375 ALT="Kotebe Metropolitan
University FCS" BORDER=0>
The tag does not require a closing tag. The attributes are as follows:
SRC - The image_URL is the full web path to the image. If the image is in the same
website as the page on which it is being displayed, then the relative path can be used (see
section Error! Reference source not found., about the Anchor tag, for more information).
WIDTH - x is the width, in pixels, of the image
HEIGHT - y is the height, in pixels, of the image
(to find the width and height of an image – if you are using Windows XP or 2000, if you
point the mouse at the file and hover, the image dimensions, in pixels, are displayed).
The width and height of the image do not have to be specified, however, it is preferable
to specify them. This is because many browsers will use this information to set aside the
space required for the image, and then continue to display the text around the image
while the graphic is downloading. This is useful because images can take longer than text
to download over the internet.
In addition, if the image URL is not found or if the user has turned off the downloading
of images in the browser, the space that would be occupied by the image is marked on the
page. Specifying the height and width mean that the page will still display as desired by
the author.
ALT – This is a short piece of text that describes the image. If the user has turned off the
downloading of images or if the image URL cannot be found, many browsers will display
the ALT text in place of the images. This text will also be displayed to the user when the
mouse is pointed at the image.
BORDER – specifies the thickness of the border around the image. A value of 0 means
no border. Values of 1,2 etc. increase the border thickness.
URL is replaced with the internet address of the video file. The two XX's are replaced with the
desired width and height of the movie and controls. VALUE is replaced with either
</EMBED>
</BODY>....
OR
The <img> tag also helps you to connect video to your page the only thing to be changed from
the above is its attributes specially no use of “src” instead “dynsrc” <img dynsrc=
“yourfile.extension”>
The following are its tags to be added while you use video files on your pages
1. Start = “ ” when to start playing the movie mouseover or fileopen can be included.
3. LOOP =INFINITE |-1 |# of loops how many times to loop the movie
2.1.9.Forms
HTML Forms are used to select different kinds of user input.
Form elements are elements that allow the user to enter information (like text fields, textarea
fields, drop-down menus, radio buttons, checkboxes, etc.) in a form.
Form Tags
Tag Description
<form> Defines a form for user input
<input> Defines an input field
<textarea> Defines a text-area (a multi-line text input control)
<label> Defines a label to a control
<fieldset> Defines a fieldset
<legend> Defines a caption for a fieldset
<form>
.
input elements
.
</form>
Input
The most used form tag is the <input> tag. The type of input is specified with the type attribute.
The most commonly used input types are explained below.
Text Fields
Text fields are used when you want the user to type letters, numbers, etc. in a form.
<form>
First name:
<input type="text" name="firstname" />
<br />
Last name:
<input type="text" name="lastname" />
</form>
First name:
Last name:
Note that the form itself is not visible. Also note that in most browsers, the width of the text field
is 20 characters by default.
Radio Buttons
Radio Buttons are used when you want the user to select 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
Checkboxes are used when you want the user to select one or more options of a limited number
of choices.
<form>
I have a bike:
<input type="checkbox" name="vehicle" value="Bike" />
<br />
I have a car:
<input type="checkbox" name="vehicle" value="Car" />
<br />
I have an airplane:
<input type="checkbox" name="vehicle" value="Airplane" />
</form>
I have a bike:
I have a car:
I have an airplane:
When the user clicks on the "Submit" button, the content of the form is sent to the server. The
form's action attribute defines the name of the file to send the content to. The file defined in the
action attribute usually does something with the received input.
Submit
Username:
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_submit.asp". The page will show you the
received input.
This example demonstrates how to create check-boxes on an HTML page. A user can select or
unselect a checkbox.
<html>
<body>
<form action="">
I have a bike:
<input type="checkbox" name="vehicle" value="Bike">
<br />
I have a car:
<input type="checkbox" name="vehicle" value="Car">
<br />
I have an airplane:
<input type="checkbox" name="vehicle" value="Airplane">
</form>
</body>
</html>
I have a bike:
I have a car:
I have an airplane:
This example demonstrates how to create radio-buttons on an HTML page.
<html>
<body>
Page 24 of 49 KMU Compiled by:-Samuel A.
Internet Programming Chapter Two HTML and CSS
<form action="">
Male:
<input type="radio" checked="checked"
name="Sex" value="male">
<br>
Female:
<input type="radio"
name="Sex" value="female">
</form>
<p>
When a user clicks on a radio-button, the button becomes checked, and all other buttons with the
same name become unchecked
</p>
</body>
</html>
Male:
Female:
When a user clicks on a radio-button, the button becomes checked, and all other buttons with the
same name become unchecked
This example demonstrates how to create a simple drop-down box on an HTML page. A drop-
down box is a selectable list.
<html>
<body>
<form action="">
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</form>
</body>
</html>
Volvo
This example demonstrates how to create a text-area (a multi-line text input control). A user can
write text in the text-area. In a text-area you can write an unlimited number of characters.
This example cannot be edited because our editor uses a textarea for input,
This example demonstrates how to create a button. On the button you can define your own text.
<html>
<body>
<form action="">
<input type="button" value="Hello world!">
</form>
</body>
</html>
This example demonstrates how to add a form to a page. The form contains two input fields and
a submit button.
<html>
<body>
<form name="input" action="html_form_action.asp" method="get">
Type your first name:
<input type="text" name="FirstName" value="Mickey" size="20">
<br>Type your last name:
<input type="text" name="LastName" value="Mouse" size="20">
<br>
<input type="submit" value="Submit">
</form>
<p>
If you click the "Submit" button, you will send your input to a new page called
html_form_action.asp.
</p>
</body>
</html>
Mickey
Type your first name:
Mouse
Type your last name:
Submit
If you click the "Submit" button, you will send your input to a new page called
html_form_action.asp.
2.1.10.Frames
With frames, you can display more than one Web page in the same browser window.
Frame Tags
Tag Description
<frameset> Defines a set of frames
<frame> Defines a sub window (a frame)
<noframes> Defines a no frame section for browsers that do not handle frames
<iframe> Defines an inline sub window (frame)
Examples
Vertical frameset
This example demonstrates how to make a vertical frameset with
three different documents.
<html>
<frameset cols="25%,50%,25%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset>
</html>
Horizontal frameset
This example demonstrates how to make a horizontal frameset with three different documents.
html>
<frameset rows="25%,50%,25%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset>
</html>
Frames
With frames, you can display more than one HTML document in the same browser window.
Each HTML document is called a frame, and each frame is independent of the others.
The <frameset> tag defines how to divide the window into frames
Each frameset defines a set of rows or columns
The values of the rows/columns indicate the amount of screen area each row/column will
occupy
The <frame> tag defines what HTML document to put into each frame
In the example below we have a frameset with two columns. The first column is set to 25% of
the width of the browser window. The second column is set to 75% of the width of the browser
window. The HTML document "frame_a.htm" is put into the first column, and the HTML
document "frame_b.htm" is put into the second column:
<frameset cols="25%,75%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
</frameset>
Note: The frameset column size value can also be set in pixels (cols="200,500"), and one of the
columns can be set to use the remaining space (cols="25%,*").
If a frame has visible borders, the user can resize it by dragging the border. To prevent a user
from doing this, you can add noresize="noresize" to the <frame> tag.
Add the <noframes> tag for browsers that do not support frames.
Important: You cannot use the <body></body> tags together with the <frameset></frameset>
tags! However, if you add a <noframes> tag containing some text for browsers that do not
support frames, you will have to enclose the text in <body></body> tags! See how it is done in
the first example below.
Navigation frame
This example demonstrates how to make a navigation frame. The navigation frame contains a list
of links with the second frame as the target. The file called "tryhtml_contents.htm" contains three
links. The source code of the links:
<a href ="frame_a.htm" target ="showframe">Frame a</a><br>
<a href ="frame_b.htm" target ="showframe">Frame b</a><br>
<a href ="frame_c.htm" target ="showframe">Frame c</a>
The second frame will show the linked document
CSS Example
CSS declarations always ends with a semicolon, and declaration groups are surrounded by curly
brackets:
p {color:red;text-align:center;}
To make the CSS more readable, you can put one declaration on each line, like this:
Example
p
{
color:red;
text-align:center;
}
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;
CSS Id and Class
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 "#".
The style rule below will be applied to the element with id="para1":
Example
#para1
{
text-align:center;
color:red;
}
You can also specify that only specific HTML elements should be affected by a class.
In the example below, all p elements with class="center" will be center-aligned:
Example
p.center {text-align:center;}
Do NOT start a class name with a number! This is only supported in Internet Explorer.
<style type="text/css">
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
</style>
</head>
Inline Styles
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 example shows how to change the color and the left margin of a
paragraph:
<p style="color:sienna;margin-left:20px">This is a paragraph.</p>
The color is inherited from the external style sheet and the text-alignment and the font-size is
replaced by the internal style sheet.
CSS Background
CSS background properties are used to define the background effects of
an element.
CSS properties used for background effects:
background-color
background-image
background-repeat
background-attachment
background-position
Background Color
The background-color property specifies the background color of an element.
The background color of a page is defined in the body selector:
Example
body {background-color:#b0c4de;}
Background Image
The background-image property specifies an image to use as the background of an element.
By default, the image is repeated so it covers the entire element.
The background image for a page can be set like this:
Example
body {background-image:url('paper.gif');}
Below is an example of a bad combination of text and background image. The text is almost not
readable:
Example
body {background-image:url('bgdesert.jpg');}
If the image is repeated only horizontally (repeat-x), the background will look better:
Example
body
{
background-image:url('gradient2.png');
background-repeat:repeat-x;
In the example above, the background image is shown in the same place as the text. We want to
change the position of the image, so that it does not disturb the text too much.
The position of the image is specified by the background-position property:
Example
body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
background-position:right top;
}
color-name
transparent
inherit
background-image Sets the background image for url(URL) 1
an element none
inherit
background-position Sets the starting position of a left top 1
background image left center
left bottom
right top
right center
right bottom
center top
center center
center bottom
x% y%
xpos ypos
inherit
background-repeat Sets if/how a background image repeat 1
will be repeated repeat-x
repeat-y
no-repeat
inherit
CSS Text
TEXT FORMATTING
This text is styled with some of the text formatting
properties. The heading uses the text -align, text-transform, and
color properties. The paragraph is indented, aligned, and the
space between characters is specified. The underline is removed
from the "Try it yourself" link.
Text Color
The color property is used to set the color of the text. The color can be specified by:
name - a color name, like "red"
RGB - an RGB value, like "rgb(255,0,0)"
Hex - a hex value, like "#ff0000"
The default color for a page is defined in the body selector.
Example
body {color:blue;}
h1 {color:#00ff00;}
h2 {color:rgb(255,0,0);}
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
Text can be centered, or aligned to the left or right, or justified.
When text-align is set to "justify", each line is stretched so that every line has equal width, and
the left and right margins are straight (like in magazines and newspapers).
Example
h1 {text-align:center;}
p.date {text-align:right;}
p.main {text-align:justify;}
Text Decoration
The text-decoration property is used to set or remove decorations from text.
The text-decoration property is mostly used to remove underlines from links for design purposes:
Example
a {text-decoration:none;}
It can also be used to decorate text:
Example
h1 {text-decoration:overline;}
h2 {text-decoration:line-through;}
h3 {text-decoration:underline;}
h4 {text-decoration:blink;}
It is not recommended to underline text that is not a link, as this often confuses users.
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
The text-indentation property is used to specify the indentation of the first line of a text.
Example
p {text-indent:50px;}
normal
white-space Sets how white space inside an element is handled pre 1
nowrap
normal
word-spacing Increase or decrease the space between words 1
length
CSS Font
CSS font properties define the font family, boldness, size, and the style of a text.
On computer screens, sans-serif fonts are considered easier to read than serif fonts.
Font Family
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.
Note: If the name of a font family is more than one word, it must be in quotation marks, like
font-family: "Times New Roman".
For more commonly used font combinations, look at our Web Safe Font Combinations.
Font Style
The font-style property is mostly used to specify italic text.
This property has three values:
normal - The text is shown normally
italic - The text is shown in italics
oblique - The text is "leaning" (oblique is very similar to italic, but less supported)
Example
p.normal {font-style:normal;}
p.italic {font-style:italic;}
p.oblique {font-style:oblique;}
Font Size
The font-size property sets the size of the text.
Being able to manage the text size is important in web design. However, you should not use font
size adjustments to make paragraphs look like headings, or headings look like paragraphs.
Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for paragraphs.
The font-size value can be an absolute, or relative size.
Absolute size:
Sets the text to a specified size
Does not allow a user to change the text size in all browsers (bad for accessibility
reasons)
Absolute size is useful when the physical size of the output is known
Relative size:
Sets the size relative to surrounding elements
Allows a user to change the text size in browsers
If you do not specify a font size, the default size for normal text, like paragraphs, is 16px
(16px=1em).
The example above allows Firefox, Chrome, and Safari to resize the text, but not Internet
Explorer.
The text can be resized in all browsers using the zoom tool (however, this resizes the entire page,
not just the text).
In the example above, the text size in em is the same as the previous example in pixels.
However, with the em size, it is possible to adjust the text size in all browsers.
Unfortunately, there is still a problem with IE. When resizing the text, it becomes larger than it
should when made larger, and smaller than it should when made smaller.
Our code now works great! It shows the same text size in all browsers, and allows all browsers to
zoom or resize the text!
All CSS Font Properties
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1
or CSS2).
Property Description Values CSS
font Sets all the font properties in one font-style 1
declaration font-variant
font-weight
font-size/line-height
font-family
caption
icon
Page 42 of 49 KMU Compiled by:-Samuel A.
Internet Programming Chapter Two HTML and CSS
menu
message-box
small-caption
status-bar
inherit
font-family Specifies the font family for text family-name 1
generic-family
inherit
font-size Specifies the font size of text xx-small 1
x-small
small
medium
large
x-large
xx-large
smaller
larger
length
%
inherit
font-style Specifies the font style for text normal 1
italic
oblique
inherit
font-variant Specifies whether or not a text should be normal 1
displayed in a small-caps font small-caps
inherit
font-weight Specifies the weight of a font normal
bold
bolder
lighter
100
200
300
400
500
600
700
800
900
inherit
CSS Links
Links can be styled in different ways.
Styling Links
Links can be style with any CSS property (e.g. color, font-family, background-color).
Special for links are that they can be styled differently depending on what state they are in.
The four links states are:
a:link - a normal, unvisited link
a:visited - a link the user has visited
a:hover - a link when the user mouses over it
a:active - a link the moment it is clicked
Example
a:link {color:#FF0000;} /* unvisited link */
a:visited {color:#00FF00;} /* visited link */
a:hover {color:#FF00FF;} /* mouse over link */
a:active {color:#0000FF;} /* selected link */
When setting the style for several link states, there are some order rules:
a:hover MUST come after a:link and a:visited
a:active MUST come after a:hover
Background Color
The background-color property specifies the background color for links:
Example
a:link {background-color:#B2FF99;}
a:visited {background-color:#FFFF85;}
a:hover {background-color:#FF704D;}
a:active {background-color:#FF704D;}
CSS Lists
Page 44 of 49 KMU Compiled by:-Samuel A.
Internet Programming Chapter Two HTML and CSS
List
In HTML, there are two types of lists:
unordered lists - the list items are marked with bullets
ordered lists - the list items are marked with numbers or letters
With CSS, lists can be styled further, and images can be used as the list item marker.
Some of the property values are for unordered lists, and some for ordered lists.
Values for Unordered Lists
Value Description
None No marker
Disc Default. The marker is a filled circle
Circle The marker is a circle
Square The marker is a square
Values for Ordered Lists
Value Description
Armenian The marker is traditional Armenian numbering
Decimal The marker is a number
decimal-leading-zero The marker is a number padded by initial zeros (01, 02, 03, etc.)
Georgian The marker is traditional Georgian numbering (an, ban, gan, etc.)
lower-alpha The marker is lower-alpha (a, b, c, d, e, etc.)
lower-greek The marker is lower-greek (alpha, beta, gamma, etc.)
lower-latin The marker is lower-latin (a, b, c, d, e, etc.)
lower-roman The marker is lower-roman (i, ii, iii, iv, v, etc.)
upper-alpha The marker is upper-alpha (A, B, C, D, E, etc.)
Page 45 of 49 KMU Compiled by:-Samuel A.
Internet Programming Chapter Two HTML and CSS
The example above does not display equally in all browsers. IE and Opera will display the
image-marker a little bit higher than Firefox, Chrome, and Safari.
If you want the image-marker to be placed equally in all browsers, a crossbrowser solution is
explained below.
Example explained:
For ul:
o Set the list-style-type to none to remove the list item marker
o Set both padding and margin to 0px (for cross-browser compatibility)
For li:
Page 46 of 49 KMU Compiled by:-Samuel A.
Internet Programming Chapter Two HTML and CSS
o Set the URL of the image, and show it only once (no-repeat)
o Position the image where you want it (left 0px and down 5px)
o Position the text in the list with padding-left
CSS Tables
Table Borders
To specify table borders in CSS, use the border property.
The example below specifies a black border for table, th, and td elements:
Example
table, th, td
{
border: 1px solid black;
}
Notice that the table in the example above has double borders. This is because both the table, th,
and td elements have separate borders.
To display a single border for the table, use the border-collapse property.
Collapse Borders
The border-collapse property sets whether the table borders are collapsed into a single border or
separated:
Example
table
{
border-collapse:collapse;
}
table,th, td
{
border: 1px solid black;
}
Example
td
{
text-align:right;
}
The vertical-align property sets the vertical alignment, like top, bottom, or middle:
Example
td
{
height:50px;
vertical-align:bottom;
}
Table Padding
To control the space between the border and content in a table, use the padding property on td
and th elements:
Example
td
{
padding:15px;
}
Table Color
The example below specifies the color of the borders, and the text and background color of th
elements:
Example
table, td, th
{
border:1px solid green;
}
th
{
background-color:green;
color:white;
}