[Site map]
You are here ---
> Newbies Paradise
> Tutorials
> Official
> Website
> Reading a tutorial
Text formatting with CSS (part 1/2)
We are now about to start with an exciting chapter.

No, "text formating" has nothing to do with destroying all the data on your hard drive!

It simply means you are going to change some text's style (in other words: format it). Be reassured now: your hard disk will still be alive by the end of this chapter.
No surprises: we are still dealing with CSS, and we will use what we learned in the last chapter. Therefore, I do hope you have read and understood how to use a .css file.
This time, it is serious stuff: you will see how to change the text's size, its font, how to align it, and so forth... Heaps of things to discover, so to make it easy, I decided to split this chapter in two parts.
The good news is your website will finally start looking like a real one by the end of these two next chapters.
Hey buddy, I already know how to do that! You've just explained it in the last chapter!
Yes, I gave you an example without much explanation in the last chapter, in order to materialize how CSS works. But actually, there are many ways to change the size of your fonts.
What you already know is that
font-size is the CSS property that modifies text size. That's still the same. However, you can specify text size by many different ways:
- In pixels: it is a very precise method to define text size. You can decide exactly how many pixels the text should measure.
So if want your text to be 16 pixels big, you have to write:
font-size:16px;
The letters will be 16 pixels high, as shown in this picture:
Just below is an example of how to use font-size (I didn't put the xHTML file that goes with it, I estimated you could handle it at this point
):
Code: CSS1
2
3
4
5
6
7
8 | p
{
font-size: 14px; /* 14 pixel paragraphs */
}
h1
{
font-size: 22px; /* 22 pixel titles */
}
|
If you can't stand pixels, be aware that you can also indicate size in centimeters (i.e: "2cm") and in millimeters (i.e: "14mm"). 1 inch is 2.5 centimeters, or 25 millimeters.
Specifying text size in pixels, centimeters or millimeters is quite precise, I admit it. However, it is advised to define a relative size (you will see how to do this in a minute) so the size of the text can adapt to one's personal choices (some people prefer having enlarged text, other prefer small text, and so forth...)
- By giving a relative value: in other words, writing "big", "very big", "small", "tiny" (well, it's almost like that
) Here is the list of the different values you can put in:
- xx-small
- x-small
- small
- medium
- large
- x-large
- xx-large
Here is a CSS code which uses relative values :
Code: CSS 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | .tiny
{
font-size: xx-small;
}
.verysmall
{
font-size: x-small;
}
.small
{
font-size: small;
}
.medium
{
font-size: medium;
}
.big
{
font-size: large;
}
.verybig
{
font-size: x-large;
}
.huge
{
font-size: xx-large;
}
|
I advise you to use this method rather than pixels for indicating text size. Your website will automatically be way more "adaptable" to each one of your visitor's configuration.
- In em: it is an other way to indicate the relative text size. I agree on the fact that it isn't the simplest method to understand, but once you've tested it a little, it really gets easy to use.
All you have to do is to indicate the text size you wish to have compared with the font's normal size. Let me explain. "1em" means "normal size", which means that "1" is the EMphasis factor. If you put a number higher than 1 (generally a decimal number) like "1.3em", the text will thus have a size 1.3 time bigger than normal. It works the same way for reducing the size: "0.8em" and your text will be 0.8 time smaller.
Personally, it's the method I use the most often for text sizing (therefore, most of my font-size are in "em").
Be careful: do not put a comma instead of a dot, if you are used to doing that. Indeed, "1.4em" would be correct, whereas "1,4em" wouldn't!
Code: CSS1
2
3
4
5
6
7
8 | .em_little
{
font-size: 0.7em;
}
.em_big
{
font-size: 1.3em;
}
|
- With a percentage that's easy. If you indicate "100%", then the text will have a "normal" size. If you put "130%", the text will be sized at 130% of its normal size. It looks a lot like the "em" technique (it's somehow the same thing), after all just choose the one you prefer

Phewww! Now I'm sure you get the fact that there are a lot ways to change text size

