HTML 5, CSS3 and Javascript

Sebastian Danicic

Beginning HTML

A Template

Copy this code into a file called template.html and open it with a web browser.
<!doctype html5 lang="en">
<html>
	<head>
		<meta charset="utf-8">
		<title> Document Title </title>
		<meta name="author" content="Seb">
		<meta name="description" content="HTML Course">
	</head>
	<body>
		<h1>Web Page title</h1>
		<h2>Web Page Sub-title</h2>
		<article>
			<h1>Article Heading</h1>
			<section>
				<h1>Section 1 heading</h1>
				<p>
					Some text
				</p>
			</section>
			<section>
				<h1>Section 2 heading</h1>
			</section>
		</article>
	</body>
</html>

You should see this. Copy and paste the template above into a text editor (Bluefish or Notepad or whichever is your favourite). Write an HTML document about your family, or what you did in the summer holidays, or your best friend or your hobbies. Watch this video if you need more help.

HTML Editors

If you use an editor which knows about it may make things easier for you. These editors help you to write correct HTML and give you hints about what to do next. Some people find Bluefish quite useful for this. You can install Bluefish on your computer. Here is a list of HTML editors that you can choose from. Try some of these out.

The Structure of an HTML Document

An HTML document is made up of elements.

Elements

  1. Normal Elements : These elements can contain other elements.
  2. Text Elements : These elements do not contain other elements. They are just text.
  3. Void Elements : These elements do not contain other elements. They contain nothing. For example IMG .

The bits in angled brackets < and > are called tags . When we write elements, all except the void elements have a start tag and an end tag.

The html code, above, has an HTML element which starts with the start tag:

<HTML>
and ends with the end tag:
</HTML>
The HTML element, above has two children:
  1. A HEAD element.
  2. A BODY element.

Again, we see the HEAD element which starts with the start tag:

<HEAD>
and ends with the end tag:
</HEAD>
and the BODY element which starts with the start tag:
<BODY>
and ends with the end tag:
</BODY>

Continuing, further, the HEAD element, above, has four children, a META element, followed by a TITLE element and then two more META elements.

Then BODY element has three children: an H1 element, follwed by an H2 element and finally an article element.

How many children does the article element in this example have?

META elements are examples of Void Elements : These elements do not contain other elements. They contain nothing. Since META elements contain nothing they do not have an end tag.

Element Attributes

Some elements are written like this:

<meta name="author" content="Seb">
Inside the start tag for elements we can have lists of attributes each of which can have a value. This meta element above has two attributes: name and content which have the values "author" and "Seb" respectively.

Another example of an element with attributes is the A element that is used for links (we will explore this later).


<a href="http://www.google.co.uk">click here</a>
This one has one attribute href with value "http://www.google.co.uk".

The HTML Document Tree

The HTML above gives rise to the following strucure:

                              html
			      /  \
	    ------------------    ---------------
	   |                                    | 
	   |                                    | 
	  head                                 body
	 / |  | \                             /  | \
        /  |  |  \                           /   |  \
       /   |  |   \                         /    |   \
   meta title meta meta                    h1    h2   --------------\
           |                              /      |                   \
           |                             /       |                    \ 
   "Document Title"      "Web Page title"  "Web Page Sub-title"      article\
  								   /	|    \ 
                                                                  /     |      -----\  
                                                                 /      |            \
                                                                h1     section        section
							       /        |     \              \
                                                              /         |      \              \
							     /          |       \              \
					     "Article Heading"          h1       p              h1
					                                |         \              \
									|          \              \
		
						"Section 1 heading" "some text"   "Section 2 heading"

This structure is called the document tree. Trees have a root which is the element at the top and a (possibly empty) list of children. Each child itself is a tree. Notice, we have not included the attributes in the tree.

Trees are very important in computer science. If you would like to read more aout trees please see here.

If you would like to read more about the formal structure of elements please see www.w3.org/TR/html5/syntax.html#elements-0.

