Thursday, December 18, 2008

Zero Font Size

Each of the major browsers render text of zero units (or percentage) differently. Firefox 3 does not display the text. Internet Explorer 7 and Internet Explorer 6 render the text with a height of 1 pixel. Opera 9.6 does not display the text. Safari 3.1 renders 0em , 0ex and 0% text with a text size of 9px and renders text of 0px and zero absolute length units with a height of 1px.
Examples:
<p style="font-size: 0em;">jumps over the lazy dog</p>
<p style="font-size: 0ex;">jumps over the lazy dog</p>
<p style="font-size: 0%;">jumps over the lazy dog</p>
<p style="font-size: 0px;">jumps over the lazy dog</p>
<p style="font-size: 0pt;">jumps over the lazy dog</p>
<p style="font-size: 0mm;">jumps over the lazy dog</p>
<p style="font-size: 0cm;">jumps over the lazy dog</p>
<p style="font-size: 0in;">jumps over the lazy dog</p>
<p style="font-size: 0pc;">jumps over the lazy dog</p>

Friday, December 5, 2008

Font Size: Leading and Trailing Zeroes

When specifying a font size, the value can have leading and trailing zeroes or they can be omitted completely. This does not affect the way the text is rendered.

Leading Zeroes

Zeros that appear at the beginning of a number are called leading zeroes. Example: font-size:0.5in - the first digit is a leading zero. In all major browsers, leading zeroes are optional. Therefore font-size:.5in is a valid way of specifying font size. There can also be more than one leading zero.

Trailing zero

Zeroes that appear after a decimal point and at the end of the number are called trailing zeroes. Example: font-size:2.50em - the last digit is a trailing zero. In all major browsers, trailing zeroes are optional. Therefore, all the following declarations result in the same font size:
font-size: 2.5em
font-size:2.50em
font-size: 2.500em

Sunday, October 19, 2008

Font Size - Absolute Length Units - pt, pc, in, cm, and mm

The absolute length units are those units that are independent of inherited font size. There are five absolute length units:
point (pt)
In CSS, one point is defined as 1/72 of an inch. Therefore, one point is 1.333 pixels (since 1 inch is equivalent to 96 pixels)
pica (pc)
One pica is 12 points. In CSS, one point is 1/72 of an inch, therefore, one pica is 1/6 of an inch. One pica is 16 pixels (since 1 inch is equivalent to 96 pixels)
inch (in)
Windows and Mac computers assume 96 pixels to be one inch (even though 96 pixels on a screen may not be equal to an actual inch)
centimeters (cm)
One inch is 2.54 centimeters. Therefore, in CSS, one centimeter is 37.79 pixels
millimeters (mm)
Ten millimeters make one centimeter. One inch is 25.4 millimeters. Therefore, in CSS, one millimeter is 3.779 pixels

point (pt)

All major browsers render 1pt as 1.3333px when displaying on screen. When the computed font size is a floating point number (example: 19.3px), the browser rounds off the number to a whole number when rendering the text. After the decimal point, any number greater than 4 (that is, numbers 5 to 9) is rounded off to 1px (example: 19.5px is rendered as 20px text).
However, it is the "unrounded" computed font sizes that are inherited.

Example: div{font-size:12pt}

Font sizes in points can be specified as floating point numbers too. A floating point size is multiplied by 1.3333 and then rounded-off in the usual way before rendering. All major browsers accurately render floating point font sizes in points of two decimal places (example: font-size: 19.25pt). When sizing text with points and relative length units, point sizes of two decimal places are required to avoid unexpected results.

pica (pc)

All major browsers render 1pc as 16px on the screen.When the computed font size is a floating point number (example: 19.3px), the browser rounds off the number to a whole number when rendering the text. After the decimal point, any number greater than 4 (that is, numbers 5 to 9) is rounded off to 1px (example: 19.5px is rendered as 20px text).
However, it is the "unrounded" computed font sizes that are inherited.

Example: td{font-size:1.2pc;}

Opera 9.6 bug: Opera 9.6 incorrectly sizes 2.1pc text to 33pixels. The computed value of 2.1pc text should be 2.1*16=33.6px which should be rounded off to 34px . However, due to the bug, Opera 9.6 renders the text in 33px .

Font sizes in picas can be specified as floating point numbers too. A floating point pica size is multiplied by 16 and then rounded-off in the usual way.
All major browsers accurately render floating point font sizes in picas of two decimal places (example: font-size: 1.25pc;). However, when sizing text with picas and relative units, pica sizes of three decimal places are required to avoid unexpected results . Firefox 3 and Safari 3.1 accurately render pica font sizes of three decimal places. Opera 9.6, Internet Explorer 6 and Internet Explorer 7 ignore the third decimal place in pica font sizes.
Therefore, one must avoid mixing picas with relative length units when sizing text.

inch (in)

All major browsers assume 1in to be 96 pixels. When the computed font size is a floating point number (example: 19.3px), the browser rounds off the number to a whole number when rendering the text. After the decimal point, any number greater than 4 (that is, numbers 5 to 9) is rounded off to 1px (example: 19.5px is rendered as 20px text).
However, it is the "unrounded" computed font sizes that are inherited.

Example: h1{font-size:0.5in}

Font sizes in inches can be specified as floating point numbers too. A floating point inch size is multiplied by 96 and then rounded-off in the usual way.
All major browsers correctly render floating point font sizes in inches of two decimal places (example: font-size: 0.52in). However, when sizing text with inches and relative length units, inch sizes of three decimal places are required to avoid unexpected results.
Firefox 3, Internet Explorer 6, Internet Explorer 7, and Safari 3.1 accurately render inch font sizes of three decimal places. Opera 9.6 however, ignores the third decimal place in inch font sizes of three decimal places.

centimeter (cm)

All major browsers compute 1cm as 37.79px when rendering on the screen.When the computed font size is a floating point number (example: 19.3px), the browser rounds off the number to a whole number when rendering the text. After the decimal point, any number greater than 4 (that is, numbers 5 to 9) is rounded off to 1px (example: 19.5px is rendered as 20px text).
However, it is the "unrounded" computed font sizes that are inherited.

Example: p{font-size: 0.5cm}

Font sizes in centimeters can be specified as floating point numbers too. A floating point centimeter size is multiplied by 37.79 and then rounded-off in the usual way. All major browsers correctly render floating point font sizes in centimeters of two decimal places (example: font-size: 0.52cm). However, when sizing text with centimeters and relative length units, centimeter sizes of three decimal places are required to avoid unexpected results. Firefox 3, Internet Explorer 6, Internet Explorer 7, and Safari 3.1 accurately render centimeter font sizes of three decimal places. Opera 9.6 however, ignores the third decimal place in centimeter font sizes of three decimal places.

millimeter (mm)

