You are on page 1of 101

What is CSS?

CSS stands for Cascading Style Sheet CSS is an extension to basic HTML that allows us to style our web pages. Styles define how to display HTML elements. Styles were added to HTML 4.0

5/1/2010

Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation semantics (that is, the look and formatting) of a document written in a markup language. CSS is designed primarily to enable the separation of document content (written in HTML or a similar markup language) from document presentation, including elements such as the layout, colors, and fonts.

5/1/2010

This separation can: improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple pages to share formatting, and reduce complexity and repetition in the structural content.

5/1/2010

With CSS, we can create a custom style elsewhere and set all its properties, give it a unique name and then tag our HTML content to apply these stylistic properties.

5/1/2010

Benefits of CSS
1. Separate content from presentation. An external style sheet can contain all the styles for our web site, then if we want to change the content we only have to edit one style sheet. This is great for a web site that contains hundreds or thousands of pages.

2. Fast loading pages. Designing CSS-based web pages will speed up the loading of our pages considerably because the styles are all contained in one style sheet.

5/1/2010

3. Small file size. CSS will reduce the file size of our html document. If we look at a web page that is designed with CSS we will notice that the size is very small compared to one designed with lots of tables. This also helps reduce load times. 4. Reduce clutter. CSS allows us to get rid of a lot of extraneous html code therefore making our site code neater and cleaner. This makes it easy to alter the code if we need to make edits.

5/1/2010

5. Save time. CSS will allow us to design our site much faster than tables are some styles we can use for every site. 6. Save money. CSS will shorten the project development process and eliminate design obstacles that occur from using tables. If designing many web sites we will be able to use the same style sheets or just make a few modifications to them. We will still charge for the design project but it won t take as long as when using tables thus making more money in a shorter time period.

5/1/2010

7. Flexibility of design. We can use pixel precision in our web site designs. margins and padding of the CSS we can easily adjust the position of our content. You can also create very modern designs that can t be duplicated with tables. For example we can use a background image for a header then place our content over it using the H1 tag for better page optimization. 8. Print friendly. When a user chooses to print a web page an alternative CSS document can be called up. This document can specify that formatting, images and navigation disappear and just the page content appear on the printed version

5/1/2010

CSS Rules Cascading Style Sheets are a set of rules that govern the display of the document through a web browser. The rules simply are lines of command codes that tell the browser how to display the information.

5/1/2010

A CSS rule has two main parts: a selector, and one or more declarations:

Basic syntax of a CSS Rule Rule

HTML, Class, Id Declaration

5/1/2010

10

The selector is normally the HTML element you want to style. The selector is also referred to as an element selector or a type selector.

5/1/2010

11

The Declaration tells the browser what to do with the Selected element. It consists of two parts: The Property, which identifies which style attribute we want to change. E.g. font type, size and color. The Value, determines how the attribute works.

5/1/2010

12

A declaration is always signaled by a { bracket and is ended with a closing }. A property is separated by a : and a ; ends each declaration.

5/1/2010

13

E.g.

5/1/2010

14

Selectors Selectors are the names that we give to our different styles. In the style definition we define how each selector should work (font, color etc.). Then, in the body of our pages, we refer to these selectors to activate the styles.

5/1/2010

15

There are three types of selectors: 1. HTML selectors 2. Class selectors 3. ID selectors

5/1/2010

16

HTML Selector
Used to define styles associated to HTML tags. HTML selectors are used when we want to redefine the general look for an entire HTML tag.

The general syntax for an HTML selector is: HTMLSelector {Property:Value;}

5/1/2010

17

For example: <HTML> <HEAD> <style type="text/css"> B {font-family:arial; font-size:14px; color:red} </style> </HEAD> <BODY> <B>This is a customized headline style bold</B> </BODY> </HTML>

5/1/2010

18

Class Selector
Used to specify a style for a group of elements. This allows us to set a particular style for any HTML elements with the same class. The class selector uses the HTML class attribute, and is defined with a "." The general syntax for a Class selector is:

.ClassSelector {Property:Value;}

5/1/2010

19

For example: <HTML> <HEAD> <style type="text/css"> .headline {font-family:arial; font-size:14px; color:red} </style> </HEAD> <BODY> <b class="headline">This is a bold tag carrying the headline class</b> <br> <i class="headline">This is an italics tag carrying the headline class</i> </BODY></HTML>

5/1/2010

20