Draw trees for the following HTML code
  1. <html>
    	<head>
    		<meta charset="utf-8">
    		<title> Starts and Stripes </title>
    		<meta name="author" content="Freddy">
    		
    	</head>
    	<body>
    		<h1>Fish Fingers</h1>
    		<article>
    			<h1>Article Heading</h1>
    			<section>
    				<h1>Section 1 heading</h1>
    			        <p>Chips</p>
    			</section>
    			
    		</article>
    	</body>
    </html>
    
  2. <html>
    	<head>
    		<meta charset="utf-8">
    		<title> Document Title </title>
    		<meta name="author" content="Seb">
    		<meta name="description" content="HTML Course">
    	</head>
    	<body>
    		<article>
    			<h1>Article Heading</h1>
    			<section>
    				<h1>one</h1>
    				<p>click <a href="bla"> here</a></p>
    			</section>
    			<section>
    				<h1>two</h1>
    			</section>
    			<section>
    				<h1>three</h1>
    			</section>
    		</article>
    	</body>
    </html>
    

Checking the Syntax of you HTML

You can check your HTML for valid syntax here. Always check your HTML code before completing it.

Use the HTML validator at http://validator.w3.org/check to check the syntax of the HTML code you wrote earlier. If there are errors correct them all.

Adding Images (Pictures) to your Web Pages

To add images to a web page we use the IMG element. This is an example of a void element. It does not need an end tag since it doesn't have any children. Here is an example: <img src="images/flea-1.jpg">

This displays:

assuming you have got an image called flea-1.jpg in the images sub-directory (folder) of the directory containing your HTML document. If you don't have the image in the right place you will see:

which means your browser cannot find the image.

You can also display images that exist somewhere else on the Web. For example

<img src="http://www.google.co.uk/logos/groundhog.gif">

Will Display:

Changing the Displayed Size of your Image

Now try:

<img src="images/flea-1.jpg" width=100>

You will see the smaller flea:

We have added a width attribute to this IMG element and given it the value 100.

Add some images either from your own camera or from the Web to your HTML document. Experiment with different widths.
  1. Here is more of an explanation of the IMG element. Experiment with all the other (non-deprecated) attributes to display your images differently.
  2. If you don't know already, find out what deprecated means.

Use the site http://www.w3schools.com. It is an incredibly valuable resource if you are new to creating web pages.

Style

Now look at:
<!doctype html5 lang="en">
<html>
	<head>
		<meta charset="utf-8">
		<title> Document Title </title>
		<meta name="author" content="Seb">
		<meta name="description" content="HTML Course">
		<style type="text/css">
		      
			 h1{
			 	color:red;
			   }	 
		</style>
	</head>
	<body>
		<h1>Web Page title</h1>
		<h2>Web Page Sub-title</h2>
		<article>
			<h1>Article Heading</h1>
			<section>
				<h1>Section 1 heading</h1>
			</section>
			<section>
				<h1>Section 2 heading</h1>
			</section>
		</article>
	</body>
</html>
We have added some style in the header:
		<style type="text/css">
		      
			 h1{
			 	color:red;
			   }	 
		</style>
