Вы находитесь на странице: 1из 55

HTML Forms

HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons, submit buttons and more. A form can also contain select lists, textarea, fieldset, legend, and label elements. The <form> tag is used to create an HTML form: <form> . input elements . </form>

HTML Forms - The Input Element


The most important form element is the input element. The input element is used to select user information. An input element can vary in many ways, depending on the type attribute. An input element can be of type text field, checkbox, password, radio button, submit button, and more. The most used input types are described below.

Text Fields
<input type="text" /> defines a one-line input field that a user can enter text into: <form> First name: <input type="text" name="firstname" /><br /> Last name: <input type="text" name="lastname" /> </form> How the HTML code above looks in a browser:
Top of Form

First name: Last name:


Bottom of Form

Note: The form itself is not visible. Also note that the default width of a text field is 20 characters.

Password Field
<input type="password" /> defines a password field:

<form> Password: <input type="password" name="pwd" /> </form> How the HTML code above looks in a browser:
Top of Form

Password:
Bottom of Form

Note: The characters in a password field are masked (shown as asterisks or circles).

Radio Buttons
<input type="radio" /> defines a radio button. Radio buttons let a user select ONLY 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> How the HTML code above looks in a browser:
Top of Form

Male Female
Bottom of Form

Checkboxes
<input type="checkbox" /> defines a checkbox. Checkboxes let a user select ONE or MORE options of a limited number of choices. <form> <input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br /> <input type="checkbox" name="vehicle" value="Car" /> I have a car </form> How the HTML code above looks in a browser:
Top of Form

I have a bike I have a car


Bottom of Form

Submit Button
2

<input type="submit" /> defines a submit button. A submit button is used to send form data to a server. The data is sent to the page specified in the form's action attribute. The file defined in the action attribute usually does something with the received input: <form name="input" action="html_form_action.asp" method="get"> Username: <input type="text" name="user" /> <input type="submit" value="Submit" /> </form> How the HTML code above looks in a browser:
Top of Form

Username:
Bottom of Form

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_action.asp". The page will show you the received input.

What is CSS?

CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to solve a problem External Style Sheets can save a lot of work External Style Sheets are stored in CSS files

CSS Demo
An HTML document can be displayed with different styles: See how it works

Styles Solved a Big Problem


HTML was never intended to contain tags for formatting a document. HTML was intended to define the content of a document, like: <h1>This is a heading</h1> <p>This is a paragraph.</p> When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for web developers. Development of large web sites, where fonts and color information were added to every single page, became a long and expensive process. To solve this problem, the World Wide Web Consortium (W3C) created CSS. In HTML 4.0, all formatting could be removed from the HTML document, and stored in a separate CSS file. All browsers support CSS today.

CSS Saves a Lot of Work!


CSS defines HOW HTML elements are to be displayed. Styles are normally saved in external .css files. External style sheets enable you to change the appearance and layout of all the pages in a Web site, just by editing one single file! The HTML file below links to an external style sheet with the <link> tag:
<html> <head> <link rel="stylesheet" type="text/css" href="ex1.css" /> </head> <body> <h1>This header is 36 pt</h1>

<h2>This header is blue</h2> <p>This paragraph has a left margin of 50 pixels</p> </body> </html>

This is the style sheet file (ex1.css):


body { background-color:yellow; } h1 { font-size:36pt; } h2 { color:blue; } p { margin-left:50px; }

The result is in the frame below:

This header is 36 pt

This header is blue


This paragraph has a left margin of 50 pixels The HTML file below links to an external style sheet with the <link> tag:
<html> <head> <link rel="stylesheet" type="text/css" href="ex2.css" /> </head> <body> <h1>This is a header 1</h1> <hr /> <p>You can see that the style sheet formats the text</p> <p><a href="http://www.w3schools.com" target="_blank">This is a link</a></p> </body> </html>

This is the style sheet file (ex2.css):


body {background-color:tan;} h1 {color:maroon;font-size:20pt;} hr {color:navy;} p {font-size:11pt;margin-left:15px;} a:link {color:green;} a:visited {color:yellow;} a:hover {color:black;} a:active {color:blue;}

The result is in the frame below:

This is a header 1
You can see that the style sheet formats the text This is a link

CSS Syntax
A CSS rule has two main parts: a selector, and one or more declarations:

The selector is normally the HTML element you want to style. Each declaration consists of a property and a value. The property is the style attribute you want to change. Each property has a value.

