Chapter 3
Chapter 3
By Azmeraw D.
8/8/23
Outline
CSS overview
Using CSS
Style properties
08/08/2023 Azmeraw D 2
Outline
CSS stands for Cascading Style Sheets
Style defines how HTML elements will be displayed
A CSS rule has two main parts: a selector, and one or more
declarations & each declaration consists of a property and a
value.
CSS Comments
A CSS comment begins with "/*", and ends with "*/“
3
Using CSS
There are three ways of inserting a style sheet:
Inline Style Sheet
Internal/document level style sheet
External style sheet
Inline Style Sheet
Is defined inside a tag and applies only to the contents of that tag.
For example
<H1 style="color:blue;font-style=italic">The first heading</H1>
<p style="color:sienna;margin-left:20px">This is a paragraph.</p>
Using in-line styles is not very efficient, as the styles are spread
throughout the document and are thus difficult to maintain. 4
Internal/Document level style sheet
Can be placed inside the HEAD of a HTML document and is better than in-
line styles because it puts all the styles in one place.
Are enclosed within the <STYLE> and </STYLE> tags. The open tag should
contain type=“text/CSS” attribute to tell the browser what type of style
being used.
The styles defined inside the style tags will affect the whole document. So, to
make all text inside H1 tags appear blue and in italics, the following can be
used:
<HEAD>
<STYLE type="text/css">
<!--
H1 { color:blue; font-style=italic}
-->
</STYLE> 5
External Style Sheet
It is a text file that is saved with a .css extension. It should not contain html tag.
It is useful to change the look of the entire website by changing single CSS file. For
example Heading.css
H1 { color:blue; font-style:italic}
H2 { color:blue; font-style:italic}
To link a style sheet to the web page, use the link tag, which must be inside the header. The
attributes of the link tag are:
rel – describes the relationship of the link – must be 'stylesheet'
type – the type of the style sheet – we are using 'text/css'
href – the URL of the style sheet eg. Heading.css
title – this is a name for the style sheet, can be any text.
<HEAD>
<LINK rel=stylesheet type="text/css" href=“Heading.css" title=“Head">
</HEAD> 6
Multiple Styles Will Cascade into One
Cascading order
Generally speaking we can say that all the styles will "cascade" into a new
"virtual" style sheet by the following rules, where number four has the highest
priority:
Browser default
External style sheet
Internal style sheet (in the head section)
Inline style (inside an HTML element)
So, an inline style has the highest priority, which means that it will override a
style defined inside the <head> tag, or in an external style sheet, or in a
browser (a default value).
Note: If the link to the external style sheet is placed after the internal style
7
CSS Id
CSS allows you to specify your own selectors called "id" and "class".
The id selector is used to specify a style for a single, unique element.
The id selector uses the id attribute of the HTML element, and is defined with a "#".
Example
#para1
{
text-align:center;
color:red;
}
To apply these styles in the body of the page:
<p id=“para1”>This is CSS id</p>
Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.
8
CSS Classes
Tag-Specific Classes
Creates different sets of styles that can be applied to different occurrences of a
single tag. For example, if you wish to have a two different paragraph styles
to use in your web pages – one that is centred and italic and another that is
left-aligned but indented by a certain amount.
Style classes for a particular tag are defined by putting a class name after the
tag name, separating them by a full stop.
P.centred { font-style:italic; text-align: center; }
P.normal { text-align=left; margin-left=0.5cm;}
To apply these styles in the body of the page:
<P class="centred">This is a centred paragraph in italics</P>
<P class="normal">This is a left-aligned paragraph, indented by 0.5 cm</P>
9
Generic Classes
A style class can also be generic – which means it can be applied to
any tag.
To define a generic class, the tag name is omitted.
For example: .normal
{
Font-family:verdana,arial;
font-size:12pt;
color:black;
}
To apply these styles in the body of the page:
<p class="normal">This is normal text</p>
The benefit of defining generic classes is that they can then be used in different
tags. For example, the normal class above could be used on a P tag or in a10
Pseudo-Classes
Are used to define the styles for the appearance of links (A tags). A link has three
states – unvisited (before it has been clicked), active (when the mouse is
hovered over the link) and visited (after it has been clicked on).
The default appearance for links is blue underlined text for unvisited links and
purple underlined text for visited links. To change these defaults, you can
define the following style pseudo-classes
A:link {color:red;} /*style for unvisited links - red */
A:hover {color:red; font-weight:bold;} /* style for active links – bold and red */
A:visited {color:green} /* style for visited links - green */
Pseudo-classes and other classes can be mixed For example: A.plain:visited
{text-style: normal}
<a href='http://wku-server/IT.html" class="plain">Plain link</a>
11
Style Properties
12
CSS Background
CSS background properties are used to define the background effects of an element.
CSS properties used for background effects:
background-color(can be name like “red”, RGB value such as(250,0,0) or hexa value like
“#ff0000”
background-image
background-repeat
Example:
background-attachment h1 {background-color:#6495ed;}
background-position body {background-color:#b0c4de;}
OR
body
{
background-image:url('gradient2.png');
background-repeat:repeat-x;
background-position:right top;
} 13
CSS Text
Text-Color
used to set the color of the text.
body {color:blue;}
h1 {color:#00ff00;}
h2 {color:rgb(255,0,0);}
Text-Align
used to set the horizontal alignment of a text. Can be center, left, right and justified
h1 {text-align:center;}
p.date {text-align:right;}
p.main {text-align:justify;}
Text-Decoration
used to set or remove decorations from text. Mostly used to remove underlines from links for
design purposes.
h1 {text-decoration:overline;}
h2 {text-decoration:line-through;}
h3 {text-decoration:underline;}
h4 {text-decoration:blink;} 14
Text-Transform
used to specify uppercase and lowercase letters in a text.
p.uppercase {text-transform:uppercase;}
p.lowercase {text-transform:lowercase;}
p.capitalize {text-transform:capitalize;}
Text -Indent
used to specify the indentation of the first line of a text.
p {text-indent:50px;}
15
CSS Font
CSS font properties define the font family, boldness, size, and the style of a text.
Font-Family
In CSS, there are two types of font family names:
generic family - a group of font families with a similar look (like "Serif" or "Monospace")
font family - a specific font family (like "Times New Roman" or "Arial")
The font-family property should hold several font names as a "fallback" system.
Start with the font you want, and end with a generic family, to let the browser pick a similar
font in the generic family, if no other fonts are available.
Note: If the name of a font family is more than one word, it must be in quotation marks, like
font-family: "Times New Roman".
More than one font family is specified in a comma-separated list:
Example
p{font-family:"Times New Roman", Times, serif;}
16
Font-Style
mostly used to specify italic text.
This property has three values:
normal - The text is shown normally
italic - The text is shown in italics
oblique - The text is "leaning" (oblique is very similar to italic, but less supported)
Example
p.normal {font-style:normal;}
p.italic {font-style:italic;}
p.oblique {font-style:oblique;}
Font-Size
sets the size of the text.
If you do not specify a font size, the default size for normal text, like paragraphs, is
16px(16px=1em).
h1 {font-size:40px;}/*size in pixel*/
h2 {font-size:1.875em;} /* 30px/16=1.875em */ 17
CSS Lists
The CSS list properties allow you to:
Set different list item markers for ordered lists
Set different list item markers for unordered lists
Set an image as the list item marker
The type of list item marker is specified with the list-style-type property:
Example
ul.a {list-style-type: circle;}/*Unordered list*/
ul li {list-style-type: square;} /*Unordered list*/
An Image as The List Item Marker
To specify an image as the list item marker, use the list-style-image
property:ul
{list-style-image: url('sqpurple.gif');} /*Image Unordered list*/
ol.b {list-style-type: upper-roman;} /*Ordered list*/ 18
Values for Ordered Lists
Value Description
decimal-leading-zero The marker is a number padded by initial zeros (01, 02, 03, etc.)
Georgian The marker is traditional Georgian numbering (an, ban, gan, etc.)
OL OL LI {list-style-type: upper-roman}
OL OL OL LI {list-style-type: lower-alpha}
OL OL OL OL LI {list-style-type: decimal}
UL LI{list-style-type:square;}
UL UL LI{list-style-type:circle;}
UL UL UL LI{list-style-image:url(“image.jpg”);}
20
CSS Tables
08/08/2023
? Azmeraw D 26