CSS Syntax

Syntax

The CSS syntax is made up of three parts: a selector, a property and a value:

selector {property: value}

The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon and surrounded by curly braces:

body {color: black}

If  the value is multiple words, put quotes around the value:

p {font-family: "sans serif"}

Note: If you wish to specify more than one property, you must separate each property with a semi-colon. The example below shows how to define a center aligned paragraph, with a red text color:

p {text-align:center;color:red}

To make the style definitions more readable, you can describe one property on each line, like this:

p
{
text-align: center;
color: black;
font-family: arial
}

Grouping

You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. Each header element will be green:

h1,h2,h3,h4,h5,h6 
{
color: green
}

The class Selector

With the class selector you can define different styles for the same type of HTML element. Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:

p.right {text-align: right}
p.center {text-align: center}

You have to use the class attribute in your HTML document:

<p class="right">
This paragraph will be right-aligned.
</p>
<p class="center">
This paragraph will be center-aligned.
</p>

Note: Only one class attribute can be specified per HTML element! The example below is wrong:

<p class="right" class="center">
This is a paragraph.
</p>

You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned:

.center {text-align: center}

In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector:  

<h1 class="center">
This heading will be center-aligned
</h1>
<p class="center">
This paragraph will also be center-aligned.
</p> 

The id Selector

The id selector is different from the class selector! While a class selector may apply to SEVERAL elements on a page, an id selector always applies to only ONE element.

An ID attribute must be unique within the document.

The style rule below will match a p element that has the id value "para1":

p#para1
{
text-align: center;
color: red
}

The style rule below will match the first element that has the id value "wer345":

*#wer345 {color: green}

The rule above will match this h1 element:

<h1 id="wer345">Some text</h1>

The style rule below will match a p element that has the id value "wer345":

p#wer345 {color: green}

The rule above will not match this h2 element:

<h2 id="wer345">Some text</h2>

CSS Comments

You can insert comments in CSS to explain your code, which can help you when you edit the source code at a later date. A comment will be ignored by the browser. A CSS comment begins with "/*", and ends with "*/", like this:

/* This is a comment */
p
{
text-align: center;
/* This is another comment */
color: black;
font-family: arial
}


CSS How To...

Examples

How to Insert a Style Sheet

When a browser reads a style sheet, it will format the document according to it. There are three ways of inserting a style sheet:

External Style Sheet

An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section:

<head>
<link rel="stylesheet" type="text/css"
href="mystyle.css" />
</head>

The browser will read the style definitions from the file mystyle.css, and format the document according to it.

An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below:

hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}

NOTE: Do NOT leave spaces between the property value and the units! If you use "margin-left: 20 px" instead of "margin-left: 20px" it will only work properly in IE6 but it will not work in Mozilla or Netscape.

Internal Style Sheet

An internal style sheet should be used when a single document has a unique style. You define internal styles in the head section by using the <style> tag, like this:

<head>
<style type="text/css">
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}
</style>
</head>

The browser will now read the style definitions, and format the document according to it.

Note: A browser normally ignores unknown tags. This means that an old browser that does not support styles, will ignore the <style> tag, but the content of the <style> tag will be displayed on the page. It is possible to prevent an old browser from displaying the content by hiding it in the HTML comment element: 

<head>
<style type="text/css">
<!--
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}
-->
</style>
</head>

Inline Styles

An inline style loses many of the advantages of style sheets by mixing content with presentation. Use this method sparingly, such as when a style is to be applied to a single occurrence of an element.

To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example shows how to change the color and the left margin of a paragraph:

<p style="FONT-SIZE:10px; 
     COLOR: sienna; margin-left: 20px">
This is a paragraph
</p>

Multiple Style Sheets

If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet. 

For example, an external style sheet has these properties for the h3 selector:

h3 
{
color: red;
text-align: left;
font-size: 8pt
}

And an internal style sheet has these properties for the h3 selector:

h3 
{
text-align: right; 
font-size: 20pt
}

If the page with the internal style sheet also links to the external style sheet the properties for h3 will be:

color: red; 
text-align: right; 
font-size: 20pt

The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the internal style sheet.


CSS Background Properties

CSS Background properties define the background effects of an element.

Examples

Set the background color
This example demonstrates how to set the background color for an element.

Set an image as the background
This example demonstrates how to set an image as the background.

How to repeat a background image
This example demonstrates how to repeat a background image only vertically.

How to place the background image
This example demonstrates how to place the image on the page.

How to set a fixed background image
This example demonstrates how to set a fixed background image. The image will not scroll with the rest of the page.

All the background properties in one declaration
This example demonstrates how to use the shorthand property for setting all of the background properties in one declaration.

CSS Background

The Background properties allow you to control the background color of an element, set an image as the background, repeat a background image vertically or horizontally, and position an image on a page.

Background Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
background A shorthand property for setting all background properties in one declaration background-color
background-image
background-repeat background-attachment background-position
6.0 4.0 CSS1
background-attachment Sets whether a background image is fixed or scrolls with the rest of the page scroll
fixed
6.0 4.0 CSS1
background-color Sets the background color of an element color-rgb
color-hex
color-name
transparent
4.0 4.0 CSS1
background-image Sets an image as the background url
none
4.0 4.0 CSS1
background-position Sets the starting position of a background image top left
top center
top right
center left
center center
center right
bottom left
bottom center
bottom right
x-% y-%
x-pos y-pos
6.0 4.0 CSS1
background-repeat Sets if/how a background image will be repeated repeat
repeat-x
repeat-y
no-repeat
4.0 4.0 CSS1


CSS Text Properties

CSS Text properties define the appearance of text.

Examples

Set the color of the text
This example demonstrates how to set the color of the text.