Id Selectors Used to define styles relating to objects with a unique ID (most often

The general syntax for an ID selector is: #IDSelector {Property:Value;}

5/1/2010

21

<html> <head> <style type="text/css"> #para1 { text-align:center; color:red; } </style> </head> <body> <p id="para1">Hello World!</p> <p>This paragraph is not affected by the style.</p> </body> </html>

5/1/2010

22

CSS Comments
Comments are used to explain your code, and may help you when you edit the source code at a later date. Comments are ignored by browsers. 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; }

5/1/2010

23

Types of Style sheets CSS allows to control the document s appearance through a compartmentalized section of declarations that format the page, or site, to our specifications.

There are (3) different types of style sheets: 1. External 2. Internal / Embedded 3. Inline

5/1/2010

24

External Style sheets An external style sheet is ideal when the style is applied to many pages. With an external style sheet, we 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" </head>

type="text/css"

href="mystyle.css"

/>

5/1/2010

25

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");}

5/1/2010

26

Internal / Embedded Style sheets An Internal Style sheet resides in between the <head> </head> tags. The declarations are positioned between <style> </style> tags. Internal style sheets are only connected to the page, and can not be used by another file like External Style sheets can. Embedded Style sheets are the 2nd level in the CSS hierarchy. These commands will over-ride an external style sheet commands.

5/1/2010

27

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

5/1/2010

28

Inline style sheets Inline styles use the style attribute in the relevant tag. The style attribute can contain any CSS property. E.g. <p style="color:sienna;margin-left:20px">This is a paragraph.</p> An inline style loses many of the advantages of style sheets by mixing content with presentation.

5/1/2010

29

Cascading order
Styles can be specified: inside an HTML element ( Inline style sheets) inside the head section of an HTML page (Internal style sheets) in an external CSS file ( External style sheets)

When there is more than one style specified for an HTML element, all the styles will
1. 2. 3. 4.

"cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority: Browser default External style sheet Internal style sheet (in the head section) Inline style (inside an HTML element)

5/1/2010

30

So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the <head> tag, or in an external style sheet, or in a browser (a default value).

Note: If the link to the external style sheet is placed after the internal style sheet in HTML <head>, the external style sheet will override the internal style sheet!

5/1/2010

31

Styling Background CSS background properties are used to define the background effects of an element. CSS properties used for background effects: background-color background-image background-repeat background-attachment background-position

5/1/2010

32

Property

Description

Values

background-color

Sets the background color of an element

color-rgb color-hex color-name


transparent inherit

background-image

Sets the background image for an element

url(URL ) none inherit

5/1/2010

33

Property
background-repeat

Description
Sets how a background image will be repeated

Values
repeat repeat-x repeat-y no-repeat inherit left top left center left bottom right top right center right bottom center top center center center bottom

background-position

Sets the starting position of a background image

x% y% xpos ypos
inherit

5/1/2010

34

Property
background-attachment

Description
Sets whether a background image is fixed or scrolls with the rest of the page

Values
scroll fixed inherit

background

Sets all the background properties in one declaration

background-color background-image background-repeat background-attachment background-position


inherit

5/1/2010

35

