Chris Walshaw Computing & Information Systems University of Greenwich
Chris Walshaw Computing & Information Systems University of Greenwich
Chris Walshaw
Computing & Information Systems
University of Greenwich
1
Lecture Objectives
• An introduction to HTML
tags & attributes
anatomy of a webpage
some basic HTML elements
• A little about CSS
location & usage
• HTML forms
interaction with JavaScript
DOM manipulation
form input types
2
Motivation
3
HTML
5
Tag syntax
• An HTML element usually consists of a start tag and end tag,
with the content inserted in between, e.g.
<title> This is the page title </title>
start tag content end tag
• The HTML element is everything from the start tag to the end
tag
• Extra whitespace / line-breaks in the HTML are ignored by the
browser
i.e. the following is essentially identical to the version above
<title>
This is the page title
</title>
6
Attributes
8
Nesting
• Tags may be nested, e.g.
<head>
<title>
This is the page title
</title>
</head>
1
1
Anatomy of a web page
• Each webpage should start with a doctype tag
which declares which version of HTML is being
used
• Everything else should be enclosed in between an
<html> start and end tag
optionally the start tag may declare the language of the
content as an attribute
this is the doctype for HTML5
<!doctype html>
html start tag (also declares
<html lang="en"> the content to be English)
page content
...
html end tag
</html>
1
2
The page <head>
1
3
Basic HTML (Example 01)
1
7
Table
1
8
<table> elements
1
9
List elements
2
0
Hyperlinks
2
2
Link examples
• Links in a list
2
3
A little about CSS
http://www.w3schools.com/html/html_css.asp 2
4
CSS location
• CSS code is often placed in an external file which
is then applied to all the pages on a website via a
<link> tag in each page
in fact all the course examples use a single CSS file (there
are duplicate copies of it in each week’s download)
• CSS code which is just intended for a single page
can be embedded in the <head> of that page
using a <style> element
• CSS code can also be used inline via a style
attribute in individual HTML elements
generally not recommended
2
5
CSS examples
(Example 01)
• External and embedded CSS code
embedded CSS
2
8
The form
2
9
Including JavaScript
http://www.w3schools.com/js/js_whereto.asp 3
1
Connecting JS to HTML
3
3
The Document Object
Model (DOM)
• In fact JS has access to the entire page (all the HTML
elements) via the Document Object Model (DOM)
• This is a model of the page created by the browser
when it loads the HTML code
• JS can change any of the elements in the HTML
document – e.g.
change content
change attributes
change CSS styles
add or remove HTML elements
• This obviously makes JS very powerful!
3
4
DOM manipulation
(Example 03)
• In the next example we will look at some simple DOM
manipulation
• The page just contains a form with a single button
it also has a <div> with no content (invisible initially)
• When the button is pressed JS adds a <p> paragraph
element to the <div> saying how many times it has
been pressed
• JS also changes the button (both the text and the font
size) each time it is pressed
• Finally JS changes the background colour of the page
and the form once the button has been pressed 4 times
3
5
DOM manipulation
(Example 03)
• The different page versions
3
6
The HTML code
3
7
Button press count
3
8
Changing the button
3
9
Changing the <div>
4
0
Changing the background
colour
• Finally to change the background colour
4
1
HTML form <input> types
4
5