All major browsers compute 1mm as 3.779px when rendering on the screen.When the computed font size is a floating point number (example: 19.3px), the browser rounds off the number to a whole number when rendering the text. After the decimal point, any number greater than 4 (that is, numbers 5 to 9) is rounded off to 1px (example: 19.5px is rendered as 20px text).
However, it is the "unrounded" computed font sizes that are inherited.
Example: td {font-size:3.5mm}
Font sizes in millimeters can be specified as floating point numbers too. A floating point millimeter size is multiplied by 3.779 and then rounded-off in the usual way. All major browsers correctly render floating point font sizes in millimeters of two decimal places (example: font-size: 2.55mm). When sizing text with millimeters and relative length units, millimeter sizes of two decimal places are required to avoid unexpected results.

Absolute length unit sized text - resizing by the user

Firefox 3, Opera 9.6 and Internet Explorer allow the user to zoom text that has been sized with points, picas, inches, centimeters and millimeters.
Safari 3.1 allows the user to use the "Make text bigger" or "Make text smaller" feature to resize text that was sized with points, picas, inches, centimeters and millimeters.
Internet Explorer 6 and Internet Explorer 7 do not allow the user to resize text that was sized with points, picas, inches, centimeters and millimeters. However, IE7's zoom feature does work here.

Advantages and Disadvantages

One disadvantage of using the absolute length units to size text is they are unintuitive - one CSS "inch" is not the same as a real-world inch - the same goes for points, picas, centimeters and millimeters. Another disadvantage is Internet Explorer 6 does not allow the user to resize text that was sized with the absolute length units.
However, the absolute length units are useful when specifying text size on web pages that are meant to be printed because printers will render CSS inches (and the other absolute length units) in the same size as their real-world counterparts.

Friday, October 17, 2008

Font Size - Relative Length Units - px

Pixels (px) are considered relative length units because the CSS specification defines a pixel as 1/96th of an inch on the display screen. Since the number of dots per inch on screens varies, the actual size of the CSS pixel would have to vary to conform to the specification. Thus, the size of a pixel, in CSS, is relative to the dots per inch of the display device. All major browsers do not conform to this CSS specification when rendering a web page on the screen - they consider one CSS pixel to be equal to one actual pixel on the screen. However, many browsers do render pixels relatively when printing web pages.
The font size of an element can be specified in pixels in the following way:
p{font-size:15px}
No minimum or maximum value is specified for font size pixel values. However, negative values are not permitted. Opera 9.5 does not render text in sizes less than 9px when the font size is declared in pixels.
Floating point numbers are allowed (example: p{font-size:16.5px}).However, the major web browsers differ in their handling of floating point px values.
Firefox 3 and Safari 3.1 round off the font size in this manner: After the decimal point, any number greater than 4 (that is, numbers 5 to 9) is rounded off to 1px (example: 19.5px is rendered as 20px text). However, it is the floating point size that is inherited by descendant elements.
Internet Explorer 6, Internet Explorer 7, and Opera 9.6 ignore the decimal portion of the font size property's value. In these browsers, a font size value of 15.6px , for example, is considered as just 15px .
Note: Internet Explorer 6 and Safari 3.1 do not allow the user to increase the size of text that is sized with pixels. Firefox 3, Opera 9.6 and Internet Explorer 7, however, allow this with the "zoom" feature. This zoom feature also increases the size of everything else on the web page.

Advantages and Disadvantages of Using Font Size Pixel Units

Sizing text using pixel values can be done with consistent results if non-integral values and sizes below 9px are avoided. However, text sizing with pixel values also makes web pages less accessible because Internet Explorer and Safari 3.1 don't allow users to resize the text.

Tuesday, October 14, 2008

Font Size - Relative Length Units - ex

One of the relative length units that can be used with the font-size property is ex. ex refers to the x-height of the font.
The x-height is the distance between the base line and the mean line of a font.
The base line is the line on which most of the letters of a typeface "stand". The descenders extend below the base line.
The mean line is where the tops of lower-case letters end (excluding the ascenders). The ascenders extend above the mean line.
A typeface's x-height is usually (but not always) equal to the height of the character lowercase "x".
Here is an example of a CSS font size declaration that uses ex units: p{font-size: 2.5ex}
Font sizes in ex units can be floating point numbers, too. All the major browsers recognize up to two decimal places in ex font size declarations (example: font-size:0.36ex).
Each browser handles font size declarations in ex units differently - the resulting font size with a given font size declaration in ex varies between browsers. Here is how each of the major web browsers handles ex units.

Firefox 3

In Firefox 3, one ex is exactly equal to the vertical height of the lowercase letter "x". Therefore, when an element's font size is set to 1ex , the font size of the text will be equal to the height of the letter "x" in the inherited font.
Example:<div style="font-size:20px;font-family:Arial"><p style="font-size:2ex">the quick brown fox...</p>
When the font size of Arial is 20px , the height of the letter "x" is 11px . Therefore, in the above example, one ex will be 11px . The font size of the text in the p element will be 2ex or 22px .
Note that in Firefox 3, 1ex will never be a non-integral number because the height of the letter x will always be a whole number when measured in pixels.
Font size can be specified in ex units using floating point numbers too. Firefox 3 correctly renders font size declarations in ex with two decimal places (example: font-size: 3.25ex).

Internet Explorer 6, Internet Explorer 7 and Opera 9.5

IE6, IE7 and Opera 9.5 take 1ex to be equal to 0.5em or half the inherited font size. These browsers completely ignore the actual height of the letter "x".
Example: <div style="font-size:20px;font-family:Impact"><p style="font-size:2ex">the quick brown fox...</p>
The font size of the text in the p element will be 2opx (1ex = half the inherited font size, therefore 2ex = inherited font size). The same paragraph in Firefox 3 would have a font size of 26px.
In IE6, IE7, and Opera 9.5, 1ex can be either a whole number or a non-integral number, depending on whether the inherited font size is an even number or an odd number.
When using ex units, Opera 9.5's computed font size does not go below 9px.
Font size can be specified in ex units using floating point numbers too. Internet Explorer 6, Internet Explorer 7 and Opera 9.5 correctly render font size declarations in ex with two decimal places (example: font-size: 3.25ex).

Safari 3.1

Safari 3.1's calculation of 1ex is approximately equal to the height of the lowercase letter "x". It is not known exactly how Safari 3.1 determines the value of 1ex. Font size can be specified in ex units using floating point numbers too. Safari 3.1 correctly renders font size declarations in ex with two decimal places (example: font-size: 3.25ex).
Important note for Firefox 3 and Safari 3.1: The size of 1ex is determined by the inherited font size and typeface, even if the current element has a different font family declaration.

Advantages and Disadvantages of using ex units in font size declarations

Using ex units to size fonts does not result in consistent sizes across browsers and should therefore be avoided.

Sample data

