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 .