This topic covers website authoring: the three web development layers, HTML for content and CSS for presentation.
21.1 Web development layers
The three web development layers
| layer | what it does | language | example |
|---|---|---|---|
| content layer 内容层 | enters the content and creates the structure of the page: headings, paragraphs, tables, images, links | HTML 超文本标记语言 | <h1>Opening times</h1> |
| presentation layer 表现层 | displays and formats the elements: colours, fonts, sizes, borders 边框, layout | CSS 层叠样式表 | h1 {color: navy;} |
| behaviour layer 行为层 | uses a scripting language 脚本语言 to control elements, so the page responds to the user: pop-up messages, image slideshows, checking a form before it is sent | e.g. JavaScript | a button that shows a hidden answer |
- if all pages use the same external stylesheet 外部样式表, their shared styles can be changed by editing that stylesheet 样式表, without touching the content
- the same content can be presented differently to suit different devices
Worked example. A school's website has a table of term dates. The table and the dates are content (HTML). The table's blue header and borders are presentation (CSS). A button that highlights today's date is behaviour (a script).
21.2 Create a web page
Head and body
<!DOCTYPE html>
<html>
<head>
<title>Tawara Sports Club</title>
<meta charset="UTF-8">
<meta name="description" content="Sports club in Tawara">
<meta name="keywords" content="sport, tennis, swimming">
<meta name="author" content="A. Student">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<base target="_blank">
</head>
<body>
<h1>Welcome</h1>
<p>Open every day.</p>
</body>
</html>
- the head holds information about the page (not shown in the page itself): the title shown in the browser tab, metatags 元标签, attached stylesheets, and the default target window for links
- the body holds the content that is displayed
- metatags: charset (the character set, e.g. UTF-8), description and keywords (used by search engines), author, and viewport 视口 (makes the page display well on all devices)
- several stylesheets can be attached; when their rules for the same element are equally specific, the one attached last wins
Body elements
- headings
<h1><h2><h3>, paragraphs<p>; lists:<ol>(numbered),<ul>(bulleted), each item<li> - tables:
<table>, rows<tr>, header cells<th>, data cells<td>;colspan="2"androwspan="2"make a cell span columns or rows; sizes in pixels or %, e.g.width="80%" - tables are used to structure the page, placing text and images in rows and columns
- image:
<img src="logo.jpg" alt="Club logo" width="200" height="100">;altis alternative text shown if the image does not load and read by screen readers - video:
<video src="match.mp4" width="400" controls autoplay></video>; removecontrolsto hide them; audio:<audio src="song.mp3" controls></audio> <div>groups elements so a style or class 类 can be applied to them all:<div class="news">…</div>
Bookmarks and hyperlinks
- a bookmark 书签 is a named place on a page, made with an
id:<h2 id="fees">Fees</h2> - an anchor 锚点
<a>makes a hyperlink:
| link to | code |
|---|---|
| a bookmark on the same page | <a href="#fees">See fees</a> |
| another local page | <a href="contact.html">Contact</a> |
| a website | <a href="https://www.example.com">Example</a> |
| an email address | <a href="mailto:info@example.com">Email us</a> |
| an image as a link | <a href="home.html"><img src="logo.png" alt="Home"></a> |
- target:
target="_self"(same window),"_blank"(new window), or a name, e.g.target="results"(opens in the window named results)
File paths
- a relative path gives the location from the current page, e.g.
images/logo.png - an absolute path gives the full location, e.g.
C:/Users/Ann/site/images/logo.png - local pages and objects must use relative paths: an absolute path points to one computer's folders, so links break when the site is moved to a web server
21.3 Use stylesheets
Cascading stylesheets
An external stylesheet is a separate .css file that can be attached to one or more pages; an inline style 行内样式 is written inside one tag 标签 with the style attribute 属性.
/* styles.css: house style for the club website */
body { background-color: #f0f0f0; font-family: Arial, sans-serif; }
h1 { color: navy; font-size: 28px; text-align: center; }
p { font-size: 14px; }
table { border-collapse: collapse; width: 80%; }
th, td { border: 1px solid black; padding: 5px; text-align: left; vertical-align: top; }
.highlight { background-color: yellow; font-weight: bold; }
- comments in a stylesheet go between
/*and*/ - inline style:
<p style="color: red;">Closed today</p> - a style for a tag (e.g.
h1 {…}) applies to every element of that type - a class (e.g.
.highlight {…}) applies only where it is used:<p class="highlight">; one class can be used on different kinds of element
The cascade (hierarchy)
- an inline style overrides styles from attached stylesheets
- when several stylesheets are attached and their rules are equally specific (no
!important), the one attached last overrides the earlier ones - attached stylesheets must use relative file paths, so the link still works when the site is uploaded
| property | examples |
|---|---|
| background | background-color: blue; background-image: url("sky.jpg"); |
| font | font-family, font-size, color, text-align, font-weight: bold;, font-style: italic; |
| table | width, height, background-color, text-align, vertical-align, padding, border-spacing, border: 2px solid red;, border-collapse: collapse;, border-style: none; (invisible) |
Worked example. The stylesheet sets p { color: black; }. One paragraph is written <p style="color: red;">. That paragraph is red: the inline style is applied last, so it wins.
Words in the questions
The exercise sheets for this topic also use these words:
- head section 头部区域
- body section 主体区域
- relative file path 相对文件路径
- absolute file path 绝对文件路径
- cascade 层叠
- padding 内边距