The following table shows the number of pixels in 1ex in each of the major browsers for an inherited font size of 20px.
Size of 1ex in pixels for an inherited font size of 20px and various typefaces
Font FamilyTimes New RomanArialVerdanaComic Sans MS Courier NewGaramondGeorgiaImpactLucida SansMicrosoft Sans Serif
Measured font height18192020171819182020
height of the letter "x"91111109810131111
1ex in px in Firefox391111109810131111
1ex in px in IE6, IE7 and Opera9.510101010101010101010
1ex in px in Safari3.11010111111910131110

Monday, October 13, 2008

Font Size - Relative Length Units - em

One of the relative length units that can be used with the font-size property is em. In CSS, one em is equivalent to 100%. Therefore, 0.01em is 1%, and so on. Like percentage values, the em unit specifies the font size relative to the inherited font size.
Example: div {font-size: 1.5em}
Font sizes in em's specify the text size of an element relative to the inherited font size.
Font size declarations in em's can be floating point numbers (example: 0.625em). When sizing text using em's, there are times when three decimal places are required to avoid unexpected results. However, Internet Explorer 6, Internet Explorer 7 and Opera 9.6 ignore the third decimal place. These browsers only recognize two decimal places in em font size declarations. Inherited font sizes and calculated font size values from em declarations can be non-integral. Therefore, browsers round off the value.
All browsers round off this way: After the decimal point, any number greater than 4 (that is, numbers 5 to 9) is rounded off to 1px (example: 19.5px is rendered as 20px text). However, it is the floating point size that is inherited by descendant elements.
One must avoid using font size declarations that result in computed values containing more than one decimal place because this brings out the bugs in browsers, giving unpredictable results.
Example: Ancestor font size: 35px, Parent font size: 0.91em (computed value = 31.85px), Child font size: 0.8em (computed value = 25.48px, rounded off actual value = 25px). Browsers round off this value to 25px when rendering the text. However, Internet Explorer 6, Internet Explorer 7 and Opera 9.6 render the font in 26px .
One way of avoiding computed sizes with many decimal places is by specifying the em values with up to two decimal places in order to obtain a calculated value that is nearly a whole number. In the above example, the child font size can be specified as 0.79em, which gives a computed font size of 25.16px . All browsers round this off correctly.
Note: For a given inherited font size, the final font size obtained when using em values is dependent on the inherited font size in pixels. Whether a given inherited font size in pixels is obtained by using inches, keywords or any other units is unimportant.

Font Size - Percentage Values

The value of the CSS property font-size can be specified as a percentage. Negative percentages are not allowed.
Example: p {font-size: 120%;}
Specifying font size in percentages sizes the text of an element relative to the inherited font size.
Font size declarations can be floating point percentages too (example: font-size:62.5%). However, Internet Explorer 6 and Internet Explorer 7 ignore the fractional part in percentage font size declarations. This could cause problems - using percentages can sometimes give unexpected results in Internet Explorer 6 and Internet Explorer 7. Careful calculation of inherited and computed sizes can prevent this.
Inherited font sizes and computed values from percentages can be floating point numbers. Therefore, browsers round off the value when rendering the text.
All browsers round off this way: After the decimal point, any number greater than 4 (that is, numbers 5 to 9) is rounded off to 1px (example: 19.5px is rendered as 20px text). However, it is the floating point size that is inherited by descendant elements.
One must avoid using font size declarations that result in computed values containing more than one decimal place because this brings out the bugs in browsers, giving unpredictable results.
Note: For a given inherited font size, the final font size obtained when using percentages is dependent on the inherited font size in pixels. Whether a given inherited font size in pixels is obtained by using inches, keywords or any other units is unimportant.

Friday, October 10, 2008

Font Size - Relative Size Keywords With Relative Length Units (px, em, ex) and Percentage Values

The relative size keywords can be applied to text whose inherited size was specified using percentage values or the relative length units px, em and ex. Unfortunately, with a given combination of relative size keywords and relative length units (or percentage values), the resulting font size varies across browsers.
In a particular browser, whether the font size is inherited from relative length units or percentage values, for a particular computed font size, the relative size keywords behave the same (except for IE6 and IE7 when the computed size is 18px).
Example 1:<p style="font-size:0.9em">Text at the inherited size and <span style="font-size:larger">larger text</span> and <span style="font-size:smaller">smaller text</span></p>

Example 2:<p style="font-size:9px">Text at the inherited size and <span style="font-size:larger">larger text</span> and <span style="font-size:smaller">smaller text</span></p>

Example 3:<p style="font-size:90%">Text at the inherited size and <span style="font-size:larger">larger text</span> and <span style="font-size:smaller">smaller text </span></p>

Example 4:<p style="font-size:1.8ex">Text at the inherited size and <span style="font-size:larger">larger text</span> and <span style="font-size:smaller">smaller text</span></p>
Advantages and Disadvantages: The application of relative size keywords to text whose inherited size was specified using percentage values and the relative length units px, em and ex results in inconsistent font sizes across browsers. Therefore, this combination must be avoided if consistent appearance across browsers is desired.

Notes and Example Data:
Firefox 3: If the keywords larger or smaller are applied to text whose computed size corresponds to an absolute keyword font size (9, 10, 13, 16, 18, 24 and 32 pixels), then FF3 "upgrades" or "downgrades" the text to the nearest absolute keyword's font size.
For other computed sizes, FF3 multiplies or divides the computed font size by a number ranging from 1.11 to 1.5 .
Opera 9.5: Opera's relative keyword scaling of 9, 10, 13, 16, 18, 24 and 32 pixels sized text is also consistent with its absolute keyword scaling. For other px sizes, Opera 9.5 multiplies or divides with a number ranging from 1.11 or 1.29 .
IE7 and IE6: The relative keyword scaling of 9, 10, 13, 16, 18, 24 and 32 pixels sized text is not consistent with its absolute keyword scaling. The scaling of 10px and 13 px with the keyword smaller does not match the scaling of x-small and small with smaller.
IE6 and IE7 multiplies or divides the relative keywords with a number ranging from 1 to 1.5
Safari 3.1: Safari's relative keyword scaling of 9, 10, 13, 16, 18, 24 and 32 pixels sized text is largely consistent with its absolute keyword scaling with the exception that font sizes are allowed to go below 9px.
Safari 3.1 does relative keyword scaling by multiplying or dividing by a figure ranging from 1.12 to 1.29
Internet Explorer 6 and 7 relative size keyword bug:
largerlarger-larger
parent style: font-size: 18px18 24
ancestor font size = 10px inherited font size: 1.8em or 3.6ex or 180%2432