CSS Example
A CSS declaration 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; } Try it yourself

CSS Comments
6

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 and class Selectors


In addition to setting a style for a HTML element, CSS allows you to specify your own selectors called "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; } Try it yourself

Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.

The 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 "." In the example below, all HTML elements with class="center" will be center-aligned:

Example
.center {text-align:center;} Try it yourself 8

<html> <head> <style type="text/css"> .center { text-align:center; } </style> </head>

<body> <h1 class="center">Center-aligned heading</h1> <p class="center">Center-aligned paragraph.</p> </body> </html> o/p:

Center-aligned heading
Center-aligned paragraph. 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;}

Three Ways to Insert CSS


There are three ways of inserting a style sheet:
9 External style sheet Internal style sheet

Inline style

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 changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section:
<head> <link rel="stylesheet" type="text/css" href="mystyle.css" /> </head>

An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below:
hr {color:sienna;} p {margin-left:20px;} body {background-image:url("images/back40.gif");}

Do not leave spaces between the property value and the units! "margin-left:20 px" (instead

of "margin-left:20px") will work in IE, but not in Firefox or Opera.

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>

Inline Styles
An inline style loses many of the advantages of style sheets by mixing content with presentation. Use this method sparingly!

10

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>

Multiple Style Sheets


If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet. For example, an external style sheet has these properties for the h3 selector:
h3 { color:red; text-align:left; font-size:8pt; }

And an internal style sheet has these properties for the h3 selector:
h3 { text-align:right; font-size:20pt; }

If the page with the internal style sheet also links to the external style sheet the properties for h3 will be:
color:red; text-align:right; font-size:20pt;

The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the internal style sheet.

Multiple Styles Will Cascade into One


Styles can be specified:
inside an HTML element inside the head section of an HTML page in an external CSS file

Tip: Even multiple external style sheets can be referenced inside a single HTML document.
11

Cascading order

What style will be used when there is more than one style specified for an HTML element? Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority:
1. Browser default 2. External style sheet 3. Internal style sheet (in the head section) 4. Inline style (inside an HTML element)

So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the <head> tag, or in an external style sheet, or in a browser (a default value). Note: If the link to the external style sheet is placed after the internal style sheet in HTML

<head>, the external style sheet will override 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;} Try it yourself

With CSS, a color is most often specified by:


12 a HEX value - like "#ff0000" an RGB value - like "rgb(255,0,0)" a color name - like "red"

Look at CSS Color Values for a complete list of possible color values. In the example below, the h1, p, and div elements have different background colors:

Example
h1 {background-color:#6495ed;} p {background-color:#e0ffff;} div {background-color:#b0c4de;} Try it yourself

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');} Try it yourself

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');} Try it yourself

Background Image - Repeat Horizontally or Vertically


By default, the background-image property repeats an image both horizontally and vertically. Some images should be repeated only horizontally or vertically, or they will look strange, like this:

Example
13

body { background-image:url('gradient2.png'); } Try it yourself

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; } Try it yourself

Background Image - Set position and no-repeat


When using a background image, use an image that does not disturb the text.

Showing the image only once is specified by the background-repeat property:

Example
body { background-image:url('img_tree.png'); background-repeat:no-repeat; } Try it yourself

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
14

body { background-image:url('img_tree.png'); background-repeat:no-repeat; background-position:right top; } Try it yourself

Background - Shorthand property


As you can see from the examples above, there are many properties to consider when dealing with backgrounds. To shorten the code, it is also possible to specify all the properties in one single property. This is called a shorthand property. The shorthand property for background is simply "background":

Example
body {background:#ffffff url('img_tree.png') no-repeat right top;} Try it yourself

When using the shorthand property the order of the property values are:
background-color background-image background-repeat background-attachment background-position

It does not matter if one of the property values is missing, as long as the ones that are present are in this order. This example uses more advanced CSS. Take a look: Advanced example

More Examples
15

How to set a fixed background image This example demonstrates how to set a fixed background image. The image will not scroll with the rest of the page.

All CSS Background Properties


Property background background-attachment background-color background-image background-position background-repeat Description Sets all the background properties in one declaration Sets whether a background image is fixed or scrolls with the rest of the page Sets the background color of an element Sets the background image for an element Sets the starting position of a background image Sets how a background image will be repeated

CSS Text TEXT FORMATTING


This text is styled with properties. The heading uses the color properties. The paragraph is between characters is specified. the "Try it yourself" link. some of the text formatting text-align, text-transform, and indented, aligned, and the space The underline is removed from

Text Color
The color property is used to set the color of the text. With CSS, a color is most often specified by:
a HEX value - like "#ff0000" an RGB value - like "rgb(255,0,0)" a color name - like "red"

Look at CSS Color Values for a complete list of possible color values. The default color for a page is defined in the body selector.

Example

16

body {color:blue;} h1 {color:#00ff00;} h2 {color:rgb(255,0,0);} Try it yourself

For W3C compliant CSS: If you define the color property, you must also define the

background-color property.

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;} Try it yourself

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;} Try it yourself

