CSS3 Web Development Handout
CSS3 Web Development Handout
Cascading Style Sheets (CSS) is a fantastic tool to add layout to your websites. It can save you a
lot of time and it enables you to design websites in a completely new way. CSS is a must for
anyone working with web design.
This tutorial will get you started with CSS in just a few hours. It is easy to understand and it will
teach you all the sophisticated techniques.
Learning CSS is fun. As you go along through the tutorial, remember to take enough time to
properly experiment with what you learn in each lesson.
Using CSS requires basic experience with HTML. If you are not familiar with HTML, please start with our HTML
tutorial before moving on to CSS.
For example, Microsoft Windows comes with a program called Notepad. It is usually located in Accessories in the
start menu under Programs. Alternatively, you can use a similar text editor e.g. Pico for Linux or Simple Text for
Macintosh.
A simple text editor is ideal for learning HTML and CSS because it doesn't affect or change the codes you type.
That way, your successes and errors can only be attributed to yourself - not the software.
You can use any browser with this tutorial. We encourage you to always keep your browser updated and use the
latest version.
HTML can be (mis-)used to add layout to websites. But CSS offers more options and is more accurate and
sophisticated. CSS is supported by all browsers today.
After only a few lessons of this tutorial you will be able to make your own style sheets using CSS to give your
website a new great look.
1|Page
What is the difference between CSS and HTML?
HTML is used to structure content. CSS is used for formatting structured content.
Okay, it sounds a bit technical and confusing. But please continue reading. It will all make sense to you soon.
Back in the good old days when Madonna was a virgin and a guy called Tim Berners Lee invented the World Wide
Web, the language HTML was only used to add structure to text. An author could mark his text by stating "this is
a headline" or "this is a paragraph" using HTML tags such as <h1> and <p>.
As the Web gained popularity, designers started looking for possibilities to add layout to online documents. To
meet this demand, the browser producers (at that time Netscape and Microsoft) invented new HTML tags such
as for example <font> which differed from the original HTML tags by defining layout - and not structure.
This also led to a situation where original structure tags such as <table> were increasingly being misused to layout
pages instead of adding structure to text. Many new layout tags such as <blink> were only supported by one type
of browser. "You need browser X to view this page" became a common disclaimer on web sites.
CSS was invented to remedy this situation by providing web designers with sophisticated layout opportunities
supported by all browsers. At the same time, separation of the presentation style of documents from the content
of documents, makes site maintenance a lot easier.
Many of the properties used in Cascading Style Sheets (CSS) are similar to those of HTML. Thus, if you are used to
use HTML for layout, you will most likely recognize many of the codes. Let us look at a concrete example.
<body bgcolor="#FF0000">
As you will note, the codes are more or less identical for HTML and CSS. The above example also shows you the
fundamental CSS model:
2|Page
But where do you put the CSS code? This is exactly what we will go over now.
3|Page
The trick is to create a link from the HTML document (default.htm) to the style sheet (style.css). Such link can be
created with one line of HTML code:
<link rel="stylesheet" type="text/css" href="style/style.css" />
Notice how the path to our style sheet is indicated using the attribute href.
The line of code must be inserted in the header section of the HTML code i.e. between
the <head> and </head> tags. Like this:
<html>
<head>
<title>My document</title>
<link rel="stylesheet" type="text/css" href="style/style.css" />
</head>
<body>
...
This link tells the browser that it should use the layout from the CSS file when displaying the HTML file.
The really smart thing is that several HTML documents can be linked to the same style sheet. In other words, one
CSS file can be used to control the layout of many HTML documents.
This technique can save you a lot of work. If you, for example, would like to change the background color of a
website with 100 pages, a style sheet can save you from having to manually change all 100 HTML documents.
Using CSS, the change can be made in a few seconds just by changing one code in the central style sheet.
Let's put what we just learned into practice.
Try it yourself
Open Notepad (or whatever text editor you use) and create two files - an HTML file and a CSS file - with the
following contents:
default.htm
<html>
<head>
<title>My document</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h1>My first stylesheet</h1>
</body>
</html>
4|Page
style.css
body {
background-color: #FF0000;
}
Now place the two files in the same folder. Remember to save the files with the right extensions (respectively
".htm" and ".css")
Open default.htm with your browser and see how the page has a red background. Congratulations! You have
made your first style sheet!
Hurry on to the next lesson where we will take a look at some of the properties in CSS.
h1 {
color: #ff0000;
}
Colors can be entered as hexadecimal values as in the example above (#ff0000), or you can use the names of the
colors ("red") or rgb-values (rgb(255,0,0)).
h1 {
color: #990000;
background-color: #FC9804;
}
5|Page
Notice that we applied two properties to <h1> by dividing them by a semicolon.
To insert the image of the butterfly as a background image for a web page, simply apply the background-image
property to <body> and specify the location of the image.
body {
background-color: #FFCC66;
background-image: url("butterfly.gif");
}
h1 {
color: #990000;
background-color: #FC9804;
}
Value Description
background-repeat: repeat-x The image is repeated horizontally
background-repeat: repeat-y The image is repeated vertically
background-repeat: repeat The image is repeated both horizontally and vertically
background-repeat: no-repeat The image is not repeated
6|Page
For example, to avoid repetition of a background image the code should look like this:
body {
background-color: #FFCC66;
background-image: url("butterfly.gif");
background-repeat: no-repeat;
}
h1 {
color: #990000;
background-color: #FC9804;
}
A fixed background image will not move with the text when a reader is scrolling the page, whereas an unlocked
background image will scroll along with the text of the web page.
The table below outlines the two different values for background-attachment. Click on the examples to see the
difference between scroll and fixed.
Value Description
Background-attachment: scroll The image scrolls with the page - unlocked
Background-attachment: fixed The image is locked
For example, the code below will fix the background image.
body {
background-color: #FFCC66;
background-image: url("butterfly.gif");
background-repeat: no-repeat;
background-attachment: fixed;
}
h1 {
color: #990000;
background-color: #FC9804;
}
There are numerous ways to set the values of background-position. However, all of them are formatted as a set of
coordinates. For example, the value '100px 200px' positions the background image 100px from the left side and
200px from the top of the browser window.
7|Page
The coordinates can be indicated as percentages of the browser window, fixed units (pixels, centimetres, etc.) or
you can use the words top, bottom, center, left and right. The model below illustrates the system:
The code example below positions the background image in the bottom right corner:
body {
background-color: #FFCC66;
background-image: url("butterfly.gif");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: right bottom;
}
h1 {
color: #990000;
background-color: #FC9804;
}
Compiling [background]
The property background is a short hand for all the background properties listed in this lesson.
With background you can compress several properties and thereby write your style sheet in a shorter way which
makes it easier to read.
For example, look at these five lines:
background-color: #FFCC66;
background-image: url("butterfly.gif");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: right bottom;
8|Page
Using background the same result can be achieved in just one line of code:
These two properties that are not specified would merely be set to their default values which as you know are
scroll and top left.
Summary
In this lesson, you have already learned new techniques that would not be possible using HTML. The fun continues
in the next lesson which examines the broad range of possibilities when using CSS to describe fonts.
Lesson 4: Fonts
In this lesson you will learn about fonts and how they are applied using CSS. We will also look at how to work
around the issue that specific fonts chosen for a website can only be seen if the font is installed on the PC used to
access the website. The following CSS properties will be described:
font-family
font-style
font-variant
font-weight
font-size
font
There are two types of names used to categorize fonts: family-names and generic families. The two terms are
explained below.
Family-name
Examples of a family-name (often known as "font") can e.g. be "Arial", "Times New Roman" or "Tahoma".
Generic family
Generic families can best be described as groups of family-names with uniformed appearances. An
example is sans-serif, which is a collection of fonts without "feet".
9|Page
The difference can also be illustrated like this:
When you list fonts for your web site, you naturally start with the most preferred font followed by some
alternative fonts. It is recommended to complete the list with a generic font family. That way at least the page will
be shown using a font of the same family if none of the specified fonts are available.
An example of a prioritized list of fonts could look like this:
Headlines marked with <h1> will be displayed using the font "Arial". If this font is not installed on the user's
computer, "Verdana" will be used instead. If both these fonts are unavailable, a font from the sans-serif family
will be used to show the headlines.
Notice how the font name "Times New Roman" contains spaces and therefore is listed using quotation marks.
If font-variant is set to small-caps and no small-caps font is available the browser will most likely show the text in
uppercase instead.
h1 {font-variant: small-caps;}
h2 {font-variant: normal;}
10 | P a g e
Font weight [font-weight]
The property font-weight describes how bold or "heavy" a font should be presented. A font can either
be normal or bold. Some browsers even support the use of numbers between 100-900 (in hundreds) to describe
the weight of a font.
h1 {font-size: 30px;}
h2 {font-size: 12pt;}
h3 {font-size: 120%;}
p {font-size: 1em;}
There is one key difference between the four units above. The units 'px' and 'pt' make the font size absolute, while
'%' and 'em' allow the user to adjust the font size as he/she see fit. Many users are disabled, elderly or simply
suffer from poor vision or a monitor of bad quality. To make your website accessible for everybody, you should
use adjustable units such as '%' or 'em'.
Below you can see an illustration of how to adjust the text size in Mozilla Firefox and Internet Explorer. Try it
yourself - neat feature, don't you think?
11 | P a g e
Compiling [font]
Using the font short hand property it is possible to cover all the different font properties in one single property.
For example, imagine these four lines of code used to describe font-properties for <p>:
p{
font-style: italic;
font-weight: bold;
font-size: 30px;
font-family: arial, sans-serif;
}
p{
font: italic bold 30px arial, sans-serif;
}
Summary
You have now learned about some of the possibilities related to fonts. Remember that one of the major
advantages of using CSS to specify fonts is that at any given time, you can change font on an entire website in just
a few minutes. CSS saves time and makes your life easier. In the next lesson we will look at text.
Lesson 5: Text
Formatting and adding style to text is a key issue for any web designer. In this lesson you will be introduced to the
amazing opportunities CSS gives you to add layout to text. The following properties will be described:
text-indent
text-align
text-decoration
letter-spacing
text-transform
p{
text-indent: 30px;
}
In the example below the text in table headings <th> is aligned to the right while the table data <td> are centred.
In addition, normal text paragraphs are justified:
12 | P a g e
th {
text-align: right;
}
td {
text-align: center;
}
p{
text-align: justify;
}
h1 {
text-decoration: underline;
}
h2 {
text-decoration: overline;
}
h3 {
text-decoration: line-through;
}
h1 {
letter-spacing: 6px;
}
p{
letter-spacing: 3px;
}
An example could be the word "headline" which can be presented to the user as "HEADLINE" or "Headline".
13 | P a g e
There are four possible values for text-transform:
capitalize
Capitalizes the first letter of each word. For example: "john doe" will be "John Doe".
uppercase
Converts all letters to uppercase. For example: "john doe" will be "JOHN DOE".
lowercase
Converts all letters to lowercase. For example: "JOHN DOE" will be "john doe".
none
No transformations - the text is presented as it appears in the HTML code.
As an example, we will use a list of names. The names are all marked with <li> (list-item). Let's say that we want
names to be capitalized and headlines to be presented in uppercase letters.
Try to take a look at the HTML code for this example and you will see that the text actually is in lowercase.
h1 {
text-transform: uppercase;
}
li {
text-transform: capitalize;
}
Summary
In the last three lessons you have already learned several CSS properties, but there is much more to CSS. In
the next lesson we will take a look at links.
Lesson 6: Links
You can apply what you already learned in the previous lessons to links (i.e. change colors, fonts, underline, etc).
The new thing is that CSS allows you to define these properties differently depending on whether the link is
unvisited, visited, active, or whether the cursor is on the link. This makes it possible to add fancy and useful effects
to your website. To control these effects you use so-called pseudo-classes.
What is a pseudo-class?
A pseudo-class allows you to take into account different conditions or events when defining a property for an
HTML tag.
Let's look at an example. As you know, links are specified in HTML with <a> tags. We can therefore use a as a
selector in CSS:
a{
color: blue;
}
A link can have different states. For example, it can be visited or not visited. You can use pseudo-classes to assign
different styles to visited and unvisited links.
14 | P a g e
a:link {
color: blue;
}
a:visited {
color: red;
}
Use a:link and a:visited for unvisited and visited links respectively. Links that are active have the pseudo-
class a:active and a:hover is when the cursor is on the link.
We will now go through each of the four pseudo-classes with examples and further explanation.
Pseudo-class: link
The pseudo-class :link is used for links leading to pages that the user has not visited.
In the code example below, unvisited links will be light blue.
a:link {
color: #6699CC;
}
Pseudo-class: visited
The pseudo-class :visited is used for links leading to pages that the user has visited. For example, the code below
would make all visited links dark purple:
a:visited {
color: #660099;
}
Pseudo-class: active
The pseudo-class :active is used for links that are active.
This example gives active links a yellow background color:
a:active {
background-color: #FFFF00;
}
Pseudo-class: hover
The pseudo-class :hover is used when the mouse pointer hovers over a link.
This can be used to create interesting effects. For example, if we want our links to be orange and be italicized
when the cursor is pointed at them, our CSS should look like this:
a:hover {
color: orange;
font-style: italic;
}
15 | P a g e
Example 1: Effect when the cursor is over a link
It is particular popular to create different effects when the cursor is over a link. We will therefore look at a few
extra examples related to the pseudo-class :hover.
a:hover {
letter-spacing: 10px;
font-weight:bold;
color:red;
}
a:hover {
text-transform: uppercase;
font-weight:bold;
color:blue;
background-color:yellow;
}
The two examples gives you an idea about the almost endless possibilities for combining different properties. You
can create your own effects - give it a try!
That said, it is very simple to remove the underlining of links. As you will recall from lesson 5, the property text-
decoration can be used to determine whether text is underlined or not. To remove underlining, simply set the value
of text-decoration to none.
a{
text-decoration:none;
}
Alternatively, you can set text-decoration along with other properties for all four pseudo-classes.
a:link {
color: blue;
text-decoration:none;
}
16 | P a g e
a:visited {
color: purple;
text-decoration:none;
}
a:active {
background-color: yellow;
text-decoration:none;
}
a:hover {
color:red;
text-decoration:none;
}
Summary
In this lesson you have learned about pseudo-classes, using some of the properties from the previous lessons. This
should give you an idea of some the possibilities CSS provides.
In the next lesson we will teach you how to define properties for specific elements and groups of elements.
How can you color one particular headline differently than the other headlines on your website? How can you
group your links into different categories and give each category a special style? These are just examples of
questions we will answer in this lesson.
17 | P a g e
Then we want the white wine links to be yellow, the red wine links to be red and the rest of the existing links on
the webpage to stay blue.
To achieve this, we divide the links into two categories. This is done by assigning a class to each link using the
attribute class.
We can hereafter define special properties for links belonging to whitewine and redwine, respectively.
a{
color: blue;
}
a.whitewine {
color: #FFBB00;
}
a.redwine {
color: #800000;
}
As shown in the example you can define the properties for elements which belong to a certain class by
using .classname in the style sheet of the document.
What is special about the attribute id is that there cannot be two elements in the same document with the same id.
Each id has to be unique. In other cases, you should use the class attribute instead. Now, let us take a look at an
example of a possible usage of id:
<h1>Chapter 1</h1>
...
<h2>Chapter 1.1</h2>
...
<h2>Chapter 1.2</h2>
18 | P a g e
...
<h1>Chapter 2</h1>
...
<h2>Chapter 2.1</h2>
...
<h3>Chapter 2.1.2</h3>
...
The above could be headings of any document split into chapters or paragraphs. It would be natural to assign
an id to each chapter as follows:
#c1-2 {
color: red;
}
As shown in the example above you can define the properties in a specific element by using #id in the stylesheet
of the document.
Summary
In this lesson we have learned that through the use of the selectors, class and id, you are able to specify properties
for specific elements.
In the next lesson, we will take a closer look at two HTML-elements which is widely used in connection with
CSS: <span> and <div>.
19 | P a g e
<p>Early to bed and early to rise makes a man healthy, wealthy and wise.</p>
Lets us say we want what Mr. Franklin sees as the benefits of not sleeping your day away emphasized in red. For
that purpose, we can mark the benefits with <span>. Each span is then added a class, which we can define in our
style sheet:
span.benefit {
color:red;
}
Of course you may also use id to add style to the <span>-elements. Just as long as you remember, that you'll have
to apply a unique id to each of the three <span>-elements, as you learned in the previous lesson.
Aside from this difference, the grouping with <div> works in more or less the same way. Let us take a look at an
example with two lists of U.S. presidents divided into their political affiliations:
<div id="democrats">
<ul>
<li>Franklin D. Roosevelt</li>
<li>Harry S. Truman</li>
<li>John F. Kennedy</li>
<li>Lyndon B. Johnson</li>
<li>Jimmy Carter</li>
<li>Bill Clinton</li>
</ul>
</div>
<div id="republicans">
<ul>
<li>Dwight D. Eisenhower</li>
<li>Richard Nixon</li>
<li>Gerald Ford</li>
<li>Ronald Reagan</li>
<li>George Bush</li>
<li>George W. Bush</li>
</ul>
</div>
20 | P a g e
And in our style sheet, we can utilize the grouping in the exact same way as above:
#democrats {
background:blue;
}
#republicans {
background:red;
}
In the examples above, we have only used <div> and <span> on very simple things such as text and background
colors. Both elements have the potential to do much more advanced things. However this will not be introduced
in this lesson. We will look into these later in this tutorial.
Summary
In lesson 7 and 8, you have learned about the selectors id and class and the elements span and div.
You should now be able to group and identify, more or less, all parts of a document, which is a big step in the
direction of mastering CSS. In lesson 9 we will introduce you to the box model.
The illustration above might seem pretty theoretical to look at, so let's try to use the model in an actual case with
a headline and some text. The HTML for our example is (from the Universal Declaration of Human Rights):
<h1>Article 1:</h1>
21 | P a g e
By adding some color and font-information the example could be presented as follows:
The example contains two elements: <h1> and <p>. The box model for the two elements can be illustrated as
follows:
Even though it may look a bit complicated, the illustration shows how each HTML-element is surrounded by boxes.
Boxes which we can adjust using CSS.
The properties which regulate the different boxes are: padding, margin and border. The next two lessons deal with
exactly these three properties:
Lesson 10: The box model - margin & padding
Lesson 11: The box model - border
When you have finished these two lessons, you will master the box model and be able to layout your documents
much finer and more precise than in the old fashion using tables in HTML.
Summary
In this lesson you have been introduced to the box model. In the following three lesson we will take a closer look
at how to create and control elements in the box model.
22 | P a g e
Lesson 10: Margin and padding
In the previous lesson you were introduced to the box model. In this lesson, we will look at how you can change
the presentation of elements by setting the marginand padding properties.
Set the margin in an element
Set the padding in an element
As the first example, we will look at how you define margins for the document itself i.e. for the element <body>.
The illustration below shows how we want the margins in our pages to be.
body {
margin-top: 100px;
margin-right: 40px;
margin-bottom: 10px;
margin-left: 70px;
}
body {
margin: 100px 40px 10px 70px;
}
You can set the margins in the same way on almost every element. For example, we can choose to define margins
for all of our text paragraphs marked with <p>:
body {
margin: 100px 40px 10px 70px;
}
23 | P a g e
p{
margin: 5px 50px 5px 50px;
}
h1 {
background: yellow;
}
h2 {
background: orange;
}
By defining padding for the headlines, you change how much filling there will be around the text in each headline:
h1 {
background: yellow;
padding: 20px 20px 20px 80px;
}
h2 {
background: orange;
padding-left:120px;
}
Summary
You are now on your way to master the box model in CSS. In the next lesson, we will take a closer look at how to
set borders in different colors and how to shape your elements.
24 | P a g e
Lesson 11: Borders
Borders can be used for many things, for example as a decorative element or to underline a separation of two
things. CSS gives you endless options when using borders in your pages.
border-width
border-color
border-style
border
The values none or hidden can be used if you do not want any border.
25 | P a g e
Examples of defining borders
The three properties described above can be put together by each element and thereby produce different
borders. To illustrate this, we will take a look at a document where we define different borders
for <h1>, <h2>, <ul> and <p>. The result may not be that pretty but it illustrates some of the many possibilities:
h1 {
border-width: thick;
border-style: dotted;
border-color: gold;
}
h2 {
border-width: 20px;
border-style: outset;
border-color: red;
}
p{
border-width: 1px;
border-style: dashed;
border-color: blue;
}
ul {
border-width: thin;
border-style: solid;
border-color: orange;
}
It is also possible to state special properties for top-, bottom-, right- or left borders. The following example shows
you how:
h1 {
border-top-width: thick;
border-top-style: solid;
border-top-color: red;
border-bottom-width: thick;
border-bottom-style: solid;
border-bottom-color: blue;
border-right-width: thick;
border-right-style: solid;
border-right-color: green;
border-left-width: thick;
border-left-style: solid;
border-left-color: orange;
}
Compilation [border]
As it is also the case for many other properties, you can compile many properties into one using border. Let us
take a look at an example:
26 | P a g e
p{
border-width: 1px;
border-style: solid;
border-color: blue;
}
p{
border: 1px solid blue;
}
Summary
In this lesson you have learned about the endless options CSS gives you when using borders in your pages.
In the next lesson, we will look at how you define the dimensions in the box model - height and width.
div.box {
width: 200px;
border: 1px solid black;
background: orange;
}
div.box {
height: 500px;
width: 200px;
border: 1px solid black;
background: orange;
}
Summary
Lesson 9, 10, 11 and 12 have given you an introduction to the box model in CSS. As you can probably see, the box
model gives you many new options. You might have been using tables in HTML to create your layouts until now,
but with CSS and the box model you should now be able to achieve elegant layouts more precisely and in
accordance with the recommendations of W3C.
27 | P a g e
Lesson 13: Floating elements (floats)
An element can be floated to the right or to left by using the property float. That is to say that the box with its
contents either floats to the right or to the left in a document (or the containing box) (see lesson 9 for a description
of the Box model). The following figure illustrates the principle:
If we for example would like to have a text wrapping around a picture, the result would be like this:
How is it done?
The HTML code for the example above, look as follows:
<div id="picture">
<img src="bill.jpg" alt="Bill Gates">
</div>
To get the picture floating to the left and the text to surround it, you only have to define the width of the box
which surrounds the picture and thereafter set the property float to left:
#picture {
float:left;
width: 100px;
}
28 | P a g e
Another example: columns
Floats can also be used for columns in a document. To create the columns, you simply have to structure the desired
columns in the HTML-code with <div> as follows:
<div id="column1">
<p>Haec disserens qua de re agatur
et in quo causa consistat non videt...</p>
</div>
<div id="column2">
<p>causas naturales et antecedentes,
idciro etiam nostrarum voluntatum...</p>
</div>
<div id="column3">
<p>nam nihil esset in nostra
potestate si res ita se haberet...</p>
</div>
Now the desired width of the columns is set to e.g. 33%, and then you simply float each column to the left by
defining the property float:
#column1 {
float:left;
width: 33%;
}
#column2 {
float:left;
width: 33%;
}
#column3 {
float:left;
width: 33%;
}
<div id="picture">
<img src="bill.jpg" alt="Bill Gates">
</div>
29 | P a g e
<h1>Bill Gates</h1>
To avoid the text from floating up next to the picture, we can add the following to our CSS:
#picture {
float:left;
width: 100px;
}
.floatstop {
clear:both;
}
Summary
Floats are useful in many situations and will often be used together with positioning. In the next lesson we will
take a closer look at how to position a box, either relative or absolute.
30 | P a g e
The principle behind CSS positioning is that you can position any box anywhere in the system of coordinates.
Let's say we want to position a headline. By using the box model (see lesson 9) the headline will appear as follows:
If we want this headline positioned 100px from the top of the document and 200px from the left of the document,
we could type the following in our CSS:
h1 {
position:absolute;
top: 100px;
left: 200px;
}
As you can see, positioning with CSS is a very precise technique to place elements. It is much easier than trying to
use tables, transparent images or anything else.
Absolute positioning
An element which is positioned absolute does not obtain any space in the document. This means that it does not
leave an empty space after being positioned.
To position an element absolutely, the position property is set as absolute. You can subsequently use the
properties left, right, top, and bottom to place the box.
As an example of absolute positioning, we choose to place 4 boxes in each corner of the document:
#box1 {
position:absolute;
top: 50px;
left: 50px;
}
#box2 {
position:absolute;
top: 50px;
right: 50px;
}
31 | P a g e
#box3 {
position:absolute;
bottom: 50px;
right: 50px;
}
#box4 {
position:absolute;
bottom: 50px;
left: 50px;
}
Relative positioning
To position an element relatively, the property position is set as relative. The difference between absolute and
relative positioning is how the position is being calculated.
The position for an element which is relatively positioned is calculated from the original position in the
document. That means that you move the element to the right, to the left, up or down. This way, the element still
obtains a space in the document after it is positioned.
As an example of relative positioning, we can try to position three pictures relatively to their original position on
the page. Notice how the pictures leave empty spaces at their original positions in the document:
#dog1 {
position:relative;
left: 350px;
bottom: 150px;
}
#dog2 {
position:relative;
left: 150px;
bottom: 500px;
}
#dog3 {
position:relative;
left: 50px;
bottom: 700px;
}
Summary
In the previous two lessons, you learned how to float and position elements. These two methods give you many
opportunities to construct your pages without having to use some of the old-fashioned methods with tables and
transparent images in HTML. Use CSS instead. It is more precise, gives you more advantages, and it is also far
easier to maintain.
32 | P a g e
Lesson 15: Layer on layer with z-index (Layers)
CSS operates in three dimensions - height, width and depth. We have seen the first two dimensions in previous
lessons. In this lesson, we will learn how to let different elements become layers. In short, this means the order
of which the elements overlap one another.
For that purpose, you can assign each element a number (z-index). The system is that an element with a higher
number overlaps an element with a lower number.
Let us say we are playing poker and have a royal flush. Our hand can be presented in a way where each card has
got a z-index:
In this case, the numbers follow on another (1-5) but the same result can be obtained by using 5 different numbers.
The important thing is the chronological sequence of the numbers (the order).
The code in the card example could look like this:
#ten_of_diamonds {
position: absolute;
left: 100px;
top: 100px;
z-index: 1;
}
#jack_of_diamonds {
position: absolute;
left: 115px;
top: 115px;
z-index: 2;
}
#queen_of_diamonds {
position: absolute;
left: 130px;
top: 130px;
z-index: 3;
}
#king_of_diamonds {
position: absolute;
left: 145px;
top: 145px;
z-index: 4;
}
33 | P a g e
#ace_of_diamonds {
position: absolute;
left: 160px;
top: 160px;
z-index: 5;
}
The method is relatively simple but the possibilities are several. You can place images on text or text above text
etc.
Summary
Layers can be used in many situations. For example, try to use z-index to create effects in headlines instead of
creating these as graphics. For one thing, it is faster to load text and for another, it provides a potentially better
ranking in search engines.
If you have been working just a bit with web design, you probably know that there can be a big differences in how
a webpage is presented across different browsers. It can be very frustrating and time-consuming to create a
webpage which can be viewed in Mozilla, Internet Explorer, Opera and all the rest of the existing browsers.
The idea of having standards is to agree upon a common denominator on how to use web technologies. This
means that by observing the standards, a webdeveloper has a certainty that what he or she does will work in a
more appropriate manner across different platforms. We therefore recommend that you back up the work
carried out by the W3C and validate your CSS in order to observe the standard.
34 | P a g e