Set the background-color of the text
This example demonstrates how to set the background-color of a part of the text.

Specify the space between characters
This example demonstrates how to increase or decrease the space between characters.

Align the text
This example demonstrates how to align the text.

Decorate the text
This example demonstrates how to add decoration to text.

Indent text
This example demonstrates how to indent the first line of a paragraph.

Control the letters in a text
This example demonstrates how to control the letters in a text.

CSS Text

Text properties allow you to control the appearance of text. It is possible to change the color of a text, increase or decrease the space between characters in a text, align a text, decorate a text, indent the first line in a text, and more.

Text Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Possible Values NN IE W3C
color Sets the color of a text color 4.0 3.0 CSS1
direction Sets the text direction ltr
rtl
    CSS2
letter-spacing Increase or decrease the space between characters normal
length
6.0 4.0 CSS1
text-align Aligns the text in an element left
right
center
justify
4.0 4.0 CSS1
text-decoration Adds decoration to text none
underline
overline
line-through
blink
4.0 4.0 CSS1
text-indent Indents the first line of text in an element length
%
4.0 4.0 CSS1
text-shadow   none
color
length
     
text-transform Controls the letters in an element none
capitalize
uppercase
lowercase
4.0 4.0 CSS1
unicode-bidi   normal
embed
bidi-override
  5.0 CSS2
white-space Sets how white space inside an element is handled normal
pre
nowrap
4.0 5.5 CSS1
word-spacing Increase or decrease the space between words normal
length
6.0 6.0 CSS1


CSS Font Properties

CSS Font properties define the font in text.

Examples

Set the font of a text
This example demonstrates how to set a font of a text.

Set the size of the font
This example demonstrates how to set the size of a font.

Set the style of the font
This example demonstrates how to set the style of a font.

Set the variant of the font
This example demonstrates how to set the variant of a font.

Set the boldness of the font
This example demonstrates how to set the boldness of a font.

All the font properties in one declaration
This example demonstrates how to use the shorthand property for setting all of the font properties in one declaration.

CSS Fonts

The Font properties allow you to change the font family, boldness, size, and the style of a text.

Notes - Useful Tips

Fonts are identified by their name in CSS1. Note that if a browser does not support the font that is specified, it will use a default font.

Font Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
font
A shorthand property for setting all of the properties for a font in one declaration font-style
font-variant
font-weight
font-size/line-height
font-family
caption
icon
menu
message-box
small-caption
status-bar
4.0 4.0 CSS1
font-family
A prioritized list of font family names and/or generic family names for an element family-name
generic-family
4.0 3.0 CSS1
font-size
Sets the size of a font xx-small
x-small
small
medium
large
x-large
xx-large
smaller
larger
length
%
4.0 3.0 CSS1
font-size-adjust Specifies an aspect value for an element that will preserve the x-height of the first-choice font none
number
    CSS2
font-stretch Condenses or expands the current font-family normal
wider
narrower
ultra-condensed
extra-condensed
condensed
semi-condensed
semi-expanded
expanded
extra-expanded
ultra-expanded
    CSS2
font-style
Sets the style of the font normal
italic
oblique
4.0 4.0 CSS1
font-variant
Displays text in a small-caps font or a normal font normal
small-caps
6.0 4.0 CSS1
font-weight
Sets the weight of a font normal
bold
bolder
lighter
100
200
300
400
500
600
700
800
900
4.0 4.0 CSS1


CSS Border Properties

CSS Border properties define the borders around an element.

Examples

Set the style of the four borders
This example demonstrates how to set the style of the four borders.

Set different borders on each side
This example demonstrates how to set different borders on each side of the element.

Set the color of the four borders
This example demonstrates how to set the color of the four borders. It can have from one to four colors.

Set the width of the bottom border
This example demonstrates how to set the width of the bottom border.

Set the width of the left border
This example demonstrates how to set the width of the left border.

Set the width of the right border
This example demonstrates how to set the width of the right border.

Set the width of the top border
This example demonstrates how to set the width of the top border.

All the bottom border properties in one declaration
This example demonstrates a shorthand property for setting all of the properties for the bottom border in one declaration.

All the left border properties in one declaration
This example demonstrates a shorthand property for setting all of the properties for the left border in one declaration.

All the right border properties in one declaration
This example demonstrates a shorthand property for setting all of the properties for the right border in one declaration.

All the top border properties in one declaration
This example demonstrates a shorthand property for setting all of the properties for the top border in one declaration.

All the width of the border properties in one declaration
This example demonstrates a shorthand property for setting the width of the four borders in one declaration, can have from one to four values.

All the border properties in one declaration
This example demonstrates a shorthand property for setting all of the properties for the four borders in one declaration, can have from one to three values.

CSS Borders

The Border properties allow you to specify the style, color, and width of an element's border. In HTML we use tables to create borders around a text, but with the CSS Border properties we can create borders with nice effects, and it can be applied to any element.