It can also be used to decorate text:

Example
17

h1 h2 h3 h4

{text-decoration:overline;} {text-decoration:line-through;} {text-decoration:underline;} {text-decoration:blink;}

Try it yourself

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;} Try it yourself

Text Indentation
The text-indentation property is used to specify the indentation of the first line of a text.

Example
p {text-indent:50px;} Try it yourself

18

More Examples
Specify the space between characters This example demonstrates how to increase or decrease the space between characters. Specify the space between lines This example demonstrates how to specify the space between the lines in a paragraph. Set the text direction of an element This example demonstrates how to change the text direction of an element. Increase the white space between words This example demonstrates how to increase the white space between words in a paragraph. Disable text wrapping inside an element This example demonstrates how to disable text wrapping inside an element. Vertical alignment of an image This example demonstrates how to set the vertical align of an image in a text.

All CSS Text Properties


Property color direction letterspacing line-height text-align textdecoration text-indent Description Sets the color of text Specifies the text direction/writing direction Increases or decreases the space between characters in a text Sets the line height Specifies the horizontal alignment of text Specifies the decoration added to text Specifies the indentation of the first line in a textblock

text-shadow Specifies the shadow effect added to text texttransform unicode-bidi verticalalign Sets the vertical alignment of an element Controls the capitalization of text

white-space Specifies how white-space inside an element is 19

handled wordspacing Increases or decreases the space between words in a text

All CSS Font Properties


Property font font-family font-size font-style font-variant font-weight Description Sets all the font properties in one declaration Specifies the font family for text Specifies the font size of text Specifies the font style for text Specifies whether or not a text should be displayed in a small-caps font Specifies the weight of a font

CSS Links
Links can be styled in different ways.

Styling Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.). 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 */ Try it yourself 20

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

Common Link Styles


In the example above the link changes color depending on what state it is in. Lets go through some of the other common ways to style links:

Text Decoration
The text-decoration property is mostly used to remove underlines from links:

Example
a:link {text-decoration:none;} a:visited {text-decoration:none;} a:hover {text-decoration:underline;} a:active {text-decoration:underline;} Try it yourself

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;} Try it yourself

More Examples
Add different styles to hyperlinks This example demonstrates how to add other styles to hyperlinks.

21

Advanced - Create link boxes This example demonstrates a more advanced example where we combine several CSS properties to display links as boxes.

CSS Lists
The CSS list properties allow you to:
Set different list item markers for ordered lists Set different list item markers for unordered lists Set an image as the list item marker

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.

Different List Item Markers


The type of list item marker is specified with the list-style-type property:

Example
ul.a {list-style-type: circle;} ul.b {list-style-type: square;} ol.c {list-style-type: upper-roman;} ol.d {list-style-type: lower-alpha;} Try it yourself

Some of the values are for unordered lists, and some for ordered lists.

An Image as The List Item Marker


To specify an image as the list item marker, use the list-style-image property:

Example
ul { 22

list-style-image: url('sqpurple.gif'); } Try it yourself

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.

Crossbrowser Solution
The following example displays the image-marker equally in all browsers:

Example
ul { list-style-type: none; padding: 0px; margin: 0px; } li { background-image: url(sqpurple.gif); background-repeat: no-repeat; background-position: 0px 5px; padding-left: 14px; } Try it yourself

Example explained:
For ul: For li: Set the URL of the image, and show it only once (no-repeat) Position the image where you want it (left 0px and down 5px) Position the text in the list with padding-left Set the list-style-type to none to remove the list item marker Set both padding and margin to 0px (for cross-browser compatibility)

List - Shorthand property


23

