Lab 2 CSS
Lab 2 CSS
1. Introduction to CSS
The word "cascading" means that multiple style rules can be applied to the same HTML element
simultaneously. The browser follows a certain "cascading order" in finalizing a style to format an HTML element
in a predictable fashion.
index.html
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<h1 >First Page </h1>
<h3>
HI! My name is Selina. Today, I have learned about HTML and CSS.
1
TMT2654 Web-based System Development Lab 2 S2 2017/2018
page.html
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<h1> Second Page </h1>
<h3>
CSS stands for Cascading Style Sheets
With CSS, your HTML document can be displayed using different
output styles.Styles are normally stored in Style Sheets
</h3>
( create a hyperlink )
</body>
</html>
styles.css
h3 { color: red}
body { background-color:blue }
1. selector {
2. property-name-1: property-value-1-1, property-value-1-2, ... ;
3. property-name-2: property-value-2-1, property-value-2-2, ... ;
4. ......
5. }
2
TMT2654 Web-based System Development Lab 2 S2 2017/2018
For example,
The selector selects the <body> tag. Hence, the defined style is applied to
the <body>...</body> element. Many (but not all) of the CSS properties (such as color, font)
are inherited by the descendents, unless it is overridden by another style definition.
2. The property name and value are separated by a colon ':' in the form of name:value. Multiple
values are separated by commas ',' or whitespace. Values containing space must be quoted, e.g.,
"Times New Roman" or 'Times New Roman'.
3. The property name:value pairs are separated by semicolon ';'. You can omit the last semi-
colon ';' before the closing brace '}'. But I recommend that you keep it, so that it is easier to include
new entries without a missing ';'.
6. Comments can be inserted inside the sheet sheet enclosed between /* and */. (The end-of-line
comment // does not work in CSS?!)
Take note that CSS and HTML have different syntaxes. In HTML, tags' attributes uses '=' to separate
the name and value pair, in the form of name="value"; attributes are separated by spaces. For example,
1. Inline Style : included inside a particular HTML tag's attribute style="style-rules", and is
applicable to that particular HTML element only.
2. Embedded Style Sheet : embedded inside the <style>...</style> tag in the HEAD section of
the HTML document. The styles are applicable to that document only.
3
TMT2654 Web-based System Development Lab 2 S2 2017/2018
3. External Style Sheet (Recommended) : stored in an external file, which is then linked to HTML
documents, via a <link> tag in the HEAD section. An external style sheet can be applied to many HTML
documents to ensure uniformity (e.g., all pages in your website).
Inline Styles
To apply inline style to an HTML element, include the list of style properties in the style attribute of the tag.
For example,
<!DOCTYPE html>
<html>
<body>
<p style="font-size:18px; font-family:cursive">This paragraph uses 18px cursive font.</p>
<p>This paragraph uses default font.</p>
<p>This paragraph uses <span style="font-size:20px">20px inside this span</span>
but default font size here.</p>
</body>
</html>
This paragraph uses 20px inside this span but default font size here.
The scope of an inline style is limited to that particular tag. Inline style defeats the stated goal of style sheets,
which is to separate the document’s content and presentation. Hence, inline style should be avoided and only
be used sparsely for touching up a document, e.g., setting the column width of a particular table.
Embedded Styles
Embedded styles are defined within the <style>...</style> tag in the HEAD section. For example,
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { background-color:cyan }
h2 { color:white; background-color:black }
p.cursive { font-size:18px; font-family:cursive }
p.f20px { font-size:20px }
</style>
</head>
<body>
<h2>H2 is white on black</h2>
<p>This paragraph is normal.</p>
<p class="cursive">This paragraph uses 18-px cursive font.</p>
<p class="f20px">This paragraph uses 20-px font.</p>
</body>
</html>
4
TMT2654 Web-based System Development Lab 2 S2 2017/2018
Embedded styles separate the presentation and content (in the HEAD and BODY sections) and can be
used if page-to-page uniformity is not required. That is, this set of styles is used for only one single page!?
(I use embedded style in this article for illustration, but you should use external style in production.)
The attribute type="text/css" in the <style> opening tag is not needed in HTML 5.
This HTML document references the external style sheet via the <link> tag in the HEAD section:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="testExternal.css">
</head>
<body>
<h2>H2 is white on black</h2>
<h2 id="green">This H2 is green on black</h2>
<p>The default paragraph uses 12-pt small-cap font.</p>
<p class="f24pt">This paragraph uses 24-pt, italics font with text-indent of 1cm.
It inherits the small-cap property from the default paragraph selector.</p>
</body>
</html>
The main advantage of external style sheet is that the same styles can be applied to many HTML pages to
ensure uniformity in presentation for your entire website. External style sheet is the now-preferred approach.
Note: HTML 5 does not requires the type="text/css" attribute in <link> tag.
5
TMT2654 Web-based System Development Lab 2 S2 2017/2018
Modern-day HTML pages use <div> and <span> extensively to layout the document via CSS. (Older HTML
pages uses tables and frames, which should be avoided. HTML 5 introduces new tags such
as <header>, <footer>, <section>, <nav>, <article>to help you better organize your page.)
You can treat <div> and <span> as generic tags for identifying contents, which shall be further qualified via
the id or class attribute. Similarly, <div> and <span> does not have any inherit visual properties
(unlike <h1>), you need to attach presentation properties via CSS.
First of all, partition your web page in logical sections via <div>...</div>, such as header, content, navigation
menu, footer, etc.
CSS Selector
As mentioned earlier, a CSS selector is used to select a group of HTML elements to apply the defined styles. The
selection can be carried out via:
1. HTML tag name, e.g., body, p, ol, table, and div.
2. HTML tag's id attribute, e.g., <div id="header">.
3. HTML tag's class attribute, e.g., <span class="highlight">.
4. others.
2. The class value needs not be unique. That is, the same class value can be assigned to many HTML
elements. In other words, these HTML elements form a sub-class. The class attribute is primarily used
by CSS to apply a common set of styles to all the elements of the same class. (HTML 5 can also
selects elements based on class name). For example,
<p class="highlight">A highlighted paragraph</p>
<p class="highlight">Another highlighted paragraph</p>
Example
The following HTML page is divided into three divisions, with unique id's of "header", "content" and
"footer" respectively. Selected texts are marked with the <span> tags, with class of "green" and "blue".
6
TMT2654 Web-based System Development Lab 2 S2 2017/2018
<html>
<head>
<style>
/* style-definitions - see below */
</style>
</head>
<body>
<div id="header">
<h1>Heading</h1>
</div>
<div id="content">
<h2>Hello</h2>
<p>Lorem ipsum dolor sit amet, <span class="green">consectetur adipisicing</span> elit, sed do
eiusmod <span class="green">tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
Duis aute irure dolor in</span> reprehenderit in voluptate <span class="blue">velit
esse</span>
cillum dolore eu fugiat nulla pariatur.</p>
</div>
<div id="footer">
<p>Footer</p>
</div>
</body>
</html>
ID-Selector: You can use CSS to set the display style for each of the three divisions, via the so-called id-
selector. A CSS id-selector begins with a '#' followed by the id's value. It selects a specific element. For
example,
/* id-selector (id value must be unique) */
#header { font-size:18px; color:cyan; }
#content { font-size:14px; color:black; }
#footer { font-size:12px; color:orange; }
Class Selector: CSS also provides the so-called class-selector for selecting elements of the same class value.
A CSS class-selector begins with a '.' followed by the classname. For example,
/* class-selector (class value needs not be unique) */
.green { color:green; text-decoration:underline; }
.blue { color:blue; }
7
TMT2654 Web-based System Development Lab 2 S2 2017/2018
Note: Because of the wide popularity of using <div>'s with id="header", id="footer" to organize an HTML
page, HTML 5 defines new tags such as <header>, <footer>, <section>, <article>, <nav> to help you to
better organize the HTML page.
Example:
h2 { background-color:black; color:white; }
Example:
T1 T2 - Descendant Selector
You can define a style rule that takes effect only when a tag occurs within a certain contextual structure, e.g.,
descendent, child, first-child, sibling, etc.
To create a descendant selector, list the tags in their hierarchical order, with no commas separating them
(commas are meant for grouping selectors). For example,
ul li { color:red; }
ul ul li { color:blue; }
ul ul ul li { color:green; }
The first-level list items are in red; second-level in blue; and third-level in green.
8
TMT2654 Web-based System Development Lab 2 S2 2017/2018
Take note that the class attribute may contain multiple values. This means that you can apply multiple class
style rules to an HTML tag. For example,
<p class="f14px_i underline">Text is 14px and italic, and underlined.</p>
<p class="f16px_b red underline">Text is 16px and bold, in red and underlined.</p>
For example,
Note: Do NOT start a class-name with a number! (This is the same restriction for identifiers in most of the
programming languages.)
#D - ID Selector
9
TMT2654 Web-based System Development Lab 2 S2 2017/2018
The id-selector, begins with a '#' followed by the id's value, select a specific element with the given
unique id value (or none). Recall that the id value must be unique in an HTML document.
You can use <div>'s with unique id to divide the document into parts of different styles. For example,
/* ID selector for the 3 major division of the document */
#header { font-size:16px; align:center; font-style:bold; }
#header h1 { text-transform:uppercase; } /* contextual selector */
#content { font-size:14px; align:justify; }
#content h3 { color:#FF0000; text-decoration:underline; } /* red, underline */
#footer { font-size:12px; align:right; }
#footer p { color:#00FF00; text-decoration:none; } /* green, not underline */
<body>
<div id="header">
<h1>H1 in the "header" division</h1>
<h3>H3 in the "header" division</h3>
<p>Paragraph in "header" division</p>
</div>
<div id="content">
<h1>H1 in the "content" division</h1>
<h3>H3 in the "content" division</h3>
<p>Paragraph in "content" division</p>
</div>
<div id="footer">
<p>Paragraph in "footer" division</p>
</div>
</body>
The * universal selector selects ALL the tags in the document. For example,
Note:
10
TMT2654 Web-based System Development Lab 2 S2 2017/2018
The order is important for anchor pseudo-classes in applying styles. It shall be link-visited-focus-
hover-active (LVFHA).
It is called pseudo-class, as it sub-divide the <a> tag into four sub-classes that cannot be marked out
manually.
You can further restrict the selection via a.classname:pesudo-class.
Another Example,
a {
font-size:10pt;
font-decoration:underline;
color:red;
}
a.blue:link { color:blue; }
a.green:link { color:green; }
a:hover {
font-decoration:none;
color:yellow;
}
<a class="green" href="http://www.aaa.com">www.aaa.com</a>
<a class="blue" href="http://www.bbb.com">www.bbb.com</a>
<a href="http://www.ccc.com">www.ccc.com</a>
The anchor pseudo classes can be combined with id-selectors (as a descendant selector), so that the
appearance of the links is different for the different divisions of the document, e.g.,
These pseudo classes is often used with <a> tag. But some of them, such as :hover, :focus, :active can
also be applied to other elements, e.g., <p>, <li>, etc.
1.8 Inheritance
Many (but not all) CSS properties, such as color properties, affect not only the elements selected by the
selector, but also inherited by their descendants. Some properties (such as border, margin, padding, width and
height) are not inherited. You can use a special property value called "inherit" to force the inheritance. For
example,
<!DOCTYPE html>
<html>
<head>
<style>
11
TMT2654 Web-based System Development Lab 2 S2 2017/2018
Although <em> is nested under the <p> tag, the border property is not inherited from the parent element.
That is, you will not see a border around the <em>'s content. We can force the inheritance by assigning a special
value "inherit" as shown.
The Law of Specificity states that "the more specific the selector, the stronger the rule". For example,
<!DOCTYPE html>
<html>
<head>
<style>
p { color:black; }
p.red { color:red; }
p#id1 { color:yellow; background:lightblue; }
p#id2 { color:blue; }
p#id1 { color:green; } /* Override the color property in the earlier rule */
</style>
</head>
<body>
<p id="id1">Paragraph with "id1" - green</p>
<p id="id2">Paragraph with "id2" - blue</p>
<p class="red">Paragraph of class "red" - red</p>
<p id="id1" class="red">Paragraph with "id1" and class "red" - green</p>
<p>Paragraph without id and class</p>
</body>
</html>
The p selector is the most general, which selects all the <p> elements; the p.red selects a group
of <p> elements with attribute class="red"; the p#id1 and p#id2 selects only one element each with the
unique id value. The id-selector is the most specific (and takes precedence); followed by class-selector; and
followed by general tag-name selector.
If the "Law of Specificity" cannot resolve the conflict, apply the "Law of Locality". The style-rule that read in last
by the browser takes effect. In the above example, there are two selector for id1, the later overrides
the color property defined earlier.
The inline style (applied to a specific tag) overrides the embedded style and external style sheet. For embedded
style and external style sheet, the order of <link> (for external style sheets) and <style> (for embedded style)
tags in the <head> section determine the precedence. You may place <link> before or after <style> and
there could be multiple <link> tags for multiple external style sheets. Again, the last rule that read in by the
browser takes effect.
12
TMT2654 Web-based System Development Lab 2 S2 2017/2018
You can override all the cascading rules by appending a special value " !important", e.g.,
p { color:blue !important; background-color:grey; } /* color cannot be overridden */
p { color:red; background-color:lightblue; } /* override background-color */
<p>color is blue but background is lightblue</p>
p {
13
TMT2654 Web-based System Development Lab 2 S2 2017/2018
There are two types of length measurements: relative (to another length properties) and absolute (e.g., inches,
centimeters, millimeters).
in (inch)
cm (centimeter)
mm (millimeter)
pt (point): 1 inch has 72 points. 1pt is 1/72 in ≈ 0.014in ≈ 0.35mm.
pc (pica): 1 pica is 12 points. 1 inch has 6 picas. 1pc ≈ 1/6 in ≈ 0.17in ≈ 4.2mm. pc is not commonly used.
There shall be no space between the number and the unit, as space is used to separate multiple values.
Take note that % and em measurement are relative to another element (percentage values are always relative,
e.g., 50% of something). For example,
p {
width: 80%; /* 80% of the parent's width */
font-size: 1.2em; /* 1.2 times of the parent's font */
margin: 1.2em; /* 1.2 times of the current font's letter 'm' */
padding: 10px; /* 10 pixels */
border: 0; /* zero does not need a unit */
}
14
TMT2654 Web-based System Development Lab 2 S2 2017/2018
To add to the confusion, some properties, such as line-height, can also accept a bare number, without a
unit. This bare number is treated as a factor to be multiplied by a reference. For example,
line-height: 20px; /* 20 pixels */
line-height: 150%; /* 150% of the parent's line-height */
line-height: 1.2em; /* 1.2 times of the current font's letter 'm' */
line-height: 1.5; /* 1.5 times of the current font */
Take note that in HTML tag attributes, such as width="400", the bare number is measured in pixels.
A block element (such as <p>, <div>, <h1> to <h6>) is rectangular in shape and exhibits the so-called box
model, with four virtual rectangles wrap around it "content area", representing the content
area, padding, border, margin, as illustrated below.
3. The border goes between padding and margin. You can set a color and a style (such as solid, dash,
dotted) to the border.
4. The margin is the space outside the border (to another element). It clears an area outside the border.
The margin does not have a background, and is totally transparent.
As illustrated in the box model diagram, margin pushes its border (and content) away with a transparent
background showing the parent (having the effect of pushing itself away from the parent); while padding
pushes its content inwards with the same background. Margin and padding serve the same purpose if there is
no border and background applied.
Take note that the width and height that you set for the element specify its content area, exclude the margin,
border and padding. To get the actual size of the element, you need to add the margin, border and padding to
the width/height. For example, suppose that:
15
TMT2654 Web-based System Development Lab 2 S2 2017/2018
#elm {
width: 300px;
margin: 10px;
border: 5px solid black;
padding: 20px;
}
These properties allow you to set up the dimension, such as the width and height of an element.
width: auto|n|n%
heigth: auto|n|n%
The width and height are specified in units such as px (pixels), or percent (relative to the parent
element).
max-width: none|n|n%
max-height: none|n|n%
min-width: none|n|n%
min-height: none|n|n%
Set the minimum and maximum width and height.
As mentioned earlier, CSS length measurement requires a proper unit, e.g., width:400px or width:80%. Take
note that width:400 is meaningless in CSS (this is a very common error!) However, in
HTML, width="400" means 400 pixels.
The width and height properties are NOT inherited by its descendants. The default value is " auto", which lets
the browser to compute a suitable value. We shall discuss "width:auto" value later.
CSS Margin, Border and Padding Properties
Set the four margins individually. The "n" shall be expressed in a proper unit (e.g. 10px and 1.2em). You
could use a negative value to overlap two elements (e.g., margin-left:-100px). The value of "auto" lets
the browser to compute an appropriate number. "n%" is relative to the same property (i.e. margin-xxx) of
the parent.
16
TMT2654 Web-based System Development Lab 2 S2 2017/2018
margin: all-4-margins
These are one-line shorthand notations to set all the four margins. If four values are given, they are
applied to top, right, bottom, left (in the clockwise manner). If two values are given, they are applied to
top-and-bottom, left-and-right. If one value is given, it is applied to all the four borders.
padding-top: n|n%
padding-right: n|n%
padding-bottom: n|n%
padding-left: n|n%
Set the four paddings individually, similar to margin-xxx.
border-width: thin|medium|thick|n
Set the width of the four borders. "n" can be used to set an absolute thickness. border-width is a
shorthand notation, you can use border-width-top, border-width-right, border-width-bottom,
and border-width-right to set the four borders individually.
border-style: none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset
Set the style of the 4 borders. Similarly, you can use border-style-top, border-style-
right, border-style-bottom, and border-style-right to set the four borders individually.
border-color: #rrggbb|rgb(r,g,b)|rgba(r,g,b,a)|color-name
Set the color of the 4 borders. Similarly, you can use border-color-top, border-color-
right, border-color-bottom, border-color-right to set the four borders individually.
Shorthand notation to set all the properties of the borders, in the order shown. You can also use
the border-top, border-right, border-bottom, and border-left to set the four borders
individually.
17
TMT2654 Web-based System Development Lab 2 S2 2017/2018
div#header {
margin: 10px auto; /* 20px for top and bottom, auto for left and right */
}
div#footer {
margin: 10px auto 5px auto /* top right, bottom, left */
}
div#content {
margin-top: 10px;
margin-right: auto;
margin-bottom: 5px;
margin-left: auto;
}
Example
18
TMT2654 Web-based System Development Lab 2 S2 2017/2018
font-size: n|n%|xx-small|x-small|small|medium|large|x-large|xx-
large|smaller|larger
font-weight: normal|bold|bolder|lighter|100|200|...|800|900
You can use a number between 100 to 900, in multiple of 100. The value of 400 is the normal weight; while
700 is bold.
font-style: normal|italic|oblique
The italic uses italic font installed (some font families include the italic version); while the oblique is
done by tilting the normal font.
font-variant: normal|small-caps
The samll-caps is smaller than the uppercase.
font: font-style font-variant font-weight font-size/line-height font-family
Set all the font properties using a one-line shorthand notation. The properties must follow the order
shown above. However, the leading and trailing items can be omitted. For example,
p {
font-size: 14px;
font-weight: bold;
line-height: 140%;
font-family: Arial, sans-serif;
}
text-align: left|right|center|justify
line-height: normal|n|n%|factor
Set the height of the line. The factor gives the factor to be multiplied by the current font-size. E.g,, factor
of 1.5 means 1.5 times of the current font.
text-decoration: none|underline|overline|line-through|blink
Graphic designer dislikes "underline" and considers it as a legacy of typewriter. "blink" is even worse!
text-transform: none|uppercase|lowercase|capitalize
The capitalize transforms the first letter to uppercase.
text-indent: n|n%
Indent the first-line of the paragraph. To indent all the lines of a paragraph (i.e., the whole block), use
padding or margin.
letter-spacing: normal|n
word-spacing: normal|n
Additional spacing to be applied to letters or words.
19
TMT2654 Web-based System Development Lab 2 S2 2017/2018
white-space: normal|pre|nowrap
Specify how white spaces inside the element is to be handled. For " pre" (pre-formatted), preserve the
white-spaces.
background-color: #rrggbb|rgb(r,g,b)|rgba(r,g,b,a)|color-name|transparent
Set the background color of an element. The default is transparent.
background-image: url(imageURL)|none
Use an image as the background.
background-repeat: repeat|repeat-x|repeat-y|no-repeat
Define how the background image shall be repeated in x and y direction or both.
background-attachment: scroll|fixed
Define whether background image shall scroll with the page or fixed.
background-position: x y|x% y%|top left|top center|top right|center left|center
center|center right|bottom left|bottom center|bottom right
Set the initial position of the background image. Note that there are two values, specifying the x and y
position respectively.
The background properties has a one-line shorthand notation, with the order shown as below:
background: background-color background-image background-repeat background-
attachment background-position
In all the above, the term background refers to the background of the elements selected (not necessary the
entire window). In other words, you can set an image as the background of an element.
20
TMT2654 Web-based System Development Lab 2 S2 2017/2018
border-collapse: collapse|separate
Collapse or separate the adjacent cells shared border into one.
border-spacing: n
For separate border, specify the distance between border (i.e., the deprecated cellspacing attribute)
caption-side: top|bottom|left|right
Specify which side to show the caption.
empty-cells: show|hide
table-layout: auto|fixed
21