The first row shows the resulting font size in pixels when the inherited font size was specified as 18px. The second row shows the resulting font size when the ancestor font size was 10px and the inherited font size was specified as 1.8em or 3.6em or 180% (which results in a computed value of 18px).
The resulting font size is supposed to the same - that is, both the rows should have the same pair of values. This is an IE6 and IE7 bug.
Here is a table that shows the resulting font size when the relative keywords are applied to text with various inherited font sizes. All font sizes are in pixels. The columns smaller-smaller and larger-larger indicate nested relative keywords ie. the relative keyword was applied twice through nested elements.
Table Caption: Relative sizing of text with inherited font size from relative length units or percentage values
inherited font sizesmallersmaller-smallerlargerlarger-larger
9871013Firefox 3
10981316
11981417
121091517
131091618
141191720
1512101722
1613101824
1715122128
1816132432
1916142535
2418163248
3224184872
9871113Opera 9.5
10981214
11981316
121091517
131091619
141191720
1512101822
1613101923
1714112024
1814122226
1915122327
2419152935
3226203846
9641013IE6 , IE7
10741013
111071316
121071316
131071316
1413101618
1513101618
1613101824
1716131824
1816132432
1918162432
2418163248
3224184872
9761113Safari 3.1
10871214
11981316
121081417
131191619
1412101720
1512101822
1613111923
1714122024
1815122226
1917132327
2420172935
3227223846

Wednesday, October 8, 2008

Font Size - Relative Size Keywords with Absolute Keyword Sized Text

The relative size keywords can be applied to text whose inherited size was specified using the absolute size keywords. Each browser handles this combination differently.

Firefox 3 and Internet Explorer 6 and 7

When the larger keyword is used with absolute keyword sized text, Firefox 3 and Internet Explorer 6 and 7 will "upgrade" the text size to the next higher absolute keyword's size.
Example: <p style="font-size:medium">Some text here <span style="font-size:larger">larger span text</span>
In this example, the inherited font size is medium which translates to 16px. Therefore, Firefox 3 or Internet Explorer 6 and 7 will increase the span text's font size to 18px which is equivalent to the absolute size keyword large.
When the keyword larger is applied to text sized with xx-large, Firefox 3 and Internet Explorer 6 and 7 multiply the font size by 1.5.
Example: <p style="font-size:xx-large">Some text here <span style="font-size:larger">larger span text</span>
The keyword xx-large corresponds to a font size of 32px. If to this the keyword larger is applied the resulting font size is 32 * 1.5 = 48px .
When the smaller keyword is used with absolute keyword sized text, Firefox 3 and Internet Explorer 6 and 7 will "downgrade" the text size to the next lower absolute keyword's size.
When the keyword smaller is applied to text sized with xx-small Firefox 3 divides the font size by approximately 1.13 .
Example: <p style="font-size:xx-small">Some text here <span style="font-size:smaller">larger span text</span>
The keyword xx-small corresponds to a font size of 9px. If to this the keyword smaller is applied, the resulting font size is 9 / 1.13 = 8px (approx.). However, Internet Explorer 6 and 7 don't behave this way. Therefore, using the relative size keyword smaller with text whose inherited font size is xx-small is not recommended.

Opera 9.5

When the larger keyword is used with absolute keyword sized text, Opera 9.5 does not "upgrade" the text size to the next higher absolute keyword's size. Instead, it multiplies the font size by approximately 1.2 .
When the smaller keyword is used with absolute keyword sized text, Opera 9.5 does not "downgrade" the text size to the next lower absolute keyword's size. Instead, it divides the font size by a number ranging from 1.1 to 1.29 .

Safari 3.1

When the larger keyword is used with absolute keyword sized text, Safari 3.1 does not "upgrade" the text size to the next higher absolute keyword's size. Instead, it multiplies the font size by approximately 1.2 .
When the smaller keyword is used with absolute keyword sized text, Opera 9.5 does not "downgrade" the text size to the next lower absolute keyword's size. Instead, it divides the font size by approximately 1.2 . However, if the resulting font size is less than 9px , it is ignored and the font size is made 9px . Therefore, the font size is never less than 9px .

Advantages and Disadvantages

There is no consistency across browsers in the application of the relative keywords to text that has been sized with absolute keywords. Therefore, this combination of keywords must be avoided.
Here is a table showing the resulting font sizes when the relative keywords are applied to text that was sized with absolute keywords. All the sizes are in px, smaller smaller and larger larger indicate nested relative keywords ie. the relative keyword was applied twice through nested elements.
Relative sizing of text that was sized with absolute size keywords
keywordsizesmallersmaller-smallerlargerlarger-larger
xx-small9871013Firefox 3
x-small10981316
small131091618
medium1613101824
large1816132432
x-large2418163248
xx-large3224184872
xx-small9871113Opera 9.5
x-small10981214
small131091619
medium1613101923
large1814122226
x-large2419152935
xx-large3226203846
xx-small9641013IE6 , IE7
x-small10741316
small131071618
medium1613101824
large1816132432
x-large2418163248
xx-large3224184872
xx-small9991113Safari 3.1
x-small10991214
small131191619
medium1613111923
large1815122226
x-large2420172935
xx-large3227223846

Font Size - Relative Size Keywords

There are two font-size relative size keywords - smaller and larger which decrease or increase the font size of text with respect to the inherited size.
Example 1: <p>Some text here <span style="font-size: larger">span text</span></p>
Example 2: <p style="font-size:small">Some text here <span style="font-size:larger">larger span text</span></p>
Example 3: <p style="font-size:15px">Some text here <span style="font-size:smaller">smaller span text</span></p>
There are 5 ways to use relative size keywords on an element:
  • On their own - the ancestors of the element do not have their text sized using the font-size property
  • With absolute size keywords - the ancestors of the element have their text sized using the absolute sized keywords
  • With absolute length units - the ancestors of the element have their text sized in in, cm, mm, pt or pc
  • With relative length units - the ancestors of the element have their text sized in em, ex or px .
  • With percentages - the ancestors of the element have their text sized in percentages.

Tuesday, September 30, 2008

Font Size - Absolute Size Keywords

The font-size absolute size keywords are a set of keywords that specify certain font sizes regardless of the inherited font size.
These are the font-size absolute size keywords with their resulting font size in pixels:
xx-small
9px
x-small
10px
small
13px
medium
16px
large
18px
x-large
24px
xx-large
32px

Example: <p style="font-size: large">
Note that the ratio of the font size between two consecutive keywords is not fixed: the ratio of one keyword and the next larger one varies between 1.11 to 1.33 .
The font sizes that the keywords map to are consistent across browsers. Also, text that is sized with absolute size keywords can be resized by using the browser's Zoom or Text Size feature. Therefore, absolute keywords are recommended for text sizing provided that one can accept the specific sizes that the keywords map to.
Note: Some sources erroneously state that each absolute size keyword's corresponding font size is 1.5 or 1.2 times the previous keyword's font size.

Sunday, September 28, 2008

Font Size Inheritance

