[Site map]
You are here ---
> Newbies Paradise
> Tutorials
> Official
> Website
> Reading a tutorial
Wrap it up! (part 2/2)
In the first part of this "Wrap it up" chapter, we have seen some CSS properties to size blocks and change their appearance. Roughly:
- How to change blocks size
- How to change blocks borders
- How to change blocks margin
In the second part, we will see how to specify their position. Yup, we need to know how to say: "I want the menu on the left side, the content on its right, my logo on the top and 12 pixels from the right border" etc.
But let's make it clear: positionning with CSS is not that easy... There is a theorical part and a practical one: in this chapter, we will go through the theorical part (but we have to!), and the practical part will be dealt with in the following chapter (which will be more substantial for you).
Basically, and please keep this in mind, you will never be able to have your website displayed the same way depending on the browser. But we can make it so it would be OK whatever the browser is, and that is not so bad!
Now, I am going to teach you how to
go beyond the limits of physics change the rules of CSS (brrr...).
Pictures ( <img /> ) are inline elements. You would prefer them to be blocks? No problem!
Headers ( <h1> ) are block type elements and this has always bothered you. You would prefer them to be inline elements? No problem!
There exists a powerful CSS property, called
display (handle it carefully, otherwise the whole thing will blow up

).
This property can take the following values:
- block: the element will be displayed as a block-level element.
- inline: the element will be displayed as an inline one.
Actually,
display can take maaaany other values (that is to say how powerful this property is

) but I will just teach you the two most used ones for now.
To change an inline element into a block one, you will have to type:
display:block;
For instance, if I wanted ALL my pictures to be at block level, I would write this:
Code: CSS
Be careful: after doing this, all your pictures will be preceded and followed by a line break, like any other block-level element.
However, all your images do not HAVE to be at a block-level, okay? I hope you will remember this: it is possible to use the "class" attribute on a tag so it is displayed differently.
For instance :
Code: HTML1
2 | <img src="image.png" alt="This picture is normal (inline element)" />
<img src="image.png" class="imageblock" alt="This picture has been modified (block-level element)" />
|
Of course, you will have to write the following CSS code for the second picture to be at block level:
Code: CSS1
2
3
4 | .imageblock
{
display: block;
}
|
I hope that you are not learning anything new about how the
class attribute works, otherwise you should read the first chapter of part II again... slowly this time!
What about the opposite? How do I transform a block-level element into an inline one?
Well it is the same thing except that this time we will use
display:inline;.