Border Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
border A shorthand property for setting all of the properties for the four borders in one declaration border-width
border-style
border-color
4.0 4.0 CSS1
border-bottom A shorthand property for setting all of the properties for the bottom border in one declaration border-bottom-width
border-style
border-color
6.0 4.0 CSS1
border-bottom-color Sets the color of the bottom border border-color 6.0 4.0 CSS2
border-bottom-style Sets the style of the bottom border border-style 6.0 4.0 CSS2
border-bottom-width Sets the width of the bottom border thin
medium
thick
length
4.0 4.0 CSS1
border-color Sets the color of the four borders, can have from one to four colors color 6.0 4.0 CSS1
border-left A shorthand property for setting all of the properties for the left border in one declaration border-left-width
border-style
border-color
6.0 4.0 CSS1
border-left-color Sets the color of the left border  border-color 6.0 4.0 CSS2
border-left-style Sets the style of the left border border-style 6.0 4.0 CSS2
border-left-width Sets the width of the left border thin
medium
thick
length
4.0 4.0 CSS1
border-right A shorthand property for setting all of the properties for the right border in one declaration border-right-width
border-style
border-color
6.0 4.0 CSS1
border-right-color Sets the color of the right border border-color 6.0 4.0 CSS2
border-right-style Sets the style of the right border border-style 6.0 4.0 CSS2
border-right-width Sets the width of the right border thin
medium
thick
length
4.0 4.0 CSS1
border-style Sets the style of the four borders, can have from one to four styles none
hidden
dotted
dashed
solid
double
groove
ridge
inset
outset
6.0 4.0 CSS1
border-top A shorthand property for setting all of the properties for the top border in one declaration border-top-width
border-style
border-color
6.0 4.0 CSS1
border-top-color Sets the color of the top border  border-color 6.0 4.0 CSS2
border-top-style Sets the style of the top border border-style 6.0 4.0 CSS2
border-top-width Sets the width of the top border thin
medium
thick
length
4.0 4.0 CSS1
border-width A shorthand property for setting the width of the four borders in one declaration, can have from one to four values thin
medium
thick
length
4.0 4.0 CSS1


CSS Margin Properties

CSS Margin properties define the space around elements.

Examples

Set the left margin of a text
This example demonstrates how to set the left margin of a text.

Set the right margin of a text
This example demonstrates how to set the right margin of a text.

Set the top margin of a text
This example demonstrates how to set the top margin of a text.

Set the bottom margin of a text
This example demonstrates how to set the bottom margin of a text.

All the margin properties in one declaration
This example demonstrates how to set a shorthand property for setting all of the margin properties in one declaration.

CSS Margins

The Margin properties define the space around elements. It is possible to use negative values to overlap content. The top, right, bottom, and left margin can be changed independently using separate properties. A shorthand margin property can also be used to change all of the margins at once.

Browser Note: Netscape and IE give the body tag a default margin of 8px. Opera does not! Instead, Opera applies a default padding of 8px, so if one wants to adjust the margin for an entire page and have it display correctly in Opera, the body padding must be set as well!

Margin Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
margin A shorthand property for setting the margin properties in one declaration margin-top
margin-right
margin-bottom
margin-left
4.0 4.0 CSS1
margin-bottom Sets the bottom margin of an element auto
length
%
4.0 4.0 CSS1
margin-left Sets the left margin of an element auto
length
%
4.0 3.0 CSS1
margin-right Sets the right margin of an element auto
length
%
4.0 3.0 CSS1
margin-top Sets the top margin of an element auto
length
%
4.0 3.0 CSS1


CSS Padding Properties

CSS Padding properties define the space between the element border and the element content.

Examples

Set the left padding
This example demonstrates how to set the left padding of a tablecell.

Set the right padding
This example demonstrates how to set the right padding of a tablecell.

Set the top padding
This example demonstrates how to set the top padding of a tablecell.

Set the bottom padding
This example demonstrates how to set the bottom padding of a tablecell.

All the padding properties in one declaration
This example demonstrates a shorthand property for setting all of the padding properties in one declaration, can have from one to four values.

CSS Padding

The Padding properties define the space between the element border and the element content. Negative values are not allowed. The top, right, bottom, and left padding can be changed independently using separate properties. A shorthand padding property is also created to control multiple sides at once.

Padding Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
padding A shorthand property for setting all of  the padding properties in one declaration padding-top
padding-right
padding-bottom
padding-left
4.0 4.0 CSS1

padding-bottom

Sets the bottom padding of an element length
%
4.0 4.0 CSS1

padding-left

Sets the left padding of an element length
%
4.0 4.0 CSS1

padding-right

Sets the right padding of an element length
%
4.0 4.0 CSS1
padding-top Sets the top padding of an element length
%
4.0 4.0 CSS1


CSS List Properties

The List properties allow you to change between different list-item markers, set an image as a list-item marker, and set where to place a list-item marker.

Examples

The different list-item markers in unordered lists
This example demonstrates the different list-item markers in CSS.

The different list-item markers in ordered lists
This example demonstrates the different list-item markers in CSS.

Set an image as the list-item marker
This example demonstrates how to set an image as the list-item marker.

Place the list-item marker
This example demonstrates where to place the list-item marker.

All list properties in one declaration
This example demonstrates a shorthand property for setting all of the properties for a list in one declaration.

List Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
list-style A shorthand property for setting all of the properties for a list in one declaration list-style-type
list-style-position
list-style-image
6.0 4.0 CSS1
list-style-image Sets an image as the list-item marker none
url
6.0 4.0 CSS1
list-style-position Places the list-item marker in the list inside
outside
6.0 4.0 CSS1
list-style-type Sets the type of the list-item marker none
disc
circle
square
decimal
decimal-leading-zero
lower-roman
upper-roman
lower-alpha
upper-alpha
lower-greek
lower-latin
upper-latin
hebrew
armenian
georgian
cjk-ideographic
hiragana
katakana
hiragana-iroha
katakana-iroha 
4.0 4.0 CSS1
marker-offset   auto
length
    CSS2


CSS Dimension Properties

The Dimension properties allow you to control the height and width of an element. It also allows you to increase the space between two lines.

Examples

Set the height and width of an image
This example demonstrates how to set the height and width of an image.

Increase the space between lines
This example demonstrates how to increase the space between the lines.

Dimension Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
height Sets the height of an element auto
length
%
6.0 4.0 CSS1
line-height Sets the distance between lines normal
number
length
%
4.0 4.0 CSS1
max-height Sets the maximum height of an element none
length
%
    CSS2