This means h1 tags will all appear red. You should see this. Here is the style that produced this web page
		      
		      
		      
			 body{
			        background-image: url(silly4.jpg);
			   
			   }
			   
			 body > h1{
			 	text-align:center;
			   }	 
			  
			 body > h2{
			 	text-align:center;
			   }
			   
			 article > h1{
			 	color:red;
			 	
			   }
			   
			 section > h1{
			 	color:blue;
			 	
			 exercise:before
			     {content: "Exercise: ";}
			 
			diagram{
				display:block;
				text-decoration:none;

				border: 5px double;
				color: #ffffff;
				background-color: #404090;
				margin: 5px 5px 9px 5px;
				padding: 15px 0;
				position: relative;		
			       } 
			       
			   exercise{
			 	display:block;
				text-decoration:none;
				border: 5px double;
				color: #ffffff;
				background-color: green;
				margin: 5px 5px 9px 5px;
				padding: 15px 0;
				position: relative;				        
								
				}          
	


A Typical Flea

How to Write HTML

Here is some HTML: This essay reveals the importants of fleas. Go To bit about templates

This

Article is inside another article!

This one

is nested even further!

This

Article one is nested only once.

Java Script and Firebug

watch this video.

watch this video.

watch this video.

watch this video about if statements.

watch this video.

watch this video.

Different Sorts of Input

To do anything interesting in a web page (meaningful interaction for example) we need take input in from a user and process it. Below, I show different kinds of input, some which work (ansd some which don't) wiith different browsers. I have included a simple bit of javascript with each one which displays the value of each input.

Simple Text Input

<p>
Result: <output type=text id="result0" value="" readonly> </output>
</p>

<input type=text onchange="document.getElementById('result0').value=this.value;">

Result:

Type someing in here and when you press enter or click the mouse outside the box it appears in the result above.

Make it so that two copies of the input string appear next to result: (Hint: use +).

Integer Input

<p>
Result: <output type=text id="resultInt1" value="" readonly> </output>
</p>

<input type=text onchange="document.getElementById('resultInt1').value=parseInt(this.value)+1;">

Result:

Type someing in here and when you press enter or click the mouse outside the box one more than the input appears in the result above. Notice we have used the parseInt function. You can read about it here .

Change it so that the result is double the input.

Buttons

<input type="button" value="Click me!" onclick="alert('hello')" />
Change it so that there is another button called "bing". When you click on bing it alerts "goodbye".
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>

<input type="button" value="Click me again!" onclick="displaymessage()"/>

This example is very important because it shows how to define and the call a function. Understanding this is crucial in programming. Rewrite all the previous examples to use functions.
<p>
<input id="Number1"> 
</p>


<p>
<input id="Number2"> 
</p>

<p>
<input type="button" value="Add up the two numbers!" 
onclick="alert(document.getElementById('Number1').value + document.getElementById('Number2').value)"/>
</p>

Try the above. It doesn't quite work. Correct it! Noice the use of the document.getElementById() and its .value attribute. These are very useful. Rewrite the above example so it uses an output element like 'result' instead of an alert. Like this:

Result:

Your button code must be this:
<input type="button" value="Add up the two numbers!" onclick="addUpNumbers()"/>
So you must write your own addUpNumbers() function.

Prompts

<script type="text/javascript">
function addTwo()
{
var x= prompt("enter first");
var y= prompt("enter second");
var z = parseInt(x)+parseInt(y);
alert(x + "+" + y + "=" +  z);
}
</script>


<input type="button" value="Add up the two numbers!"
onclick="addTwo()"/>

Watch this video about if statements and write a web page for checking whether years are leap years or not.

bla: Try this:
<script type="text/javascript">
function getY()
{
var d=new Date()
var x=d.getFullYear(); 
alert(x);
}
</script>


<input type="button" value="this year" onclick="getY()"/>

Read this.

Write a web page where the user inputs their date of birth and the web page displays the user's age in years. This program will need to use today's date! Try this
<img id="die" src="2.png">
<input type="button" value="shake" onclick="getElementById('die').setAttribute('src','4.png')"/>

A Text Area

<textarea  rows=10 cols=6 onchange="document.getElementById('resultA').value = this.value;"> </textarea>
Write a web page which emulates throwing a dice. You will need 6 images of each face of the dice and you also need to work out houw to generate random numbers between 0 and 5 in Javascript.

Result:

Type someing in here and when you click the mouse outside the box it appears in the result above.

A Date Input

Some browsers will show nice calenders to let you input a date!
<input  type=date onchange="document.getElementById('result1').value = this.value;">

Result:

Result:

Result:

input[type="search"] {-webkit-appearance: textfield;}

Result:

red blue green

5