I will not show you another example, you are surely smart enough to guess how it works by yourself!
By the way: we usually change inline elements into block-level ones, but the opposite is seldom done. Actually, block elements have many advantages: you can modify their size, borders, margins, etc. Anyway you already know all this.
The first method we will see is called
floating. I admit it is is a peculiar name, but it will not be hard to understand.
In order for you to see what we are dealing with, this is a picture of what we will learn to do:
A picture floating on the left
A picture floating on the right
It allows you to wrap text around an element (a picture in this case): it makes it look better.
I guess the question you are asking yourself is: "Which magic CSS property can make things float like that?"
The answer is...float (surprising, isn't it?). This property can take two values, which you should have guessed by now.
- left: the element will float on the left.
- right: the element will float... on the right! Yes, good job!

Using the floating method is such a simple thing than one often makes it more complicated (and I know what I am talking about). Actually, there are only two things to do:
- You set a float for a tag.
- Then you write text as usual.
The float property can be used with block and inline tags, as you will see...
Making a picture float
Let's learn how to make a picture float (a picture being an inline tag, as you may remember). First of all, this is the xHTML code you have to type:
Code: HTML1
2
3
4 | <p><img src="../images/flash.gif" class="floatingpicture" alt="Floating picture" /></p>
<p>This is a normal paragraph which will surround the picture because it is "floating".<br />
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum mollis scelerisque nulla. Donec feugiat augue et sem. Nulla ut purus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla volutpat leo quis magna. Donec accumsan lobortis ligula. Donec nec ante eu wisi tempus dictum. Sed nulla est, laoreet nec, consequat quis, iaculis non, tortor. Nunc sed purus. Sed metus. Donec posuere. Pellentesque porttitor tortor non eros. Ut rutrum erat a nunc. Duis non erat et orci viverra mollis. Sed lacinia velit a magna. Etiam aliquam, felis eu imperdiet auctor, ante augue dignissim odio, a pharetra tellus neque vel urna. Cras gravida adipiscing lectus. Nullam lorem ipsum, convallis eleifend, congue placerat, dictum non, leo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae.</p>
|
A common mistake is to set the floating picture after the text. You must not do that: the floating element has to be
before the text (no matter what side it is floating on).
Try to set the <img /> tag after the paragraph: it will not work anymore.

Now let's only change the picture's CSS code but not the paragraph's: the text will automatically be displayed correctly.
This is the simple CSS code that we have to type to make the picture float to the left:
Code: CSS1
2
3
4 | .floatingpicture
{
float: left;
}
|
You can also try to make the picture float to the right: just set
float:right; and that's it!
Making a drop cap (exercise)
Sometimes, I have an horrible nightmare. I imagine myself making a long tutorial about floating elements in CSS and everyone is yawning, falling asleep, in other words everyone is bored to tears. Wow... It always gives me the chills.
So, in order to avoid my nightmare to come true, I decided to create a fun exercise for you (at least I hope it will be fun.

).
Do you know what a drop cap is? No, I bet.
Ok, never mind.
A "drop cap" is the first letter of a paragraph when it is written bigger than the rest of the paragraph. It can often be seen in newspapers.
The only rule is to use the following xHTML code.
You may not modify this code.:
Code: HTML1 | <p>The cry brought him skulking back to his master and a blunt bootless kick sent him unscathed across a spit of sand, crouched in flight. He slunk back in a curve. Doesn't see me. Along by the edge of the mole he lolloped, dawdled, smelt a rock. and from under a cocked hindleg pissed against it. He trotted forward and, lifting again his hindleg, pissed quick short at an unsmelt rock. The simple pleasures of the poor. His hindpaws then scattered the sand: then his forepaws dabbled and delved. Something he buried there, his grandmother. He rooted in the sand, dabbling, delving and stopped to listen to the air, scraped up the sand again with a fury of his claws, soon ceasing, a pard, a panther, got in spousebreach, vulturing the dead.</p>
|
(It comes from James Joyce's
Ulysses: I told you you could not change any bit of it.

)
You have to make a CSS file which will display this text with a drop cap. You cannot modify the xHTML file but you can do everything you want inside the CSS file: all properties are allowed.
Come on, get to work!
...
...
...
Okay,that's it! Correction time!
Of course, you had to use
float:left. But how can it affect the first letter only?
If you found by yourself that you had to use :first-letter, then congratulations! That was the only difficult thing and the rest is easy, you could set the properties you wanted.
You could use font-size to set the font on... three lines high, I would say. For that, I put the "3em" (= 3 lines high) value, but "300%" works as well! However, it is not a good idea to set a size with pixels because the height of a line is not always the same.
This is the code I wrote to make a drop cap:
Code: CSS1
2
3
4
5
6
7
8 | p:first-letter /* I want the first letter to... */
{
float: left; /* Float on the left */
font-size: 3em; /* Three lines high */
font-family: Arial, Georgia, "Times New Roman", Times, serif; /* Arial if possible */
font-weight: bold; /* Bold (it stands out better) */
margin-right: 5px; /* I want to put a 5px margin on the right so the letter is not too close from the rest of the text */
}
|
Do not stop here! You can write the drop cap in white with a black background, or even use a background picture. Just do tests!
Breaking a float
We have seen that a floating element is surrounded with text.
But how to make the text continue
under the floating element? We can use several <br />, but that would neither be easy nor clean.
In short, we would like to do this :
Actually, there is a CSS property (no kidding) which allows us to say: "Stop, I want this text to be under the floating element". That is the
clear property. It can take three values:
- left: the text continues under after a float:left
- right: the text continues under after a float:right
- both: the text continues under after a float:left or a float:right
To make things a little easier, we will always use a clear:both because it works with both float:left and float:right (always, actually).
Let's use the following xHTML code to use this property:
Code: HTML1
2
3 | <p><img src="../images/flash.gif" class="floatingpicture" alt="Floating Picture" /></p>
<p>This text is written around the floating picture.</p>
<p class="under">This one is written under the floating picture.</p>
|
Code: CSS1
2
3
4
5
6
7
8 | .floatingpicture
{
float: left;
}
.under
{
clear: both;
}
|
There we are

We only need to apply a
clear:both; on the paragraph we want to be continued under the floating picture. And that's it!
There are three ways of positioning a block with CSS (except floats):
- Absolute positioning: a block can be set anywhere on a page (top-left corner, bottom-right corner, middle, etc.)
- Fixed positioning: it's almost the same as absolute, except the block doesn't move when you scroll down the page, like when you use background-attachment:fixed;. (if you still remember that one
)
- Relative positioning: it's the most complicated positioning method. A block can be moved from its original position.
Like with floaters, you can use those three positioning methods with inline tags like <img />.
However, they're mostly with block tags.
First, you must choose a positioning method with the "position" CSS property:
- absolute: absolute positioning.
- fixed: fixed positioning.
- relative: relative positioning.
We're going to study them one after the other.
Absolute positioning
You already know you have to type that to make an absolute positioning:
position:absolute;
But that's not enough!

Indeed, we said we wanted to make an absolute positioning but we haven't said
where we want the block to be yet.
To do so, we'll use four CSS properties you've already seen a lot of times:
- left: positioning from the left of the page.
- right: positioning from the right of the page.
- top: positioning from the top of the page.
- bottom: positioning from the bottom of the page.
We can set a pixel value of like "14px" or a percent value like "50%".
If you don't understand very well, this scheme may help you:
Now you should be able to position your blocks correctly
So you have to use the
position property and at least one of the four previous properties (top, left, right or bottom). For instance, if we write that:
position:absolute;
right:0px;
bottom:0px;
... that means the block is positioned in the bottom-right corner of the page (0 pixels from the right, 0 pixels from the bottom).
This time, we'll use the <div> universal tag (block tag). I'm going to write a paragraph, and then I'll put a few words inside the <div> tag.
Code: HTML1
2
3
4
5
6 | <p>
I am a normal paragraph.<br />
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer vel libero. Cras dolor. Quisque quis odio eget justo pulvinar aliquet. Morbi luctus mi. Fusce leo. Integer eleifend condimentum felis. Phasellus vitae nibh. Mauris pellentesque porta magna. Quisque at turpis. Praesent semper odio eget risus. Praesent bibendum imperdiet massa. Quisque ac arcu ac augue dapibus vestibulum. Pellentesque gravida. Mauris turpis. Aenean molestie. Praesent at quam et ligula pellentesque luctus.
</p>
<div>Positionned block.</div>
|
That's no magic. There are just a paragraph and a div tag.
Now, let's add some CSS code to position our div tag:
Code: CSS1
2
3
4
5
6
7
8
9 | div
{
background-color: yellow;
border: 1px solid green;
position: absolute;
left: 35px;
top: 50px;
}
|
I put a yellow background and a border so we can see the div better.
As you can see, the block is positioned over the paragraph. The blocks are overlayed; that's one of the greatests assets and also one of the greatests inconveniences of absolute positioning: a block can be positioned
anywhere on a web page, but you must be careful because it may hide some text.
Here is another example of the possibilities you have with absolute positioning:
Code: CSS1
2
3
4
5
6
7
8
9 | div
{
background-color: yellow;
border: 1px solid green;
position: absolute;
right: 50%;
bottom: 20px;
}
|
Fixed positioning
It works
exactly the same as absolute positioning, but your block stays fixed this time, even if you scroll down the page.
Fixed positioning works with any web browser except Internet Explorer (6th edition or previous). Unfortunately, your block won't be positioned at all with that browser...
Instead of writing
position:absolute;, we'll write
position:fixed; inside our CSS code. We'll use top, bottom, left and right to position our block, just as before.
The
position:fixed; property is very useful to make a menu you can always see:
Code: CSS 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | div
{
background-color: yellow;
border: 1px solid green;
width: 150px;
height: 250px;
position: fixed;
right: 40px;
top: 60px;
}
p
{
width: 300px;
}
|
If your menu appears at the bottom of the page and it's not working, that means you're using Internet Explorer (an old version of it)

Because of that problem, people usually don't use fixed positioning to create a menu on their webpage: they use absolute positioning instead.
Relative positioning
That's a bit more complicated than the other methods. Actually, I don't use that one very often. It's only useful to make "fits".
For instance, let's take an important text, inside a <strong> tag.
First, I'll put a red background so it's more attractive:
Code: CSS1
2
3
4
5 | strong
{
background-color: red; /* Red background */
color: yellow; /* Yellow text */
}
|
This time, the scheme I showed you doesn't work anymore. Why? Because the origin is different: the point which has the (0,0) coordinates is not at the top-left corner of your page as before. Indeed, that point is now at the top-left corner of the element.
Strange, isn't it? Yes, it's relative positioning. This scheme should help you to understand how it works:
So, if you put
position:relative and apply one of the properties
top,
bottom,
left,
right, the text with a red background will be moved
from its original position.
I told you it was strange.

Actually, we should test it to understand it better.
Let's take an example: I want my text to be shifted 55 pixels to the right and 10 pixels lower. So I will set it 55 pixels from the "left corner" and 10 pixels from the "top corner".
Code: CSS1
2
3
4
5
6
7
8
9 | strong
{
background-color: red;
color: yellow;
position: relative;
left: 55px;
top: 10px;
}
|
The text moved from its original position, like that:
That's the way it works. Now that you can use it, you have to find where
Well it seems math can be usefull after all: at least you know what the origin of a graph is (the point with the 0, 0 coordinates).
For absolute positioning, there must be an origin... That helps us to set a block position (and even an inline tag position, like img) anywhere on the webpage!
I guess you are ready for the next chapter now...
By the way, what is it about?... Oh yeah! It is a PW (= Practical Work).
In fact, you will not learn anything new from the following chapter, but I bet you will like it: I will show you how to design a website from A to Z thanks to our newly acquired knowledge.
Now you have everything it takes, but still, you need to know the ropes! Whenever you are ready!