There's even almost too many, it could even get confusing...
As I told you earlier, pixels are very useful if you wish to be precise, but it isn't that "advised" because using too big (or too small) fonts size may be disturbing for your visitors.
My personal opinion is that the "em" method (or the % method) gets to be the most useful ones: your font size is automatically adapted to your visitors' configurations and it's also easy to use.
Well... fonts... that's a pretty touchy point
The problem for a font is the following one: in order to be displayed correctly, all the web users must have it. If an Internet user doesn't have the same font as yours, then his browser will automatically choose to take the default font, which will turn out to be totally different from what you expected initially!
I'm sure you're thinking everybody has "Times New Roman" and "Arial"... Yes, although Macs have "Time" instead of "Times New Roman".
However, if you plan to use the most Gothic fancy-dancy font for your Heavy Metal website, I advise you to reconsider your project
Ok, I just want to know how to do it.
The CSS property that indicates the font is
font-family. This is how you should indicate the font name:
font-family:font;
However, to prevent any problem, if the web user doesn't have the same font as yours, we generally put
several font names, seperated by commas:
font-family:font1, font2, font3, font4;
The browser will first try to use font1. If it does not know it, it will try font2. If it does not know it it will try font3, and so forth...
Usually, you are supposed to put "serif" at the very end. Serif is a standard font (that will only be used if none of the other fonts was found).
Fine, but which are the most common fonts we are "allowed" to use, would you be tempted to ask me?
Here is a list of fonts which work well on most browsers. You can thus use them, no problem :
- Arial
- Arial Black
- Comic Sans MS
- Courier New
- Georgia
- Impact
- Times New Roman
- Trebuchet MS
- Verdana
If you wish to test the different fonts listed above, click on the following link:
Therefore, if I write:
font-family:Impact, "Arial Black", Arial, Verdana, serif;
...it means:
"Put the Impact font, or, if you don't have it, Arial Black, or else Arial, or, if worse comes to worse, you can always put Verdana; if nothing worked just put the standard font (serif).".
Usually, it is recommended that you choose a set of 3-4 fonts (+ serif), this way you are sure that at least one will be available on the visitor's computer.
If the name of a font contains spaces, you will have to put quotations marks on it, like I just did for "Arial Black".
How about a little summarizing example as a conclusion, and then it will all be good:
Code: CSS1
2
3
4
5
6
7
8
9 | h1
{
font-family: "Arial Black", Arial, Verdana, serif; /* Let's try to put a priority on Arial Black */
}
p
{
/* I think the Comic Sans Ms font makes paragraphs nice and smooth to read */
font-family: "Comic Sans MS", "Trebuchet MS", Georgia, serif;
}
|
Simple alignments
The CSS language allows you to align the text in any of the most-known ways: to the left, to the right, centered and justified.
It's really easy: you have to use the
text-align property and set the alignment you want :
- left: is the default setting.
- center
- right
- justify Justifying text allows the text to fill in any blank spaces at the end of lines. For instance, news paper text is always justified.
Look at the different alignments in this example:
Code: CSS 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | h1
{
text-align: center; /* The text will be centered */
font-family: "Arial Black", Arial, Verdana, serif; /* Nothing better than a good old title put in Arial font ;) */
}
blockquote
{
text-align: justify; /* The quote will be justified */
}
.signature
{
text-align: right; /* So that my signature will be aligned to the right */
font-family: "Comic Sans MS", Georgia, "Times New Roman", serif;
font-size: 80%;
}
|
Don't forget that some xHTML code always goes with the CSS, of course. By the way, for this specific example I will show you the code I'm using. Keep in mind that it is always possible to look at the xHTML code of a web page by simply doing a right click / "Show source code".
Below you will see the xHTML code for this alignment-testing example:
Code: HTML1
2
3
4
5
6
7 | <h1>The sage's word</h1>
<p>Once, a great sage said :</p>
<blockquote><p>Morbi fermentum libero non libero. Nunc in turpis in justo adipiscing scelerisque. Donec id elit non diam aliquet semper. Maecenas pede. Maecenas fringilla. Fusce eleifend dui quis lectus. Praesent facilisis, ligula a consequat posuere, metus purus porta mi, at consectetuer justo wisi id dui. Maecenas mattis. Ut imperdiet pharetra enim. Suspendisse quis leo nec arcu interdum aliquam. Vivamus dictum quam id tellus. Maecenas in quam sit amet risus semper auctor. Integer leo dui, malesuada eu, fermentum at, vehicula at, nisl. Pellentesque hendrerit. Proin ut libero. Curabitur sem ipsum, porta non, feugiat vel, mollis ut, justo. Sed a orci id metus pretium lobortis. Morbi ultrices, quam a facilisis faucibus, odio nunc dignissim enim, eget rhoncus purus erat ac quam.</p></blockquote>
<p class="signature">Signed M@teo21</p>
|
You cannot modify the alignment of an inline-type marker (such as span, a, em, strong...). Alignment only works with block-type markers (p, div, blockquote, h1, h2, ...). It's somewhat logical though, when you think about it: you can't just modify the alignment of a couple of words in the middle of a whole paragraph!
That's why you'll generally have to align a whole paragraph.
Indentation
What on earth is indentation? It's nothing but spacing the text from the margin. It's useful, for instance, if you want a paragraph to start a bit more on the right, which makes the reading much easier and more pleasant (that's my opinion). It's a touch of style that you might find in many books by the way.
Here is a illustration of what an indented paragraph looks like:
Indented text
We have to use the
text-indent property. Surprising isn't it?

You need to indicate size of the indentation: you can either do it in pixels, in centimeters, or in millimeters...
Anyhow, I advise you to set it in pixels

. Take a look at how I indent the paragraph text with
text-indent:
Code: CSS1
2
3
4
5
6 | p
{
text-indent: 30px; /* The paragraphs will start at 30 pixels to the right of the left margin */
text-align: justify; /* They will be justified */
font-size: large; /* Let's get a little crazy and make the text bigger :D */
}
|
What's so neat with CSS is that you just need to say, once and for all: "
I want all my paragraphs to be indented by 30 pixels on the right", and then, each time you will start a new paragraph, it will automatically be spaced from the margin.
Before xHTML existed (in the HTML days), that type of thing was iterative and complicated to do, to the point where it got close to impossible. Now, aligning your text the way you wish to is as easy as tying your shoe-laces
You might want to take note of these two similar properties. I don't really need to discuss longly :
- word-spacing: space between words (in pixels).
- letter-spacing: space between letters (in pixels).
We are done with the first half!
Whenever you feel ready, you can go to the following chapter to discover another stack of CSS properties dedicated to text formating.
Change your template |
Learn more about NP |
Site map |
Terms of use |
Rules |
RSS feed |
XHTML 1.0 |
CSS 2.0
Powered by Simple IT SARL:
Contact us
There is nothing else to read, you have to go up now !
Do you want to be published here? Contact us.
10 Newbies Connected |
10 Requests |
0.135s (0.124s)