It is also possible to specify all the list properties in one, single property. This is called a shorthand property. The shorthand property used for lists, is the list-style property:

Example
ul { list-style: square url("sqpurple.gif"); } Try it yourself

When using the shorthand property, the order of the values are:
list-style-type list-style-position (for a description, see the CSS properties table below) list-style-image

It does not matter if one of the values above are missing, as long as the rest are in the specified order.

More Examples
All the different list-item markers for lists This example demonstrates all the different list-item markers in CSS.

All CSS List Properties


Property list-style Description Sets all the properties for a list in one declaration

list-style-image Specifies an image as the list-item marker list-styleposition list-style-type Specifies if the list-item markers should appear inside or outside the content flow Specifies the type of list-item marker

CSS Tables

Table Borders
24

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; } Try it yourself

Notice that the table in the example above has double borders. This is because both the table and the th/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; } Try it yourself

Table Width and Height


Width and height of a table is defined by the width and height properties. The example below sets the width of the table to 100%, and the height of the th elements to 50px:

Example
table { 25

width:100%; } th { height:50px; } Try it yourself

Table Text Alignment


The text in a table is aligned with the text-align and vertical-align properties. The text-align property sets the horizontal alignment, like left, right, or center:

Example
td { text-align:right; } Try it yourself

The vertical-align property sets the vertical alignment, like top, bottom, or middle:

Example
td { height:50px; vertical-align:bottom; } Try it yourself

Table Padding
To control the space between the border and content in a table, use the padding property on td and th elements:
26

Example
td { padding:15px; } Try it yourself

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; } example: <html> <head> <style type="text/css"> #customers { font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; width:100%; border-collapse:collapse; } 27

#customers td, #customers th { font-size:1em; border:1px solid #98bf21; padding:3px 7px 2px 7px; } #customers th { font-size:1.1em; text-align:left; padding-top:5px; padding-bottom:4px; background-color:#A7C942; color:#ffffff; } #customers tr.alt td { color:#000000; background-color:#EAF2D3; } </style> </head>

<body> <table id="customers"> <tr>

28

<th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr class="alt"> <td>Berglunds snabbkp</td> <td>Christina Berglund</td> <td>Sweden</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr class="alt"> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td>

29

<td>Helen Bennett</td> <td>UK</td> </tr> <tr class="alt"> <td>Kniglich Essen</td> <td>Philip Cramer</td> <td>Germany</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr class="alt"> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> <tr> <td>North/South</td> <td>Simon Crowther</td> <td>UK</td> </tr> <tr class="alt"> <td>Paris spcialits</td> <td>Marie Bertrand</td>

30

<td>France</td> </tr> </table> </body> </html>

31

Example2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <style type="text/css"> caption {caption-side:bottom;} </style> </head>

<body>

<table border="1"> <caption>Table 1.1 Customers</caption> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Berglunds snabbkp</td> 32

<td>Christina Berglund</td> <td>Sweden</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> <tr> <td>North/South</td> <td>Simon Crowther</td>

33

<td>UK</td> </tr> </table>

<p><b>Note:</b> IE8 supports the caption-side property if a !DOCTYPE is specified.</p> </body> </html> Repeat background image <html> <head> <style type="text/css"> body { background-image:url('gradient2.png'); background-repeat:repeat-x; } </style> </head>

<body> <h1>Hello World!</h1> </body> </html> Example2: <html>

34