The css property font-size is an inherited property. That is, the font size of an element is inherited from ancestor properties.
When using relative-size length values, the computed value may be a non-integral number. This value is rounded off to give the actual displayed font size. However, it is the computed non-integral value that is inherited by descendant elements.
Example: <p style="font-size:15px">Some text <span style="font-size:1.5em"> span text</span>
The computed size for the text in the span is 22.5px . This is rounded-off by the browser to 23px. The font size that is inherited by elements is the computed size not the rounded-off size.
Example: <p style="font-size:15px">Some text <span style="font-size:1.5em"> span text <span "font-size:1.2em">nested span text</span></span></p>
The computed font size for the text in the outer span is 22.5px and the actual font size is the rounded-off figure of 23px. The inherited font size of the inner span is 22.5px and the computed font size is 27px.

Wednesday, September 17, 2008

The font-size Property

The font-size property specifies the size of the text in an element. This property can have as its value keywords, length values or percentages. The length values and keywords can be absolute or relative size.
The absolute size keywords are:
  • xx-small
  • x-small
  • small
  • medium
  • larger
  • x-large
  • xx-large
The relative size keywords are:
  • smaller
  • larger
The absolute size length units are:
  • points (pt)
  • picas (pc)
  • inches (in)
  • centimeters (cm)
  • millimeters (mm)
The relative size length units are:
  • em-height (em)
  • x-height (ex)
  • pixels (px)

The font-family Property

The CSS property font-family is used to specify the font-family to be used by an (X)HTML document element.
The value of this property is either a single string or keyword or a comma-separated list of strings or keywords . If the string has any spaces in it, then it must be enclosed in quotes.
Example: p { font-family: "Times New Roman", "Georgia", "Times", serif; }
Note that the quote marks are optional if the font name does not have spaces. Therefore the above rule can also be written as:
p {font-family: "Times New Roman", Georgia, Times, serif;} If the enclosing quote marks of Times New Roman are omitted, then the font family name may not be matched.
Note that the generic font-family names are keywords (not strings) therefore they must not be enclosed in quotes. Any text that is quoted is considered a string and therefore the name of a font, not a keyword. Also, if the font-family keyword is used in inline styles, using the style attribute, the quotes used for strings should be single quotes if the attribute uses double and vice versa.
Here is list of generic font family names and their CSS keywords:
Serif Fonts
serif
Sans-serif Fonts
sans-serif
Monospace Fonts
monospace
Cursive Fonts
cursive
Fantasy Fonts
fantasy
The keyword fantasy doesn't always work - the recognized fantasy fonts may not be installed on the computer.
At the end of a list of font-families, a generic font family is usually specified so that in case none of the specified font-families are found, the browser can choose any available font from a generic font-family.
The browser checks the availability of font-families starting from the left most font-family in the comma-separated list. The browser uses the first available font-family.
The font-family property is inherited (from ancestor elements).

Wednesday, September 10, 2008

Font Families

There are many terms associated with fonts, we'll describe some of them here.
Serifs are the decorative hooks and barbs on the ends of letters found in some fonts. Fonts that have serifs are called serif fonts. Sans-serif fonts are those fonts whose letters do not have serifs. Proportional Fonts are those fonts in which the width of the its letters vary. For example, the letter "i" would take up less horizontal space than the letter "w".
Monospace Fonts are those fonts in which the width of all the letters are the same. Font families are a collection of different variants of a font. For example, the Times New Roman font-family consists of regular Times New Roman, Times New Roman Bold, Times New Roman Bold Italic, and Times New Roman Italic. CSS defines five generic font families:
Serif Fonts
This generic font family consists of proportional serif font-families. Examples: Times New Roman, Garamond, Georgia etc.
Sans-serif fonts
This generic font family consists of proportional sans-serif font families. Examples: Arial, Verdana, Tahoma etc.
Monospace Fonts
This generic font family consists of monospace fonts such as Courier, Courier New etc.
Cursive Fonts
This generic font family consists of proportional fonts that look like handwriting. Examples: Comic Sans MS, Apple Chancery etc.
Fantasy fonts
This generic font family consists of those font families that are stylized and decorative and don't fall into any of the above categories. Examples: Aston-F1, Impact etc.

Tuesday, September 9, 2008

CSS Values - Strings

Some CSS properties take string values. A string value is enclosed in single or double quotes and can contain any text. Note that if the string has the single quote character in it then double quotes should be used to enclose the string and vice versa.
Unprintable characters can be represented using the escape character - backslash (\) followed by a hexadecimal number representing a Unicode character. The hexadecimal number must be followed by a space. However, if the character following the special character is not a, b, c, d, e, or f, then the space can be omitted.

CSS Values - URLs

