Go to the menu - Go to the contents

[Site map] You are here --- > Newbies Paradise > Tutorials > Official > Website > Reading a tutorial

Text formatting with CSS (part 1/2)

Avatar
Author : M@teo21
Creation : : on 01/23/2007 11:13:24 AM
Last modification: : on 07/22/2008 02:08:17 PM
Rate and comment this tutorial
Print this tutorial
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! :D
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. :p

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. ^^
Chapter Contents :
Previous Chapter Contents Next Chapter

Text size

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:

Phewww! Now I'm sure you get the fact that there are a lot ways to change text size :lol:
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.

Fonts

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 :

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: CSS
1
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;
}


Alignment

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 :

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: HTML
1
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:

User image
Indented text


We have to use the text-indent property. Surprising isn't it? :p 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: CSS
1
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 :D

You might want to take note of these two similar properties. I don't really need to discuss longly :

Quiz

Which one of the following units cannot set a text size?
What is the meaning of this piece of CSS code?


Code : CSS
font-family:Verdana, "Arial Black", Arial, serif;
Which one of the following values cannot be set to the text-align property?
How to shift my h3 titles 20 pixels to the right?
Which one of the following is to avoid, whenever it is possible, to set some text's size?
What is the problem in the following piece of CSS code?:


Code : CSS
font-family:Verdana, Trebuchet MS, Georgia, Impact, Arial, serif;


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. ^^
Previous Chapter Contents Next Chapter
Author : M@teo21
Rate and comment this tutorial
Print this tutorial

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.

Number of Newbies connected 10 Newbies Connected | SQL requests 10 Requests | Page loading delay 0.135s (0.124s)