<head> <style type="text/css"> body { margin-left:200px; background:#5d9ab2 url('img_tree.png') no-repeat top left; }

.container { text-align:center; }

.center_div { border:1px solid gray; margin-left:auto; margin-right:auto; width:90%; background-color:#d0f0f6; text-align:left; padding:8px; } </style> </head> Text wrape:

35

<html> <head> <style type="text/css"> p { white-space:nowrap; } </style> </head> <body>

<p> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </p>

</body> </html>

36

Control the letters in a text: <html> <head> <style type="text/css"> p.uppercase {text-transform:uppercase;} p.lowercase {text-transform:lowercase;} p.capitalize {text-transform:capitalize;} </style> </head>

<body> <p class="uppercase">This is some text.</p> <p class="lowercase">This is some text.</p> <p class="capitalize">This is some text.</p> </body> </html> Style of button borders: <html> <head> <style type="text/css"> p {border-style:solid;} p.none {border-bottom-style:none;} p.dotted {border-bottom-style:dotted;} p.dashed {border-bottom-style:dashed;} p.solid {border-bottom-style:solid;} p.double {border-bottom-style:double;} 37

p.groove {border-bottom-style:groove;} p.ridge {border-bottom-style:ridge;} p.inset {border-bottom-style:inset;} p.outset {border-bottom-style:outset;} </style> </head>

<body> <p class="none">No bottom border.</p> <p class="dotted">A dotted bottom border.</p> <p class="dashed">A dashed bottom border.</p> <p class="solid">A solid bottom border.</p> <p class="double">A double bottom border.</p> <p class="groove">A groove bottom border.</p> <p class="ridge">A ridge bottom border.</p> <p class="inset">An inset bottom border.</p> <p class="outset">An outset bottom border.</p> </body> </html> Changing of cursor <html> <body> <p>Mouse over the words to change the cursor.</p> <span style="cursor:auto">auto</span><br /> <span style="cursor:crosshair">crosshair</span><br /> <span style="cursor:default">default</span><br /> <span style="cursor:e-resize">e-resize</span><br /> <span style="cursor:help">help</span><br /> <span style="cursor:move">move</span><br /> 38

<span style="cursor:n-resize">n-resize</span><br /> <span style="cursor:ne-resize">ne-resize</span><br /> <span style="cursor:nw-resize">nw-resize</span><br /> <span style="cursor:pointer">pointer</span><br /> <span style="cursor:progress">progress</span><br /> <span style="cursor:s-resize">s-resize</span><br /> <span style="cursor:se-resize">se-resize</span><br /> <span style="cursor:sw-resize">sw-resize</span><br /> <span style="cursor:text">text</span><br /> <span style="cursor:w-resize">w-resize</span><br /> <span style="cursor:wait">wait</span><br /> </body> </html> Set the browser automatically <html> <head> <style type="text/css"> div { background-color:#00FFFF; width:150px; height:150px; overflow:auto; } </style> </head>

<body>

39

<p>The overflow property decides what to do if the content inside an element exceeds the given width and height properties.</p>

<div> You can use the overflow property when you want to have better control of the layout. Try to change the overflow property to: visible, hidden, scroll, or inherit and see what happens. The default value is visible. </div> </body> </html> Horizontal menu <html> <head> <style type="text/css"> ul { float:left; width:100%; padding:0; margin:0; list-style-type:none; } a { float:left; width:6em; text-decoration:none; color:white; background-color:purple; padding:0.2em 0.6em;

40

border-right:1px solid white; } a:hover {background-color:#ff3300;} li {display:inline;} </style> </head>

<body> <ul> <li><a href="#">Link one</a></li> <li><a href="#">Link two</a></li> <li><a href="#">Link three</a></li> <li><a href="#">Link four</a></li> </ul>

<p> In the example above, we let the ul element and the a element float to the left. The li elements will be displayed as inline elements (no line break before or after the element). This forces the list to be on one line. The ul element has a width of 100% and each hyperlink in the list has a width of 6em (6 times the size of the current font). We add some colors and borders to make it more fancy. </p>

</body> </html> Horizontal navigation bar: <html> <head> <style type="text/css"> 41

ul { list-style-type:none; margin:0; padding:0; overflow:hidden; } li { float:left; } a:link,a:visited { display:block; width:120px; font-weight:bold; color:#FFFFFF; background-color:#98bf21; text-align:center; padding:4px; text-decoration:none; text-transform:uppercase; } a:hover,a:active { background-color:#7A991A; }

</style> 42

</head>

<body> <ul> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> </body> </html>

43

JavaScript Objects Introduction


avaScript is an Object Based Programming language. An Object Based Programming language allows you to define your own objects and make your own variable types.

Object Based Programming


JavaScript is an Object Based Programming language, and allows you to define your own objects and make your own variable types. However, creating your own objects will be explained later, in the Advanced JavaScript section. We will start by looking at the built-in JavaScript objects, and how they are used. The next pages will explain each built-in JavaScript object in detail. Note that an object is just a special kind of data. An object has properties and methods.

Properties
Properties are the values associated with an object. In the following example we are using the length property of the String object to return the number of characters in a string: <script type="text/javascript"> var txt="Hello World!"; document.write(txt.length); </script> The output of the code above will be: 12

Methods
Methods are the actions that can be performed on objects. In the following example we are using the toUpperCase() method of the String object to display a text in uppercase letters: <script type="text/javascript"> var str="Hello world!"; document.write(str.toUpperCase()); </script> The output of the code above will be: HELLO WORLD!

44

JavaScript String Object


The String object is used to manipulate a stored piece of text. <html> <body> <script type="text/javascript"> var txt = "Hello World!"; document.write(txt.length); </script> </body> </html>

The code above will result in the following output: 12


Style strings <html> <body>

<script type="text/javascript">

var txt = "Hello World!";

document.write("<p>Big: " + txt.big() + "</p>"); document.write("<p>Small: " + txt.small() + "</p>");

document.write("<p>Bold: " + txt.bold() + "</p>"); document.write("<p>Italic: " + txt.italics() + "</p>");

document.write("<p>Fixed: " + txt.fixed() + "</p>"); document.write("<p>Strike: " + txt.strike() + "</p>");

document.write("<p>Fontcolor: " + txt.fontcolor("green") + "</p>"); 45

document.write("<p>Fontsize: " + txt.fontsize(6) + "</p>");

document.write("<p>Subscript: " + txt.sub() + "</p>"); document.write("<p>Superscript: " + txt.sup() + "</p>");

document.write("<p>Link: " + txt.link("http://www.w3schools.com") + "</p>");

document.write("<p>Blink: " + txt.blink() + " (does not work in IE, Chrome, or Safari)</p>");

</script>

</body> </html> O/P:

Big: Hello World! Small: Hello World! Bold: Hello World! Italic: Hello World! Fixed: Hello World! Strike: Hello World! Fontcolor: Hello World! Fontsize:

Hello World!

Subscript: Hello World! Superscript: Hello World! Link: Hello World! Blink: Hello World! (does not work in IE, Chrome, or Safari)
Match() method: <html> <body>

46

<script type="text/javascript"> var str="Hello world!"; document.write(str.match("world") + "<br />"); document.write(str.match("World") + "<br />"); document.write(str.match("worlld") + "<br />"); document.write(str.match("world!")); </script>

</body> </html> O/P: world null null world! Replace the charecters in a string replace() <html> <body> <script type="text/javascript"> var str="Visit Microsoft!"; document.write(str.replace("Microsoft","W3Schools")); </script> </body> </html> O/P: Visit W3Schools! The indexof() method: How to return the position of the first found occurrence of a specified value in a string. <html> <body>

47

<script type="text/javascript"> var str="Hello world!"; document.write(str.indexOf("d") + "<br />"); document.write(str.indexOf("WORLD") + "<br />"); document.write(str.indexOf("world")); </script> </body> </html> O/P: 10 -1 6

JavaScript RegExp Object

RegExp Object
A regular expression is an object that describes a pattern of characters. Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text.

Syntax
var patt=new RegExp(pattern,modifiers); or more simply: var patt=/pattern/modifiers; pattern specifies the pattern of an expression modifiers specify if a search should be global, case-sensitive, etc.

For a tutorial about the RegExp object, read our JavaScript RegExp Object tutorial.

Modifiers
Modifiers are used to perform case-insensitive and global searches:
Modifier i g 48 Description Perform case-insensitive matching Perform a global match (find all matches rather than stopping

after the first match) m Perform multiline matching

Brackets
Brackets are used to find a range of characters:
Expression [abc] [^abc] [0-9] [A-Z] [a-z] [A-z] [adgk] [^adgk] (red|blue| green) Description Find any character between the brackets Find any character not between the brackets Find any digit from 0 to 9 Find any character from uppercase A to uppercase Z Find any character from lowercase a to lowercase z Find any character from uppercase A to lowercase z Find any character in the given set Find any character outside the given set Find any of the alternatives specified

Metacharacters
Metacharacters are characters with a special meaning:
Metacharacte Description r . \w \W \d \D \s 49 Find a single character, except newline or line terminator Find a word character Find a non-word character Find a digit Find a non-digit character Find a whitespace character

\S \b \B \0 \n \f \r \t \v \xxx \xdd \uxxxx

Find a non-whitespace character Find a match at the beginning/end of a word Find a match not at the beginning/end of a word Find a NUL character Find a new line character Find a form feed character Find a carriage return character Find a tab character Find a vertical tab character Find the character specified by an octal number xxx Find the character specified by a hexadecimal number dd Find the Unicode character specified by a hexadecimal number xxxx

Quantifiers
Quantifier n+ n* n? n{X} n{X,Y} n{X,} n$ ^n 50 Description Matches any string that contains at least one n Matches any string that contains zero or more occurrences of n Matches any string that contains zero or one occurrences of n Matches any string that contains a sequence of X n's Matches any string that contains a sequence of X to Y n's Matches any string that contains a sequence of at least X n's Matches any string with n at the end of it Matches any string with n at the beginning of it

?=n ?!n

Matches any string that is followed by a specific string n Matches any string that is not followed by a specific string n

RegExp Object Properties


Property Description global Specifies if the "g" modifier is set

ignoreCa Specifies if the "i" modifier is set se lastIndex The index at which to start the next match

multiline Specifies if the "m" modifier is set source The text of the RegExp pattern

RegExp Object Methods


Method compile() exec() test() Description Compiles a regular expression Tests for a match in a string. Returns the first match Tests for a match in a string. Returns true or false

51

Introduction to XML
XML was designed to transport and store data. HTML was designed to display data.

What is XML?
XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data XML tags are not predefined. You must define your own tags XML is designed to be self-descriptive XML is a W3C Recommendation

The Difference Between XML and HTML


XML is not a replacement for HTML. XML and HTML were designed with different goals:
XML was designed to transport and store data, with focus on what data is HTML was designed to display data, with focus on how data looks

HTML is about displaying information, while XML is about carrying information.

XML Does Not DO Anything


Maybe it is a little hard to understand, but XML does not DO anything. XML was created to structure, store, and transport information. The following example is a note to Tove, from Jani, stored as XML:
<note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

The note above is quite self descriptive. It has sender and receiver information, it also has a heading and a message body. But still, this XML document does not DO anything. It is just information wrapped in tags. Someone must write a piece of software to send, receive or display it.

52

With XML You Invent Your Own Tags


The tags in the example above (like <to> and <from>) are not defined in any XML standard. These tags are "invented" by the author of the XML document. That is because the XML language has no predefined tags. The tags used in HTML are predefined. HTML documents can only use tags defined in the HTML standard (like <p>, <h1>, etc.). XML allows the author to define his/her own tags and his/her own document structure.

XML is Not a Replacement for HTML


XML is a complement to HTML. It is important to understand that XML is not a replacement for HTML. In most web applications, XML is used to transport data, while HTML is used to format and display the data. My best description of XML is this: XML is a software- and hardware-independent tool for carrying information.

XML is a W3C Recommendation


XML became a W3C Recommendation on February 10, 1998. To read more about the XML activities at W3C, please read our W3C Tutorial.

XML is Everywhere
XML is now as important for the Web as HTML was to the foundation of the Web. XML is the most common tool for data transmissions between all sorts of applications. XML is used in many aspects of web development, often to simplify data storage and sharing.

XML Separates Data from HTML


If you need to display dynamic data in your HTML document, it will take a lot of work to edit the HTML each time the data changes. With XML, data can be stored in separate XML files. This way you can concentrate on using HTML for layout and display, and be sure that changes in the underlying data will not require any changes to the HTML. With a few lines of JavaScript code, you can read an external XML file and update the data content of your web page.

XML Simplifies Data Sharing


53

In the real world, computer systems and databases contain data in incompatible formats. XML data is stored in plain text format. This provides a software- and hardware-independent way of storing data. This makes it much easier to create data that can be shared by different applications.

XML Simplifies Data Transport


One of the most time-consuming challenges for developers is to exchange data between incompatible systems over the Internet. Exchanging data as XML greatly reduces this complexity, since the data can be read by different incompatible applications.

XML Simplifies Platform Changes


Upgrading to new systems (hardware or software platforms), is always time consuming. Large amounts of data must be converted and incompatible data is often lost. XML data is stored in text format. This makes it easier to expand or upgrade to new operating systems, new applications, or new browsers, without losing data.

XML Makes Your Data More Available


Different applications can access your data, not only in HTML pages, but also from XML data sources. With XML, your data can be available to all kinds of "reading machines" (Handheld computers, voice machines, news feeds, etc), and make it more available for blind people, or people with other disabilities.

XML is Used to Create New Internet Languages


A lot of new Internet languages are created with XML. Here are some examples: XHTML WSDL for describing available web services WAP and WML as markup languages for handheld devices RSS languages for news feeds RDF and OWL for describing resources and ontology SMIL for describing multimedia for the web

54

55

Вам также может понравиться