max-width Sets the maximum width of an element none
length
%
    CSS2
min-height Sets the minimum height of an element length
%
    CSS2
min-width Sets the minimum width of an element length
%
    CSS2
width Sets the width of an element auto
%
length
  
4.0 4.0 CSS1


CSS Classification Properties

The Classification properties allow you to control how to display an element, set where an image will appear in another element, position an element relative to its normal position, position an element using an absolute value, and how to control the visibility of an element.

Examples

How to display an element
This example demonstrates how to display an element.

A simple use of the float property
Let an image float to the right in a paragraph.

An image with border and margins that floats to the right in a paragraph
Let an image float to the right in a paragraph. Add border and margins to the image.

An image with a caption that floats to the right
Let an image with a caption float to the right.

Let the first letter of a paragraph float to the left
Let the first letter of a paragraph float to the left and style the letter.

Creating a horizontal menu
Use float with a list of hyperlinks to create a horizontal menu.

Creating a homepage without tables
Use float to create a homepage with a header, footer, left content and main content.

Position:relative
This example demonstrates how to position an element relative to its normal position.

Position:absolute
This example demonstrates how to position an element using an absolute value.

How to make an element invisible
This example demonstrates how to make an element invisible. Do you want the element to show or not?

Change the cursor
This example demonstrates how to change the cursor.

Classification Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
clear Sets the sides of an element where other floating elements are not allowed left
right
both
none
4.0 4.0 CSS1
cursor Specifies the type of cursor to be displayed url
auto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help
6.0 4.0 CSS2
display Sets how/if an element is displayed none
inline
block
list-item
run-in
compact
marker
table
inline-table
table-row-group
table-header-group
table-footer-group
table-row
table-column-group
table-column
table-cell
table-caption
4.0 4.0 CSS1
float Sets where an image or a text will appear in another element left
right
none
4.0 4.0 CSS1
position Places an element in a static, relative, absolute or fixed position static
relative
absolute
fixed
4.0 4.0 CSS2
visibility Sets if an element should be visible or invisible visible
hidden
collapse
6.0 4.0 CSS2


CSS Positioning Properties

CSS Positioning properties define the position of an element.

Examples

Set the shape of an element
This example demonstrates how to set the shape of an element. The element is clipped into this shape, and displayed.

Set the left and top position of an element
This example demonstrates how to set the left and top position of an element.

Set the right and bottom position of an element
This example demonstrates how to set the right and bottom position of an element.

Overflow
This example demonstrates how to set the overflow property to specify what should happen when an element's content is too big to fit in a specified area.

Vertical align an image
This example demonstrates how to vertical align an image in a text.

Z-index
Z-index can be used to place an element "behind" another element, using Z-index priority.

Z-index
Check that the elements now have changed their Z-index priority.

CSS Positioning

The Positioning properties allow you to specify the left, right, top, and bottom position of an element. It also allows you to set the shape of an element, place an element behind another, and to specify what should happen when an element's content is too big to fit in a specified area.

Positioning Properties:

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
bottom Specifies the bottom edge of an element auto
%
length
6.0 5.0 CSS2
clip Sets the shape of an element. The element is clipped into this shape, and displayed shape
auto
6.0 4.0 CSS2
left Specifies the left edge of an element auto
%
length
4.0 4.0 CSS2
overflow
Sets what happens if the content of an element overflow its area visible
hidden
scroll
auto
6.0 4.0 CSS2
position Places an element in a static, relative, absolute or fixed position static
relative
absolute
fixed
4.0 4.0 CSS2
right Specifies the right edge of an element auto
%
length
  5.0 CSS2
top Sets how far the top edge of an element is above/below the top edge of the parent element auto
%
length
4.0 4.0 CSS2
vertical-align Sets the vertical alignment of an element baseline
sub
super
top
text-top
middle
bottom
text-bottom
length
%
4.0 4.0 CSS1
z-index Sets the stack order of an element auto
number
6.0 4.0 CSS2


CSS Pseudo-classes

Pseudo-classes are used in CSS to add different effects to some selectors, or to a part of some selectors.

Examples

Hyperlink
This example demonstrates how to add different colors to a hyperlink in a document.

Hyperlink 2
This example demonstrates how to add other styles to hyperlinks.

Syntax

The syntax of pseudo-classes:

selector:pseudo-class {property: value}

CSS classes can also be used with pseudo-classes:

selector.class:pseudo-class {property: value}

Anchor Pseudo-classes

A link that is active, visited, unvisited, or when you mouse over a link can all be displayed in different ways in a CSS-supporting browser:

