Lesson 8, 9 & 10
Lesson 8, 9 & 10
Lesson 8, 9 & 10
LEARNING OUTCOMES:
• Understand what CSS is and its kind
COURSE MATERIALS:
STYLE SHEETS
CSS describes how HTML elements are to be displayed on screen, paper, or in other
media.
CSS saves a lot of work. It can control the layout of multiple web pages all at once.
The most common way to add CSS, is to keep the styles in separate CSS files. However, here
we will use inline and internal styling, because this is easier to demonstrate, and easier for you
to try it yourself.
Inline CSS
This example sets the text color of the <h1> element to blue:
Example
<h1 style="color:blue;">This is a Blue Heading</h1>
Internal CSS
An internal CSS is defined in the <head> section of an HTML page, within a <style> element:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
External CSS
An external style sheet is used to define the style for many HTML pages.
With an external style sheet, you can change the look of an entire web site, by changing
one file!
To use an external style sheet, add a link to it in the <head> section of the HTML page:
Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
1 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y F O R
ADMINISTRATIVE PROFESSIONALS
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
An external style sheet can be written in any text editor. The file must not contain any HTML
code, and must be saved with a .css extension.
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p{
color: red;
}
2 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y F O R
ADMINISTRATIVE PROFESSIONALS
LESSON 9 STYLING WITH CSS
OVERVIEW:
In this lesson, you will learn how to provide text with much more style and effects via
CSS. Furthermore, with text properties, you can indicate the alignment, decoration, indention,
case and line spacing.
LEARNING OUTCOMES:
• Understand typography
• Identify font, text properties and CSS styles
• Know how to change font, size, case, alignment, indent, spacing and color.
• Apply a background color and border to a text.
COURSE MATERIALS:
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
}
p {
color: red;
font-family: courier;
font-size: 160%;
}
</style>
3 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y F O R
ADMINISTRATIVE PROFESSIONALS
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Example
p{
border: 1px solid powderblue;
}
CSS Padding
The CSS padding property defines a padding (space) between the text and the border:
Example
p{
border: 1px solid powderblue;
padding: 30px;
}
CSS Margin
The CSS margin property defines a margin (space) outside the border:
Example
p{
border: 1px solid powderblue;
margin: 50px;
}
Notes:
Background Color
The CSS background-color property defines the background color for an HTML element.
Example
<body style="background-color:powderblue;">
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
Text Color
The CSS color property defines the text color for an HTML element:
Example
<h1 style="color:blue;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
Fonts
The CSS font-family property defines the font to be used for an HTML element:
Example
<h1 style="font-family:verdana;">This is a heading</h1>
<p style="font-family:courier;">This is a paragraph.</p>
Text Size
The CSS font-size property defines the text size for an HTML element:
5 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y F O R
ADMINISTRATIVE PROFESSIONALS
Example
<h1 style="font-size:300%;">This is a heading</h1>
<p style="font-size:160%;">This is a paragraph.</p>
Text Alignment
The CSS text-align property defines the horizontal text alignment for an HTML element:
Example
<h1 style="text-align:center;">Centered Heading</h1>
<p style="text-align:center;">Centered paragraph.</p>
Notes:
6 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y F O R
ADMINISTRATIVE PROFESSIONALS
LESSON 10 SELECTORS, DIV, SPAN AND LINK
OVERVIEW:
CSS selectors are the part of CSS rules that determine what HTML elements that are
affected by the CSS rule. There are several different types of CSS selectors. Both CSS 1.0,
CSS 2.1 and CSS 3.0 added selectors to the CSS standard. The rest of this text will go through
these CSS selectors.
LEARNING OUTCOMES:
COURSE MATERIALS:
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style.
The element selector selects HTML elements based on the element name.
Example
Here, all <p> elements on the page will be center-aligned, with a red text color:
p{
text-align: center;
color: red;
}
The id selector uses the id attribute of an HTML element to select a specific element.
7 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y F O R
ADMINISTRATIVE PROFESSIONALS
The id of an element is unique within a page, so the id selector is used to select one unique
element!
To select an element with a specific id, write a hash (#) character, followed by the id of the
element.
Example
The CSS rule below will be applied to the HTML element with id="para1":
#para1 {
text-align: center;
color: red;
}
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
Example
In this example all HTML elements with class="center" will be red and center-aligned:
.center {
text-align: center;
color: red;
}
You can also specify that only specific HTML elements should be affected by a class.
Example
p.center {
text-align: center;
color: red;
}
8 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y F O R
ADMINISTRATIVE PROFESSIONALS
Example
In this example the <p> element will be styled according to class="center" and to class="large":
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
*{
text-align: center;
color: blue;
}
The grouping selector selects all the HTML elements with the same style definitions.
Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
9 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y F O R
ADMINISTRATIVE PROFESSIONALS
Example
In this example we have grouped the selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
element,element,.. div, p Selects all <div> elements and all <p> elements
The CLASS attribute is used to specify the style class to which the element belongs. For
example, the style sheet may have created the punk and warning classes:
In this example, the punk class may be applied to any BODY element since it does not have an
HTML element associated with it in the style sheet. Using the example's style sheet, the
warning class may only be applied to the P element.
A good practice is to name classes according to their function rather than their appearance. The
warning class in the previous example could have been named red, but this name would
become meaningless if the author decided to change the style of the class to a different color, or
if the author wished to define an aural style for those using speech synthesizers.
10 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y
FOR ADMINISTRATIVE PROFESSIONALS
Classes can be a very effective method of applying different styles to structurally identical
sections of an HTML document. For example, this page uses classes to give a different style to
CSS code and HTML code.
The ID Attribute
The ID attribute is used to define a unique style for an element. A CSS rule such as
Each ID attribute must have a unique value over the document. The value must be an initial
letter followed by letters, digits, or hyphens. The letters are restricted to A-Z and a-z.
Note that HTML 4 allows periods in ID attribute values, but CSS1 does not allow periods in ID
selectors. Also note that CSS1 allows the Unicode characters 161-255 as well as escaped
Unicode characters as a numeric code, but HTML 4 does not allow these characters in an ID
attribute value.
The use of ID is appropriate when a style only needs to be applied once in any document. ID
contrasts with the STYLE attribute in that the former allows medium-specific styles and can also
be applied to multiple documents (though only once in each document).
The SPAN element was introduced into HTML to allow authors to give style that could not be
attached to a structural HTML element. SPAN may be used as a selector in a style sheet, and it
also accepts the STYLE, CLASS, and ID attributes.
SPAN is an inline element, so it may be used just as elements such as EM and STRONG in
HTML. The important distinction is that while EM and STRONG carry structural meaning, SPAN
has no such meaning. It exists purely to apply style, and so has no effect when the style sheet is
disabled.
The DIV element is similar to the SPAN element in function, with the main difference being that
DIV (short for "division") is a block-level element. DIV may contain paragraphs, headings,
tables, and even other divisions. This makes DIV ideal for marking different classes of
containers, such as a chapter, abstract, or note. For example:
<DIV CLASS=note>
<H1>Divisions</H1>
<P>The DIV element is defined in HTML 3.2, but only the ALIGN attribute is permitted in HTML
3.2. HTML 4.0 adds the CLASS, STYLE, and ID attributes, among others.</P>
<P>Since DIV may contain other block-level containers, it is useful for marking large sections of
a document, such as this note.</P>
<P>The closing tag is required.</P>
</DIV>
12 |O F A D 3 0 0 9 3 I N T E R N E T A N D W E B D E S I G N W I T H L A B O R A T O R Y
FOR ADMINISTRATIVE PROFESSIONALS