Stying Fonts using CSS
This demonstration shows how fonts can be styled using CSS.
Basic Layout
Let us start with a basic layout, where no fonts have been styled.
CSS
/*basic layout*/
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 80%;
color: #333333;
background-color: #E2D3B2;
padding: 0px;
margin: 0px;
}
#wrapper {
border: 1px solid #81371A;
width: 540px;
margin: 0 auto 0 auto;
padding: 10px;
background-color: #FFFFFF;
}
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-za" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="fonts1.css" rel="stylesheet" type="text/css" />
<title>Fonts - No Style</title>
</head>
<body>
<div id="wrapper">
<h1>No Styles</h1>
<h2>The Hobbit</h2>
<h3>J.R.R. Tolkien</h3>
<p>In a hole in the ground there lived a hobbit. Not a nasty, dirty,
wet hole, filled with the ends of worms and an oozy smell, nor yet
a dry, bare, sandy hole with nothing in it to sit down on or to eat:
it was a hobbit-hole, and that means <a href="#nogo">comfort</a>.</p>
<p>It had a perfectly round door like a porthole, painted green, with a
shiny yellow brass knob in the exact middle. The door opened on to a
tube-shaped hall like a tunnel: a very comfortable tunnel without smoke,
with panelled walls, and floors tiled and carpeted, provided with
polished chairs, and lots and lots of pegs for hats and coats - the
hobbit was fond of visitors. The tunnel wound on and on, going fairly
but not quite straight into the side of the hill - <a href="#nogo">The Hill</a>,
as all the people for many miles round called it - and many little
round doors opened out of it, first on one side and then on another. No
going upstairs for the hobbit: bedrooms, bathrooms, cellars, pantries
(lots of these), wardrobes (he had whole rooms devoted to clothes),
kitchens, dining-rooms, all were on the same floor, and indeed on the
same passage. The best rooms were all on the lefthand side (going in),
for these were the only ones to have windows, deep-set round windows
looking over his garden, and meadows beyond, sloping down to the river.</p>
<p>This hobbit was a very well-to-do hobbit, and his name was Baggins.
The Bagginses had lived in the neighbourhood of <a href="#nogo">The Hill</a>
for time out of mind, and people considered them very respectable,
not only because most of them were rich, but also because they never
had any adventures or did anything unexpected; you could tell what a
Baggins would say on any question without the bother of asking him.
This is a story of how a Baggins had an adventure, and found himself
doing and saying things altogether unexpected. He may have lost the
neighbours' respect, but he gained - well, you will see
whether he gained anything in the end.</p>
</div>
</body>
</html>
EXAMPLE
Basic Styles
Now lets add some basic styling.
At this point there are no changes made to the HTML, but there are additions to the CSS.
CSS
/*fonts*/
h1 {
font-size: 2em;
color: #000066;
margin: 0px;
padding: 5px 0px 10px 0px;
}
h2 {
font-family: "Times New Roman", Times, serif;
font-size: 1.8em;
color: #800000;
text-align: center;
margin: 0px;
padding: 10px 0px 10px 0px;
}
h3 {
font-family: "Times New Roman", Times, serif;
font-size: 1.5em;
font-weight: normal;
color: #006600;
text-align: right;
font-style: italic;
margin: 0px;
padding: 0px 0px 10px 0px;
}
p {
font-size: 1em;
line-height: 1.4em;
margin: 0px;
padding: 5px 0px 10px 0px;
}
a {
color: #FF3300;
text-decoration: none;
}
a:visited {
color: #FF9900;
}
a:hover {
text-decoration: underline;
}
EXAMPLE
Changing Colours
Okay now let us change the colours of second and third paragraph. Please note the minor changes to the html (highlighted sections).
CSS
/*Additional Styles*/
.dark-blue {
font-family: "comic Sans MS", sans-serif;
color: #000099;
}
.light-blue {
font-family: "Times New Roman", Times, serif;
font-style: italic;
color: #0066FF;
}
HTML
<div id="wrapper">
<h1>No Styles</h1>
<h2>The Hobbit</h2>
<h3>J.R.R. Tolkien</h3>
<p>In a hole in the ground there lived a hobbit. Not a nasty, dirty,
wet hole, filled with the ends of worms and an oozy smell, nor yet
a dry, bare, sandy hole with nothing in it to sit down on or to eat:
it was a hobbit-hole, and that means <a href="#nogo">comfort</a>.</p>
<p class="dark-blue">It had a perfectly round door like a porthole,
painted green, with a shiny yellow brass knob in the exact middle.
The door opened on to a tube-shaped hall like a tunnel: a very
comfortable tunnel without smoke, with panelled walls, and floors
tiled and carpeted, provided with polished chairs, and lots and
lots of pegs for hats and coats - the hobbit was fond of visitors.
The tunnel wound on and on, going fairly but not quite straight
into the side of the hill - <a href="#nogo">The Hill</a>,
as all the people for many miles round called it - and many little
round doors opened out of it, first on one side and then on another. No
going upstairs for the hobbit: bedrooms, bathrooms, cellars, pantries
(lots of these), wardrobes (he had whole rooms devoted to clothes),
kitchens, dining-rooms, all were on the same floor, and indeed on the
same passage. The best rooms were all on the lefthand side (going in),
for these were the only ones to have windows, deep-set round windows
looking over his garden, and meadows beyond, sloping down to the river.</p>
<p class="light-blue">This hobbit was a very well-to-do hobbit,
and his name was Baggins. The Bagginses had lived in the neighbourhood
of <a href="#nogo">The Hill</a> for time out of mind, and people considered
them very respectable, not only because most of them were rich, but
also because they never had any adventures or did anything unexpected;
you could tell what a Baggins would say on any question without the bother
of asking him. This is a story of how a Baggins had an adventure, and found
himself doing and saying things altogether unexpected. He may have lost the
neighbours' respect, but he gained - well, you will see
whether he gained anything in the end.</p>
</div>
EXAMPLE
Special Links
Now we can make changes to the links. The HTML stays the same but there are additions to the CSS.
CSS
/*Additional Styles*/
.dark-blue {
font-family: "comic Sans MS", sans-serif;
color: #000099;
}
.dark-blue a {
border: 1px #000099 solid;
text-decoration: none;
color: #FFFFFF;
background-color: #000099;
}
.dark-blue a:hover {
text-decoration: none;
color: #000099;
background-color: #FFFFFF;
}
.light-blue {
font-family: "Times New Roman", Times, serif;
font-style: italic;
color: #0066FF;
}
.light-blue a{
color: #000099;
text-decoration: none;
border: 1px solid #CCCCCC;
}
.light-blue a:hover {
color: #CCCCCC;
background-color: #0066FF;
}
EXAMPLE
Using Spans
Now let us make changes within the paragraphs, using spans.
CSS
.green {
color: #009900;
font-weight: bold;
}
HTML
<div id="wrapper">
<h1>No Styles</h1>
<h2>The Hobbit</h2>
<h3>J.R.R. Tolkien</h3>
<p>In a <span class="green">hole</span> in the ground there lived a hobbit.
Not a nasty, dirty, wet hole, filled with the ends of worms and an
oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit
down on or to eat: it was a hobbit-hole, and that means
<a href="#nogo">comfort</a>.</p>
<p class="dark-blue">It had a perfectly round door like a porthole,
painted green, with a shiny yellow brass knob in the exact middle.
The door opened on to a tube-shaped hall like a tunnel: a very
comfortable tunnel without smoke, with panelled walls, and floors
tiled and carpeted, provided with polished chairs, and lots and
lots of pegs for hats and coats - the hobbit was fond of visitors.
The tunnel wound on and on, going fairly but not quite straight
into the side of the hill - <a href="#nogo">The Hill</a>,
as all the people for many miles round called it - and many little
round doors opened out of it, first on one side and then on another. No
going upstairs for the hobbit: <span class="green">bedrooms, bathrooms,
cellars, pantries (lots of these), wardrobes (he had whole rooms
devoted to clothes), kitchens, dining-rooms, all were on the same
floor,</span> and indeed on the same passage. The best rooms were all on the
lefthand side (going in), for these were the only ones to have windows,
deep-set round windows looking over his garden, and meadows beyond,
sloping down to the river.</p>
<p class="light-blue">This hobbit was a very well-to-do hobbit,
and his name was Baggins. The Bagginses had lived in the neighbourhood
of <a href="#nogo">The Hill</a> for time out of mind, and people considered
them very respectable, not only because most of them were rich, but
also because they never had any adventures or did anything unexpected;
you could tell what a Baggins would say on any question without the bother
of asking him. This is a story of how a Baggins had an adventure, and found
himself doing and saying things altogether unexpected. He may have lost the
<span class="green">neighbours' respect,</span> but he gained - well,
you will see whether he gained anything in the end.</p>
</div>
EXAMPLE
Website Development Resources
- Show / Hide text
- Random photograph gallery
- Cross browser transparency
- Magnify images with Gzoom
- CSS3 techniques
CSS Templates
Website Authoring Tutorials
- Styling and placement of images within a web page
- Creating a styled link list using CSS and HTML
- Building a single column webpage from scratch
- CSS mouse-overs
- CSS photograph gallery
- Styling Fonts