For CSS properties that accept URLs as their values, the URL specified in this format: url(protocol://server/pathname) . For example: #main {background-image: url(http://www.chetancrasta.com/images/texture.jpg)} Relative URLs can also be used:
#main{background-image: url(images/bg.gif)}
Relative URLs are relative to the path of the document that contains the CSS declaration. There should be no whitespace between url and (http://www... .

Spcecifying Lengths in CSS - Length Values

A length value consists of a real number followed by a unit. Some properties accept only positive numbers. There should be no whitespace between the real number and the unit. If the real number is zero then a unit need not be entered. Lengths can be absolute or relative.

Absolute Length Units

There are five absolute units in CSS:
  • millimeters (mm)
  • centimeters (cm)
  • inches (in)
  • points (pt) - one point is 1/72 of an inch
  • picas (pc) - one pica is equal to 12 points, therefore 1/6 of an inch is one pica
The problem with absolute length units is that the number of pixels per inch on monitors are not fixed. The number of pixels per inch on a laptop screen is usually higher than on desktop monitors. To complicate things further, these settings can be customized by the user. For the purpose of displaying font sizes, Windows and Mac computers assume that there are 96 pixels per inch on the monitor. Therefore, if a font's size is specified as 72 points (1 inch), then the operating system converts that to a height of 96 pixels. However, rarely are 96 pixels equal to 1 inch on a computer monitor. The actual length of 96 pixels varies a lot depending on the kind of monitor and the resolution that the user has set it to.
It is for the above reason that on a windows or mac computer specifying size in anything other than pixels is inaccurate.
However, absolute length units are useful in print style sheets. Printers usually reproduce absolute lengths correctly.
Example: height: 10cm;

Relative Length Units

Relative Length Units are those that are specified relative to the another quantity. There are three relative length units in CSS:
  • em - 1em is the current font size (default or inherited)
  • ex - 1ex is the x-height of the current font
  • px - pixels
em units
In CSS, one em is defined as equal to the current font size. The "current" font size may be inherited, may be the browser default or may be specified using the font-size property. em units can be used to specify font-sizes, margins, borders, widths, heights etc.
Example: If the inherited font size of the p element is 12 pixels and if the left-margin is specified as 2em (margin-left: 2em), then the left-margin will be equal to 24 pixels.
ex units
One ex unit is the height of a lowercase x in the current font. Therefore the ex height will vary with different fonts (Times, Verdana etc) of the same size. The ex height is defined even for those fonts that don't have a lowercase in them - the ex height is specified in the font file itself. In practice, however, most fonts do not specify their ex height.
Internet Explorer 6, Internet Explorer 7 and Opera 9.6 simply consider 1ex to be equal to 0.5em
Pixel Lengths
Even though pixels appear to be absolute units, in CSS they are defined as relative units because on high-resolution output devices such as printers, CSS pixel units are scaled. On such devices, one pixel is considered to be 1/96 inch. However on computer monitors, one CSS pixel unit is equal to one pixel on the display screen.
Font sizes specified in pixels cannot be resized by the user in IE6
Example:
padding: 5px 5px;

CSS Values - Keywords

Many CSS properties take on various keywords such as left, block, none, small, green etc. A keyword is an identifier and therefore should be entered after the css property name and colon. A semi-colon is put after the keyword. Example:
display: none;

Sunday, September 7, 2008

Specifying Color in CSS

There are five kinds of values that specify color in CSS — color keywords, system color keywords, hexadecimal color notation, decimal functional rgb notation, and percentage functional rgb notation.

Color Keywords

There are 17 Color Keywords, also called named colors, defined in CSS2.1:

aqua
rgb(0,255,255)
black
rgb(0,0,0)
blue
rgb(0,0,255)
fuchsia
rgb(255,0,255)
gray
rgb(128,128,128)
green
rgb(0,128,0)
lime
rgb(0,255,0)
maroon
rgb(128,0,0)
navy
rgb(0,0,128)
olive
rgb(128,128,0)
orange
rgb(255,80,0)
purple
rgb(128,0,128)
red
rgb(255,0,0)
silver
rgb(192,192,192)
teal
rgb(0,128,128)
white
rgb(255,255,255)
yellow
rgb(255,255,0)

Example 1: <p style="font-size:35px;font-family:'Times New Roman';color:aqua">the quick brown fox jumps over the lazy dog</p>

There are 148 color keywords defined in CSS 3. However, there are only 139 individual colors. This is because there are a pair of color keywords for each of the gray colors — one spelt "gray" and the other spelt "grey". Also, fuchsia and magenta  is the same color, and so is aqua and cyan.

Internet Explorer 6 and 7 ignore the following color keywords: lightgray, darkgrey, grey, dimgrey, lightslategrey, slategrey, darkslategrey. Therefore, the keywords  lightgrey, darkgray, gray, ddimgray, lightslategray, slategray, darkslategray can be used instead because they refer to the same colors

Here is a table of the CSS 3 colors, source(http://en.wikipedia.org/wiki/Web_colors#X11_color_names):

Color Keyword Hex code
R G B
Decimal code
R G B
Red colors
indianred CD 5C 5C 205 92 92
lightcoral F0 80 80 240 128 128
salmon FA 80 72 250 128 114
darksalmon E9 96 7A 233 150 122
lightsalmon FF A0 7A 255 160 122
crimson DC 14 3C 220 20 60
red FF 00 00 255 0 0
firebrick B2 22 22 178 34 34
darkred 8B 00 00 139 0 0
Pink colors
pink FF C0 CB 255 192 203
lightpink FF B6 C1 255 182 193
hotpink FF 69 B4 255 105 180
deeppink FF 14 93 255 20 147
mediumvioletred C7 15 85 199 21 133
palevioletred DB 70 93 219 112 147
Orange colors
coral FF 7F 50 255 127 80
tomato FF 63 47 255 99 71
orangered FF 45 00 255 69 0
darkorange FF 8C 00 255 140 0
orange FF A5 00 255 165 0
Yellow colors
gold FF D7 00 255 215 0
yellow FF FF 00 255 255 0
lightyellow FF FF E0 255 255 224
lemonchiffon FF FA CD 255 250 205
lightgoldenrodyellow FA FA D2 250 250 210
papayawhip FF EF D5 255 239 213
moccasin FF E4 B5 255 228 181
peachpuff FF DA B9 255 218 185
palegoldenrod EE E8 AA 238 232 170
khaki F0 E6 8C 240 230 140
darkkhaki BD B7 6B 189 183 107
Purple colors
lavender E6 E6 FA 230 230 250
thistle D8 BF D8 216 191 216
plum DD A0 DD 221 160 221
violet EE 82 EE 238 130 238
orchid DA 70 D6 218 112 214
fuchsia FF 00 FF 255 0 255
magenta FF 00 FF 255 0 255
mediumorchid BA 55 D3 186 85 211
mediumpurple 93 70 DB 147 112 219
blueviolet 8A 2B E2 138 43 226
darkviolet 94 00 D3 148 0 211
darkorchid 99 32 CC 153 50 204
darkmagenta 8B 00 8B 139 0 139
purple 80 00 80 128 0 128
indigo 4B 00 82 75 0 130
slateblue 6A 5A CD 106 90 205
darkslateblue 48 3D 8B 72 61 139
mediumslateblue 7B 68 EE 123 104 238
Color Keyword Hex code
R G B
Decimal code
R G B
Green colors
greenyellow AD FF 2F 173 255 47
chartreuse 7F FF 00 127 255 0
lawngreen 7C FC 00 124 252 0
lime 00 FF 00 0 255 0
limegreen 32 CD 32 50 205 50
palegreen 98 FB 98 152 251 152
lightgreen 90 EE 90 144 238 144
mediumspringgreen 00 FA 9A 0 250 154
springgreen 00 FF 7F 0 255 127
mediumseagreen 3C B3 71 60 179 113
seagreen 2E 8B 57 46 139 87
forestgreen 22 8B 22 34 139 34
green 00 80 00 0 128 0
darkgreen 00 64 00 0 100 0
yellowgreen 9A CD 32 154 205 50
olivedrab 6B 8E 23 107 142 35
olive 80 80 00 128 128 0
darkolivegreen 55 6B 2F 85 107 47
mediumaquamarine 66 CD AA 102 205 170
darkseagreen 8F BC 8F 143 188 143
lightseagreen 20 B2 AA 32 178 170
darkcyan 00 8B 8B 0 139 139
teal 00 80 80 0 128 128
Blue/Cyan colors
aqua 00 FF FF 0 255 255
cyan 00 FF FF 0 255 255
lightcyan E0 FF FF 224 255 255
paleturquoise AF EE EE 175 238 238
aquamarine 7F FF D4 127 255 212
turquoise 40 E0 D0 64 224 208
mediumturquoise 48 D1 CC 72 209 204
darkturquoise 00 CE D1 0 206 209
cadetblue 5F 9E A0 95 158 160
steelblue 46 82 B4 70 130 180
lightsteelblue B0 C4 DE 176 196 222
powderblue B0 E0 E6 176 224 230
lightblue AD D8 E6 173 216 230
skyblue 87 CE EB 135 206 235
lightskyblue 87 CE FA 135 206 250
deepskyblue 00 BF FF 0 191 255
dodgerblue 1E 90 FF 30 144 255
cornflowerblue 64 95 ED 100 149 237
royalblue 41 69 E1 65 105 225
blue 00 00 FF 0 0 255
mediumblue 00 00 CD 0 0 205
darkblue 00 00 8B 0 0 139
navy 00 00 80 0 0 128
midnightblue 19 19 70 25 25 112
Color Keyword Hex code
R G B
Decimal code
R G B
Brown colors
cornsilk FF F8 DC 255 248 220
blanchedalmond FF EB CD 255 235 205
bisque FF E4 C4 255 228 196
navajowhite FF DE AD 255 222 173
wheat F5 DE B3 245 222 179
burlywood DE B8 87 222 184 135
tan D2 B4 8C 210 180 140
rosybrown BC 8F 8F 188 143 143
sandybrown F4 A4 60 244 164 96
goldenrod DA A5 20 218 165 32
darkgoldenrod B8 86 0B 184 134 11
peru CD 85 3F 205 133 63
chocolate D2 69 1E 210 105 30
saddlebrown 8B 45 13 139 69 19
sienna A0 52 2D 160 82 45
brown A5 2A 2A 165 42 42
maroon 80 00 00 128 0 0
White colors
white FF FF FF 255 255 255
snow FF FA FA 255 250 250
honeydew F0 FF F0 240 255 240
mintcream F5 FF FA 245 255 250
azure F0 FF FF 240 255 255
aliceblue F0 F8 FF 240 248 255
ghostwhite F8 F8 FF 248 248 255
whitesmoke F5 F5 F5 245 245 245
seashell FF F5 EE 255 245 238
beige F5 F5 DC 245 245 220
oldlace FD F5 E6 253 245 230
floralwhite FF FA F0 255 250 240
ivory FF FF F0 255 255 240
antiquewhite FA EB D7 250 235 215
linen FA F0 E6 250 240 230
lavenderblush FF F0 F5 255 240 245
mistyrose FF E4 E1 255 228 225
Grey colors
gainsboro DC DC DC 220 220 220
lightgrey D3 D3 D3 211 211 211
silver C0 C0 C0 192 192 192
darkgray A9 A9 A9 169 169 169
gray 80 80 80 128 128 128
dimgray 69 69 69 105 105 105
lightslategray 77 88 99 119 136 153
SlateGray 70 80 90 112 128 144
darkslategray 2F 4F 4F 47 79 79
black 00 00 00 0 0 0

Example 2: <p style="font-size:35px;font-family:'Times New Roman';color:crimson">the quick brown fox jumps over the lazy dog</p>

 

System Color Keywords

The system color keywords refer to the colors used by the operating system in its GUI. These keywords make it possible to create a web page with an interface that mimics the viewer's operating system GUI. CSS 2.1 specifies the following system color keywords (source: http://www.w3.org/TR/CSS2/ui.html#system-colors).

ActiveBorder
Active window border.
ActiveCaption
Active window caption background color.
AppWorkspace
Background color of multiple document interface.
Background
Desktop background color.
ButtonFace
Face color for three-dimensional display elements.
ButtonHighlight
Highlight color for three-dimensional display elements (for edges facing away from the light source).
ButtonShadow
Shadow color for three-dimensional display elements.
ButtonText
Text on push buttons.
CaptionText
Text in caption, size box, and scrollbar arrow box.
GrayText
Grayed (disabled) text. This color is set to #000 if the current display driver does not support a solid gray color.
Highlight
Item(s) selected in a control background color.
HighlightText
Text of item(s) selected in a control.
InactiveBorder
Inactive window border.
InactiveCaption
Inactive window caption background color.
InactiveCaptionText
Color of text in an inactive caption.
InfoBackground
Background color for tooltip controls.
InfoText
Text color for tooltip controls.
Menu
Menu background color.
MenuText
Text in menus.
Scrollbar
Scroll bar gray area.
ThreeDDarkShadow
Dark shadow for three-dimensional display elements.
ThreeDFace
Face color for three-dimensional display elements.
ThreeDHighlight
Highlight color for three-dimensional display elements.
ThreeDLightShadow
Light color for three-dimensional display elements (for edges facing the light source).
ThreeDShadow
Dark shadow for three-dimensional display elements.
Window
Window background color.
WindowFrame
Window frame.
WindowText
Text in windows.

Example 3: <p style="font-size:35px;font-family:'Times New Roman';color:WindowText">the quick brown fox jumps over the lazy dog</p>

Hexadecimal Notation

A hexadecimal notation color value consists of the pound sign (#) followed by six or three hexadecimal digits - example: #344da4. The first two digits (from the left) represent red, the next two digits represent green and the last two represent blue.

Since each two digit pair is in hexadecimal, the maximum value is ff or 255 in decimal. Thus, each of the three primary colors - red, green, blue are represented by 256 different levels.

For example, #344da4 represents a red level of 34 hexadecimal or 52 decimal, a green level of 4d hexadecimal or 77 decimal and a blue level of a4 hexadecimal or 164 decimal. A level of 255 represents the strongest level of a particular color and a level of 0 represents the weakest level - complete absence of the color.

Numbers can be converted from hexadecimal to decimal and vice versa using a calculator.

Example 4: <p style="font-size:35px;font-family:'Times New Roman';color:#FF7F50">coral the quick brown fox jumps over the lazy dog</p>

In the above example the color in decimal rgb notation is rgb(255,127,80).

A hexadecimal notation value can be shortened if the value of each primary color consists of a repeated number or letter.

Example 5: <p style="font-size:35px;font-family:'Times New Roman';color:#AD9"> the quick brown fox jumps over the lazy dog</p>

In the above example, the color value can be expanded to #AADD99 which is equivalent to rgb(170,221,153).

Functional RGB Notation

The Functional RGB Notation consists of a decimal number triplet of either percentages or integers. The triplet represents the three primary colors red, green and blue.

Decimal Functional RGB Notation

In this notation the rgb triplet is specified as three comma separated decimal numbers enclosed in parentheses. The range of values is 0 to 255. Here is an example of the functional rgb notation using a decimal number triplet: rgb(156,45,56). In this example the level of red is 156, green is 45 and blue is 56.

Example 6: <p style="font-size:35px;font-family:'Times New Roman';color:rgb(255,105,180)">the quick brown fox jumps over the lazy dog</p>

 
Percentage Functional RGB Notation

In this notation the rgb triplet is specified as three comma separated decimal percentages enclosed in parentheses. The range of values is 0% to 100%, decimal values (example 10.5%) are allowed. Any percentage greater than 100% is considered equal to 100%. Example: (350% is considered 100%). The color in the previous decimal functional rgb notation can be converted to percentage function rgb notation using the formula:

(primary color value ÷ 255) × 100

Only one decimal place is necessary since the rounded-off computed value can represent all the numbers from 0 to 255.

Example 7: <p style="font-size:35px;font-family:'Times New Roman';color:rgb(100%,49.8%,31.3%)">coral the quick brown fox jumps over the lazy dog</p>

In the above example, the color value can also be expressed as rgb(255, 127,80)

Friday, September 5, 2008

CSS Values - Percentages

A CSS percentage value is a real number followed by a percent sign. Percentage values can be negative or decimal. There should be no character between the real number and the percentage symbol.
Example: #nav {width: 80%}
Properties vary on the restrictions they place on the kind of percentage values that they accept (only positive or less-than-or-equal-to 100%).

CSS Values - Numbers

A CSS number value can be positive or negative, a whole number or a decimal number (example: 2.4). However, properties can restrict the kind of numbers that they accept - some properties do not accept negative numbers and so on.
Some properties accept plain numbers whereas others accept numbers combined with a length unit.

Types of CSS Property Values

There are many types of values that a CSS property can have - keywords, color values, number values, percentage values, length values strings and URLs.

Wednesday, September 3, 2008

The Cascade

When two or more CSS declarations target the same element and have the same specificity, the CSS declaration that is finally applied is based on the Cascade. The Cascade is the process by which the web browser selects the property that has to be applied to an element.

The Cascade Process

  1. For a particular element, all the CSS declarations that apply to it are found.
  2. All the declarations by origin - author, user and user agent are sorted. The author's styles have the highest weight, the user's styles come second and the user-agent's (browser's) styles have the lowest weight.
    • Among author styles, inline style declarations (loaded with the style attribute) have the highest weight.
    • Next in weight are the styles loaded with the <style> element and the <link> element. In the (X)HTML document, the styles that appear later get higher weight (whether loaded by <style> or by <link>). Therefore, if the <link> element (that links a style sheet) comes after the <style> element, then the external CSS declarations will have higher weight. If the order is switched, then the converse is true.
    • In the <style> element, the @import directive must appear before any CSS rules. The CSS declarations loaded from style sheets by @import directives in the <style> element are lower in weight than any CSS declarations that are directly contained in the element. This is because these declarations are considered to come before the other CSS rules in the <style> element.
    • In external style sheets (loaded with the <link> tag), if there are any CSS styles loaded with the @import directive, those styles are considered to have come before any later styles, and therefore, are of lower weight.
  3. All the declarations that apply to the element are sorted by specificity - higher the specificity, higher the weight.
  4. If any declarations from a particular origin have the same specificity, the declarations are sorted by the order in which they appear - declarations that appear later have higher weight.
  5. Among author's styles, Important Declarations have the highest weight regardless of specificity. The same goes for user styles and user agent styles. However, Important Declarations in user styles override any conflicting Important Declarations in author styles and user-agent styles.
Note: Any presentational attributes that an (X)HTML document has are converted to their CSS equivalents. These are considered to be at the beginning of the author styles and have a specificity of zero. This gives them them the lowest weight among author styles. Therefore, any author CSS declarations that target the (X)HTML element will override presentational attribute styling.

Property Inheritance

Inheritance is the system by which elements automatically obtain certain properties from their ancestor elements. For example if the declaration color: red; is applied to a div element, then that declaration will also apply to p elements that are within it.
Inherited properties have no specificity at all, not even zero. An inherited property is overridden by values applied to the property using the universal selector (*). This is because the universal selector has a specificity of zero.
Most of the properties that apply to the "box" of an element such as borders, margins, padding and backgrounds, are not inherited by descendants of that element.

Important Declarations - !important

An Important Declaration is one which has the string !important inserted before the terminating semi-colon of the declaration.
Example: #main {color: black !important;}
An Important Declaration has the highest weight among CSS declarations. An Important Declaration will outweigh inline style declarations and internal stylesheet declarations too.
If more than one Important Declaration targets a particular element, then the more specific selector will be applied.

Tuesday, September 2, 2008

Selector Specificity

When two or more CSS rules target the same (X)HTML document element, the style that is applied to the element is determined by a system called selector specificity. Selector specificity is just a system to calculate how specific a particular CSS selector is. Among two or more CSS selectors, the most specific selector is the one that is applied to the element.

Calculating Specificity

Inline styles have the highest specificity. Second highest in specificity are ID selectors, third are Class selectors, Pseudo-Class selectors and Attribute selectors and last are Element Type selectors and Pseudo-Element selectors.
A selector chain with more than one selector of a specific kind is more specific than a selector chain that has only one or fewer selectors of that kind. An example will make this clear. Assume that the following two CSS selector chains target the same (X)HTML element:
img { float: right; }
div p img { float: left; }

The second CSS rule is more specific because it has more Element Type selectors. Therefore, the second CSS rule will be applied to the element.
When determining the specificity of CSS selectors, it is convenient to represent the specificity as four comma separated numbers - 0,0,0,0 . Each number represents one kind of selector - starting from the left and with the highest specificity - inline styles, and next, ID selectors, and next, Class, Pseudo-Class Selectors and Attribute selectors, and last, with the lowest specificity, Element Type selectors and Pseudo-Element selectors. Each instance of a particular kind of selector is calculated as one point.
Example:
#menu ul li a:link
The selector has one ID selector - #menu, therefore 1 should be added to the third digit (from the right) - 0,1,0,0 . The selector has one Pseudo-Class selector - :link, therefore 1 should be added to the second digit (from the right) - 0,1,1,0 . The selector has three Element Type selectors - ul li a , therefore 3 should be added to the first digit (from the right) - 0,1,1,3 . The element does not have any inline styles therefore the last digit is zero.
The universal selector (*) has a specificity of zero, but this does not mean it has no specificity. Combinators - the space character, the greater-than symbol, the tilde and the plus sign - have no effect on specificity.

Monday, September 1, 2008

Pseudo-Element and Pseudo-Class Selectors

There are some selectors that are not names of (X)HTML elements. They are not classes or ID's either nor are they attribute names or attribute values. These selectors are pseudo-element selectors and pseudo-class selectors.

Pseudo-element selectors

These are CSS selectors that target "virtual elements" - smaller parts of elements. In CSS2, pseudo-element selectors begin with a colon (:). In CSS3, pseudo-element selectors begin with two colons (::). These are the pseudo-elements: :first-letter , :first-line , :before , :after and ::selection . Pseudo-elements should always appear at the end of a selector chain. Therefore, there can be only one pseudo-element in a selector.

Pseudo-class selectors

These are CSS selectors that target parts of an (X)HTML element based on its dynamic state, language or position in the (X)HTML tree. Unlike pseudo-elements, pseudo-classes can appear anywhere in the selector chain. Pseudo-classes begin with a colon (:). These are the widely supported pseudo-classes:
:link , :visited , :focus , :hover , :active , :lang() and :first-child .