a:link {color: #FF0000}     /* unvisited link */
a:visited {color: #00FF00}  /* visited link */
a:hover {color: #FF00FF}   /* mouse over link */
a:active {color: #0000FF}   /* selected link */

Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective!!

Note: a:active MUST come after a:hover in the CSS definition in order to be effective!!

Note: Pseudo-class names are not case-sensitive.

Note: IE 4 and higher supports the anchor pseudo-class. NN 4.5 and Netscape 6 support the anchor pseudo-class only partially.

Pseudo-classes and CSS Classes

Pseudo-classes can be combined with CSS classes: 

a.red:visited {color: #FF0000}
<a class="red" href="example.htm">Example</a>

If the link in the example above has been visited, it will be displayed in red.

CSS2 - The :first-child Pseudo-class

The :first-child pseudo-class matches a specified element that is the first child of another element.

In this example, the selector matches any p element that is the first child of a div element, and indents the first paragraph inside a div element:

div > p:first-child
{
text-indent:25px
}

This selector will match the first paragraph inside the div in the following HTML:

<div>
<p>
First paragraph in div.
This paragraph will be indented.
</p>
<p>
Second paragraph in div.
This paragraph will not be indented.
</p>
</div>

but it will not match the paragraph in this HTML:

<div>
<h1>Header</h1>
<p>
The first paragraph inside the div.
This paragraph will not be indented.
</p>
</div>

In this example, the selector matches any em element that is the first child of a p element, and sets the font-weight to bold for the first em inside a p element:

p:first-child em
{
font-weight:bold
}

For example, the em in the HTML below is the first child of the paragraph:

<p>I am a <em>strong</em> man.</p>

In this example, the selector matches any a element that is the first child of any element, and sets the text-decoration to none:

a:first-child
{
text-decoration:none
}

For example, the first a in the HTML below is the first child of the paragraph and will not be underlined. But the second a in the paragraph is not the first child of the paragraph and will be underlined:

<p>
Visit <a href="http://www.example.com">example</a>
and learn CSS!
Visit <a href="http://www.example.com">example</a>
and learn HTML!
</p>

CSS2 - The :lang Pseudo-class

The :lang pseudo-class allows the author to specify a language to use in a document or to use in a specified element.

In the example below, the rule sets the type of quotation marks for an HTML document that is in Norwegian:

html:lang(no)
{
quotes: ' ' ' '
}

In the next example, the rule sets the type of quotation marks for blockquote elements:

blockquote:lang(no)
{
quotes: ' ' ' '
}

Pseudo-classes

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Pseudo-classes NN IE W3C Purpose
active   4.0 CSS1 Adds special style to a selected link
hover 7.1 4.0 CSS1 Adds special style to a link when you mouse over  it
link 4.0 3.0 CSS1 Adds special style to an unvisited link
visited 4.0 3.0 CSS1 Adds special style to a visited link
:first-child 7.0   CSS2 Adds special style to an element that is the first child of some other element
:lang     CSS2

Allows the author to specify a language to use in a specified element



CSS Pseudo-elements

Pseudo-elements are used in CSS to add different effects to some selectors, or to a part of some selectors.

Examples

Make the first letter special
This example demonstrates how to add special style to the first letter in a text.

Make the first line special
This example demonstrates how to add special style to the first line in a text.

Syntax

The syntax of pseudo-elements:

selector:pseudo-element {property: value}

CSS classes can also be used with pseudo-elements:

selector.class:pseudo-element {property: value}

The :first-line Pseudo-element

The "first-line" pseudo-element is used to add special styles to the first line of the text in a selector:

p {font-size: 12pt}
p:first-line {color: #0000FF; 
                  font-variant: small-caps}
<p>Two or more lines of text</p>

The output could be something like this:

Some text that ends
up on two or more lines

In the example above the browser displays the first line formatted according to the "first-line" pseudo element. Where the browser breaks the line depends on the size of the browser window.

Note: The "first-line" pseudo-element can only be used with block-level elements.

Note: The following properties apply to the "first-line" pseudo-element: 

  • font properties
  • color properties 
  • background properties
  • word-spacing
  • letter-spacing
  • text-decoration
  • vertical-align
  • text-transform
  • line-height
  • clear

Note: IE 4.01 and IE 5.0 do not support the "first-line" pseudo-element, but IE 5.5 does.

Note: NN 4.5 does not support the "first-line" pseudo-element.

The :first-letter Pseudo-element

The "first-letter" pseudo-element is used to add special style to the first letter of the text in a selector:

p {font-size: 12pt}
p:first-letter {font-size: 200%; float: left}
<p>The first words of an article.</p>

The output could be something like this:

___
  |  he first 
  |  words of an
article.

Note: The "first-letter" pseudo-element can only be used with block-level elements.

Note: The following properties apply to the "first-letter" pseudo- element: 

  • font properties
  • color properties 
  • background properties
  • margin properties
  • padding properties
  • border properties
  • text-decoration
  • vertical-align (only if 'float' is 'none')
  • text-transform
  • line-height
  • float
  • clear

Note: IE 4.01 and IE 5.0 do not support the "first-letter" pseudo-element, but IE 5.5 does.

Note: NN 4.5 does not support the "first-letter" pseudo-element.

Pseudo-elements and CSS Classes

Pseudo-elements can be combined with CSS classes: 

p.article:first-letter {color: #FF0000}
<p class="article">A paragraph in an article</p>

The example above will make the first letter of all paragraphs with class="article" red.

Multiple Pseudo-elements

Several pseudo-elements can be combined:

p {font-size: 12pt}
p:first-letter {color: #FF0000; font-size: 200%}
p:first-line {color: #0000FF}
<p>The first words of an article</p>

The output could be something like this:

___
  |  he first 
  |  words of an
article.

In the example above the first letter of the paragraph will be red with a font size of 24pt. The rest of the first line would be blue while the rest of the paragraph would be the default color.

CSS2 - The :before Pseudo-element

The ":before" pseudo-element can be used to insert some content before an element.

The style below will play a sound before each occurrence of a header one element.

h1:before
{
content: url(beep.wav)
}


CSS2 - The :after Pseudo-element

The ":after" pseudo-element can be used to insert some content after an element.

The style below will play a sound after each occurrence of a header one element.

h1:after
{
content: url(beep.wav)
}


Pseudo-elements

NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Pseudo-elements NN IE W3C Purpose
first-letter   5.5 CSS1 Adds special style to the first letter of a text
first-line   5.5 CSS1 Adds special style to the first line of a text
:before     CSS2 Inserts some content before an element
:after     CSS2 Inserts some content after an element


CSS2 Media Types

Media Types allow you to specify how documents will be presented in different media. The document can be displayed differently on the screen, on the paper, with an aural browser, etc. 

Media Types

Some CSS properties are only designed for a certain media. For example the "voice-family" property is designed for aural user agents. Some other properties can be used for different media types. For example, the "font-size" property can be used for both screen and print media, but perhaps with different values. A document usually needs a larger font-size on a screen than on paper, and sans-serif fonts are easier to read on the screen, while serif fonts are easier to read on paper.

The @media Rule

The @media rule allows different style rules for different media in the same style sheet.

The style in the example below tells the browser to display a 14 pixels Verdana font on the screen. But if the page is printed, it will be in a 10 pixels Times font. Notice that the font-weight is set to bold, both on screen and on paper:

<html>
<head>
<style>
@media screen
{
p.test {font-family:verdana,sans-serif; 
           font-size:14px}
}

@media print
{
p.test {font-family:times,serif; font-size:10px}
}
@media screen,print
{
p.test {font-weight:bold}
}
</style>
</head>
<body>
....
</body>
</html>

See it yourself ! If you use IE 5.0 (or higher) and print this page, you will see that the paragraph under "Media Types" will be displayed in another font, and have a smaller font size than the rest of the text.

Different Media Types

Note: The media type names are not case-sensitive.

Media Type Description NN IE
all Used for all media type devices   4.0
aural Used for speech and sound synthesizers    
braille Used for braille tactile feedback devices    
embossed Used for paged braille printers    
handheld Used for small or handheld devices    
print Used for printers   4.0
projection Used for projected presentations, like slides    
screen Used for computer screens   4.0
tty Used for media using a fixed-pitch character grid, like teletypes and terminals    
tv Used for television-type devices    


CSS2 Reference

The links in the "Property" column point to more useful information about the specific property.

Browser support: NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Background

On-line examples

Property Description Values NN IE W3C
background A shorthand property for setting all background properties in one declaration background-color
background-image
background-repeat background-attachment background-position
6.0 4.0 CSS1
background-attachment Sets whether a background image is fixed or scrolls with the rest of the page scroll
fixed
6.0 4.0 CSS1
background-color Sets the background color of an element color-rgb
color-hex
color-name
transparent
4.0 4.0 CSS1
background-image Sets an image as the background url
none
4.0 4.0 CSS1
background-position Sets the starting position of a background image top left
top center
top right
center left
center center
center right
bottom left
bottom center
bottom right
x-% y-%
x-pos y-pos
6.0 4.0 CSS1
background-repeat Sets if/how a background image will be repeated repeat
repeat-x
repeat-y
no-repeat
4.0 4.0 CSS1

Border

On-line examples

Property Description Values NN IE W3C
border A shorthand property for setting all of the properties for the four borders in one declaration border-width
border-style
border-color
4.0 4.0 CSS1
border-bottom A shorthand property for setting all of the properties for the bottom border in one declaration border-bottom-width
border-style
border-color
6.0 4.0 CSS1
border-bottom-color Sets the color of the bottom border border-color 6.0 4.0 CSS2
border-bottom-style Sets the style of the bottom border border-style 6.0 4.0 CSS2
border-bottom-width Sets the width of the bottom border thin
medium
thick
length
4.0 4.0 CSS1
border-color Sets the color of the four borders, can have from one to four colors color 6.0 4.0 CSS1
border-left A shorthand property for setting all of the properties for the left border in one declaration border-left-width
border-style
border-color
6.0 4.0 CSS1
border-left-color Sets the color of the left border  border-color 6.0 4.0 CSS2
border-left-style Sets the style of the left border border-style 6.0 4.0 CSS2
border-left-width Sets the width of the left border thin
medium
thick
length
4.0 4.0 CSS1
border-right A shorthand property for setting all of the properties for the right border in one declaration border-right-width
border-style
border-color
6.0 4.0 CSS1
border-right-color Sets the color of the right border border-color 6.0 4.0 CSS2
border-right-style Sets the style of the right border border-style 6.0 4.0 CSS2
border-right-width Sets the width of the right border thin
medium
thick
length
4.0 4.0 CSS1
border-style Sets the style of the four borders, can have from one to four styles none
hidden
dotted
dashed
solid
double
groove
ridge
inset
outset
6.0 4.0 CSS1
border-top A shorthand property for setting all of the properties for the top border in one declaration border-top-width
border-style
border-color
6.0 4.0 CSS1
border-top-color Sets the color of the top border  border-color 6.0 4.0 CSS2
border-top-style Sets the style of the top border border-style 6.0 4.0 CSS2
border-top-width Sets the width of the top border thin
medium
thick
length
4.0 4.0 CSS1
border-width A shorthand property for setting the width of the four borders in one declaration, can have from one to four values thin
medium
thick
length
4.0 4.0 CSS1

Classification

On-line examples

Property Description Values NN IE W3C
clear Sets the sides of an element where other floating elements are not allowed left
right
both
none
4.0 4.0 CSS1
cursor Specifies the type of cursor to be displayed url
auto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help
6.0 4.0 CSS2
display Sets how/if an element is displayed none
inline
block
list-item
run-in
compact
marker
table
inline-table
table-row-group
table-header-group
table-footer-group
table-row
table-column-group
table-column
table-cell
table-caption
4.0 4.0 CSS1
float Sets where an image or a text will appear in another element left
right
none
4.0 4.0 CSS1
position Places an element in a static, relative, absolute or fixed position static
relative
absolute
fixed
4.0 4.0 CSS2
visibility Sets if an element should be visible or invisible visible
hidden
collapse
6.0 4.0 CSS2

Dimension

On-line examples

Property Description Values NN IE W3C
height Sets the height of an element auto
length
%
6.0 4.0 CSS1
line-height Sets the distance between lines normal
number
length
%
4.0 4.0 CSS1
max-height Sets the maximum height of an element none
length
%
6.0   CSS2
max-width Sets the maximum width of an element none
length
%
6.0   CSS2
min-height Sets the minimum height of an element length
%
6.0   CSS2
min-width Sets the minimum width of an element length
%
6.0   CSS2
width Sets the width of an element auto
%
length
  
4.0 4.0 CSS1

Font

On-line examples

Property Description Values NN IE W3C
font
A shorthand property for setting all of the properties for a font in one declaration font-style
font-variant
font-weight
font-size/line-height
font-family
caption
icon
menu
message-box
small-caption
status-bar
4.0 4.0 CSS1
font-family
A prioritized list of font family names and/or generic family names for an element family-name
generic-family
4.0 3.0 CSS1
font-size
Sets the size of a font xx-small
x-small
small
medium
large
x-large
xx-large
smaller
larger
length
%
4.0 3.0 CSS1
font-size-adjust Specifies an aspect value for an element that will preserve the x-height of the first-choice font none
number
    CSS2
font-stretch Condenses or expands the current font-family normal
wider
narrower
ultra-condensed
extra-condensed
condensed
semi-condensed
semi-expanded
expanded
extra-expanded
ultra-expanded
    CSS2
font-style
Sets the style of the font normal
italic
oblique
4.0 4.0 CSS1
font-variant
Displays text in a small-caps font or a normal font normal
small-caps
6.0 4.0 CSS1
font-weight
Sets the weight of a font normal
bold
bolder
lighter
100
200
300
400
500
600
700
800
900
4.0 4.0 CSS1

Generated Content

Property Description Values NN IE W3C
content Generates content in a document. Used with the :before and :after pseudo-elements string
url
counter(name)
counter(name, list-style-type)
counters(name, string)
counters(name, string, list-style-type)
attr(X)
open-quote
close-quote
no-open-quote
no-close-quote
6.0   CSS2
counter-increment Sets how much the counter increments on each occurrence of a selector  none
identifier number
    CSS2
counter-reset Sets the value the counter is set to on each occurrence of a selector none
identifier number
    CSS2
quotes Sets the type of quotation marks none
string string
6.0   CSS2

List and Marker

On-line examples

Property Description Values NN IE W3C
list-style A shorthand property for setting all of the properties for a list in one declaration list-style-type
list-style-position
list-style-image
6.0 4.0 CSS1
list-style-image Sets an image as the list-item marker none
url
6.0 4.0 CSS1
list-style-position Sets where the list-item marker is placed in the list inside
outside
6.0 4.0 CSS1
list-style-type Sets the type of the list-item marker none
disc
circle
square
decimal
decimal-leading-zero
lower-roman
upper-roman
lower-alpha
upper-alpha
lower-greek
lower-latin
upper-latin
hebrew
armenian
georgian
cjk-ideographic
hiragana
katakana
hiragana-iroha
katakana-iroha 
4.0 4.0 CSS1
marker-offset   auto
length
7.0   CSS2

Margin

On-line examples

Property Description Values NN IE W3C
margin A shorthand property for setting the margin properties in one declaration margin-top
margin-right
margin-bottom
margin-left
4.0 4.0 CSS1

margin-bottom

Sets the bottom margin of an element auto
length
%
4.0 4.0 CSS1

margin-left

Sets the left margin of an element auto
length
%
4.0 3.0 CSS1

margin-right

Sets the right margin of an element auto
length
%
4.0 3.0 CSS1
margin-top Sets the top margin of an element auto
length
%
4.0 3.0 CSS1

Outlines

Property Description Values NN IE W3C
outline A shorthand property for setting all the outline properties in one declaration outline-color
outline-style
outline-width
    CSS2
outline-color Sets the color of the outline around an element color
invert
    CSS2
outline-style Sets the style of the outline around an element none
dotted
dashed
solid
double
groove
ridge
inset
outset
    CSS2
outline-width Sets the width of the outline around an element thin
medium
thick
length
    CSS2

Padding

On-line examples

Property Description Values NN IE W3C
padding A shorthand property for setting all of  the padding properties in one declaration padding-top
padding-right
padding-bottom
padding-left
4.0 4.0 CSS1

padding-bottom

Sets the bottom padding of an element length
%
4.0 4.0 CSS1

padding-left

Sets the left padding of an element length
%
4.0 4.0 CSS1

padding-right

Sets the right padding of an element length
%
4.0 4.0 CSS1
padding-top Sets the top padding of an element length
%
4.0 4.0 CSS1

Positioning

On-line examples

Property Description Values NN IE W3C
bottom Sets how far the bottom edge of an element is above/below the bottom edge of the parent element auto
%
length
6.0 5.0 CSS2
clip Sets the shape of an element. The element is clipped into this shape, and displayed shape
auto
6.0 4.0 CSS2
left Sets how far the left edge of an element is to the right/left of the left edge of the parent element auto
%
length
4.0 4.0 CSS2
overflow
Sets what happens if the content of an element overflow its area visible
hidden
scroll
auto
6.0 4.0 CSS2
right Sets how far the right edge of an element is to the left/right of the right edge of the parent element auto
%
length
6.0 5.5 CSS2
top Sets how far the top edge of an element is above/below the top edge of the parent element auto
%
length
4.0 4.0 CSS2
vertical-align Sets the vertical alignment of an element baseline
sub
super
top
text-top
middle
bottom
text-bottom
length
%
4.0 4.0 CSS1
z-index Sets the stack order of an element auto
number
6.0 4.0 CSS2

Table

Property Description Values NN IE W3C
border-collapse Sets the border model of a table collapse
separate
7.0  5.5 CSS2
border-spacing Sets the distance between the borders of adjacent cells (only for the "separated borders" model) length length 6.0   CSS2
caption-side Sets the position of the caption according to the table top
bottom
left
right
6.0   CSS2
empty-cells Sets whether cells with no visible content should have borders or not (only for the "separated borders" model)  show
hide
6.0   CSS2
table-layout Sets the algorithm used to lay out the table auto
fixed
6.0 5.5 CSS2

Text

On-line examples

Property Description Possible Values NN IE W3C
color Sets the color of a text color 4.0 3.0 CSS1
direction Sets the text direction ltr
rtl
    CSS2
letter-spacing Increase or decrease the space between characters normal
length
6.0 4.0 CSS1
text-align Aligns the text in an element left
right
center
justify
4.0 4.0 CSS1
text-decoration Adds decoration to text none
underline
overline
line-through
blink
4.0 4.0 CSS1
text-indent Indents the first line of text in an element length
%
4.0 4.0 CSS1
text-shadow   none
color
length
     
text-transform Controls the letters in an element none
capitalize
uppercase
lowercase
4.0 4.0 CSS1
unicode-bidi   normal
embed
bidi-override
  5.0 CSS2
white-space Sets how white space inside an element is handled normal
pre
nowrap
4.0 5.5 CSS1
word-spacing Increase or decrease the space between words normal
length
6.0 6.0 CSS1


CSS2 Print Reference

Print Properties

Printing HTML documents has always been problematic. In CSS2 the print properties are added to make it easier to print from the Web.

The links in the "Property" column point to more useful information about the specific property.

Browser support: NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
orphans Sets the minimum number of lines for a paragraph that must be left at the bottom of a page number     CSS2
marks Sets what sort of marks should be rendered outside the page box none
crop
cross
     
page Sets a page type to use when displaying an element auto
identifier
    CSS2
page-break-after Sets the page-breaking behavior after an element auto
always
avoid
left
right
  4.0 CSS2
page-break-before

Sets the page-breaking behavior before an element

auto
always
avoid
left
right
  4.0 CSS2
page-break-inside Sets the page-breaking behavior inside an element auto
avoid
    CSS2
size Sets the orientation and size of a page auto
portrait
landscape
     
widows Sets the minimum number of lines for a paragraph that must be left at the top of a page number     CSS2


CSS2 Aural Reference

Aural Style Sheets

Aural style sheets use a combination of speech synthesis and sound effects to make the user listen to information, instead of reading information.

Aural presentation can be used:

  • by blind people
  • to help users learning to read
  • to help users who have reading problems
  • for home entertainment
  • in the car
  • by print-impaired communities

The aural presentation converts the document to plain text and feed this to a screen reader (a program that reads all the characters on the screen).

An example of an Aural style sheet:

h1, h2, h3, h4
{
voice-family: male;
richness: 80;
cue-before: url("beep.au")
}

The example above will make the speech synthesizer play a sound, then speak the headers in a very rich male voice.

CSS2 Aural Reference

The links in the "Property" column point to more useful information about the specific property.

Browser support: NN: Netscape, IE: Internet Explorer, W3C: Web Standard

Property Description Values NN IE W3C
azimuth Sets where the sound/voices should come from (horizontally) angle
left-side
far-left
left
center-left
center
center-right
right
far-right
right-side
behind
leftwards
rightwards
    CSS2
cue A shorthand property for setting the cue-before and cue-after properties in one declaration cue-before
cue-after
    CSS2
cue-after Specifies a sound to be played after speaking an element's content to delimit it from other none
url
    CSS2
cue-before Specifies a sound to be played before speaking an element's content to delimit it from other none
url
    CSS2
elevation Sets where the sound/voices should come from (vertically) angle
below
level
above
higher
lower 
    CSS2
pause A shorthand property for setting the pause-before and pause-after properties in one declaration pause-before
pause-after
    CSS2
pause-after Specifies a pause after speaking an element's content time
%
    CSS2
pause-before Specifies a pause before speaking an element's content time
%
    CSS2
pitch Specifies the speaking voice frequency
x-low
low
medium
high
x-high 
    CSS2
pitch-range Specifies the variation in the speaking voice. (Monotone voice or animated voice?) number     CSS2
play-during Specifies a sound to be played while speaking an element's content auto
none
url
mix
repeat
    CSS2
richness Specifies the richness in the speaking voice. (Rich voice or thin voice?) number     CSS2
speak Specifies whether content will render aurally normal
none
spell-out
    CSS2
speak-header Specifies how to handle table headers. Should the headers be spoken before every cell, or only before a cell with a different header than the previous cell always
once
    CSS2
speak-numeral Specifies how to speak numbers digits
continuous
    CSS2
speak-punctuation Specifies how to speak punctuation characters none
code
    CSS2
speech-rate Specifies the speed of the speaking number
x-slow
slow
medium
fast
x-fast
faster
slower 
    CSS2
stress Specifies the "stress" in the speaking voice number     CSS2
voice-family A prioritized list of voice family names that contain specific voices specific-voice
generic-voice
    CSS2
volume Specifies the volume of the speaking number
%
silent
x-soft
soft
medium
loud
x-loud 
    CSS2


CSS Units

Measurements

Unit Description
% a percentage of something
in inch
cm centimeter
mm millimeter
em one em is equal to the font size of the current element
ex one ex is the x-height of a font, the x-height is usually about half the font-size
pt point (1 pt is the same as 1/72 inch)
pc pica (1 pc is the same as 12 points)
px pixels (a dot on the computer screen)

Colors

Unit Description
color_name A color name (red)
rgb(x,x,x) A rgb value (rgb(255,0,0))
rgb(y%, y%, y%) A rgb percentage value (rgb(100%,0%,0%))
#rrggbb A hex number (#ff0000).