HTML & CSS: Basic Text Styling
Since CSS is all about styling different aspects and properties of elements on your page, you need to know which properties (and values) exist so that you can style different things. This tutorial aims to introduce you to some of the basic CSS properties for styling text.
color
As has been shown in previous tutorials - text colour is set for an element by using the color
property in CSS. This can be set to any CSS supported colour values, including hex colours (#FF0000
, or in this case, #F00
for short), RGB colours (rgb(255, 0, 0)
), and word colours (red
).
An example of the color property is simply setting the body text to red (as we've done in previous tutorials):
1 2 3 |
|
font-family
Another really important customisation for text is setting the font. Setting the type (or 'family') of the font is done using the font-family
property in CSS.
You usually set this property to a list of fonts - this consists of the font you'd ideally like to display if the user has it on their computer (note that fonts with spaces are usually surrounded in single or double quotes, for example 'Myriad Pro'
), and then the font you'd like if they don't have that, then if they don't have that, and eventually going down to a really general font that the user is likely to have (if they don't have any of the fonts in your list, the browser will take over font control - we usually don't want this!). A common font for the last fallback is sans-serif
.
In the following example, I want the font in the body section to be 'Myriad Pro' if possible, if they don't have that then 'Helvetica', and if they don't have that then 'sans-serif':
1 2 3 |
|
font-size
The size of text is also very important and usually varies from element to element. You can specify text/font size using the font-size
property.
This can be set to a value in units such as em
(size based around the default font size), pt
('point' font size), and px
(pixels).
Remember that because you'll often want different font sizes for different elements, you will probably want to specify the font-size
in many of the elements in your CSS (whereas you might only do the font-family
once in the body). The following example shows some simple sizing on all a
s:
1 2 3 |
|
line-height
Setting the space between lines (or the line height) is also very important for text on modern webpages. Giving text space to breathe by using a higher than usual line-height is very common in the modern web at the time of writing this tutorial - it can really add something special to a page. The line height is set using the line-height
property and can be set using a variety of units, although usually em
s or px
s -- I most commonly use em
s.
An example of using this property is simply setting the line height for the body:
1 2 3 |
|
font
As a quick aside, there is a really useful shorthand property for setting the font-size
, line-height
and font-family
at the same time using only one property! The font
property does exactly this and is commonly used for establishing base text properties for the whole page, commonly used in the body
. The format is as follows:
1
|
|
For example setting some base font/text properties for the body section of our webpage:
1 2 3 |
|
text-align
We can also set the text alignment by using the text-align
property. This can have a few different values:
left
- left alignment.right
- right alignment.center
- centered alignment.justify
- all lines are of an equal length.
An example of the text-align
property is setting the body text alignment to be centred:
1 2 3 |
|
text-decoration
We can set the decoration of the text (underline, overline and some other decorative properties) of text by using the text-decoration
property. We can set this to a couple of preset values:
underline
- an underlineoverline
- an overlineline-through
- a line throughnone
- no decoration
An example of the text-decoration
property is removing the default underline on links (a
elements) by setting the decoration to none
:
1 2 3 |
|
text-transform
We can transform text in various ways using the text-transform
property. This can be set to a few preset values:
uppercase
- uppercase textlowercase
- lowercase textcapitalize
- capitalized textnone
- 'normal' text
An example of the text-transform
property is setting all the a
s to be uppercase:
1 2 3 |
|