HTML / CSS – Creating Your Website Design !
This chapter is a bit particular, rather different to the ones seen up to now. In fact it will be an exercise; Now you won’t be able to read the chapter half asleep, you’ll have to code your site at the same time as me
We’ll be re-using everything we’ve learnt up to here. If you feel like you need to, don’t hesitate to re-read the other chapters to jog your memory.
What is the point of this exercise ?
I think the title is clear enough for this question : we’ll be making a proper web site
As I am quite certain that this’ll be the first time for a lot of you, I suggest you pay close attention to this chapter. Creating a design is not a simple task : you need creativity, a good taste in colours, a bit of talent, and finally experience.
All that I can show you, as you’ll have understood, is my experience. After that, talent & creativity is something that you do or don’t have
Right, Where Do We Start ?
As you now know, a web page is a combination of 2 files :
- An XHTML file (.html or .htm) : the content of the page (the text) is in this file. The file is composed of tags such as , which indicate if the text is important, if it’s a paragraph, a quotation etc…
- A CSS file (.css) : this file creates the presentation of our web page. It indicates that the text is red, that the font is “Times New Roman” etc… In theory CSS is optional. However without CSS our pages would be very ugly
If you have been following properly up to here, you’ll know how to start :
- First we need to write the content of the page (XHTML)
- Then modify the presentation (CSS)
You’ll see that the creation of the XHTML file will be (very) quick and easy. However, we’ll spend more time on the CSS, because it’s tricky, there are quite a few CSS properties to know, and because, no matter how good you may be, the browser may not show things as you wish.
Fixed or Stretched Design ?
A fixed design is a design where the design is defined by a fixed width (for example 800 pixels). Generally there is a margin to the left and right, and the content of the page is centred. Here’s a fixed page design :
A stretched design is a design that automatically adapts itself to the visitor’s screen resolution. If the visitor’s resolution is 1024*768 the width of the site will be 1024 pixels. If the resolution is 1600*1400, the width will be 1600 pixels.
Personally, I prefer stretched designs : I find it a shame to leave part of the screen unused.
However, if you browse around a few sites, you’ll notice that a lot of them have fixed designs. Some people say that they look nicer, but I’ll tell you the truth : they’re also a lot easier to create
What kind of design will we be using ?
To tell the truth, coding a stretched design can sometimes be quite complicated, and as this is your first design I don’t want to complicate things too much. Therefore we’ll be using a fixed page design, which isn’t a bad start.
When you’re a bit better, and you’ve got more experience, you’ll be able to start coding stretched page designs. You’ll have plenty of time to learn
First, The XHTML
First step : we need to open our text editor (Notepad ++ if you took the one I showed you at the beginning), and create a new XHTML file.
Come on! Come on! Do it at the same time as me
To start, we’ll write the basic
code of an XHTML page which I showed you in the first chapters. Here it is again :
Code : HTML
1 | http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
2 | < html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" > |
3 | < head > |
4 | < title >Welcome to my site ! title > |
5 | < meta http-equiv = "Content-Type" content = "text/html; charset=iso-8859-1" /> |
6 |
head > |
7 |
8 | < body > |
9 |
10 |
body > |
11 |
html > |
As you already know, all the page content will go between the
tags.Save this file as
index.html, which is always the first page of a web site.
The General Structure
Very often, a web page is structured using
blocktag I told you about. Generally, we’ll have at least 4 blocks, with the following layout :
- At the top, the header. Generally the header contains the title of your site in the form of a banner.
- Under that is the main part of the page with :
- to the left or right, the menu.
- on the other side, the body of the page, meaning the part with the page’s content.
- to the left or right, the menu.
- Finally, at the bottom of the page, is the footer, in which you write the author’s name, maybe an email address, and a copyright.
In XHTML, we create the blocks with 4 different
Code : HTML
1 | < div id = "header" > |
2 | <-- Put the banner here --> |
3 |
div > |
4 |
5 | < div id = "menu" > |
6 | <-- Put the menu here --> |
7 |
div > |
8 |
9 | < div id = "body" > |
10 | <-- Put the content (text) of the page here --> |
11 |
div > |
12 |
13 | < div id = "footer" > |
14 | <-- Put the site's author's name, copyright etc. here --> |
15 |
div > |
It’s simple enough, we’ve just made a
One thing that might have surprised you is that I used id instead of class for the
To explain things quickly, id and class are more or less the same; however an id can only be used once in a page, whereas class can be used several times. Generally there are few id in a web page, they are mainly used for the main blocks
of a page as you can see here.
You must write your blocks in the order that they appear (from top to bottom) in the XHTML code. First is the header, last is the footer. For the menu and the body, which are at the same level, it isn’t too important. You can change their position in the CSS (menu to the right or to the left).
Right, now what needs doing ? Well, we’ll see in more detail what needs putting in the different
The Header
This block will be quick. Often the header is just a banner (an image), or for those who don’t want to do any drawing, a simple
(the page’s title) will do the job.
We’ll be using a banner this time :
All the images used in this chapter are copyright zaz and venom all rights reserved (I promised them I’d put their names into the tutorial ). Thanks to them for the help on this chapter !
How do we put this image into the header ? Well we have 2 choices :
- Either we can insert it using an tag. That’s an easy way to insert an image, anyone can do that.
- Or we can write the title
in the header and insert the banner using CSS.
I admit that the second solution may be a bit surprising. However we’ll be using this method so that the image is shown by simply using CSS. We’ll also add a link to the title of the page so that it sends users back to the home page of the site.
Code : HTML
1 | < div id = "header" > |
2 | < h1 >< a href = "index.html" title = "Return to home page" >Mechanical Spirit a > h1 > |
3 |
div > |
As you can see the link goes to index.html. That’s because the home page of a web site must always be called index.html.
The Menu & Sub-Menus
In the menu block (in blue), we’ll need to make several sub-blocks to separate the different elements in the menu.
Take a good look at the following diagram : it’s a zoom on the menu block I showed earlier :
As you can see, it contains 2 sub-blocks (green) which will be sub-menus.
How do we do this ? In XHTML it is very easy :
Code : HTML
1 | < div id = "menu" > |
2 |
3 | < div class = "sub-menu" > |
4 | Text of the first menu |
5 |
div > |
6 |
7 | < div class = "sub-menu" > |
8 | Text of the second menu |
9 |
div > |
10 |
11 |
div > |
All you have to do is nest
the
sub-menuso that we can modify their CSS later.
Right, now we need to add text to the menu so that it’s like the diagram I showed above.
In each sub-menu
we need to add a title menu. Do you remember the tag to add titles ? Yes, there are 6 tags (depending on the importance of the title) : from
to . As the titles of menus are less important (and we want to reserve the important titles for the page content) we’ll put the menu titles in .
After that, we’ll add a list () to organise the menu.
Code : HTML
1
<
div
id
=
"menu"
>
2
3
<
div
class
=
"sub-menu"
>
4
<
h4
>Menu title
h4
>
5
<
ul
>
6
<
li
>Link
li
>
7
<
li
>Link
li
>
8
<
li
>Link
li
>
9
ul
>
10
div
>
11
12
<
div
class
=
"sub-menu"
>
13
<
h4
>Menu title
h4
>
14
<
ul
>
15
<
li
>Link
li
>
16
<
li
>Link
li
>
17
<
li
>Link
li
>
18
ul
>
19
div
>
20
21
div
>
Generally, we tend to put lists in menus because it allows us to make an organised menu easily.
You may have noticed that I wrote link
but I haven’t added any …> tags. It’s just to avoid complicating the code too quickly, however we will add links to the menu or else it won’t be very useful
Code : HTML
1
<
div
id
=
"menu"
>
2
3
<
div
class
=
"sub-menu"
>
4
<
h4
>Menu title
h4
>
5
<
ul
>
6
<
li
><
a
href
=
"page1.html"
title
=
"Go to page 1"
>Link
a
>
li
>
7
<
li
><
a
href
=
"page2.html"
title
=
"Go to page 2"
>Link
a
>
li
>
8
<
li
><
a
href
=
"page3.html"
title
=
"Go to page 3"
>Link
a
>
li
>
9
ul
>
10
div
>
11
12
<
div
class
=
"sub-menu"
>
13
<
h4
>Menu title
h4
>
14
<
ul
>
15
<
li
><
a
href
=
"page4.html"
title
=
"Go to page 4"
>Link
a
>
li
>
16
<
li
><
a
href
=
"page5.html"
title
=
"Go to page 5"
>Link
a
>
li
>
17
<
li
><
a
href
=
"page6.html"
title
=
"Go to page 6"
>Link
a
>
li
>
18
ul
>
19
div
>
20
21
div
>
And that’s it, you’ve got a nice clean menu, with sub-menus !
You can, if you want, add other sub-menus. There are no limits, but don’t go over the top or visitor’s won’t know what to click on.
You don’t have to (even if it is more common) put links and lists in a menu. If you want to add text to it just use
.
The body
The body is the main part of the page. That’s where we’ll put the content.
So, as I don’t know what your site will talk about, I can’t tell you what to put in it. Only you know that
However I can give you some indications to get you started.
To start, we’ll add a
title which will be the main title of the page ( is the name of the website). Generally, I put the same title in the tag (in ) and in the tag.
After, we need to write paragraphs (
), and maybe add some sub-titles (
) to give some structure to the text
That gives us quite a simple code for the body :
Code : HTML
1
<
div
id
=
"body"
>
2
<
h2
>My Super Site
h2
>
3
4
<
p
>
5
Welcome to my super site !<
br
/>
6
You will love it here, it's a super site that talks about...hmmm...I'm still looking for a subject for my site.
7
p
>
8
9
<
h3
>Who is this site for ?
h3
>
10
<
p
>
11
For everybody ! If I start to privilege certain people, I'll be accused of discrimination :-0 )<
br
/>
12
Whether you're a fan of weapons or Barbie and Ken, this site is made for you ! Yes it is !
13
p
>
14
15
<
h3
>The author
h3
>
16
<
p
>
17
Who's the author of this site ? It's me, what kind of question is that :-p<
br
/>
18
I will try to make the best site in the world (it can't be that hard). My aim is to attract as many people as possible to my site, to make them addicted to it, then to get them under my control.<
br
/>
19
Then I'll take control of the world. After that I'll search every corner of the Earth for more people to put under my great power. Mouahahahaha !!!
20
p
>
21
div
>
Don’t take any notice of the text. It’s just a load of rubbish to fill up the space
Don’t forget that you can put whatever you want here. If you want to add images, text, more paragraphs, then go on, feel free ! It is the content of your site after all
Finally : The Footer
As I’ve already said, the footer is normally for writing the author’s name, and, if you want, you can add a copyright to your site to give it a bit of extra class
Code : HTML
1
<
div
id
=
"footer"
>
2
<
p
>Copyright © 2010, "Rubbish Corporation". All Rights Reserved
p
>
3
div
>
The Complete XHTML Code
We have seen in detail what needs to be put in the 4 blocks.
Now, we’ll assemble all the code we’ve been working on, without forgetting the DOCTYPE,
etc…
This is what we’ve got :
Code : HTML
1
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
xml:lang
=
"en"
>
3
<
head
>
4
<
title
>My Super Site
title
>
5
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=iso-8859-1"
/>
6
head
>
7
8
<
body
>
9
<
div
id
=
"header"
>
10
<
h1
><
a
href
=
"index.html"
title
=
"Return to home page"
>Mechanical Spirit
a
>
h1
>
11
div
>
12
13
<
div
id
=
"menu"
>
14
<
div
class
=
"sub-menu"
>
15
<
h4
>Title menu
h4
>
16
<
ul
>
17
<
li
><
a
href
=
"page1.html"
title
=
"Go to page 1"
>Link
a
>
li
>
18
<
li
><
a
href
=
"page2.html"
title
=
"Go to page 2"
>Link
a
>
li
>
19
<
li
><
a
href
=
"page3.html"
title
=
"Go to page 3"
>Link
a
>
li
>
20
ul
>
21
div
>
22
23
<
div
class
=
"sub-menu"
>
24
<
h4
>Title menu
h4
>
25
<
ul
>
26
<
li
><
a
href
=
"page4.html"
title
=
"Go to page 4"
>Link
a
>
li
>
27
<
li
><
a
href
=
"page5.html"
title
=
"Go to page 5"
>Link
a
>
li
>
28
<
li
><
a
href
=
"page6.html"
title
=
"Go to page 6"
>Link
a
>
li
>
29
ul
>
30
div
>
31
div
>
32
33
<
div
id
=
"body"
>
34
<
h2
>My Super Site
h2
>
35
<
p
>
36
Welcome to my super site !<
br
/>
37
You will love it here, it's a super site that talks about...hmmm...I'm still looking for a subject for my site.
38
p
>
39
40
<
h3
>Who is this site for ?
h3
>
41
<
p
>
42
For everybody ! If I start to privilege certain people, I'll be accused of discrimination)<
br
/>
43
Whether you're a fan of weapons or Barbie and Ken, this site is made for you ! Yes it is !
44
p
>
45
46
<
h3
>The author
h3
>
47
<
p
>
48
Who's the author of this site ? It's me, what kind of question is that :-p<
br
/>
49
I will try to make the best site in the world (it can't be that hard). My aim is to attract as many people as possible to my site, to make them addicted to it, then to get them under my control.<
br
/>
50
Then I'll take control of the world. After that I'll search every corner of the Earth for more people to put under my great power. Mouahahahaha !!!
51
p
>
52
div
>
53
54
<
div
id
=
"footer"
>
55
<
p
>Copyright © 2010, "Rubbish Corporation". All Rights Reserved
p
>
56
div
>
57
body
>
58
html
>
Well, I’ve got to say : well done !
You’ve just created your first real XHTML page !
I realise that you probably wouldn’t have figured all of that out by yourselves, but the main thing is that you understand and follow along with me. There isn’t anything new in this XHTML code; we’ve already seen all these XHTML tags.
And you’ve got to admit that this code isn’t very long (it was a lot longer a few years ago).
However we haven’t finished yet. Without the CSS our page doesn’t look like anything spectacular.
The funny thing is that the XHTML code is enough to make the design. All we need to do now is add a bit of CSS and our site will be nice and clean
You don’t believe me ?
Be careful … don’t underestimate the power of CSS, you could regret it
Second, The CSS
Now we can start the CSS.
This part is important because you must understand how each line of code changes the design.
For that reason I’ll break this part down into lots of stages
, and I suggest that you check out your site after each little step.
If you want to understand properly, also try making a few changes to the CSS. If you can’t figure out a line, try taking it out and see what happens. That’s how I learnt, and I think it’s one of the best ways to learn
Resetting The Layout
By default, each browser has its own way of showing different tags on the screen. For example, in our design, there is a big space between the titles and the paragraphs. So that we can start from scratch we need to get rid of these default settings.
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
Easy ! The * means everything
, so with this code we’re taking the margins off all the tags.
Now save the file as style.css
(or something else if you want ). All that’s left to do now is attach the CSS to the XHTML file in the
:
Code : HTML
1
<
link
rel
=
"stylesheet"
media
=
"screen"
type
=
"text/css"
title
=
"example-css"
href
=
"style.css"
/>
As you can now see, if you refresh the web page, all the margins have gone !
Aligning The Design
To start with, we’re just going to work on the
tag. Yes, I know we haven’t really worked on this tag before, but it works in the same way as everything else.
As a reminder, is the tag that surrounds all the content in your page. If we say the is so wide in pixels (for a fixed design), and we want it to be centred (with margin:auto;), then our job is nearly finished
For the width of the page, I’m going to put 760 pixels. Why 760 ? Because it’s less than 800px, so visitors using a resolution of 800*600 won’t have to scroll from left to right to see the whole page.
As for the margins, a margin: auto; should be enough to centre the design. I’ll also add a margin to the top and bottom to stop the design from being glued to the edges of the browser.
Finally I’ll add a background image so that the page is a bit less empty.
Download Source Files
CLICK HERE TO DOWNLOAD THE IMAGES FOR THIS TUTORIAL
Here’s what it should give :
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
/* align the page in the centre */
10
margin-top
:
20px
;
/* add a 20 pixel margin at the top */
11
margin-bottom
:
20px
;
/* add a 20 pixel margin at the bottom */
12
background-image
:
url
(
"images/bg.png"
);
/* a background image to avoid it being ugly and white :-p */
13
}
Try taking out a few lines of code to see what they all do :
- If you take out the background-image, the background of the page will be white
- If you take out margin-top or margin-bottom the page will be
glued
to the edges of the browser
- If you take out margin:auto your page will align to the left of the browser.
The Header
As promised, we’ll show the header of the site using CSS. Earlier we only put in the title of the site using a
tag. Inserting the header using CSS is useful for sites with the ability to change designs, and using this technique you don’t need to insert an alt
value for you header image
Now we have to insert the banner using CSS :
The first thing to do, if we want the banner to be visible, is to enlarge the size of the header
block so that we can see the whole banner.
As the banner is 758*100 pixels, we need to input these dimensions :
width: 758 px;
height: 100px;
Next we need to insert our background-image (the banner) because the block is still empty.
background-image: url(images/banner.png
);
Our banner should only have enough room to appear once, but it doesn’t take a lot to make sure that it isn’t repeated :
background-repeat: no-repeat;
Finally, to avoid the content of the page from being glued to the banner we need to add a small margin to the bottom of the header :
margin-bottom: 10px;
That’s it. We should now have this :
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
However you will notice that the title we made is still on the page. To get rid of it we need to do the following :
set the same widths as the banner :
width: 758px;
height: 100px;
transform the link from an inline to a block so that the dimensions can be applied to it :
display: block;
indent the text so that it goes out of the screen :
text-indent: -9999px;
Now we have this :
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
22
23
h
1
a {
24
width
:
758px
;
25
height
:
100px
;
26
display
:
block
;
27
text-indent
:
-9999px
;
28
}
Now that we have inserted the banner using CSS, all you have to do to change the design of the banner is change the background-image.
Why is there a # before header ? Don’t you normally use a point ?
No, a quick reminder for those who may have forgotten :
- For a class, you use a
.
(point) in front of the name in the CSS.
- For an id, you use a
#
before the name. And look at the XHTML code, header
is an id !
The Menu
This is a bit harder.
We’ll have to put the menu to the left
of the body, whereas at the moment it is above.
To do this, the most common and quickest method, is to use the CSS property float. It’s a bit of a particular property, as we have already seen, and it will be very useful here to correctly position the body and the menu.
We will also give the menu a width of 120 pixels, so that it isn’t too big.
After that we’ll work on the class sub-menu
(which is a part of the menu). We’ll give it a bit of a darker background colour, a background image with repeat-x (which only repeats horizontally).
Then we’ll add a border to each element of the menu to make them stand out more. 2px
solid black should be good.
Finally we’ll put a margin-bottom on each element to separate them.
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
22
23
h
1
a {
24
width
:
758px
;
25
height
:
100px
;
26
display
:
block
;
27
text-indent
:
-9999px
;
28
}
29
30
#menu {
31
float
:
left
;
/* The menu will float to the left */
32
width
:
120px
;
/* Very important : give the menu a width */
33
}
34
35
.sub-menu {
36
background-color
:
#626262
;
37
background-image
:
url
(
"images/top-bg.png"
);
/* background image repeated horizontally across the top of the menu */
38
background-repeat
:
repeat-x
;
39
border
:
2px
solid
black
;
40
margin-bottom
:
20px
;
/* separate the different elements of the menu */
41
}
Yet again, if you want to understand everything, I suggest you remove a few lines of code to see their effect.
As you can see if you test the code, it’s not wonderful yet, but it is getting better. You can already distinguish the submenus without a problem, however the content goes beneath the menu when it’s finished.
That is normal because that’s how a float works. Take another look at this image I showed in a previous chapter :
How do we stop the text from dropping beneath the menu ?
We need to add a margin-left to the body. This is a simple trick that does the job quickly. However we’ll see that when we get to the body.
We still haven’t finished on the menu. We still need to add a few effects.
I won’t waste time explaining everything in detail. It’s simple, and it’s just to make things look a bit better.
The main thing to understand here, is that I’m nesting elements.
Do you remember, when we write for example :
.sub-menu ul
… means all the
tags inside a sub-menu.
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
22
23
h
1
a {
24
width
:
758px
;
25
height
:
100px
;
26
display
:
block
;
27
text-indent
:
-9999px
;
28
}
29
30
#menu {
31
float
:
left
;
/* The menu will float to the left */
32
width
:
120px
;
/* Very important : give the menu a width */
33
}
34
35
.sub-menu {
36
background-color
:
#626262
;
37
background-image
:
url
(
"images/top-bg.png"
);
38
background-repeat
:
repeat-x
;
39
border
:
2px
solid
black
;
40
margin-bottom
:
20px
;
41
}
42
43
/* a few effects on the menu */
44
45
.sub-menu h
4
{
/* all the titles in the menu */
46
font-size
:
20px
;
47
margin-top
:
18px
;
48
margin-bottom
:
12px
;
49
color
:
#b3b3b3
;
50
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
51
text-align
:
center
;
52
}
53
54
.sub-menu ul {
/* all the lists in the menu */
55
list-style-image
:
url
(
"images/point.png"
);
/* change the looks of the points */
56
padding
:
0px
;
/* get rid of any inner margin there may be */
57
padding-left
:
20px
;
/* ... then add a 20 px margin to the left */
58
margin
:
0px
;
/* same thing on the outer margin */
59
margin-bottom
:
5px
;
/* add a 5 px margin to the bottom */
60
}
61
62
.sub-menu a {
/* all the links in the menu */
63
color
:
#b3b3b3
;
64
}
65
66
.sub-menu a:hover {
/* when the mouse rolls over the links */
67
background-color
:
#b3b3b3
;
68
color
:
black
;
69
}
It’s already looking nicer isn’t it ?
And the effect of the :hover on the links in the menu, is easy to do, so everything’s good so far
I’ve got to precise some things though. You have seen that I put a padding
(which modifies the interior margins), then a padding-left
(which only modifies the inside margine on the left).
This is just a method to save time. Just read the lines in order :
- First, modify all the margins and give them all a value of
0 px
with padding
- Second, modify the left margin. It was 0 px, now we change its value and make it
20 px
with padding-left (we squash
the old value)
If I wanted, I could have done this instead :
padding-top: 0px;
padding-bottom: 0px;
padding-right: 0px;
padding-left: 20px;
But that’s long, and like all other webmasters, I’m lazy
The Body
For the body, it’s very important to define the correct margins.
The problem that needs sorting out urgently is the problem of the text going under the menu. As I said earlier, this is normal because we used float: left on the menu.
How do we stop the text from going under the menu then ?
We just need to change the margin to the left of the body. It’s the outer margin, so we’ll use margin-left. We need to give it a big enough value to push
the text over to the right, so that it doesn’t go under the menu (so we need a greater value than the width of the menu). Here for example, we’ll put a value of 140px. As if by magic, you’ll see, the body will be correctly placed !
Let’s not stop now
We’ll add a margin-bottom so that the footer won’t be glued
to the body :
margin-bottom: 20px;
We’ll also add a small padding (inside margin) on every side so that the text doesn’t touch the edge of the body :
padding: 5px;
Then we’ll add a background colour, a background-image that repeats across the top of the body, a border … well, all that’s decoration
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
22
23
h
1
a {
24
width
:
758px
;
25
height
:
100px
;
26
display
:
block
;
27
text-indent
:
-9999px
;
28
}
29
30
#menu {
31
float
:
left
;
/* The menu will float to the left */
32
width
:
120px
;
/* Very important : give the menu a width */
33
}
34
35
.sub-menu {
36
background-color
:
#626262
;
37
background-image
:
url
(
"images/top-bg.png"
);
38
background-repeat
:
repeat-x
;
39
border
:
2px
solid
black
;
40
margin-bottom
:
20px
;
41
}
42
43
.sub-menu h
4
{
44
font-size
:
20px
;
45
margin-top
:
18px
;
46
margin-bottom
:
12px
;
47
color
:
#b3b3b3
;
48
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
49
text-align
:
center
;
50
}
51
52
.sub-menu ul {
53
list-style-image
:
url
(
"images/point.png"
);
54
padding
:
0px
;
55
padding-left
:
20px
;
56
margin
:
0px
;
57
margin-bottom
:
5px
;
58
}
59
60
.sub-menu a {
61
color
:
#b3b3b3
;
62
}
63
64
.sub-menu a:hover {
65
background-color
:
#b3b3b3
;
66
color
:
black
;
67
}
68
69
/* the body of the page */
70
71
#body {
72
margin-left
:
140px
;
/* margin to the left to stop the text going under the menu */
73
margin-bottom
:
20px
;
/* bottom margin to separate the body from the footer */
74
padding
:
5px
;
/* to stop the text from touching the edge of the body */
75
color
:
#b3b3b3
;
76
background-color
:
#626262
;
/* a background colour for the body */
77
background-image
:
url
(
"images/top-bg.png"
);
/* a small image that repeats across the top of the body */
78
background-repeat
:
repeat-x
;
79
border
:
2px
solid
black
;
/* a border to show the edges of the body and look nice */
80
}
There are still a few things to decorate in the body. For example, the titles : we’ll change the font and use Arial
which I find is nicer on titles.
We’ll also add a background image to the
titles with background-repeat:no-repeat, which means that the image won’t repeat (so it will stay to the left).
To avoid the text of the title going over the top of the image, we’ll add a padding left of a few pixels. That way every time we use a title we’ll automatically get a picture of a cog !
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
}
5
6
body {
7
width
:
760px
;
8
margin
:
auto
;
9
margin-top
:
20px
;
10
margin-bottom
:
20px
;
11
background-image
:
url
(
"images/bg.png"
);
12
}
13
14
#header {
15
width
:
758px
;
16
height
:
100px
;
17
background-image
:
url
(
"images/banner.png"
);
18
background-repeat
:
no-repeat
;
19
margin-bottom
:
10px
;
20
}
21
22
h
1
a {
23
width
:
758px
;
24
height
:
100px
;
25
display
:
block
;
26
text-indent
:
-9999px
;
27
}
28
29
#menu {
30
float
:
left
;
/* The menu will float to the left */
31
width
:
120px
;
/* Very important : give the menu a width */
32
}
33
34
.sub-menu {
35
background-color
:
#626262
;
36
background-image
:
url
(
"images/top-bg.png"
);
37
background-repeat
:
repeat-x
;
38
border
:
2px
solid
black
;
39
margin-bottom
:
20px
;
40
}
41
42
.sub-menu h
4
{
43
font-size
:
20px
;
44
margin-top
:
18px
;
45
margin-bottom
:
12px
;
46
color
:
#b3b3b3
;
47
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
48
text-align
:
center
;
49
}
50
51
.sub-menu ul {
52
list-style-image
:
url
(
"images/point.png"
);
53
padding
:
0px
;
54
padding-left
:
20px
;
55
margin
:
0px
;
56
margin-bottom
:
5px
;
57
}
58
59
.sub-menu a {
60
color
:
#b3b3b3
;
61
}
62
63
.sub-menu a:hover {
64
background-color
:
#b3b3b3
;
65
color
:
black
;
66
}
67
68
#body {
69
margin-left
:
140px
;
70
margin-bottom
:
20px
;
71
padding
:
5px
;
72
color
:
#b3b3b3
;
73
background-color
:
#626262
;
74
background-image
:
url
(
"images/top-bg.png"
);
75
background-repeat
:
repeat-x
;
76
border
:
2px
solid
black
;
77
}
78
79
#body h
2
{
/* all the h2 titles in body */
80
margin-top
:
10px
;
81
margin-bottom
:
10px
;
82
font-size
:
28px
;
83
color
:
#b3b3b3
;
84
text-align
:
center
;
85
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
86
}
87
88
#body h
3
{
/* all the h3 titles in body */
89
height
:
30px
;
90
margin-top
:
10px
;
91
margin-bottom
:
10px
;
92
background-image
:
url
(
"images/title.png"
);
/* a small background image for h3 titles */
93
background-repeat
:
no-repeat
;
/* the image won't repeat, it will be to the left */
94
padding-left
:
30px
;
95
font-size
:
22px
;
96
color
:
#b3b3b3
;
97
text-align
:
left
;
98
}
Nice, isn’t it
The Footer (And We’ve Finished !)
For the footer there is nothing complicated or extraordinary, it’s just similar to the rest. We add a background colour, a border, check that the borders are okay etc …
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
}
5
6
body {
7
width
:
760px
;
8
margin
:
auto
;
9
margin-top
:
20px
;
10
margin-bottom
:
20px
;
11
background-image
:
url
(
"images/bg.png"
);
12
}
13
14
#header {
15
width
:
758px
;
16
height
:
100px
;
17
background-image
:
url
(
"images/banner.png"
);
18
background-repeat
:
no-repeat
;
19
margin-bottom
:
10px
;
20
}
21
22
h
1
a {
23
width
:
758px
;
24
height
:
100px
;
25
display
:
block
;
26
text-indent
:
-9999px
;
27
}
28
29
#menu {
30
float
:
left
;
/* The menu will float to the left */
31
width
:
120px
;
/* Very important : give the menu a width */
32
}
33
34
.sub-menu {
35
background-color
:
#626262
;
36
background-image
:
url
(
"images/top-bg.png"
);
37
background-repeat
:
repeat-x
;
38
border
:
2px
solid
black
;
39
margin-bottom
:
20px
;
40
}
41
42
.sub-menu h
4
{
43
font-size
:
20px
;
44
margin-top
:
18px
;
45
margin-bottom
:
12px
;
46
color
:
#b3b3b3
;
47
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
48
text-align
:
center
;
49
}
50
51
.sub-menu ul {
52
list-style-image
:
url
(
"images/point.png"
);
53
padding
:
0px
;
54
padding-left
:
20px
;
55
margin
:
0px
;
56
margin-bottom
:
5px
;
57
}
58
59
.sub-menu a {
60
color
:
#b3b3b3
;
61
}
62
63
.sub-menu a:hover {
64
background-color
:
#b3b3b3
;
65
color
:
black
;
66
}
67
68
#body {
69
margin-left
:
140px
;
70
margin-bottom
:
20px
;
71
padding
:
5px
;
72
color
:
#b3b3b3
;
73
background-color
:
#626262
;
74
background-image
:
url
(
"images/top-bg.png"
);
75
background-repeat
:
repeat-x
;
76
border
:
2px
solid
black
;
77
}
78
79
#body h
2
{
/* all the h2 titles in body */
80
margin-top
:
10px
;
81
margin-bottom
:
10px
;
82
font-size
:
28px
;
83
color
:
#b3b3b3
;
84
text-align
:
center
;
85
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
86
}
87
88
#body h
3
{
/* all the h3 titles in body */
89
height
:
30px
;
90
margin-top
:
10px
;
91
margin-bottom
:
10px
;
92
background-image
:
url
(
"images/title.png"
);
93
background-repeat
:
no-repeat
;
94
padding-left
:
30px
;
95
font-size
:
22px
;
96
color
:
#b3b3b3
;
97
text-align
:
left
;
98
}
99
100
/* The footer (generally at the bottom for the copyright) */
101
102
#footer {
103
padding
:
5px
;
104
text-align
:
center
;
105
color
:
#b3b3b3
;
106
background-color
:
#626262
;
107
background-image
:
url
(
"images/top-bg.png"
);
108
background-repeat
:
repeat-x
;
109
border
:
2px
solid
black
;
110
}
And that’s the end of the CSS, the design is finished !
To make your whole site just save your page as page1.html
, page2.html
, page3.html
etc… then you can change the content of the different pages !
Our site is finally ready. The Mechanical Spirit
design, which was created just for this tutorial, probably won’t suit your needs. However that was done a bit on purpose
Now that the design is finished, you know what to do, and the main ideas to follow. It’s up to you to create the complete design for your site, to add your colours, your backgrounds, your fonts, your effects …
Well, now you know how to do it
You are allowed (and encouraged) to use my CSS as an inspiration. I know that it isn’t the easiest thing in the world when you’re starting and trying to create first design, and all those lines of code can make you go dizzy.
The good news, is that with practice, not only will you stop getting dizzy, but you’ll also be able to create your complete design without anybody’s help
Download Source Files
CLICK HERE TO DOWNLOAD SOURCE CODE FOR THIS TUTORIAL
Download Source Files
If you want to re-use our source code, modify it, or simply check it out, just click here to download. The file contains the index.html page, the CSS, and the images used during this tutorial.
Wait, It’s Not Finished !
I’ve got a few bits of information to give you. Most of it’s just details, but you might discover a few new things, so pay attention, it won’t be long
Change The CSS, Change The Design !
By modifying the CSS, you can change the whole design without touching the XHTML code !
In fact, have a look at our final XHTML code : it doesn’t contain any information on the design. It just says that there’s a header, a menu, a body to put the content in, and a footer. That’s it !
Let The Visitor Choose The Design
One last thing I would like to show you : alternative style sheets.
If you have made several designs for your site, you can let your visitors decide which design they want to use.
For that, you have to add tags between
and .
As well as the tag to link the CSS to the XHTML page, you have to add this line :
This line indicates that there is another stylesheet (alternate stylesheet
). If the visitor has a recent browser (pretty much anything apart from Internet Explorer), he (or she) will be able to choose the design he wants.
For information : in Firefox if you go to View -> Stylesheet, you can choose the design you want.
Sites offering several designs are rare. A lot of webmasters don’t even know it’s possible to easily do, others can’t be bothered making several designs
.
After that, we’ll add a list () to organise the menu.
Code : HTML
1
<
div
id
=
"menu"
>
2
3
<
div
class
=
"sub-menu"
>
4
<
h4
>Menu title
h4
>
5
<
ul
>
6
<
li
>Link
li
>
7
<
li
>Link
li
>
8
<
li
>Link
li
>
9
ul
>
10
div
>
11
12
<
div
class
=
"sub-menu"
>
13
<
h4
>Menu title
h4
>
14
<
ul
>
15
<
li
>Link
li
>
16
<
li
>Link
li
>
17
<
li
>Link
li
>
18
ul
>
19
div
>
20
21
div
>
Generally, we tend to put lists in menus because it allows us to make an organised menu easily.
You may have noticed that I wrote link
but I haven’t added any …> tags. It’s just to avoid complicating the code too quickly, however we will add links to the menu or else it won’t be very useful
Code : HTML
1
<
div
id
=
"menu"
>
2
3
<
div
class
=
"sub-menu"
>
4
<
h4
>Menu title
h4
>
5
<
ul
>
6
<
li
><
a
href
=
"page1.html"
title
=
"Go to page 1"
>Link
a
>
li
>
7
<
li
><
a
href
=
"page2.html"
title
=
"Go to page 2"
>Link
a
>
li
>
8
<
li
><
a
href
=
"page3.html"
title
=
"Go to page 3"
>Link
a
>
li
>
9
ul
>
10
div
>
11
12
<
div
class
=
"sub-menu"
>
13
<
h4
>Menu title
h4
>
14
<
ul
>
15
<
li
><
a
href
=
"page4.html"
title
=
"Go to page 4"
>Link
a
>
li
>
16
<
li
><
a
href
=
"page5.html"
title
=
"Go to page 5"
>Link
a
>
li
>
17
<
li
><
a
href
=
"page6.html"
title
=
"Go to page 6"
>Link
a
>
li
>
18
ul
>
19
div
>
20
21
div
>
And that’s it, you’ve got a nice clean menu, with sub-menus !
You can, if you want, add other sub-menus. There are no limits, but don’t go over the top or visitor’s won’t know what to click on.
You don’t have to (even if it is more common) put links and lists in a menu. If you want to add text to it just use
.
The body
The body is the main part of the page. That’s where we’ll put the content.
So, as I don’t know what your site will talk about, I can’t tell you what to put in it. Only you know that
However I can give you some indications to get you started.
To start, we’ll add a
title which will be the main title of the page ( is the name of the website). Generally, I put the same title in the tag (in ) and in the tag.
After, we need to write paragraphs (
), and maybe add some sub-titles (
) to give some structure to the text
That gives us quite a simple code for the body :
Code : HTML
1
<
div
id
=
"body"
>
2
<
h2
>My Super Site
h2
>
3
4
<
p
>
5
Welcome to my super site !<
br
/>
6
You will love it here, it's a super site that talks about...hmmm...I'm still looking for a subject for my site.
7
p
>
8
9
<
h3
>Who is this site for ?
h3
>
10
<
p
>
11
For everybody ! If I start to privilege certain people, I'll be accused of discrimination :-0 )<
br
/>
12
Whether you're a fan of weapons or Barbie and Ken, this site is made for you ! Yes it is !
13
p
>
14
15
<
h3
>The author
h3
>
16
<
p
>
17
Who's the author of this site ? It's me, what kind of question is that :-p<
br
/>
18
I will try to make the best site in the world (it can't be that hard). My aim is to attract as many people as possible to my site, to make them addicted to it, then to get them under my control.<
br
/>
19
Then I'll take control of the world. After that I'll search every corner of the Earth for more people to put under my great power. Mouahahahaha !!!
20
p
>
21
div
>
Don’t take any notice of the text. It’s just a load of rubbish to fill up the space
Don’t forget that you can put whatever you want here. If you want to add images, text, more paragraphs, then go on, feel free ! It is the content of your site after all
Finally : The Footer
As I’ve already said, the footer is normally for writing the author’s name, and, if you want, you can add a copyright to your site to give it a bit of extra class
Code : HTML
1
<
div
id
=
"footer"
>
2
<
p
>Copyright © 2010, "Rubbish Corporation". All Rights Reserved
p
>
3
div
>
The Complete XHTML Code
We have seen in detail what needs to be put in the 4 blocks.
Now, we’ll assemble all the code we’ve been working on, without forgetting the DOCTYPE,
etc…
This is what we’ve got :
Code : HTML
1
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
xml:lang
=
"en"
>
3
<
head
>
4
<
title
>My Super Site
title
>
5
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=iso-8859-1"
/>
6
head
>
7
8
<
body
>
9
<
div
id
=
"header"
>
10
<
h1
><
a
href
=
"index.html"
title
=
"Return to home page"
>Mechanical Spirit
a
>
h1
>
11
div
>
12
13
<
div
id
=
"menu"
>
14
<
div
class
=
"sub-menu"
>
15
<
h4
>Title menu
h4
>
16
<
ul
>
17
<
li
><
a
href
=
"page1.html"
title
=
"Go to page 1"
>Link
a
>
li
>
18
<
li
><
a
href
=
"page2.html"
title
=
"Go to page 2"
>Link
a
>
li
>
19
<
li
><
a
href
=
"page3.html"
title
=
"Go to page 3"
>Link
a
>
li
>
20
ul
>
21
div
>
22
23
<
div
class
=
"sub-menu"
>
24
<
h4
>Title menu
h4
>
25
<
ul
>
26
<
li
><
a
href
=
"page4.html"
title
=
"Go to page 4"
>Link
a
>
li
>
27
<
li
><
a
href
=
"page5.html"
title
=
"Go to page 5"
>Link
a
>
li
>
28
<
li
><
a
href
=
"page6.html"
title
=
"Go to page 6"
>Link
a
>
li
>
29
ul
>
30
div
>
31
div
>
32
33
<
div
id
=
"body"
>
34
<
h2
>My Super Site
h2
>
35
<
p
>
36
Welcome to my super site !<
br
/>
37
You will love it here, it's a super site that talks about...hmmm...I'm still looking for a subject for my site.
38
p
>
39
40
<
h3
>Who is this site for ?
h3
>
41
<
p
>
42
For everybody ! If I start to privilege certain people, I'll be accused of discrimination)<
br
/>
43
Whether you're a fan of weapons or Barbie and Ken, this site is made for you ! Yes it is !
44
p
>
45
46
<
h3
>The author
h3
>
47
<
p
>
48
Who's the author of this site ? It's me, what kind of question is that :-p<
br
/>
49
I will try to make the best site in the world (it can't be that hard). My aim is to attract as many people as possible to my site, to make them addicted to it, then to get them under my control.<
br
/>
50
Then I'll take control of the world. After that I'll search every corner of the Earth for more people to put under my great power. Mouahahahaha !!!
51
p
>
52
div
>
53
54
<
div
id
=
"footer"
>
55
<
p
>Copyright © 2010, "Rubbish Corporation". All Rights Reserved
p
>
56
div
>
57
body
>
58
html
>
Well, I’ve got to say : well done !
You’ve just created your first real XHTML page !
I realise that you probably wouldn’t have figured all of that out by yourselves, but the main thing is that you understand and follow along with me. There isn’t anything new in this XHTML code; we’ve already seen all these XHTML tags.
And you’ve got to admit that this code isn’t very long (it was a lot longer a few years ago).
However we haven’t finished yet. Without the CSS our page doesn’t look like anything spectacular.
The funny thing is that the XHTML code is enough to make the design. All we need to do now is add a bit of CSS and our site will be nice and clean
You don’t believe me ?
Be careful … don’t underestimate the power of CSS, you could regret it
Second, The CSS
Now we can start the CSS.
This part is important because you must understand how each line of code changes the design.
For that reason I’ll break this part down into lots of stages
, and I suggest that you check out your site after each little step.
If you want to understand properly, also try making a few changes to the CSS. If you can’t figure out a line, try taking it out and see what happens. That’s how I learnt, and I think it’s one of the best ways to learn
Resetting The Layout
By default, each browser has its own way of showing different tags on the screen. For example, in our design, there is a big space between the titles and the paragraphs. So that we can start from scratch we need to get rid of these default settings.
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
Easy ! The * means everything
, so with this code we’re taking the margins off all the tags.
Now save the file as style.css
(or something else if you want ). All that’s left to do now is attach the CSS to the XHTML file in the
:
Code : HTML
1
<
link
rel
=
"stylesheet"
media
=
"screen"
type
=
"text/css"
title
=
"example-css"
href
=
"style.css"
/>
As you can now see, if you refresh the web page, all the margins have gone !
Aligning The Design
To start with, we’re just going to work on the
tag. Yes, I know we haven’t really worked on this tag before, but it works in the same way as everything else.
As a reminder, is the tag that surrounds all the content in your page. If we say the is so wide in pixels (for a fixed design), and we want it to be centred (with margin:auto;), then our job is nearly finished
For the width of the page, I’m going to put 760 pixels. Why 760 ? Because it’s less than 800px, so visitors using a resolution of 800*600 won’t have to scroll from left to right to see the whole page.
As for the margins, a margin: auto; should be enough to centre the design. I’ll also add a margin to the top and bottom to stop the design from being glued to the edges of the browser.
Finally I’ll add a background image so that the page is a bit less empty.
Download Source Files
CLICK HERE TO DOWNLOAD THE IMAGES FOR THIS TUTORIAL
Here’s what it should give :
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
/* align the page in the centre */
10
margin-top
:
20px
;
/* add a 20 pixel margin at the top */
11
margin-bottom
:
20px
;
/* add a 20 pixel margin at the bottom */
12
background-image
:
url
(
"images/bg.png"
);
/* a background image to avoid it being ugly and white :-p */
13
}
Try taking out a few lines of code to see what they all do :
- If you take out the background-image, the background of the page will be white
- If you take out margin-top or margin-bottom the page will be
glued
to the edges of the browser
- If you take out margin:auto your page will align to the left of the browser.
The Header
As promised, we’ll show the header of the site using CSS. Earlier we only put in the title of the site using a
tag. Inserting the header using CSS is useful for sites with the ability to change designs, and using this technique you don’t need to insert an alt
value for you header image
Now we have to insert the banner using CSS :
The first thing to do, if we want the banner to be visible, is to enlarge the size of the header
block so that we can see the whole banner.
As the banner is 758*100 pixels, we need to input these dimensions :
width: 758 px;
height: 100px;
Next we need to insert our background-image (the banner) because the block is still empty.
background-image: url(images/banner.png
);
Our banner should only have enough room to appear once, but it doesn’t take a lot to make sure that it isn’t repeated :
background-repeat: no-repeat;
Finally, to avoid the content of the page from being glued to the banner we need to add a small margin to the bottom of the header :
margin-bottom: 10px;
That’s it. We should now have this :
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
However you will notice that the title we made is still on the page. To get rid of it we need to do the following :
set the same widths as the banner :
width: 758px;
height: 100px;
transform the link from an inline to a block so that the dimensions can be applied to it :
display: block;
indent the text so that it goes out of the screen :
text-indent: -9999px;
Now we have this :
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
22
23
h
1
a {
24
width
:
758px
;
25
height
:
100px
;
26
display
:
block
;
27
text-indent
:
-9999px
;
28
}
Now that we have inserted the banner using CSS, all you have to do to change the design of the banner is change the background-image.
Why is there a # before header ? Don’t you normally use a point ?
No, a quick reminder for those who may have forgotten :
- For a class, you use a
.
(point) in front of the name in the CSS.
- For an id, you use a
#
before the name. And look at the XHTML code, header
is an id !
The Menu
This is a bit harder.
We’ll have to put the menu to the left
of the body, whereas at the moment it is above.
To do this, the most common and quickest method, is to use the CSS property float. It’s a bit of a particular property, as we have already seen, and it will be very useful here to correctly position the body and the menu.
We will also give the menu a width of 120 pixels, so that it isn’t too big.
After that we’ll work on the class sub-menu
(which is a part of the menu). We’ll give it a bit of a darker background colour, a background image with repeat-x (which only repeats horizontally).
Then we’ll add a border to each element of the menu to make them stand out more. 2px
solid black should be good.
Finally we’ll put a margin-bottom on each element to separate them.
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
22
23
h
1
a {
24
width
:
758px
;
25
height
:
100px
;
26
display
:
block
;
27
text-indent
:
-9999px
;
28
}
29
30
#menu {
31
float
:
left
;
/* The menu will float to the left */
32
width
:
120px
;
/* Very important : give the menu a width */
33
}
34
35
.sub-menu {
36
background-color
:
#626262
;
37
background-image
:
url
(
"images/top-bg.png"
);
/* background image repeated horizontally across the top of the menu */
38
background-repeat
:
repeat-x
;
39
border
:
2px
solid
black
;
40
margin-bottom
:
20px
;
/* separate the different elements of the menu */
41
}
Yet again, if you want to understand everything, I suggest you remove a few lines of code to see their effect.
As you can see if you test the code, it’s not wonderful yet, but it is getting better. You can already distinguish the submenus without a problem, however the content goes beneath the menu when it’s finished.
That is normal because that’s how a float works. Take another look at this image I showed in a previous chapter :
How do we stop the text from dropping beneath the menu ?
We need to add a margin-left to the body. This is a simple trick that does the job quickly. However we’ll see that when we get to the body.
We still haven’t finished on the menu. We still need to add a few effects.
I won’t waste time explaining everything in detail. It’s simple, and it’s just to make things look a bit better.
The main thing to understand here, is that I’m nesting elements.
Do you remember, when we write for example :
.sub-menu ul
… means all the
tags inside a sub-menu.
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
22
23
h
1
a {
24
width
:
758px
;
25
height
:
100px
;
26
display
:
block
;
27
text-indent
:
-9999px
;
28
}
29
30
#menu {
31
float
:
left
;
/* The menu will float to the left */
32
width
:
120px
;
/* Very important : give the menu a width */
33
}
34
35
.sub-menu {
36
background-color
:
#626262
;
37
background-image
:
url
(
"images/top-bg.png"
);
38
background-repeat
:
repeat-x
;
39
border
:
2px
solid
black
;
40
margin-bottom
:
20px
;
41
}
42
43
/* a few effects on the menu */
44
45
.sub-menu h
4
{
/* all the titles in the menu */
46
font-size
:
20px
;
47
margin-top
:
18px
;
48
margin-bottom
:
12px
;
49
color
:
#b3b3b3
;
50
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
51
text-align
:
center
;
52
}
53
54
.sub-menu ul {
/* all the lists in the menu */
55
list-style-image
:
url
(
"images/point.png"
);
/* change the looks of the points */
56
padding
:
0px
;
/* get rid of any inner margin there may be */
57
padding-left
:
20px
;
/* ... then add a 20 px margin to the left */
58
margin
:
0px
;
/* same thing on the outer margin */
59
margin-bottom
:
5px
;
/* add a 5 px margin to the bottom */
60
}
61
62
.sub-menu a {
/* all the links in the menu */
63
color
:
#b3b3b3
;
64
}
65
66
.sub-menu a:hover {
/* when the mouse rolls over the links */
67
background-color
:
#b3b3b3
;
68
color
:
black
;
69
}
It’s already looking nicer isn’t it ?
And the effect of the :hover on the links in the menu, is easy to do, so everything’s good so far
I’ve got to precise some things though. You have seen that I put a padding
(which modifies the interior margins), then a padding-left
(which only modifies the inside margine on the left).
This is just a method to save time. Just read the lines in order :
- First, modify all the margins and give them all a value of
0 px
with padding
- Second, modify the left margin. It was 0 px, now we change its value and make it
20 px
with padding-left (we squash
the old value)
If I wanted, I could have done this instead :
padding-top: 0px;
padding-bottom: 0px;
padding-right: 0px;
padding-left: 20px;
But that’s long, and like all other webmasters, I’m lazy
The Body
For the body, it’s very important to define the correct margins.
The problem that needs sorting out urgently is the problem of the text going under the menu. As I said earlier, this is normal because we used float: left on the menu.
How do we stop the text from going under the menu then ?
We just need to change the margin to the left of the body. It’s the outer margin, so we’ll use margin-left. We need to give it a big enough value to push
the text over to the right, so that it doesn’t go under the menu (so we need a greater value than the width of the menu). Here for example, we’ll put a value of 140px. As if by magic, you’ll see, the body will be correctly placed !
Let’s not stop now
We’ll add a margin-bottom so that the footer won’t be glued
to the body :
margin-bottom: 20px;
We’ll also add a small padding (inside margin) on every side so that the text doesn’t touch the edge of the body :
padding: 5px;
Then we’ll add a background colour, a background-image that repeats across the top of the body, a border … well, all that’s decoration
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
6
7
body {
8
width
:
760px
;
9
margin
:
auto
;
10
margin-top
:
20px
;
11
margin-bottom
:
20px
;
12
background-image
:
url
(
"images/bg.png"
);
13
}
14
15
#header {
16
width
:
758px
;
17
height
:
100px
;
18
background-image
:
url
(
"images/banner.png"
);
19
background-repeat
:
no-repeat
;
20
margin-bottom
:
10px
;
21
}
22
23
h
1
a {
24
width
:
758px
;
25
height
:
100px
;
26
display
:
block
;
27
text-indent
:
-9999px
;
28
}
29
30
#menu {
31
float
:
left
;
/* The menu will float to the left */
32
width
:
120px
;
/* Very important : give the menu a width */
33
}
34
35
.sub-menu {
36
background-color
:
#626262
;
37
background-image
:
url
(
"images/top-bg.png"
);
38
background-repeat
:
repeat-x
;
39
border
:
2px
solid
black
;
40
margin-bottom
:
20px
;
41
}
42
43
.sub-menu h
4
{
44
font-size
:
20px
;
45
margin-top
:
18px
;
46
margin-bottom
:
12px
;
47
color
:
#b3b3b3
;
48
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
49
text-align
:
center
;
50
}
51
52
.sub-menu ul {
53
list-style-image
:
url
(
"images/point.png"
);
54
padding
:
0px
;
55
padding-left
:
20px
;
56
margin
:
0px
;
57
margin-bottom
:
5px
;
58
}
59
60
.sub-menu a {
61
color
:
#b3b3b3
;
62
}
63
64
.sub-menu a:hover {
65
background-color
:
#b3b3b3
;
66
color
:
black
;
67
}
68
69
/* the body of the page */
70
71
#body {
72
margin-left
:
140px
;
/* margin to the left to stop the text going under the menu */
73
margin-bottom
:
20px
;
/* bottom margin to separate the body from the footer */
74
padding
:
5px
;
/* to stop the text from touching the edge of the body */
75
color
:
#b3b3b3
;
76
background-color
:
#626262
;
/* a background colour for the body */
77
background-image
:
url
(
"images/top-bg.png"
);
/* a small image that repeats across the top of the body */
78
background-repeat
:
repeat-x
;
79
border
:
2px
solid
black
;
/* a border to show the edges of the body and look nice */
80
}
There are still a few things to decorate in the body. For example, the titles : we’ll change the font and use Arial
which I find is nicer on titles.
We’ll also add a background image to the
titles with background-repeat:no-repeat, which means that the image won’t repeat (so it will stay to the left).
To avoid the text of the title going over the top of the image, we’ll add a padding left of a few pixels. That way every time we use a title we’ll automatically get a picture of a cog !
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
}
5
6
body {
7
width
:
760px
;
8
margin
:
auto
;
9
margin-top
:
20px
;
10
margin-bottom
:
20px
;
11
background-image
:
url
(
"images/bg.png"
);
12
}
13
14
#header {
15
width
:
758px
;
16
height
:
100px
;
17
background-image
:
url
(
"images/banner.png"
);
18
background-repeat
:
no-repeat
;
19
margin-bottom
:
10px
;
20
}
21
22
h
1
a {
23
width
:
758px
;
24
height
:
100px
;
25
display
:
block
;
26
text-indent
:
-9999px
;
27
}
28
29
#menu {
30
float
:
left
;
/* The menu will float to the left */
31
width
:
120px
;
/* Very important : give the menu a width */
32
}
33
34
.sub-menu {
35
background-color
:
#626262
;
36
background-image
:
url
(
"images/top-bg.png"
);
37
background-repeat
:
repeat-x
;
38
border
:
2px
solid
black
;
39
margin-bottom
:
20px
;
40
}
41
42
.sub-menu h
4
{
43
font-size
:
20px
;
44
margin-top
:
18px
;
45
margin-bottom
:
12px
;
46
color
:
#b3b3b3
;
47
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
48
text-align
:
center
;
49
}
50
51
.sub-menu ul {
52
list-style-image
:
url
(
"images/point.png"
);
53
padding
:
0px
;
54
padding-left
:
20px
;
55
margin
:
0px
;
56
margin-bottom
:
5px
;
57
}
58
59
.sub-menu a {
60
color
:
#b3b3b3
;
61
}
62
63
.sub-menu a:hover {
64
background-color
:
#b3b3b3
;
65
color
:
black
;
66
}
67
68
#body {
69
margin-left
:
140px
;
70
margin-bottom
:
20px
;
71
padding
:
5px
;
72
color
:
#b3b3b3
;
73
background-color
:
#626262
;
74
background-image
:
url
(
"images/top-bg.png"
);
75
background-repeat
:
repeat-x
;
76
border
:
2px
solid
black
;
77
}
78
79
#body h
2
{
/* all the h2 titles in body */
80
margin-top
:
10px
;
81
margin-bottom
:
10px
;
82
font-size
:
28px
;
83
color
:
#b3b3b3
;
84
text-align
:
center
;
85
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
86
}
87
88
#body h
3
{
/* all the h3 titles in body */
89
height
:
30px
;
90
margin-top
:
10px
;
91
margin-bottom
:
10px
;
92
background-image
:
url
(
"images/title.png"
);
/* a small background image for h3 titles */
93
background-repeat
:
no-repeat
;
/* the image won't repeat, it will be to the left */
94
padding-left
:
30px
;
95
font-size
:
22px
;
96
color
:
#b3b3b3
;
97
text-align
:
left
;
98
}
Nice, isn’t it
The Footer (And We’ve Finished !)
For the footer there is nothing complicated or extraordinary, it’s just similar to the rest. We add a background colour, a border, check that the borders are okay etc …
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
}
5
6
body {
7
width
:
760px
;
8
margin
:
auto
;
9
margin-top
:
20px
;
10
margin-bottom
:
20px
;
11
background-image
:
url
(
"images/bg.png"
);
12
}
13
14
#header {
15
width
:
758px
;
16
height
:
100px
;
17
background-image
:
url
(
"images/banner.png"
);
18
background-repeat
:
no-repeat
;
19
margin-bottom
:
10px
;
20
}
21
22
h
1
a {
23
width
:
758px
;
24
height
:
100px
;
25
display
:
block
;
26
text-indent
:
-9999px
;
27
}
28
29
#menu {
30
float
:
left
;
/* The menu will float to the left */
31
width
:
120px
;
/* Very important : give the menu a width */
32
}
33
34
.sub-menu {
35
background-color
:
#626262
;
36
background-image
:
url
(
"images/top-bg.png"
);
37
background-repeat
:
repeat-x
;
38
border
:
2px
solid
black
;
39
margin-bottom
:
20px
;
40
}
41
42
.sub-menu h
4
{
43
font-size
:
20px
;
44
margin-top
:
18px
;
45
margin-bottom
:
12px
;
46
color
:
#b3b3b3
;
47
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
48
text-align
:
center
;
49
}
50
51
.sub-menu ul {
52
list-style-image
:
url
(
"images/point.png"
);
53
padding
:
0px
;
54
padding-left
:
20px
;
55
margin
:
0px
;
56
margin-bottom
:
5px
;
57
}
58
59
.sub-menu a {
60
color
:
#b3b3b3
;
61
}
62
63
.sub-menu a:hover {
64
background-color
:
#b3b3b3
;
65
color
:
black
;
66
}
67
68
#body {
69
margin-left
:
140px
;
70
margin-bottom
:
20px
;
71
padding
:
5px
;
72
color
:
#b3b3b3
;
73
background-color
:
#626262
;
74
background-image
:
url
(
"images/top-bg.png"
);
75
background-repeat
:
repeat-x
;
76
border
:
2px
solid
black
;
77
}
78
79
#body h
2
{
/* all the h2 titles in body */
80
margin-top
:
10px
;
81
margin-bottom
:
10px
;
82
font-size
:
28px
;
83
color
:
#b3b3b3
;
84
text-align
:
center
;
85
font-family
:
Arial
,
"Arial Black"
,
"Times New Roman"
, Times,
serif
;
86
}
87
88
#body h
3
{
/* all the h3 titles in body */
89
height
:
30px
;
90
margin-top
:
10px
;
91
margin-bottom
:
10px
;
92
background-image
:
url
(
"images/title.png"
);
93
background-repeat
:
no-repeat
;
94
padding-left
:
30px
;
95
font-size
:
22px
;
96
color
:
#b3b3b3
;
97
text-align
:
left
;
98
}
99
100
/* The footer (generally at the bottom for the copyright) */
101
102
#footer {
103
padding
:
5px
;
104
text-align
:
center
;
105
color
:
#b3b3b3
;
106
background-color
:
#626262
;
107
background-image
:
url
(
"images/top-bg.png"
);
108
background-repeat
:
repeat-x
;
109
border
:
2px
solid
black
;
110
}
And that’s the end of the CSS, the design is finished !
To make your whole site just save your page as page1.html
, page2.html
, page3.html
etc… then you can change the content of the different pages !
Our site is finally ready. The Mechanical Spirit
design, which was created just for this tutorial, probably won’t suit your needs. However that was done a bit on purpose
Now that the design is finished, you know what to do, and the main ideas to follow. It’s up to you to create the complete design for your site, to add your colours, your backgrounds, your fonts, your effects …
Well, now you know how to do it
You are allowed (and encouraged) to use my CSS as an inspiration. I know that it isn’t the easiest thing in the world when you’re starting and trying to create first design, and all those lines of code can make you go dizzy.
The good news, is that with practice, not only will you stop getting dizzy, but you’ll also be able to create your complete design without anybody’s help
Download Source Files
CLICK HERE TO DOWNLOAD SOURCE CODE FOR THIS TUTORIAL
Download Source Files
If you want to re-use our source code, modify it, or simply check it out, just click here to download. The file contains the index.html page, the CSS, and the images used during this tutorial.
Wait, It’s Not Finished !
I’ve got a few bits of information to give you. Most of it’s just details, but you might discover a few new things, so pay attention, it won’t be long
Change The CSS, Change The Design !
By modifying the CSS, you can change the whole design without touching the XHTML code !
In fact, have a look at our final XHTML code : it doesn’t contain any information on the design. It just says that there’s a header, a menu, a body to put the content in, and a footer. That’s it !
Let The Visitor Choose The Design
One last thing I would like to show you : alternative style sheets.
If you have made several designs for your site, you can let your visitors decide which design they want to use.
For that, you have to add tags between
and .
As well as the tag to link the CSS to the XHTML page, you have to add this line :
This line indicates that there is another stylesheet (alternate stylesheet
). If the visitor has a recent browser (pretty much anything apart from Internet Explorer), he (or she) will be able to choose the design he wants.
For information : in Firefox if you go to View -> Stylesheet, you can choose the design you want.
Sites offering several designs are rare. A lot of webmasters don’t even know it’s possible to easily do, others can’t be bothered making several designs
Code : HTML
1 | < div id = "menu" > |
2 |
3 | < div class = "sub-menu" > |
4 | < h4 >Menu title h4 > |
5 | < ul > |
6 | < li >Link li > |
7 | < li >Link li > |
8 | < li >Link li > |
9 |
ul > |
10 |
div > |
11 |
12 | < div class = "sub-menu" > |
13 | < h4 >Menu title h4 > |
14 | < ul > |
15 | < li >Link li > |
16 | < li >Link li > |
17 | < li >Link li > |
18 |
ul > |
19 |
div > |
20 |
21 |
div > |
Generally, we tend to put lists in menus because it allows us to make an organised menu easily.
You may have noticed that I wrote link
but I haven’t added any …> tags. It’s just to avoid complicating the code too quickly, however we will add links to the menu or else it won’t be very useful
Code : HTML
1 | < div id = "menu" > |
2 |
3 | < div class = "sub-menu" > |
4 | < h4 >Menu title h4 > |
5 | < ul > |
6 | < li >< a href = "page1.html" title = "Go to page 1" >Link a > li > |
7 | < li >< a href = "page2.html" title = "Go to page 2" >Link a > li > |
8 | < li >< a href = "page3.html" title = "Go to page 3" >Link a > li > |
9 |
ul > |
10 |
div > |
11 |
12 | < div class = "sub-menu" > |
13 | < h4 >Menu title h4 > |
14 | < ul > |
15 | < li >< a href = "page4.html" title = "Go to page 4" >Link a > li > |
16 | < li >< a href = "page5.html" title = "Go to page 5" >Link a > li > |
17 | < li >< a href = "page6.html" title = "Go to page 6" >Link a > li > |
18 |
ul > |
19 |
div > |
20 |
21 |
div > |
And that’s it, you’ve got a nice clean menu, with sub-menus !
You can, if you want, add other sub-menus. There are no limits, but don’t go over the top or visitor’s won’t know what to click on.
You don’t have to (even if it is more common) put links and lists in a menu. If you want to add text to it just use
.The body
The body is the main part of the page. That’s where we’ll put the content.
So, as I don’t know what your site will talk about, I can’t tell you what to put in it. Only you know that
However I can give you some indications to get you started.
To start, we’ll add a
title which will be the main title of the page ( is the name of the website). Generally, I put the same title in the tag (in ) and in the tag.
After, we need to write paragraphs (
), and maybe add some sub-titles (
) to give some structure to the text
That gives us quite a simple code for the body :
Code : HTML
1
<
div
id
=
"body"
>
2
<
h2
>My Super Site
h2
>
3
4
<
p
>
5
Welcome to my super site !<
br
/>
6
You will love it here, it's a super site that talks about...hmmm...I'm still looking for a subject for my site.
7
p
>
8
9
<
h3
>Who is this site for ?
h3
>
10
<
p
>
11
For everybody ! If I start to privilege certain people, I'll be accused of discrimination :-0 )<
br
/>
12
Whether you're a fan of weapons or Barbie and Ken, this site is made for you ! Yes it is !
13
p
>
14
15
<
h3
>The author
h3
>
16
<
p
>
17
Who's the author of this site ? It's me, what kind of question is that :-p<
br
/>
18
I will try to make the best site in the world (it can't be that hard). My aim is to attract as many people as possible to my site, to make them addicted to it, then to get them under my control.<
br
/>
19
Then I'll take control of the world. After that I'll search every corner of the Earth for more people to put under my great power. Mouahahahaha !!!
20
p
>
21
div
>
Don’t take any notice of the text. It’s just a load of rubbish to fill up the space
Don’t forget that you can put whatever you want here. If you want to add images, text, more paragraphs, then go on, feel free ! It is the content of your site after all
Finally : The Footer
As I’ve already said, the footer is normally for writing the author’s name, and, if you want, you can add a copyright to your site to give it a bit of extra class
Code : HTML
1
<
div
id
=
"footer"
>
2
<
p
>Copyright © 2010, "Rubbish Corporation". All Rights Reserved
p
>
3
div
>
The Complete XHTML Code
We have seen in detail what needs to be put in the 4 blocks.
Now, we’ll assemble all the code we’ve been working on, without forgetting the DOCTYPE,
etc…
This is what we’ve got :
Code : HTML
1
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
xml:lang
=
"en"
>
3
<
head
>
4
<
title
>My Super Site
title
>
5
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=iso-8859-1"
/>
6
head
>
7
8
<
body
>
9
<
div
id
=
"header"
>
10
<
h1
><
a
href
=
"index.html"
title
=
"Return to home page"
>Mechanical Spirit
a
>
h1
>
11
div
>
12
13
<
div
id
=
"menu"
>
14
<
div
class
=
"sub-menu"
>
15
<
h4
>Title menu
h4
>
16
<
ul
>
17
<
li
><
a
href
=
"page1.html"
title
=
"Go to page 1"
>Link
a
>
li
>
18
<
li
><
a
href
=
"page2.html"
title
=
"Go to page 2"
>Link
a
>
li
>
19
<
li
><
a
href
=
"page3.html"
title
=
"Go to page 3"
>Link
a
>
li
>
20
ul
>
21
div
>
22
23
<
div
class
=
"sub-menu"
>
24
<
h4
>Title menu
h4
>
25
<
ul
>
26
<
li
><
a
href
=
"page4.html"
title
=
"Go to page 4"
>Link
a
>
li
>
27
<
li
><
a
href
=
"page5.html"
title
=
"Go to page 5"
>Link
a
>
li
>
28
<
li
><
a
href
=
"page6.html"
title
=
"Go to page 6"
>Link
a
>
li
>
29
ul
>
30
div
>
31
div
>
32
33
<
div
id
=
"body"
>
34
<
h2
>My Super Site
h2
>
35
<
p
>
36
Welcome to my super site !<
br
/>
37
You will love it here, it's a super site that talks about...hmmm...I'm still looking for a subject for my site.
38
p
>
39
40
<
h3
>Who is this site for ?
h3
>
41
<
p
>
42
For everybody ! If I start to privilege certain people, I'll be accused of discrimination)<
br
/>
43
Whether you're a fan of weapons or Barbie and Ken, this site is made for you ! Yes it is !
44
p
>
45
46
<
h3
>The author
h3
>
47
<
p
>
48
Who's the author of this site ? It's me, what kind of question is that :-p<
br
/>
49
I will try to make the best site in the world (it can't be that hard). My aim is to attract as many people as possible to my site, to make them addicted to it, then to get them under my control.<
br
/>
50
Then I'll take control of the world. After that I'll search every corner of the Earth for more people to put under my great power. Mouahahahaha !!!
51
p
>
52
div
>
53
54
<
div
id
=
"footer"
>
55
<
p
>Copyright © 2010, "Rubbish Corporation". All Rights Reserved
p
>
56
div
>
57
body
>
58
html
>
Well, I’ve got to say : well done !
You’ve just created your first real XHTML page !
I realise that you probably wouldn’t have figured all of that out by yourselves, but the main thing is that you understand and follow along with me. There isn’t anything new in this XHTML code; we’ve already seen all these XHTML tags.
And you’ve got to admit that this code isn’t very long (it was a lot longer a few years ago).
However we haven’t finished yet. Without the CSS our page doesn’t look like anything spectacular.
The funny thing is that the XHTML code is enough to make the design. All we need to do now is add a bit of CSS and our site will be nice and clean
You don’t believe me ?
Be careful … don’t underestimate the power of CSS, you could regret it
Second, The CSS
Now we can start the CSS.
This part is important because you must understand how each line of code changes the design.
For that reason I’ll break this part down into lots of stages
, and I suggest that you check out your site after each little step.
If you want to understand properly, also try making a few changes to the CSS. If you can’t figure out a line, try taking it out and see what happens. That’s how I learnt, and I think it’s one of the best ways to learn
Resetting The Layout
By default, each browser has its own way of showing different tags on the screen. For example, in our design, there is a big space between the titles and the paragraphs. So that we can start from scratch we need to get rid of these default settings.
Code : CSS
1
* {
2
border
:
0
;
3
margin
:
0
;
4
padding
:
0
;
5
}
Easy ! The * means everything
, so with this code we’re taking the margins off all the tags.
Now save the file as style.css
(or something else if you want ). All that’s left to do now is attach the CSS to the XHTML file in the
:
Code : HTML
1
<
link
rel
=
"stylesheet"
media
=
"screen"
type
=
"text/css"
title
=
"example-css"
href
=
"style.css"
/>
As you can now see, if you refresh the web page, all the margins have gone !
Aligning The Design
To start with, we’re just going to work on the
tag. Yes, I know we haven’t really worked on this tag before, but it works in the same way as everything else.
As a reminder, is the tag that surrounds all the content in your page. If we say the is so wide in pixels (for a fixed design), and we want it to be centred (with margin:auto;), then our job is nearly finished
tag.
After, we need to write paragraphs (
), and maybe add some sub-titles (
) to give some structure to the text
That gives us quite a simple code for the body :
Code : HTML
1 | < div id = "body" > |
2 | < h2 >My Super Site h2 > |
3 |
4 | < p > |
5 | Welcome to my super site !< br /> |
6 | You will love it here, it's a super site that talks about...hmmm...I'm still looking for a subject for my site. |
7 |
p > |
8 |
9 | < h3 >Who is this site for ? h3 > |
10 | < p > |
11 | For everybody ! If I start to privilege certain people, I'll be accused of discrimination :-0 )< br /> |
12 | Whether you're a fan of weapons or Barbie and Ken, this site is made for you ! Yes it is ! |
13 |
p > |
14 |
15 | < h3 >The author h3 > |
16 | < p > |
17 | Who's the author of this site ? It's me, what kind of question is that :-p< br /> |
18 | I will try to make the best site in the world (it can't be that hard). My aim is to attract as many people as possible to my site, to make them addicted to it, then to get them under my control.< br /> |
19 | Then I'll take control of the world. After that I'll search every corner of the Earth for more people to put under my great power. Mouahahahaha !!! |
20 |
p > |
21 |
div > |
Don’t take any notice of the text. It’s just a load of rubbish to fill up the space
Don’t forget that you can put whatever you want here. If you want to add images, text, more paragraphs, then go on, feel free ! It is the content of your site after all
Finally : The Footer
As I’ve already said, the footer is normally for writing the author’s name, and, if you want, you can add a copyright to your site to give it a bit of extra class
Code : HTML
1 | < div id = "footer" > |
2 | < p >Copyright © 2010, "Rubbish Corporation". All Rights Reserved p > |
3 |
div > |
The Complete XHTML Code
We have seen in detail what needs to be put in the 4 blocks.
Now, we’ll assemble all the code we’ve been working on, without forgetting the DOCTYPE,
This is what we’ve got :
Code : HTML
1 | http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
2 | < html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" > |
3 | < head > |
4 | < title >My Super Site title > |
5 | < meta http-equiv = "Content-Type" content = "text/html; charset=iso-8859-1" /> |
6 |
head > |
7 |
8 | < body > |
9 | < div id = "header" > |
10 | < h1 >< a href = "index.html" title = "Return to home page" >Mechanical Spirit a > h1 > |
11 |
div > |
12 |
13 | < div id = "menu" > |
14 | < div class = "sub-menu" > |
15 | < h4 >Title menu h4 > |
16 | < ul > |
17 | < li >< a href = "page1.html" title = "Go to page 1" >Link a > li > |
18 | < li >< a href = "page2.html" title = "Go to page 2" >Link a > li > |
19 | < li >< a href = "page3.html" title = "Go to page 3" >Link a > li > |
20 |
ul > |
21 |
div > |
22 |
23 | < div class = "sub-menu" > |
24 | < h4 >Title menu h4 > |
25 | < ul > |
26 | < li >< a href = "page4.html" title = "Go to page 4" >Link a > li > |
27 | < li >< a href = "page5.html" title = "Go to page 5" >Link a > li > |
28 | < li >< a href = "page6.html" title = "Go to page 6" >Link a > li > |
29 |
ul > |
30 |
div > |
31 |
div > |
32 |
33 | < div id = "body" > |
34 | < h2 >My Super Site h2 > |
35 | < p > |
36 | Welcome to my super site !< br /> |
37 | You will love it here, it's a super site that talks about...hmmm...I'm still looking for a subject for my site. |
38 |
p > |
39 |
40 | < h3 >Who is this site for ? h3 > |
41 | < p > |
42 | For everybody ! If I start to privilege certain people, I'll be accused of discrimination)< br /> |
43 | Whether you're a fan of weapons or Barbie and Ken, this site is made for you ! Yes it is ! |
44 |
p > |
45 |
46 | < h3 >The author h3 > |
47 | < p > |
48 | Who's the author of this site ? It's me, what kind of question is that :-p< br /> |
49 | I will try to make the best site in the world (it can't be that hard). My aim is to attract as many people as possible to my site, to make them addicted to it, then to get them under my control.< br /> |
50 | Then I'll take control of the world. After that I'll search every corner of the Earth for more people to put under my great power. Mouahahahaha !!! |
51 |
p > |
52 |
div > |
53 |
54 | < div id = "footer" > |
55 | < p >Copyright © 2010, "Rubbish Corporation". All Rights Reserved p > |
56 |
div > |
57 |
body > |
58 |
html > |
Well, I’ve got to say : well done !
You’ve just created your first real XHTML page !
I realise that you probably wouldn’t have figured all of that out by yourselves, but the main thing is that you understand and follow along with me. There isn’t anything new in this XHTML code; we’ve already seen all these XHTML tags.
And you’ve got to admit that this code isn’t very long (it was a lot longer a few years ago).
However we haven’t finished yet. Without the CSS our page doesn’t look like anything spectacular.
The funny thing is that the XHTML code is enough to make the design. All we need to do now is add a bit of CSS and our site will be nice and clean
You don’t believe me ?
Be careful … don’t underestimate the power of CSS, you could regret it
Second, The CSS
Now we can start the CSS.
This part is important because you must understand how each line of code changes the design.
For that reason I’ll break this part down into lots of stages
, and I suggest that you check out your site after each little step.
If you want to understand properly, also try making a few changes to the CSS. If you can’t figure out a line, try taking it out and see what happens. That’s how I learnt, and I think it’s one of the best ways to learn
Resetting The Layout
By default, each browser has its own way of showing different tags on the screen. For example, in our design, there is a big space between the titles and the paragraphs. So that we can start from scratch we need to get rid of these default settings.
Code : CSS
1 | * { |
2 | border : 0 ; |
3 | margin : 0 ; |
4 | padding : 0 ; |
5 | } |
Easy ! The * means everything
, so with this code we’re taking the margins off all the tags.
Now save the file as style.css
(or something else if you want ). All that’s left to do now is attach the CSS to the XHTML file in the
Code : HTML
1 | < link rel = "stylesheet" media = "screen" type = "text/css" title = "example-css" href = "style.css" /> |
As you can now see, if you refresh the web page, all the margins have gone !
Aligning The Design
To start with, we’re just going to work on the
As a reminder, is the tag that surrounds all the content in your page. If we say the is so wide in pixels (for a fixed design), and we want it to be centred (with margin:auto;), then our job is nearly finished
For the width of the page, I’m going to put 760 pixels. Why 760 ? Because it’s less than 800px, so visitors using a resolution of 800*600 won’t have to scroll from left to right to see the whole page.
As for the margins, a margin: auto; should be enough to centre the design. I’ll also add a margin to the top and bottom to stop the design from being glued to the edges of the browser.
Finally I’ll add a background image so that the page is a bit less empty.
Download Source Files
CLICK HERE TO DOWNLOAD THE IMAGES FOR THIS TUTORIAL
Here’s what it should give :
Code : CSS
1 | * { |
2 | border : 0 ; |
3 | margin : 0 ; |
4 | padding : 0 ; |
5 | } |
6 |
7 | body { |
8 | width : 760px ; |
9 | margin : auto ; /* align the page in the centre */ |
10 | margin-top : 20px ; /* add a 20 pixel margin at the top */ |
11 | margin-bottom : 20px ; /* add a 20 pixel margin at the bottom */ |
12 | background-image : url ( "images/bg.png" ); /* a background image to avoid it being ugly and white :-p */ |
13 | } |
Try taking out a few lines of code to see what they all do :
- If you take out the background-image, the background of the page will be white
- If you take out margin-top or margin-bottom the page will be
glued
to the edges of the browser - If you take out margin:auto your page will align to the left of the browser.
The Header
As promised, we’ll show the header of the site using CSS. Earlier we only put in the title of the site using a
tag. Inserting the header using CSS is useful for sites with the ability to change designs, and using this technique you don’t need to insert an alt
value for you header image
Now we have to insert the banner using CSS :
The first thing to do, if we want the banner to be visible, is to enlarge the size of the
header
block so that we can see the whole banner.
As the banner is 758*100 pixels, we need to input these dimensions :
width: 758 px;
height: 100px;Next we need to insert our background-image (the banner) because the block is still empty.
background-image: url(images/banner.png
);Our banner should only have enough room to appear once, but it doesn’t take a lot to make sure that it isn’t repeated :
background-repeat: no-repeat;Finally, to avoid the content of the page from being glued to the banner we need to add a small margin to the bottom of the header :
margin-bottom: 10px;
That’s it. We should now have this :
Code : CSS
1 | * { |
2 | border : 0 ; |
3 | margin : 0 ; |
4 | padding : 0 ; |
5 | } |
6 |
7 | body { |
8 | width : 760px ; |
9 | margin : auto ; |
10 | margin-top : 20px ; |
11 | margin-bottom : 20px ; |
12 | background-image : url ( "images/bg.png" ); |
13 | } |
14 |
15 | #header { |
16 | width : 758px ; |
17 | height : 100px ; |
18 | background-image : url ( "images/banner.png" ); |
19 | background-repeat : no-repeat ; |
20 | margin-bottom : 10px ; |
21 | } |
However you will notice that the title we made is still on the page. To get rid of it we need to do the following :
set the same widths as the banner :
width: 758px;
height: 100px;transform the link from an inline to a block so that the dimensions can be applied to it :
display: block;indent the text so that it goes out of the screen :
text-indent: -9999px;
Now we have this :
Code : CSS
1 | * { |
2 | border : 0 ; |
3 | margin : 0 ; |
4 | padding : 0 ; |
5 | } |
6 |
7 | body { |
8 | width : 760px ; |
9 | margin : auto ; |
10 | margin-top : 20px ; |
11 | margin-bottom : 20px ; |
12 | background-image : url ( "images/bg.png" ); |
13 | } |
14 |
15 | #header { |
16 | width : 758px ; |
17 | height : 100px ; |
18 | background-image : url ( "images/banner.png" ); |
19 | background-repeat : no-repeat ; |
20 | margin-bottom : 10px ; |
21 | } |
22 |
23 | h 1 a { |
24 | width : 758px ; |
25 | height : 100px ; |
26 | display : block ; |
27 | text-indent : -9999px ; |
28 | } |
Now that we have inserted the banner using CSS, all you have to do to change the design of the banner is change the background-image.
Why is there a # before header ? Don’t you normally use a point ?
No, a quick reminder for those who may have forgotten :
- For a class, you use a
.
(point) in front of the name in the CSS. - For an id, you use a
#
before the name. And look at the XHTML code,header
is an id !
The Menu
This is a bit harder.
We’ll have to put the menu to the left
of the body, whereas at the moment it is above.
To do this, the most common and quickest method, is to use the CSS property float. It’s a bit of a particular property, as we have already seen, and it will be very useful here to correctly position the body and the menu.
We will also give the menu a width of 120 pixels, so that it isn’t too big.
After that we’ll work on the class sub-menu
(which is a part of the menu). We’ll give it a bit of a darker background colour, a background image with repeat-x (which only repeats horizontally).
Then we’ll add a border to each element of the menu to make them stand out more. 2px
solid black should be good.
Finally we’ll put a margin-bottom on each element to separate them.
Code : CSS
1 | * { |
2 | border : 0 ; |
3 | margin : 0 ; |
4 | padding : 0 ; |
5 | } |
6 |
7 | body { |
8 | width : 760px ; |
9 | margin : auto ; |
10 | margin-top : 20px ; |
11 | margin-bottom : 20px ; |
12 | background-image : url ( "images/bg.png" ); |
13 | } |
14 |
15 | #header { |
16 | width : 758px ; |
17 | height : 100px ; |
18 | background-image : url ( "images/banner.png" ); |
19 | background-repeat : no-repeat ; |
20 | margin-bottom : 10px ; |
21 | } |
22 |
23 | h 1 a { |
24 | width : 758px ; |
25 | height : 100px ; |
26 | display : block ; |
27 | text-indent : -9999px ; |
28 | } |
29 |
30 | #menu { |
31 | float : left ; /* The menu will float to the left */ |
32 | width : 120px ; /* Very important : give the menu a width */ |
33 | } |
34 |
35 | .sub-menu { |
36 | background-color : #626262 ; |
37 | background-image : url ( "images/top-bg.png" ); /* background image repeated horizontally across the top of the menu */ |
38 | background-repeat : repeat-x ; |
39 | border : 2px solid black ; |
40 | margin-bottom : 20px ; /* separate the different elements of the menu */ |
41 | } |
Yet again, if you want to understand everything, I suggest you remove a few lines of code to see their effect.
As you can see if you test the code, it’s not wonderful yet, but it is getting better. You can already distinguish the submenus without a problem, however the content goes beneath the menu when it’s finished.
That is normal because that’s how a float works. Take another look at this image I showed in a previous chapter :
How do we stop the text from dropping beneath the menu ?
We need to add a margin-left to the body. This is a simple trick that does the job quickly. However we’ll see that when we get to the body.
We still haven’t finished on the menu. We still need to add a few effects.
I won’t waste time explaining everything in detail. It’s simple, and it’s just to make things look a bit better.
The main thing to understand here, is that I’m nesting elements.
Do you remember, when we write for example :
.sub-menu ul
… means all the
- tags inside a sub-menu.
- First, modify all the margins and give them all a value of
0 px
with padding - Second, modify the left margin. It was 0 px, now we change its value and make it
20 px
with padding-left (wesquash
the old value)
Code : CSS
1 | * { |
2 | border : 0 ; |
3 | margin : 0 ; |
4 | padding : 0 ; |
5 | } |
6 |
7 | body { |
8 | width : 760px ; |
9 | margin : auto ; |
10 | margin-top : 20px ; |
11 | margin-bottom : 20px ; |
12 | background-image : url ( "images/bg.png" ); |
13 | } |
14 |
15 | #header { |
16 | width : 758px ; |
17 | height : 100px ; |
18 | background-image : url ( "images/banner.png" ); |
19 | background-repeat : no-repeat ; |
20 | margin-bottom : 10px ; |
21 | } |
22 |
23 | h 1 a { |
24 | width : 758px ; |
25 | height : 100px ; |
26 | display : block ; |
27 | text-indent : -9999px ; |
28 | } |
29 |
30 | #menu { |
31 | float : left ; /* The menu will float to the left */ |
32 | width : 120px ; /* Very important : give the menu a width */ |
33 | } |
34 |
35 | .sub-menu { |
36 | background-color : #626262 ; |
37 | background-image : url ( "images/top-bg.png" ); |
38 | background-repeat : repeat-x ; |
39 | border : 2px solid black ; |
40 | margin-bottom : 20px ; |
41 | } |
42 |
43 | /* a few effects on the menu */ |
44 |
45 | .sub-menu h 4 { /* all the titles in the menu */ |
46 | font-size : 20px ; |
47 | margin-top : 18px ; |
48 | margin-bottom : 12px ; |
49 | color : #b3b3b3 ; |
50 | font-family : Arial , "Arial Black" , "Times New Roman" , Times, serif ; |
51 | text-align : center ; |
52 | } |
53 |
54 | .sub-menu ul { /* all the lists in the menu */ |
55 | list-style-image : url ( "images/point.png" ); /* change the looks of the points */ |
56 | padding : 0px ; /* get rid of any inner margin there may be */ |
57 | padding-left : 20px ; /* ... then add a 20 px margin to the left */ |
58 | margin : 0px ; /* same thing on the outer margin */ |
59 | margin-bottom : 5px ; /* add a 5 px margin to the bottom */ |
60 | } |
61 |
62 | .sub-menu a { /* all the links in the menu */ |
63 | color : #b3b3b3 ; |
64 | } |
65 |
66 | .sub-menu a:hover { /* when the mouse rolls over the links */ |
67 | background-color : #b3b3b3 ; |
68 | color : black ; |
69 | } |
It’s already looking nicer isn’t it ?
And the effect of the :hover on the links in the menu, is easy to do, so everything’s good so far
I’ve got to precise some things though. You have seen that I put a padding
(which modifies the interior margins), then a padding-left
(which only modifies the inside margine on the left).
This is just a method to save time. Just read the lines in order :
If I wanted, I could have done this instead :
padding-top: 0px;
padding-bottom: 0px;
padding-right: 0px;
padding-left: 20px;
But that’s long, and like all other webmasters, I’m lazy
The Body
For the body, it’s very important to define the correct margins.
The problem that needs sorting out urgently is the problem of the text going under the menu. As I said earlier, this is normal because we used float: left on the menu.
How do we stop the text from going under the menu then ?
We just need to change the margin to the left of the body. It’s the outer margin, so we’ll use margin-left. We need to give it a big enough value to push
the text over to the right, so that it doesn’t go under the menu (so we need a greater value than the width of the menu). Here for example, we’ll put a value of 140px. As if by magic, you’ll see, the body will be correctly placed !
Let’s not stop now
We’ll add a margin-bottom so that the footer won’t be glued
to the body :
margin-bottom: 20px;
We’ll also add a small padding (inside margin) on every side so that the text doesn’t touch the edge of the body :
padding: 5px;
Then we’ll add a background colour, a background-image that repeats across the top of the body, a border … well, all that’s decoration
Code : CSS
1 | * { |
2 | border : 0 ; |
3 | margin : 0 ; |
4 | padding : 0 ; |
5 | } |
6 |
7 | body { |
8 | width : 760px ; |
9 | margin : auto ; |
10 | margin-top : 20px ; |
11 | margin-bottom : 20px ; |
12 | background-image : url ( "images/bg.png" ); |
13 | } |
14 |
15 | #header { |
16 | width : 758px ; |
17 | height : 100px ; |
18 | background-image : url ( "images/banner.png" ); |
19 | background-repeat : no-repeat ; |
20 | margin-bottom : 10px ; |
21 | } |
22 |
23 | h 1 a { |
24 | width : 758px ; |
25 | height : 100px ; |
26 | display : block ; |
27 | text-indent : -9999px ; |
28 | } |
29 |
30 | #menu { |
31 | float : left ; /* The menu will float to the left */ |
32 | width : 120px ; /* Very important : give the menu a width */ |
33 | } |
34 |
35 | .sub-menu { |
36 | background-color : #626262 ; |
37 | background-image : url ( "images/top-bg.png" ); |
38 | background-repeat : repeat-x ; |
39 | border : 2px solid black ; |
40 | margin-bottom : 20px ; |
41 | } |
42 |
43 | .sub-menu h 4 { |
44 | font-size : 20px ; |
45 | margin-top : 18px ; |
46 | margin-bottom : 12px ; |
47 | color : #b3b3b3 ; |
48 | font-family : Arial , "Arial Black" , "Times New Roman" , Times, serif ; |
49 | text-align : center ; |
50 | } |
51 |
52 | .sub-menu ul { |
53 | list-style-image : url ( "images/point.png" ); |
54 | padding : 0px ; |
55 | padding-left : 20px ; |
56 | margin : 0px ; |
57 | margin-bottom : 5px ; |
58 | } |
59 |
60 | .sub-menu a { |
61 | color : #b3b3b3 ; |
62 | } |
63 |
64 | .sub-menu a:hover { |
65 | background-color : #b3b3b3 ; |
66 | color : black ; |
67 | } |
68 |
69 | /* the body of the page */ |
70 |
71 | #body { |
72 | margin-left : 140px ; /* margin to the left to stop the text going under the menu */ |
73 | margin-bottom : 20px ; /* bottom margin to separate the body from the footer */ |
74 | padding : 5px ; /* to stop the text from touching the edge of the body */ |
75 | color : #b3b3b3 ; |
76 | background-color : #626262 ; /* a background colour for the body */ |
77 | background-image : url ( "images/top-bg.png" ); /* a small image that repeats across the top of the body */ |
78 | background-repeat : repeat-x ; |
79 | border : 2px solid black ; /* a border to show the edges of the body and look nice */ |
80 | } |
There are still a few things to decorate in the body. For example, the titles : we’ll change the font and use Arial
which I find is nicer on titles.
We’ll also add a background image to the
titles with background-repeat:no-repeat, which means that the image won’t repeat (so it will stay to the left).
To avoid the text of the title going over the top of the image, we’ll add a padding left of a few pixels. That way every time we use a title we’ll automatically get a picture of a cog !
Code : CSS
1 | * { |
2 | border : 0 ; |
3 | margin : 0 ; |
4 | } |
5 |
6 | body { |
7 | width : 760px ; |
8 | margin : auto ; |
9 | margin-top : 20px ; |
10 | margin-bottom : 20px ; |
11 | background-image : url ( "images/bg.png" ); |
12 | } |
13 |
14 | #header { |
15 | width : 758px ; |
16 | height : 100px ; |
17 | background-image : url ( "images/banner.png" ); |
18 | background-repeat : no-repeat ; |
19 | margin-bottom : 10px ; |
20 | } |
21 |
22 | h 1 a { |
23 | width : 758px ; |
24 | height : 100px ; |
25 | display : block ; |
26 | text-indent : -9999px ; |
27 | } |
28 |
29 | #menu { |
30 | float : left ; /* The menu will float to the left */ |
31 | width : 120px ; /* Very important : give the menu a width */ |
32 | } |
33 |
34 | .sub-menu { |
35 | background-color : #626262 ; |
36 | background-image : url ( "images/top-bg.png" ); |
37 | background-repeat : repeat-x ; |
38 | border : 2px solid black ; |
39 | margin-bottom : 20px ; |
40 | } |
41 |
42 | .sub-menu h 4 { |
43 | font-size : 20px ; |
44 | margin-top : 18px ; |
45 | margin-bottom : 12px ; |
46 | color : #b3b3b3 ; |
47 | font-family : Arial , "Arial Black" , "Times New Roman" , Times, serif ; |
48 | text-align : center ; |
49 | } |
50 |
51 | .sub-menu ul { |
52 | list-style-image : url ( "images/point.png" ); |
53 | padding : 0px ; |
54 | padding-left : 20px ; |
55 | margin : 0px ; |
56 | margin-bottom : 5px ; |
57 | } |
58 |
59 | .sub-menu a { |
60 | color : #b3b3b3 ; |
61 | } |
62 |
63 | .sub-menu a:hover { |
64 | background-color : #b3b3b3 ; |
65 | color : black ; |
66 | } |
67 |
68 | #body { |
69 | margin-left : 140px ; |
70 | margin-bottom : 20px ; |
71 | padding : 5px ; |
72 | color : #b3b3b3 ; |
73 | background-color : #626262 ; |
74 | background-image : url ( "images/top-bg.png" ); |
75 | background-repeat : repeat-x ; |
76 | border : 2px solid black ; |
77 | } |
78 |
79 | #body h 2 { /* all the h2 titles in body */ |
80 | margin-top : 10px ; |
81 | margin-bottom : 10px ; |
82 | font-size : 28px ; |
83 | color : #b3b3b3 ; |
84 | text-align : center ; |
85 | font-family : Arial , "Arial Black" , "Times New Roman" , Times, serif ; |
86 | } |
87 |
88 | #body h 3 { /* all the h3 titles in body */ |
89 | height : 30px ; |
90 | margin-top : 10px ; |
91 | margin-bottom : 10px ; |
92 | background-image : url ( "images/title.png" ); /* a small background image for h3 titles */ |
93 | background-repeat : no-repeat ; /* the image won't repeat, it will be to the left */ |
94 | padding-left : 30px ; |
95 | font-size : 22px ; |
96 | color : #b3b3b3 ; |
97 | text-align : left ; |
98 | } |
Nice, isn’t it
The Footer (And We’ve Finished !)
For the footer there is nothing complicated or extraordinary, it’s just similar to the rest. We add a background colour, a border, check that the borders are okay etc …
Code : CSS
1 | * { |
2 | border : 0 ; |
3 | margin : 0 ; |
4 | } |
5 |
6 | body { |
7 | width : 760px ; |
8 | margin : auto ; |
9 | margin-top : 20px ; |
10 | margin-bottom : 20px ; |
11 | background-image : url ( "images/bg.png" ); |
12 | } |
13 |
14 | #header { |
15 | width : 758px ; |
16 | height : 100px ; |
17 | background-image : url ( "images/banner.png" ); |
18 | background-repeat : no-repeat ; |
19 | margin-bottom : 10px ; |
20 | } |
21 |
22 | h 1 a { |
23 | width : 758px ; |
24 | height : 100px ; |
25 | display : block ; |
26 | text-indent : -9999px ; |
27 | } |
28 |
29 | #menu { |
30 | float : left ; /* The menu will float to the left */ |
31 | width : 120px ; /* Very important : give the menu a width */ |
32 | } |
33 |
34 | .sub-menu { |
35 | background-color : #626262 ; |
36 | background-image : url ( "images/top-bg.png" ); |
37 | background-repeat : repeat-x ; |
38 | border : 2px solid black ; |
39 | margin-bottom : 20px ; |
40 | } |
41 |
42 | .sub-menu h 4 { |
43 | font-size : 20px ; |
44 | margin-top : 18px ; |
45 | margin-bottom : 12px ; |
46 | color : #b3b3b3 ; |
47 | font-family : Arial , "Arial Black" , "Times New Roman" , Times, serif ; |
48 | text-align : center ; |
49 | } |
50 |
51 | .sub-menu ul { |
52 | list-style-image : url ( "images/point.png" ); |
53 | padding : 0px ; |
54 | padding-left : 20px ; |
55 | margin : 0px ; |
56 | margin-bottom : 5px ; |
57 | } |
58 |
59 | .sub-menu a { |
60 | color : #b3b3b3 ; |
61 | } |
62 |
63 | .sub-menu a:hover { |
64 | background-color : #b3b3b3 ; |
65 | color : black ; |
66 | } |
67 |
68 | #body { |
69 | margin-left : 140px ; |
70 | margin-bottom : 20px ; |
71 | padding : 5px ; |
72 | color : #b3b3b3 ; |
73 | background-color : #626262 ; |
74 | background-image : url ( "images/top-bg.png" ); |
75 | background-repeat : repeat-x ; |
76 | border : 2px solid black ; |
77 | } |
78 |
79 | #body h 2 { /* all the h2 titles in body */ |
80 | margin-top : 10px ; |
81 | margin-bottom : 10px ; |
82 | font-size : 28px ; |
83 | color : #b3b3b3 ; |
84 | text-align : center ; |
85 | font-family : Arial , "Arial Black" , "Times New Roman" , Times, serif ; |
86 | } |
87 |
88 | #body h 3 { /* all the h3 titles in body */ |
89 | height : 30px ; |
90 | margin-top : 10px ; |
91 | margin-bottom : 10px ; |
92 | background-image : url ( "images/title.png" ); |
93 | background-repeat : no-repeat ; |
94 | padding-left : 30px ; |
95 | font-size : 22px ; |
96 | color : #b3b3b3 ; |
97 | text-align : left ; |
98 | } |
99 |
100 | /* The footer (generally at the bottom for the copyright) */ |
101 |
102 | #footer { |
103 | padding : 5px ; |
104 | text-align : center ; |
105 | color : #b3b3b3 ; |
106 | background-color : #626262 ; |
107 | background-image : url ( "images/top-bg.png" ); |
108 | background-repeat : repeat-x ; |
109 | border : 2px solid black ; |
110 | } |
And that’s the end of the CSS, the design is finished !
To make your whole site just save your page as page1.html
, page2.html
, page3.html
etc… then you can change the content of the different pages !
Our site is finally ready. The Mechanical Spirit
design, which was created just for this tutorial, probably won’t suit your needs. However that was done a bit on purpose
Now that the design is finished, you know what to do, and the main ideas to follow. It’s up to you to create the complete design for your site, to add your colours, your backgrounds, your fonts, your effects …
Well, now you know how to do it
You are allowed (and encouraged) to use my CSS as an inspiration. I know that it isn’t the easiest thing in the world when you’re starting and trying to create first design, and all those lines of code can make you go dizzy.
The good news, is that with practice, not only will you stop getting dizzy, but you’ll also be able to create your complete design without anybody’s help
Download Source Files
CLICK HERE TO DOWNLOAD SOURCE CODE FOR THIS TUTORIAL
Download Source Files
If you want to re-use our source code, modify it, or simply check it out, just click here to download. The file contains the index.html page, the CSS, and the images used during this tutorial.
Wait, It’s Not Finished !
I’ve got a few bits of information to give you. Most of it’s just details, but you might discover a few new things, so pay attention, it won’t be long
Change The CSS, Change The Design !
By modifying the CSS, you can change the whole design without touching the XHTML code !
In fact, have a look at our final XHTML code : it doesn’t contain any information on the design. It just says that there’s a header, a menu, a body to put the content in, and a footer. That’s it !
Let The Visitor Choose The Design
One last thing I would like to show you : alternative style sheets.
If you have made several designs for your site, you can let your visitors decide which design they want to use.
For that, you have to add tags between
As well as the tag to link the CSS to the XHTML page, you have to add this line :
This line indicates that there is another stylesheet (alternate stylesheet
). If the visitor has a recent browser (pretty much anything apart from Internet Explorer), he (or she) will be able to choose the design he wants.
For information : in Firefox if you go to View -> Stylesheet, you can choose the design you want.
Sites offering several designs are rare. A lot of webmasters don’t even know it’s possible to easily do, others can’t be bothered making several designs
Nessun commento:
Posta un commento