CSS Colours-Text - CSS Tutorials

Colours:

CSS brings 16,777,216 colours to your disposal. They can take the form of a name, an rgb (red/green/blue) value or a hex code.

red
Is the same as
rgb(255,0,0)
Which is the same as
rgb(100%,0%,0%)
Which is the same as
#ff0000
Which is the same as
#f00

There are 17 valid predefined colour names. They are aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, orange, purple, red, silver, teal, white, and yellow.

transparent is also a valid value.

The three values in the rbg value are from 0 to 255, 0 being the lowest level (for example no red), 255 being the highest level (for example full red). These values can also be a percentage.

Hexadecimal (previously and more accurately known as ‘sexadecimal‘) is a base-16 number system. We are generally used to the decimal number system (base-10, from 0 to 9), but hexadecimal has 16 digits, from 0 to f.

The hex number is prefixed with a hash character (#) and can be three or six digits in length. Basically, the three-digit version is a compressed version of the six-digit (#f00 becomes #ff0000, #c96 becomes #cc9966 etc.). The three-digit version is easier to decipher (the first digit, like the first value in rgb, is red, the second green and the third blue) but the six-digit version gives you more control over the exact colour.

‘color’ and ‘background-color’

Colours can be applied by using color and background-color (note that this must be the American English ‘color’ and not ‘colour’).

A blue background and yellow text could look like this:


h1 {
    color: yellow;
    background-color: blue;
}

These colours might be a little too harsh, so you could change the code of your CSS file for slightly different shades:


body {
    font-size: 0.8em;
    color: navy;
} 
 
h1 {
    color: #ffc;
    background-color: #009;
}

Save the CSS file and refresh your browser. You will see the colours of the first heading (the h1 element) have changed to yellow and blue.

You can apply the color and background-color properties to most HTML elements, including body, which will change the colours of the page and everything in it.

Text

You can alter the size and shape of the text on a web page with a range of properties, outlined below

font-family

This is the font itself, such as Times New Roman, Arial, or Verdana.

The font you specify must be on the user’s computer, so there is little point in using obscure fonts. There are a select few ‘safe‘ fonts (the most commonly used are arial, verdana and times new roman), but you can specify more than one font, separated by commas. The purpose of this is that if the user does not have the first font you specify, the browser will go through the list until it finds one it does have. This is useful because different computers sometimes have different fonts installed. So font-family: arial, helvetica, for example, is used so that similar fonts are used on PC (which traditionally has arial, but not helvetica) and Apple Mac (which, traditionally, does not have arial and so helvetica, which it does normally have, will be used).

Note: if the name of a font is more than one word, it should be put in quotation marks, such as font-family: "Times New Roman".

font-size

The size of the font. Be careful with this - text such as headings should not just be a paragraph in a large font; you should still use headings (h1, h2 etc.) even though, in practice, you could make the font-size of a paragraph larger than that of a heading (not recommended for sensible people).

font-weight

This states whether the text is bold or not. In practice this usually only works as font-weight: bold or font-weight: normal. In theory it can also be bolder, lighter, 100, 200, 300, 400, 500, 600, 700, 800 or 900, but seeing as many browsers shake their heads and say “I don’t think so”, it’s safer to stick with bold and normal.

font-style

This states whether the text is italic or not. It can be font-style: italic or font-style: normal.

text-decoration

This states whether the text is underlined or not. This can be:

  • text-decoration: overline, which places a line above the text.
  • text-decoration: line-through, strike-through, which puts a line through the text.
  • text-decoration: underline should only be used for links because users generally expect underlined text to be links.

This property is usually used to decorate links, such as specifying no underline with text-decoration: none.

text-transform

This will change the case of the text.

  • text-transform: capitalize turns the first letter of every word into uppercase.
  • text-transform: uppercase turns everything into uppercase.
  • text-transform: lowercase turns everything into lowercase.
  • text-transform: none I’ll leave for you to work out.

body {
    font-family: arial, helvetica, sans-serif;
    font-size: 0.8em;
} 
 
h1 {
    font-size: 2em;
} 
 
h2 {
    font-size: 1.5em;
} 
 
a {
    text-decoration: none;
} 
 
strong {
    font-style: italic;
    text-transform: uppercase;
}

Text spacing

The letter-spacing and word-spacing properties are for spacing between letters or words. The value can be a length or normal.

The line-height property sets the height of the lines in an element, such as a paragraph, without adjusting the size of the font. It can be a number (which specifies a multiple of the font size, so ‘2′ will be two times the font size, for example), a length, a percentage or normal.

The text-align property will align the text inside an element to left, right, center or justify.

The text-indent property will indent the first line of a paragraph, for example, to a given length or percentage. This is a style traditionally used in print, but rarely in digital media such as the web.


p {
    letter-spacing: 0.5em;
    word-spacing: 2em;
    line-height: 1.5;
    text-align: center;
}

Share/Save/Bookmark

Tags: , ,

One Response to “CSS Colours-Text - CSS Tutorials”

  1. [...] News » News CSS Colours-Text - CSS Tutorials2008-08-24 00:10:37Predefined rgb(255,0,0) Which is the same as rgb(255,0,0) Which is the same as [...]

Leave a Reply