Background Color The background-color property specifies the background color of an element. The background of an element is the total size of the element, including padding and border E.g. body { background-color:yellow; } h1 { background-color:#00ff00; } p { background-color:rgb(255,0,255); }

5/1/2010

36

Value

Description Specifies the background color with a color name, like red

color_name

hex_number

Specifies the background color with a hex code, like #ff0000

rgb_number

Specifies the background color with an RGB code, like rgb(255,0,0)

5/1/2010

37

Value transparent

Description Specifies that the background color should be transparent. This is default

inherit

Specifies that the background color should be inherited from the parent element

5/1/2010

38

Background Image The background-image property specifies an image to use as the background of an element. By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.

5/1/2010

39

Value url ('URL') none

Description The URL to the image No background image will be displayed. This is default Specifies that the background image should be inherited from the parent element

inherit

5/1/2010

40

The background image for a page can be set like this: Example <html> <head> <style type="text/css"> body {background-image:url('paper.gif');} </style> </head> <body> <h1>Hello World!</h1> </body> </html>

5/1/2010

41

Background-repeat The background-repeat property sets how a background image will be repeated. By default, the background-image property repeats an image both horizontally and vertically.

5/1/2010

42

Value
repeat

Description
The background image will be repeated both vertically and horizontally. This is default

repeat-x

The background horizontally

image

will

be

repeated

only

repeat-y no-repeat

The background image will be repeated only vertically The background-image will not be repeated

inherit

Specifies that the setting of the background-repeat property should be inherited from the parent element

5/1/2010

43

E.g. <html> <head> <style type="text/css"> body { background-image: url('paper.gif'); background-repeat: repeat-x; } </style> </head> <body> <p>repeat-x will repeat a background image only horizontally.</p> </body> </html>

5/1/2010

44

Background-position The background-position property sets the starting position of a background image.

5/1/2010

45

Value
left top left center left bottom right top right center right bottom center top center center center bottom

Description
If we only specify one keyword, the second value will be "center"

x% y%

The first value is the horizontal position and the second value is the vertical. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If we only specify one value, the other value will be 50%. . Default value is: 0% 0%

5/1/2010

46

Value
xpos ypos

Description
The first value is the horizontal position and the second value is the vertical. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units.

inherit

Specifies that the setting of the backgroundposition property should be inherited from the parent element

5/1/2010

47

E.g. <html> <head> <style type="text/css"> body { background-image:url('smiley.gif'); background-repeat:no-repeat; background-attachment:fixed; background-position:center; } </style> </head> <body> <p><b>Note:</b> For this to work in Firefox and Opera, the background-attachment property must be set to "fixed".</p> </body> </html>

5/1/2010

48

Background-attachment
The background-attachment property sets whether a background image is fixed or scrolls with the rest of the page.

5/1/2010

49

Value

Description

scroll

The background image scrolls with the rest of the page. This is default

fixed

The background image is fixed

inherit

Specifies that the setting of the background-attachment property should be inherited from the parent element

5/1/2010

50

<html> <head> <style type="text/css"> body { background-image:url('smiley.gif'); background-repeat:no-repeat; background-attachment:fixed; background-position:center; } </style> </head> <body> <p><b>Note:</b> For this to work in Firefox and Opera, the background-attachment property must be set to "fixed".</p> </body> </html>

5/1/2010

51

Background The background shorthand property sets all the background properties in one declaration. The properties that can be set, are (in order): background-color, backgroundimage, background-repeat, background-attachment, and backgroundposition. It does not matter if one of the values above are missing, e.g. background:#ff0000 url('smiley.gif'); is allowed.

5/1/2010

52

Value

Description Specifies the background color to be used. See background-color for possible values Specifies the background image to be used. See backgroundimage for possible values Specifies how to repeat the background image. See backgroundrepeat for possible values Specifies whether a background image is fixed or scrolls with the rest of the page. See background-attachment for possible values

background-color background-image background-repeat background-attachment

background-position

Specifies the position of the background image. See backgroundposition for possible values

inherit

Specifies that the setting of the background property should be inherited from the parent element

5/1/2010

53

E.g. <html> <head> <style type="text/css"> body { background: #00ff00 url('smiley.gif') no-repeat fixed center; } </style> </head> <body> <p>This is some text</p> <p>This is some text</p> <p>This is some text</p> <p>This is some text</p> </body> </html>

5/1/2010

54

Styling text
Property color direction line-height Description Sets the color of a text Sets the text direction Sets the distance between lines Values

color
ltr rtl normal

number length %
normal

letter-spacing

Increase or decrease the space between characters Aligns the text in an element

length
left right center justify
55

text-align

5/1/2010

Property
text-decoration

Description
Adds decoration to text

Values
none underline overline line-through blink length %
none

text-indent text-shadow

Indents the first line of text in an element

color length
text-transform Controls the letters in an element none capitalize uppercase lowercase

5/1/2010

56

Property verticalalign

Description Sets the vertical alignment of an element

Values baseline sub super top text-top middle bottom text-bottom

length %
whitespace wordspacing Sets how white space inside an element is handled Increase or decrease the space between words normal pre nowrap normal

length

5/1/2010

57

Color Property The color property is used to set the color of the text. The color can be specified by: name - a color name, like "red" RGB - an RGB value, like "rgb(255,0,0)" Hex - a hex value, like "#ff0000" The default color for a page is defined in the body selector.

5/1/2010

58

<html> <head> <style type="text/css"> body {color:red;} h1 {color:#00ff00;} p.ex {color:rgb(0,0,255);} </style> </head> <body> <h1>This is heading 1</h1> <p>This paragraph text is red. The default text-color for a page is defined in the body selector.</p> <p class="ex">This is a paragraph with class="ex". This text is blue.</p> </body> </html>
5/1/2010 59

Direction Property The direction property specifies the text direction/writing direction. Property Values
Value ltr rtl inherit Description The writing direction is left-to-right. This is default The writing direction is right-to-left Specifies that the value of the direction property should be inherited from the parent element

5/1/2010

60

<html><head> <style type="text/css"> div.ex1 {direction:rtl;} </style> </head> <body> <div>Some text. Default writing direction.</div> <div class="ex1">Some text. Left-to-right direction.</div> </body></html>

5/1/2010

61

line-height Property The line-height property specifies the line height. Property Values
Value normal Description A normal line height. This is default A number that will be multiplied with the current font size to set the line height A fixed line height in px, pt, cm, etc. A line height in percent of the current font size Specifies that the value of the line-height property should be inherited from the parent element
62

number length %
inherit
5/1/2010

<html> <head> <style type="text/css"> p.small {line-height:90%;} p.big {line-height:2;} </style> </head> <body> <p> This is a paragraph with a standard lineheight. This is a paragraph with a standard lineheight. </p>

<p class="small"> This is a paragraph with a smaller lineheight. This is a paragraph with a smaller lineheight. This is a paragraph with a smaller lineheight. </p> <p class="big"> This is a paragraph with a bigger lineheight. This is a paragraph with a bigger lineheight. This is a paragraph with a bigger lineheight. </p> </body> </html>

5/1/2010

63

letter-spacing Property
The letter-spacing property increases or decreases the space between characters in a text. Property Values
Value normal Description No extra space between characters. This is default Defines an extra space between characters (negative values are allowed) Specifies that the value of the line-height property should be inherited from the parent element

length
inherit

5/1/2010

64

<html><head> <style type="text/css"> h1 {letter-spacing:2px;} h2 {letter-spacing:-3px;} </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> </body></html>

5/1/2010

65

text-align Property
The text-align property is used to set the horizontal alignment of a text.

Property Values
Value Description

left

Aligns the text to the left Aligns the text to the right Centers the text Stretches the lines so that each line has equal width Specifies that the value of the text-align property should be inherited from the parent element
66

right
center justify inherit

5/1/2010

<html> <head> <style type="text/css"> h1 {text-align:center;} p.date {text-align:right;} p.main {text-align:justify;} </style> </head> <body> <h1>CSS text-align Example</h1> <p class="date">April 2010</p> <p class="main">This is a text align example.'</p> </body> </html>

5/1/2010

67

Text Decoration The text-decoration property specifies the decoration added to text. Property Values
Value Description

none underline overline line-through blink inherit

Defines a normal text. This is default Defines a line below the text Defines a line above the text Defines a line through the text Defines a blinking text Specifies that the value of the text-decoration property should be inherited from the parent element

5/1/2010

68

<html> <head> <style type="text/css"> h1 {text-decoration:overline;} h2 {text-decoration:line-through;} h3 {text-decoration:underline;} h4 {text-decoration:blink;} </style> </head>

5/1/2010

69

<body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <h3>This is heading 3</h3> <h4>This is heading 4</h4> <p><b>Note:</b> The "blink" value is not supported in IE, Chrome, or Safari.</p> </body> </html>

5/1/2010

70

Text Indentation
The text-indentation property is used to specify the indentation of the first line of a text. Property Values
Value Description

length %
inherit

Defines a fixed indentation in px, pt, cm, em, etc. Defines the indentation in % of the width of the parent element Specifies that the value of the text-indent property should be inherited from the parent element

5/1/2010

71

<html> <head> <style type="text/css"> p {text-indent:50px;} </style> </head> <body> <p>Text decoration</p> </body></html>

5/1/2010

72

text-transform Property The text-transform property controls the capitalization of text. Property Values
Value none capitalize uppercase lowercase inherit Description No capitalization. The text renders as it is. This is default Transforms the first character of each word to uppercase Transforms all characters to uppercase Transforms all characters to lowercase Specifies that the value of the text-transform property should be inherited from the parent element

5/1/2010

73

<html> <head> <style type="text/css"> h1 {letter-spacing:2px;} h2 {letter-spacing:-3px;} </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> </body> </html>

5/1/2010

74

vertical-align Property
The vertical-align property sets the vertical alignment of an element. Property Values
Value Description

length
%

Raises or lower an element by the specified length. Negative values are allowed Raises or lower an element in a percent of the "line-height" property. Negative values are allowed Align the baseline of the element with the baseline of the parent element. This is default

baseline

5/1/2010

75

Value sub super top text-top middle bottom text-bottom inherit

Description Aligns the element as it was subscript Aligns the element as it was superscript The top of the element is aligned with the top of the tallest element on the line The top of the element is aligned with the top of the parent element's font The element is placed in the middle of the parent element The bottom of the element is aligned with the lowest element on the line The bottom of the element is aligned with the bottom of the parent element's font Specifies that the value of the vertical-align property should be inherited from the parent element
76

5/1/2010

<html> <head> <style type="text/css"> img.top {vertical-align:text-top;} img.bottom {vertical-align:text-bottom;} </style> </head> <body> <p>An <img src="windows_logo.gif" alt="Windows Logo" width="270" height="50" /> image with a default alignment.</p> <p>An <img class="top" src=" windows_logo.gif " alt=" Windows Logo " width="270" height="50" /> image with a text-top alignment.</p> <p>An <img class="bottom" src=" windows_logo.gif " alt=" Windows Logo " width="270" height="50" /> image with a text-bottom alignment.</p> </body> </html>

5/1/2010

77

white-space Property
The white-space property specifies how white-space inside an element is handled.

Property Values
Value normal Description Sequences of whitespace will collapse into a single whitespace. Text will wrap when necessary. This is default Sequences of whitespace will collapse into a single whitespace. Text will never wrap to the next line. The text continues on the same line until a <br /> tag is encountered

nowrap

5/1/2010

78

Value pre pre-line

Description Whitespace is preserved by the browser. Text will only wrap on line breaks Acts like the <pre> tag in HTML Sequences of whitespace will collapse into a single whitespace. Text will wrap when necessary, and on line breaks Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks Specifies that the value of the white-space property should be inherited from the parent element

pre-wrap inherit

5/1/2010

79

<html> <head> <style type="text/css"> p { white-space:nowrap; } </style> </head> <body> <p> This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. </p> </body> </html>
5/1/2010 80

word-spacing Property
The word-spacing property increases or decreases the white space between words. Property Values
Value Description

normal

Defines normal space between words . This is default Defines an extra space between words in px, pt, cm, em, etc. Negative values are allowed Specifies that the value of the word-spacing property should be inherited from the parent element

length
inherit

5/1/2010

81

<html> <head> <style type="text/css"> p { word-spacing:30px; } </style> </head> <body> <p> This is some text. This is some text. </p> </body> </html>

5/1/2010

82

CSS Font
CSS font properties define the font family, boldness, size, and the style of a text. CSS Font Families In CSS, there are two types of font family names: generic family - a group of font families with a similar look (like "Serif" or "Monospace") font family - a specific font family (like "Times New Roman" or "Arial")

5/1/2010

83

Generic family Serif

Font family Times New Roman Georgia Arial Verdana Courier New Lucida Console

Description Serif fonts have small lines at the ends on some characters "Sans" means without - these fonts do not have the lines at the ends of characters All monospace characters has the same width

Sans-serif

Monospace

5/1/2010

84

Difference Between Serif and Sans-serif Fonts:

5/1/2010

85

Property
font-family

Description
Specifies the font family for text Sets all the font properties in one declaration

Values
family-name generic-family
inherit

font

font-style font-variant font-weight font-size/line-height font-family


caption icon menu message-box small-caption status-bar inherit

5/1/2010

86

Property
font-size

Description
Specifies the font size of text font-size: <absolute-size> <relative-size> <length> <percentage>

Values
<absolute-size> xx-small x-small small medium large x-large xx-large <relative-size> <length> larger smaller A length value is formed by an optional + or -, followed by a number, followed by a two-letter abbreviation that indicates the unit. Both relative and absolute length units are supported in CSS. The following relative units are available: em (ems, the height of the element's font) ex (x-height, the height of the letter "x") px (pixels, relative to the canvas resolution) The following absolute units are available: in (inches; 1in=2.54cm) cm (centimeters; 1cm=10mm) mm (millimeters) pt (points; 1pt=1/72in) pc (picas; 1pc=12pt) Percentage Units A percentage value is formed by an optional + or -, followed by a number, followed by %. 87

5/1/2010

Property font-style

Description Specifies the font style for text

Values normal italic oblique inherit normal small-caps inherit normal bold bolder lighter 100 200 300 400 500 600 700 800 900 inherit

font-variant

Specifies whether or not a text should be displayed in a small-caps font Specifies the weight of a font

font-weight

5/1/2010

88

CSS font-family Property


The font-family property specifies the font for an element. The font-family property can hold several font names . If the browser does not support the first font, it tries the next font. There are two types of font family names: family-name - The name of a font-family, like "times", "courier", "arial", etc. generic-family - The name of a generic-family, like "serif", "sans-serif", "cursive", "fantasy", "monospace".

5/1/2010

89

Property Values
Value Description

familyname genericfamily
inherit

A prioritized list of font family names and/or generic family names

Specifies that the font family should be inherited from the parent element

5/1/2010

90

<html> <head> <style type="text/css"> p.serif{font-family:"Times New Roman",Times,serif;} p.sansserif{font-family:Arial,Helvetica,sans-serif;} </style> </head> <body> <h1>CSS font-family</h1> <p class="serif">This is a paragraph, shown in the Times New Roman font.</p> <p class="sansserif">This is a paragraph, shown in the Arial font.</p> </body> </html>
5/1/2010 91

CSS font Property


The font shorthand property sets all the font properties in one declaration. The properties that can be set, are (in order): "font-style font-variant font-weight fontsize/line-height font-family" The font-size and font-family values are required. If one of the other values are missing, the default values will be inserted, if any.

5/1/2010

92

<html> <head> <style type="text/css"> p.ex1 { font:15px arial,sans-serif; } p.ex2 { font:italic bold 12px/30px Georgia,serif; } </style> </head>

<body> <p class="ex1">This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.</p> <p class="ex2">This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.</p> </body> </html>

5/1/2010

93

CSS font-size Property


The font-size property sets the size of the text. Syntax: font-size: <absolute-size> <relative-size> <length> <percentage> Possible Values: <absolute-size> xx-small x-small small medium large x-large xx-large <relative-size> larger smaller <length> <percentage> (in relation to parent element)

5/1/2010

94

Length units A length value is formed by an optional + or -, followed by a number, followed by a two-letter abbreviation that indicates the unit. Both relative and absolute length units are supported in CSS1. Relative units are available: em (ems, the height of the element's font) ex (x-height, the height of the letter "x") px (pixels, relative to the canvas resolution)

5/1/2010

95

Absolute units are available: in (inches; 1in=2.54cm) cm (centimeters; 1cm=10mm) mm (millimeters) pt (points; 1pt=1/72in) pc (picas; 1pc=12pt) Percentage Units A percentage value is formed by an optional + or -, followed by a number, followed by %. There are no spaces in a percentage value. Percentage values are relative to other values, as defined for each property. Most often the percentage value is relative to the element's font size.

5/1/2010

96

<html> <head> <style> body {font-size:100%;} h1 {font-size:2.5em;} h2 {font-size:1.875em;} p {font-size:0.875em;} </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <p>This is a paragraph.</p> <p>Specifying the font-size in percent and em displays the same size in all browsers, and allows all browsers to resize the text!</p> </body> </html>

5/1/2010

97

CSS Links Links can be style with any CSS property (e.g. color, font-family, background-color). Special for links are that they can be styled differently depending on what state they are in. The four links states are: a:link - a normal, unvisited link a:visited - a link the user has visited a:hover - a link when the user mouses over it a:active - a link the moment it is clicked

5/1/2010

98

When setting the style for several link states, there are some order rules: a:hover MUST come after a:link and a:visited a:active MUST come after a:hover

Text Decoration
The text-decoration property is mostly used to remove underlines from links: <html> <head> <style type="text/css"> a:link {text-decoration:none;} /* unvisited link */ a:visited {text-decoration:none;} /* visited link */ a:hover {text-decoration:underline;} /* mouse over link */ a:active {text-decoration:underline;} /* selected link */ </style> </head> <body> <p><b><a href="default.asp" target="_blank">This is a link</a></b></p> <p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.</p> <p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p> </body> </html> 5/1/2010 99

Background Color The background-color property specifies the background color for links: <html> <head> <style type="text/css"> a:link {background-color:#B2FF99;} /* unvisited link */ a:visited {background-color:#FFFF85;} /* visited link */ a:hover {background-color:#FF704D;} /* mouse over link */ a:active {background-color:#FF704D;} /* selected link */ </style> </head>

5/1/2010

100

<body> <p><b><a href="default.asp" target="_blank">This is a link</a></b></p> <p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.</p> <p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p> </body> </html>

5/1/2010

101

You might also like