Chapter 4 - HTML-css_ocr
Chapter 4 - HTML-css_ocr
uu3school5.com
Communication between a web browser and a web server
Note that Chapter 6 covers how the Internet works, and
using http
how the World Wide Web and the Internet are related.
Note that when requesting a website URL, the default start
Web page page returned is index.html, unless otherwise
requested.
A web page is a document containing information that can
be displayed by a web browser. An individual web page
There are many web browsers available, such as Google
hosted on the Internet has a unique address, or URL
Chrome, Microsoft Edge, Mozilla Firefox and Apple Safari.
(uniform resource locator), and can include combinations
of HTML, CSS and JavaScript.
Web servers deliver web pages to browsers and other HTML IDEs
software systems (over the Internet) using http. Servers HTML files are created and saved in plain text format. This
are called servers because they provide services to other means that they can be created in almost any program that
computers. can be used to create a text file, e.g., Notepad.
Tasks
HTML basics
1. Research the history of HTML. Write a short
HTML describes the layout, format and content of a web
account summarising your findings. Include the
following points: page, i.e., it allows you to control and modify its
appearance.
• Who invented it
• Where and when it was invented Unlike Python, HTML is not a programming language, it is a
markup language. A markup language is a computer
• Why it was invented
language that uses “tags” (instead of code blocks) to define
elements within a document
2. There was no such thing as CSS when HTML was
invented. Research the history of CSS. Outline Tags
three points about its development.
Tags tell the web browser how to display the content that
sits inside the tags. We can think of tags and content as
two separate components of a web page.
GOLDEN KEY 96
CHAPTER 4 HTML and CSS
Open your IDE and create a new file. Enter something Using this example, we will now explain each of the aspects
similar to: of tags in more detail.
I love HTML
Element
A web browser displays the web page as follows:
A HTML element consists of the opening tag, the content
| I love HTML inside the tag that you want to display, and the closing tag.
I <b>love</b> HTML
The void element <img/> is used to display an image.
We will look at this element later in the chapter.
This displays in a web browser as:
This is a h6 heading
In the rest of the examples in this chapter, all of the
HTML code is written in between the <body></body>
tags, unless otherwise stated. For ease of reading, the other
Nested elements
tags are not included. You will need to add them to each
web page when you create it. An element can be nested inside another element, for
example:
GOLDEN KEY 98
CHAPTER 4 HTML and CSS
. ¾ %
function. ÷ —
• •
Inline elements
Inline elements display on the same line by default and can
occur within a block-level element A block-level element Metadata
however, cannot be created inside an inline element. Before we move onto the next set of HTML elements, we
Additionally, inline elements can contain many nested inline will look at metadata. We mentioned previously that
elements. We have already seen a nested inline element in metadata is located inside the <head> element. This
this example: metadata includes information about the web page’s title,
<i>I <b>love</b> HTML</i> styles, scripts, and the character set of the web page. It
typically contains, at a minimum, the following two nested
Let’s look at the following inline elements: elements: <title> and <meta>.
<i>Inline K/i>
<i>Inline 2</i> The <title> element
As its name suggests, this element defines the title for the
This displays as follows in a web browser: web page. For example:
<hlx/hl> Block-level
HTML attributes
<P></p> Block-level
A HTML attribute provides additional information about a
<b></b> Inline HTML element.
<ix/i> Inline
<ux/u> Attributes will be used in the example of a <meta> tag:
Inline
<br/> <meta name="author"/>
Inline (however, this element adds a
new line, so it looks as if it behaves
like a block-level element)
name = “value”
<hr/> Block-level
The attribute in this case is name, and the value that the
Test yourself
attribute is set to is " author".
5. Do all HTML elements need an opening and closing
Attributes are always placed inside the opening tag. tag?
Tasks
Metadata is not included in the remaining examples in this
chapter, unless explicitly needed. 1. Create a web page that displays the text “Hello
World!”. Ensure that the page has the required
page elements: <! DOCTYPE html>, <html>,
Commenting in HTML <head> and <body>.
Just as in Python, comments are added to make the source
code easier to understand. The web browser ignores 2. Modify the page from Task 1 to include two
comments and does not display them. A comment in heading elements and a paragraph element, with the
HTML looks like this: text of your choice.
The ordered list still requires the <li> tags: We can also add table header cells to the standard table
<ol> cells:
<li>ALTl</li> • <thx/th> is used for a header table cell. By default,
<li>ALT2</li> the text in these cells is in bold and is centred.
<li>ALT3</li>
• <td></td> is used for a standard table cell. By
<li>ALT4</li>
default, the text in these cells is regular and left-
</ol>
aligned.
1. ALTI <table>
2.ALT2 <tr>
3.ALT3 <th></th>
4. ALT4 <th></th>
This displays as follows in a web browser: There is a problem with the display here. An image displays
in its original size (pixel width and pixel height). If it is a
Province Number of Counties very high-quality image, it may be larger than the screen.
Connacht 5 This is exacerbated if you view it on a mobile device.
Leinster 12
Munster 6 We can fix this using the width attribute and/or the
Ulster 9 height attribute. If you only use one of these attributes
(as in the following example), the aspect ratio (ratio of the
width to the height) of the image is preserved. If you use
We can improve the presentation of the table by adding a
both attributes, this may alter the aspect ratio.
table border and a table caption. To do this, we add the
following elements just after the <table> tag at the top
For either of these attributes (width and height), you
of the previous example:
can specify two values, pixel, or percentage.
<table border>
<caption> If you specify a pixel size, the size is static (it will not
Provinces of Ireland change if the screen size changes. It will display the same
</caption> pixel width (or height) on a desktop screen and a mobile
device. For example:
This displays as follows in a web browser:
<img src="CSinc-01.png" width="200px">
Provinces of Ireland
The image displays like this in a web browser, with a fixed
Province Number of Counties
—-------- width of 200 pixels:
Connacht .5—= .-----------------.__________
Leinster 12
Munster : 6
Ulster 9
Computer Science Inclusive
The following image shows what an image using the width MIME is a standard that indicates the nature and format of
attribute with a value of 100%, might look like on different a file. In this case, the MIME-type is a video with format
devices. .mp4.
MIME-type Description
The <video> element uses the width and/or height Additional attributes can be added for both audio and
attributes to ensure that the video displays correctly on the video, but those described here are sufficient to allow you
screen of the device. The controls attribute provides to embed locally stored basic video and audio content into
video controls such as play, pause and volume. a HTML file.
Some common media types that can be used for audio are There are many ways to share a video, for example using
listed in the following table. Facebook or Twitter. However, we will select the “Embed”
option.
MIME-type Description
Embed Video X
Embedding
YouTube video
<iframe width=’ '560" height="315*
Sometimes the video that you want for a website is already
src="https: / /www. youtube. com/embed/
hosted on a video hosting service such as YouTube. The
wUn05hdkhjM" frameborder="0"
video could belong to a third party or you may have
allow="accelerometer; autoplay;
uploaded your own. YouTube and many other video
encrypted-media; gyroscope;
hosting sites allow you to embed a video that they host
picture-in-picture’'
into your website.
allowfullscreens«/iframe»
There are benefits to using a hosted video. Video files can • Start at
be very large, so using a hosted video saves storage space
on your web server. Another advantage is that there may
EMBED OPTIONS
already be an appropriate video for your purpose created
by someone else. Using this video can save you the effort
Q Show player controls.
of creating your own.
• Enable privacy-enhanced mode. Q
Let’s have a look at an example of embedding a YouTube
video into a web page. COPY
You Tube
This code is a HTML element that is required to embed the
video into your web page. Click the copy button and it will
copy the element and all of its attributes.
First, find the video that you would like to embed, then The HTML element above is an <if rame>. These are
select the share button, which is located under the video used to embed another HTML document into a web page.
(towards the bottom right corner). In this case, it is a video from YouTube:
<iframe width="560" height="315"
src="https://www.youtube.com/embed/wUnO5
hdkhjM" frameborder="0"
allow="accelerometer; autoplay;
encrypted-media; gyroscope; picture-in-
it 31K 411.5K - SHARE = SAVE picture " allowfullscreenx/iframe>
© Ipat
I. What attribute do you use to create a hyperlink
6 Vusic
inside an anchor <a> tag?
• Led
2. What is the difference between a relative URL and
ill Radio
an absolute URL for image or video sources?
C Loops
• <table>
• <tr>
• <th>
• <td>
6. The following two HTML code examples insert an
image into a web page. Explain, using a diagram, the
difference between the two images when they are
displayed on the web page:
You next have to create a name for the project, for • <img src="CSinc-01.png"
example, “HTML_Test”, and then publish the project. width="200px">
• <img src="CSinc-01.png"
j Share Project O width="80%">
Task
Buttons
A <button> element is a clickable element. We generally
HOTrS form.
Input
attribute in web
An <input> element is a void HTML element. It is used values
to allow a user to enter alphanumeric (textual or numeric)
values on a website. You will have seen this many times
before when filling in online forms. Once the data is
entered into the text box, we normally click a button to
submit the data. There are many kinds of input tags, each
representing a different type of input.
This means that a user cannot enter any values into the
<input> element, but it can be used as an output field
(see Chapter 10).
There are some very useful values for the type attribute,
which can be used to improve the user experience. A
selection of these can be seen in the following table.
Text area The user selects the province that they live in:
A <textarea> element is similar to an <input> <select>
element However, it allows a multi-line text input and it is <option value="Connacht">Connacht
not a void element. The text area can hold an unlimited </option>
number of characters: soption value="Leinster">Leinster
</option>
<textarea>< /textarea>
<option value="Munster">Munster
</option>
This displays as follows in a web browser:
<option value="Ulster ">Ulster
</option>
</select>
The values that are returned when we use JavaScript, are CSS
just the numbers, 0, 1, 2 etc. This would make the option
chosen easier to use with if statements. introduction to CSS
The second language needed to create well-designed web
We included an initial prompt “Please select a province". If pages is called Cascading Style Sheets (CSS).
the user does not choose a province, the value returned is
0. We could add some logic to inform the user that this is CSS provides the instructions for presenting the web page
not a valid option. We will cover retrieving the values from content that you have created in HTML. It is designed to
interactive HTML elements in Chapter 10. enable the separation of content from the presentation of
that content.
Test yourself
CSS was first proposed by Hakon Wium Lie in 1994 while
I. What are interactive HTML elements used for? he was working with Tim Berners-Lee, (the inventor of the
World Wide Web, see Chapter 6). He then created CSS in
2. What is a <button> element used for?
conjunction with Bert Bos around 1996.
3. Name three values that can be used in an
<input> element for the attribute type.
Describe what each value does for the element, and
sketch a picture of what it will look like for the
user.
4. What do the attributes readonly and
placeholder do for the <input> element?
5. What is the difference between a <textarea>
and an <input> element, from a user’s point of
view?
6. Explain how you would use the <select>
Hakon Wium Lie
element and the nested HTML element <option>
in the HTML code for a dropdown list.
While HTML specifies the content and structure of your
web page, CSS defines its visual presentation. Presentation
details include colours, borders, margins, fonts etc. This is
illustrated in the graphic below.
Tasks
Properties
This displays as follows in a web browser:
A property is the name of the stylistic feature that you
| ( am HTML with style!
want to specify. For example: color, background-color, or
font-size:
Here the values are applied in order. The web browser
color:red;
checks whether the first font (Arial) is installed on the
device. If it is not, then it finds out whether another sans-
The US spelling of words has been agreed as the standard,
serif font is available. If no sans serif fonts are available, the
which means that color is used instead of colour. You must
web browser uses the default font on the device.
use a valid property name. If you do not, then the web
browser will simply ignore that styling.
Examples of CSS properties
There are over 500 distinct CSS properties. You can find a While it is not possible to go through all of the CSS
full list of them here: https://www.w3schools.com/css/. properties, the following tables provide a quick guide to
some that you may find useful. The first table provides
examples of styles that apply to the HTML content. The
second table provides examples of styles that apply to the
HTML element.
To address this CSS allows you to use hexadecimal colours Another factor is the re-use of CSS. If two or more
or “hex colours” for short This allows you to select any elements have the same CSS, we need to replicate the
colour or shade. There are many online “colour code. If we applied it to 10 elements, and we wanted to
calculators” but let’s look at the following example from change say one property value, we would have to change it
w3schools.com. in all 10 elements. This could lead to errors.
hl,h2,h3{
color:red; I am HTML with style!
}
The following code brings all of these ideas together: We have separated the CSS from the HTML elements. This
is generally considered good practice as:
<!DOCTYPE html>
<html> • It is easier to read both the CSS and HTML.
<head> • If the style needs to be modified for all of the HTML
<style> hl elements, it only needs to be changed once in the
hl { CSS rule.
color:red; • The code is easier to maintain.
}
The general process of separating aspects of your code is Note that when you insert the CSS class name into the
known as separation of concerns (SoC). class attribute as the value, you drop the dot
The CSS declaration in our previous example was applied The CSS rule myStyle is only applied to the hl HTML
to a hl HTML element. We can apply a declaration to hl element where the class is applied. It is not applied to all
- h6, body and p elements and to many others. hl elements.
While this approach is useful, it has limitations. If we use We can illustrate how this CSS rule is applied to some
the previous declarations, they are applied to all instances HTML elements and not others as follows:
of those elements. You may not want this. You might want
<style>
to apply a CSS rule to only certain HTML elements. There .myStyle{
are many ways to do this. The method that we will use is font-family:arial;
the class attribute. font-size:30px;
color: blue;
CSS classes border-style:solid;
border-color:black;
Instead of attaching a CSS rule to every instance of a HTML
border-width:1Opx;
element, we can give the CSS rule a name (just like a
padding: 10px;
variable name). These are called classes. A class is a CSS
border-radius:Opx 2Opx 6Opx lOpx;
rule which can then be applied to HTML elements (or,
background-color:yellow;
not). A class is created in a similar way to a CSS rule,
}
except that it begins with a dot in the declaration, for
</style>
example:
.myStyle { <body>
color:red; <hl class="myStyle">
} I am HTML with style!
</hl>
This can then be applied to a HTML element by using the <hl>
class attribute. Let’s look at an example: I am HTML with no style!
</hl>
<style>
<div class="myStyle">
. myStyle {
I am HTML with style!
font-family:arial;
</div>
font-size:30px;
</body>
color: blue;
border-style:solid;
This displays as follows in a web browser:
border-color:black;
border-width:1Opx;
padding:lOpx; am HTML with style!
border-radius:Opx 20px 60px lOpx;
background-color:yellow; I am HTML with no style!
}
</style> I am HTML with style!
<body>
<hl class="myStyle">
I am HTML with style!
Note that the <hl> HTML element font is in bold but the
</hl>
<div> HTML element font is not. This is because the
</body>
original hl element default style is also applied, whereas
the <div> element has no default style.
This displays as follows in a web browser;
115 CO
HTML and CSS CHAPTER 4
Having to do this would be time-consuming and could give The modularity applied here by using external CSS, means
rise to errors. that we can link the style. css to any web page. If we
modify/add a declaration inside a rule in the style.css,
Using external CSS allows us to achieve modularity. The the modified rule applies to each HTML element where
CSS rules and declarations (including for class declarations) that class is applied.
are exactly the same but are stored in a CSS file. We
create a new file with a .css extension. In this example
the file is stored in the same folder as the web page. Test yourself
The .css file does not require the <style> tags. You I. What is CSS designed to do?
can just add CSS rules, as shown below:
2. Who proposed and created CSS?
.myStyle{
3. List the three ways to add CSS to a HTML
font-family:arial;
document, with a short explanation of each.
font-size:30px;
color: blue; 4. What is a CSS declaration, property, and value?
border-style:solid; Provide an example of each.
border-color:black; 5. Provide an example of multiple inline CSS
border-width:1Opx; declarations.
padding:lOpx; 6. What symbol is placed at the end of a declaration?
border-radius:Opx 2Opx 6Opx 10px;
background-color:yellow; 7. What happens if you do not use a valid property
name in a CSS declaration?
}
8. Provide an example of multiple values for one CSS
To link the .css file to the HTML file, you add a <link> declaration.
element into the web page header: 9. State two different types of value for the CSS
property font-size.
dink href="style.css" rel="stylesheet">
10. Name three values for the CSS property border-
The .myStyle class can then be applied to any element in style. What is the default value if the property is
not specified? (You may have to try them all to find
a HTML file as before, except this time the CSS is located
out.)
in a different file. Here is the HTML code for the web page:
11. What is the difference between the CSS properties
<!DOCTYPE html>
margin and padding?
<html>
<head> 12. Sketch the CSS HTML box model for the
<link href="style.css" properties padding, border and margin.
rel="stylesheet"> 13. Provide three examples of hexadecimal CSS
</head> colours.
<body> 14. What does RGB stand for in RGB colour codes?
<div class="myStyle">
I am HTML with style! 15. How does internal CSS differ from inline CSS?
</div> 16. What advantages does internal CSS have compared
</body> to using inline CSS?
</html> 17. What is a selector used for in internal (and
external) CSS?
Videos
The next page should have information about the
events/bands. This should be informative and clear to
read.
2. Create a website that consists of three web pages. Finally, the last page is for booking tickets. Although
Create a navigation bar using <div> and <a> tags it will not allow for the collection/storage of data, it
with hyperlinks to navigate to each web page and should appear to be able to take in values for the
back. The navigation bar should look like the following:
following example:
• Name
• Age
Page 1 Page 2 Page 3
• Address
• Number of tickets
• Email address
3. Add to your solution for Task 2, by creating a • Credit card information:
paragraph title and a paragraph content section with Cardholder name
suitable CSS. Use the text of your choice in the Expiry date
paragraph. Card number
CVC number
Using internal and external CSS:
You can make up the card details. Do not use any
4. Make a copy of your solution for Task 3, and change real data for testing.
the current inline CSS to internal CSS.
All pages should have suitable and user-friendly CSS
properties. The web pages should have a consistent
theme. (Designing this beforehand may help.)
5. Make another copy of your solution to Task 3. and
change the current internal CSS to external CSS. The ticket interactive elements should also have CSS
applied so that they look as though they are part of a
real ticket purchasing system.
(Hint as there are multiple pages, use an external
CSS file.)