CSS-COMPUTEC
CSS-COMPUTEC
Professional Group
CSS Tutorial
About theTutorial
CSS is used to control the style of a web document in a simple and easy way.
CSS stands for Cascading Style Sheets. This tutorial covers both the versions
CSS1 and CSS2 and gives a complete understanding of CSS, starting from its
basics to advanced concepts.
Audience
This tutorial will help both students as well as professionals who want to make
their websites or personal blogs more attractive.
Prerequisites
You should be familiar with:
If you are new to HTML and XHTML, then we would suggest you to go through
our HTML Tutorial or XHTML Tutorial first.
i
Table of Contents
About the Tutorial ..................................................................................................................................... i
Audience .................................................................................................................................................... i
Prerequisites .............................................................................................................................................. i
1. OVERVIEW ..................................................................................... 1
Advantages of CSS..................................................................................................................................... 1
CSS Versions.............................................................................................................................................. 2
2. SYNTAX ........................................................................................ 3
3. INCLUSION .................................................................................. 8
Attributes ................................................................................................................................................. 8
ii
Attributes ................................................................................................................................................. 9
Attributes ............................................................................................................................................... 10
5. COLORS ....................................................................................... 16
6. BACKGROUND ................................................................................ 22
7. FONTS ......................................................................................... 26
iii
Set the Font Weight ................................................................................................................................ 27
8. TEXT ....................................................................................... 31
9. IMAGES ........................................................................................ 37
10. LINKS....................................................................................... 41
iv
Change the Color of Active Links ............................................................................................................. 43
15. PADDINGS..................................................................................... 72
v
The padding-bottom Property ................................................................................................................ 72
18. DIMENSION.................................................................................... 84
19. SCROLLBARS.................................................................................. 90
vi
22. LAYERS ........................................................................................ 98
vii
Glow Effect ........................................................................................................................................... 121
viii
The pause Property ............................................................................................................................... 144
ix
1. OVERVIEW
CSS
What isCSS?
Cascading Style Sheets, fondly referred to as CSS, is a simple design language
intended to simplify the process of making web pages presentable.
CSS handles the look and feel part of a web page. Using CSS, you can control
the color of the text, the style of fonts, the spacing between paragraphs, how
columns are sized and laid out, what background images or colors are used, as
well as a variety of other effects.
CSS is easy to learn and understand but it provides a powerful control over the
presentation of an HTML document. Most commonly, CSS is combined with the
markup languages HTML or XHTML.
Advantages of CSS
• CSS saves time - You can write CSS once and then reuse the same sheet
in multiple HTML pages. You can define a style for each HTML element and
apply it to as many web pages as you want.
• Pages load faster - If you are using CSS, you do not need to write HTML
tag attributes every time. Just write one CSS rule of a tag and apply it to
all the occurrences of that tag. So, less code means faster download
times.
• Global web standards – Now HTML attributes are being deprecated and
it is being recommended to use CSS. So it’s a good idea to start using
CSS in all the HTML pages to make them compatible with future browsers.
1
CSS
These ratified specifications are called recommendations because the W3C has
no control over the actual implementation of the language. Independent
companies and organizations create that software.
NOTE: The World Wide Web Consortium or W3C is a group that makes
recommendations about how the Internet works and how it should evolve.
CSS Versions
Cascading Style Sheets level 1 (CSS1) came out of W3C as a recommendation in
December 1996. This version describes the CSS language as well as a simple
visual formatting model for all the HTML tags.
CSS2 became a W3C recommendation in May 1998 and builds on CSS1. This
version adds support for media-specific style sheets e.g. printers and aural
devices, downloadable fonts, element positioning and tables.
2
2. SYNTAX
CSS
A CSS comprises of style rules that are interpreted by the browser and then
applied to the corresponding elements in your document. A style rule is made of
three parts:
• Property: A property is a type of attribute of HTML tag. Put simply, all the
HTML attributes are converted into CSS properties. They could
be color, border, etc.
• Value: Values are assigned to properties. For example, color property can
have the value either red or #F1F1F1 etc.
Here table is a selector and border is a property and the given value 1px solid
#C00 is the value of that property.
You can define selectors in various simple ways based on your comfort. Let me
put these selectors one by one.
The TypeSelectors
This is the same selector we have seen above. Again, one more example to give
a color to all level 1 headings:
h1 {
color: #36CFFF;
}
3
CSS
The UniversalSelectors
Rather than selecting elements of a specific type, the universal selector quite
simply matches the name of any element type:
* {
color: #000000;
}
This rule renders the content of every element in our document in black.
The DescendantSelectors
Suppose you want to apply a style rule to a particular element only when it lies
inside a particular element. As given in the following example, the style rule will
apply to <em> element only when it lies inside the <ul> tag.
ul em {
color: #000000;
}
The ClassSelectors
You can define style rules based on the class attribute of the elements. All the
elements having that class will be formatted according to the defined rule.
.black {
color: #000000;
}
This rule renders the content in black for every element with class attribute set
to black in our document. You can make it a bit more particular. For example:
h1.black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with class
attribute set to black.
4
CSS
You can apply more than one class selectors to a given element. Consider the
following example:
The IDSelectors
You can define style rules based on the id attribute of the elements. All the
elements having that id will be formatted according to the defined rule.
#black {
color: #000000;
}
This rule renders the content in black for every element with id attribute set
to black in our document. You can make it a bit more particular. For example:
h1#black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with id attribute
set to black.
The true power of id selectors is when they are used as the foundation for
descendant selectors. For example:
#black h2 {
color: #000000;
}
In this example, all level 2 headings will be displayed in black color when those
headings will lie within tags having id attribute set to black.
The ChildSelectors
You have seen the descendant selectors. There is one more type of selector,
which is very similar to descendants but have different functionality. Consider
the following example:
5
CSS
This rule will render all the paragraphs in black if they are a direct child of the
<body> element. Other paragraphs put inside other elements like <div> or
<td> would not have any effect of this rule.
TheAttribute Selectors
You can also apply styles to HTML elements with particular attributes. The style
rule below will match all the input elements having a type attribute with a value
of text:
input[type="text"]{
color: #000000;
}
The advantage to this method is that the <input type="submit" /> element is
unaffected, and the color applied only to the desired text fields.
h1 {
color: #36C;
font-weight: normal;
6
CSS
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
Here all the property and value pairs are separated by a semicolon (;). You can
keep them in a single line or multiple lines. For better readability, we keep them
in separate lines.
For a while, don't bother about the properties mentioned in the above block.
These properties will be explained in the coming chapters and you can find the
complete detail about properties in CSS References.
Grouping Selectors
You can apply a style to many selectors if you like. Just separate the selectors
with a comma, as given in the following example:
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
This define style rule will be applicable to h1, h2 and h3 element as well. The
order of the list is irrelevant. All the elements in the selector will have the
corresponding declarations applied to them.
You can combine the various class selectors together as shown below:
7
3. INCLUSION
CSS
There are four ways to associate styles with your HTML document. Most
commonly used methods are inline CSS and External CSS.
<head>
<style type="text/css" media="...">
Style Rules
............
</style>
</head>
Attributes
Attributes associated with <style> elements are:
8
CSS
aural
all
Example
Following is an example of embed CSS based on the above syntax:
<head>
<style type="text/css" media="all">
h1{
color: #36C;
}
</style>
</head>
Attributes
Attribute Value Description
Example
Following is the example of inline CSS based on the above syntax:
9
CSS
An external style sheet is a separate text file with .css extension. You define all
the Style rules within this text file and then you can include this file in any HTML
document using <link> element.
<head>
<link type="text/css" href="..." media="..." />
</head>
Attributes
Attributes associated with <style> elements are:
href URL Specifies the style sheet file having Style rules. This
attribute is a required.
10
CSS
Example
Consider a simple style sheet file with a name mystyle.css having the following
rules:
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
Now you can include this file mystyle.css in any HTML document as follows:
<head>
<link type="text/css" href="mystyle.css" media="all" />
</head>
<head>
<@import "URL";
</head>
Here URL is the URL of the style sheet file having style rules. You can use
another syntax as well:
<head>
<@import url("URL");
</head>
Example
Following is the example showing you how to import a style sheet file into an
HTML document:
11
CSS
<head>
@import "mystyle.css";
</head>
CSS RulesOverriding
We have discussed four ways to include style sheet rules in an HTML document.
Here is the rule to override any Style Sheet Rule.
• Any inline style sheet takes the highest priority. So, it will override any
rule defined in <style>...</style> tags or the rules defined in any
external style sheet file.
• Any rule defined in <style>...</style> tags will override the rules defined
in any external style sheet file.
• Any rule defined in the external style sheet file takes the lowest priority,
and the rules defined in this file will be applied only when the above two
rules are not applicable.
<style type="text/css">
<!--
body, td {
color: blue;
}
-->
</style>
CSS Comments
Many times, you may need to put additional comments in your style sheet
blocks. So, it is very easy to comment any part in the style sheet. You can
simply put your comments inside /*.....this is a comment in style sheet. */.
12
CSS
You can use /* ....*/ to comment multi-line blocks in similar way you do in C
and C++ programming languages.
Example
13
4. MEASUREMENT UNITS
CSS
Before we start the actual exercise, we would like to give a brief idea about the
CSS Measurement Units.
We have listed out all the CSS Measurement Units along with proper Examples:
14
CSS
15
5. COLORS
CSS
CSS uses color values to specify a color. Typically, these are used to set a color
either for the foreground of an element (i.e., its text) or for the background of
the element. They can also be used to affect the color of borders and other
decorative effects.
You can specify your color values in various formats. Following table lists all the
possible formats:
A hexadecimal value can be taken from any graphics software like Adobe
Photoshop, Jasc Paintshop Pro, or even using Advanced Paint Brush.
Each hexadecimal code will be preceded by a pound or hash sign ‘#’. Following
are the examples to use Hexadecimal notation.
16
CSS
#000000
#FF0000
#00FF00
#0000FF
#FFFF00
#00FFFF
#FF00FF
#C0C0C0
#FFFFFF
A hexadecimal value can be taken from any graphics software like Adobe
Photoshop, Jasc Paintshop Pro or even using Advanced Paint Brush.
17
CSS
NOTE: All the browsers does not support rgb() property of color, so it is
recommended not to use it.
rgb(0,0,0)
rgb(255,0,0)
rgb(0,255,0)
rgb(0,0,255)
rgb(255,255,0)
rgb(0,255,255)
18
CSS
rgb(255,0,255)
rgb(192,192,192)
rgb(255,255,255)
Browser SafeColors
Here is the list of 216 colors, which are supposed to be most safe and computer
independent colors. These colors vary from hexa code 000000 to FFFFFF. These
colors are safe to use because they ensure that all computers would display the
colors correctly when running a 256 color palette:
19
CSS
20
CSS
21
6. BACKGROUND
CSS
This chapter teaches you how to set backgrounds of various HTML elements. You
can set the following background properties of an element:
<p style="background-color:yellow;">
This text has a yellow background color.
</p>
22
CSS
<table style="background-image:url(/images/pattern1.gif);
background-repeat: repeat;">
<tr><td>
This table has background image which repeats multiple times.
</td></tr>
</table>
The following example which demonstrates how to repeat the background image
vertically.
<table style="background-image:url(/images/pattern1.gif);
background-repeat: repeat-y;">
<tr><td>
This table has background image set which will repeat vertically.
</td></tr>
</table>
23
CSS
<table style="background-image:url(/images/pattern1.gif);
background-repeat: repeat-x;">
<tr><td>
This table has background image set which will repeat horizontally.
</td></tr>
</table>
<table style="background-image:url(/images/pattern1.gif);
background-position:100px;">
<tr><td>
Background image positioned 100 pixels away from the left.
</td></tr>
</table>
The following example demonstrates how to set the background image position
100 pixels away from the left side and 200 pixels down from the top.
<table style="background-image:url(/images/pattern1.gif);
background-position:100px 200px;">
<tr><td>
This table has background image positioned 100
pixels away from the left and 200 pixels from the top.
</td></tr>
</table>
24
CSS
The following example demonstrates how to set the fixed background image.
<p style="background-image:url(/images/pattern1.gif);
background-attachment:fixed;">
This parapgraph has fixed background image.
</p>
The following example demonstrates how to set the scrolling background image.
<p style="background-image:url(/images/pattern1.gif);
background-attachment:scroll;">
This parapgraph has scrolling background image.
</p>
Shorthand Property
You can use the background property to set all the background properties at
once. For example:
25
7. FONTS
CSS
This chapter teaches you how to set fonts of a content, available in an HTML
element. You can set the following font properties of an element:
<p style="font-family:georgia,garamond,serif;">
This text is rendered in either georgia, garamond, or the default
serif font depending on which font you have at your system.
</p>
<p style="font-style:italic;">
26
CSS
<p style="font-variant:small-caps;">
This text will be rendered as small caps
</p>
<p style="font-weight:bold;">
This font is bold.
</p>
<p style="font-weight:bolder;">
This font is bolder.
</p>
<p style="font-weight:900;">
This font is 900 weight.
</p>
27
CSS
<p style="font-size:20px;">
This font size is 20 pixels
</p>
<p style="font-size:small;">
This font size is small
</p>
<p style="font-size:large;">
This font size is large
</p>
28
CSS
<p style="font-size-adjust:0.61;">
This text is using a font-size-adjust value.
</p>
<p style="font-stretch:ultra-expanded;">
If this doesn't appear to work, it is likely that
your computer doesn't have a condensed or expanded
version of the font being used.
</p>
If this doesn't appear to work, it is likely that your computer doesn't have a condensed or
expanded version of the font being used.
29
CSS
Shorthand Property
You can use the font property to set all the font properties at once. For example:
30
8. TEXT
CSS
This chapter teaches you how to manipulate text using CSS properties. You can
set the following text properties of an element:
• The text-shadow property is used to set the text shadow around a text.
<p style="color:red;">
This text will be written in red.
</p>
31
CSS
<p style="direction:rtl;">
This text will be renedered from right to left
</p>
<p style="letter-spacing:5px;">
This text is having space between letters.
</p>
<p style="word-spacing:5px;">
This text is having space between words.
</p>
<p style="text-indent:1cm;">
This text will have first line indented by 1cm
and this line will remain at its actual position
this is done by CSS text-indent property.
</p>
<p style="text-align:right;">
This will be right aligned.
</p>
<p style="text-align:center;">
This will be center aligned.
</p>
<p style="text-align:left;">
This will be left aligned.
</p>
Decorating theText
The following example demonstrates how to decorate a text. Possible values are
none, underline, overline, line-through, blink.
<p style="text-decoration:underline;">
This will be underlined
</p>
<p style="text-decoration:line-through;">
This will be striked through.
</p>
<p style="text-decoration:overline;">
This will have a over line.
</p>
<p style="text-decoration:blink;">
This text will have blinking effect
</p>
34
CSS
<p style="text-transform:capitalize;">
This will be capitalized
</p>
<p style="text-transform:uppercase;">
This will be in uppercase
</p>
<p style="text-transform:lowercase;">
This will be in lowercase
</p>
35
CSS
If your browser supports the CSS text-shadow property, this text will have a blue shadow.
36
9. IMAGES
CSS
CSS plays a good role to control image display. You can set the following image
properties using CSS.
Here is an example:
37
CSS
Here is an example:
Here is an example:
38
CSS
The -moz-opacityProperty
The -moz-opacity property of an image is used to set the opacity of an image.
This property is used to create a transparent image in Mozilla. IE
uses filter:alpha(opacity=x) to create transparent images.
39
CSS
Here is an example:
40
10. LINKS
CSS
This chapter teaches you how to set different properties of a hyper link using
CSS. You can set the following properties of a hyperlink:
We will revisit the same properties when we will discuss Pseudo-Classes of CSS.
• The :hover signifies an element that currently has the user's mouse
pointer hovering over it.
Usually, all these properties are kept in the header part of the HTML document.
Remember a:hover MUST come after a:link and a:visited in the CSS definition in
order to be effective. Also, a:active MUST come after a:hover in the CSS
definition as follows:
<style type="text/css">
a:link {color: #000000}
a:visited {color: #006600}
a:hover {color: #FFCC00}
a:active {color: #FF00CC}
</style>
Now, we will see how to use these properties to give different effects to
hyperlinks.
<style type="text/css">
a:link {color:#000000}
</style>
41
CSS
Black Link
<style type="text/css">
a:visited {color: #006600}
</style>
<a href="/html/index.htm">Click this link</a>
It will produce the following link. Once you click this link, it will change its color
to green.
<style type="text/css">
a:hover {color: #FFCC00}
</style>
<a href="/html/index.htm">Bring Mouse Here</a>
It will produce the following link. Now, you bring your mouse over this link and
you will see that it changes its color to yellow.
42
CSS
<style type="text/css">
a:active {color: #FF00CC}
</style>
<a href="/html/index.htm">Click This Link</a>
It will produce the following link. It will change its color to pink when the user
clicks it.
43
11. TABLES
CSS
This chapter teaches you how to set different properties of an HTML table using
CSS. You can set the following properties of a table:
• The border-spacing specifies the width that should appear between table
cells.
The order-collapseProperty
This property can have two values collapse and separate. The following example
uses both the values:
<style type="text/css">
table.one {border-collapse:collapse;}
table.two {border-collapse:separate;}
td.a {
border-style:dotted;
border-width:3px;
border-color:#000000;
padding: 10px;
}
44
CSS
td.b {border-style:solid;
border-width:3px;
border-color:#333333;
padding:10px;
}
</style>
<table class="one">
<caption>Collapse Border Example</caption>
<tr><td class="a"> Cell A Collapse Example</td></tr>
<tr><td class="b"> Cell B Collapse Example</td></tr>
</table>
<br />
<table class="two">
<caption>Separate Border Example</caption>
<tr><td class="a"> Cell A Separate Example</td></tr>
<tr><td class="b"> Cell B Separate Example</td></tr>
</table>
45
CSS
The border-spacingProperty
The border-spacing property specifies the distance that separates the adjacent
cells’ borders. It can take either one or two values; these should be units of
length.
If you provide one value, it applies to both vertical and horizontal borders. Or
you can specify two values, in which case, the first refers to the horizontal
spacing and the second to the vertical spacing:
<style type="text/css">
/* If you provide one value */
table.example {border-spacing:10px;}
/* This is how you can provide two values */
table.example {border-spacing:10px; 15px;}
</style>
Now let's modify the previous example and see the effect:
<style type="text/css">
table.one {
border-collapse:separate;
width:400px;
border-spacing:10px;
}
table.two {
border-collapse:separate;
width:400px;
border-spacing:10px 50px;
}
</style>
<table class="one" border="1">
<caption>Separate Border Example with border-spacing</caption>
<tr><td> Cell A Collapse Example</td></tr>
<tr><td> Cell B Collapse Example</td></tr>
46
CSS
</table>
<br />
<table class="two" border="1">
<caption>Separate Border Example with border-spacing</caption>
<tr><td> Cell A Separate Example</td></tr>
<tr><td> Cell B Separate Example</td></tr>
</table>
The caption-sideProperty
The caption-side property allows you to specify where the content of a
<caption> element should be placed in relationship to the table. The table that
follows lists the possible values.
This property can have one of the four values top, bottom, left, or right. The
following example uses each value.
<style type="text/css">
caption.top {caption-side:top}
caption.bottom {caption-side:bottom}
caption.left {caption-side:left}
47
CSS
caption.right {caption-side:right}
</style>
48
CSS
<caption class="right">
This caption will appear at the right
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
The empty-cellsProperty
The empty-cells property indicates whether a cell without any content should
have a border displayed.
This property can have one of the three values - show, hide, or inherit.
Here is the empty-cells property used to hide borders of empty cells in the
<table> element.
<style type="text/css">
table.empty{
width:350px;
border-collapse:separate;
49
CSS
empty-cells:hide;
}
td.empty{
padding:5px;
border-style:solid;
border-width:1px;
border-color:#999999;
}
</style>
<table class="empty">
<tr>
<th></th>
<th>Title one</th>
<th>Title two</th>
</tr>
<tr>
<th>Row Title</th>
<td class="empty">value</td>
<td class="empty">value</td>
</tr>
<tr>
<th>Row Title</th>
<td class="empty">value</td>
<td class="empty"></td>
</tr>
</table>
The table-layoutProperty
The table-layout property is supposed to help you control how a browser should
render or lay out a table.
This property can have one of the three values: fixed, auto, or inherit.
NOTE: This property is not supported by many browsers, so do not rely on this
property.
<style type="text/css">
table.auto
{
table-layout: auto
}
table.fixed
{
table-layout: fixed
}
</style>
<table class="auto" border="1" width="100%">
<tr>
<td width="20%">1000000000000000000000000000</td>
<td width="40%">10000000</td>
<td width="40%">100</td>
</tr>
</table>
<br />
51
CSS
52
CSS
12. BORDERS
The border properties allow you to specify how the border of the box
representing an element should look. There are three properties of a border you
can change:
The border-colorProperty
The border-color property allows you to change the color of the border
surrounding an element. You can individually change the color of the bottom,
left, top and right sides of an element's border using the properties:
<style type="text/css">
p.example1{
border:1px solid;
border-bottom-color:#009900; /* Green */
border-top-color:#FF0000; /* Red */
border-left-color:#330000; /* Black */
border-right-color:#0000CC; /* Blue */
}
p.example2{
border:1px solid;
53
CSS
border-color:#009900; /* Green */
}
</style>
<p class="example1">
This example is showing all borders in different colors.
</p>
<p class="example2">
This example is showing all borders in green color only.
</p>
The border-styleProperty
The border-style property allows you to select one of the following styles of
border:
• inset: Border makes the box look like it is embedded in the page.
• outset: Border makes the box look like it is coming out of the canvas.
You can individually change the style of the bottom, left, top, and right borders
of an element using the following properties:
54
CSS
55
CSS
The border-widthProperty
The border-width property allows you to set the width of an element borders.
The value of this property could be either a length in px, pt, or cm, or it should
be set to thin, medium, or thick.
You can individually change the width of the bottom, top, left, and right borders
of an element using the following properties:
57
CSS
The following example shows how to use all the three properties into a single
property. This is the most frequently used property to set border around any
element.
58
13. MARGINS
CSS
The margin property defines the space around an HTML element. It is possible to
use negative values to overlap content.
The values of the margin property are not inherited by the child elements.
Remember that the adjacent vertical margins (top and bottom margins) will
collapse into each other so that the distance between the blocks is not the sum
of the margins, but only the greater of the two margins or the same size as one
margin if both are equal.
The MarginProperty
The margin property allows you to set all of the properties for the four margins
in one declaration. Here is the syntax to set margin around a paragraph:
<style type="text/css">
p {margin: 15px}
all four margins will be 15px
59
CSS
Here is an example:
60
CSS
The margin-bottomProperty
The margin-bottom property allows you to set the bottom margin of an element.
It can have a value in length, %, or auto.
Here is an example:
The margin-topProperty
The margin-top property allows you to set the top margin of an element. It can
have a value in length, %, or auto.
61
CSS
Here is an example:
The margin-leftProperty
The margin-left property allows you to set the left margin of an element. It can
have a value in length, %, or auto.
Here is an example:
62
CSS
The margin-rightProperty
The margin-right property allows you to set the right margin of an element. It
can have a value in length, %, or auto.
Here is an example:
63
14. LISTS
CSS
Lists are very helpful in conveying a set of either numbered or bulleted points.
This chapter teaches you how to control list type, position, style, etc., using
CSS.
We have the following five CSS properties, which can be used to control lists:
• The marker-offset specifies the distance between a marker and the text
in the list.
The list-style-typeProperty
The list-style-type property allows you to control the shape or style of a bullet
point (also known as a marker) in case of unordered lists and the style of
numbering characters in ordered lists.
Here are the values, which can be used for an unordered list:
Value Description
None NA
64
CSS
Here are the values, which can be used for an ordered list:
65
CSS
Here is an example:
<ul style="list-style-type:circle;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ul style="list-style-type:square;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol style="list-style-type:decimal;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
66
CSS
</ol>
<ol style="list-style-type:lower-alpha;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<ol style="list-style-type:lower-roman;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
67
CSS
Value Description
none NA
inside If the text goes onto a second line, the text will wrap underneath the
marker. It will also appear indented to where the text would have
started if the list had a value of outside.
outside If the text goes onto a second line, the text will be aligned with the
start of the first line (to the right of the bullet).
Here is an example:
<ul style="list-style-type:square;list-style-position:inside;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol style="list-style-type:decimal;list-stlye-position:outside;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<ol style="list-style-type:lower-alpha;list-style-position:inside;">
68
CSS
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
The list-style-imageProperty
The list-style-image allows you to specify an image so that you can use your own
bullet style. The syntax is similar to the background-image property with the
letters url starting the value of the property followed by the URL in brackets. If it
does not find the given image then default bullets are used.
Here is an example:
<ul>
<li style="list-style-image: url(/images/bullet.gif);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol>
<li style="list-style-image: url(/images/bullet.gif);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
69
CSS
</ol>
The list-styleProperty
The list-style allows you to specify all the list properties into a single expression.
These properties can appear in any order.
Here is an example:
The marker-offsetProperty
The marker-offset property allows you to specify the distance between the
marker and the text relating to that marker. Its value should be a length as
shown in the following example:
Here is an example:
71
15. PADDINGS
CSS
The padding property allows you to specify how much space should appear
between the content of an element and its border:
The value of this attribute should be either a length, a percentage, or the word
inherit. If the value is inherit, it will have the same padding as its parent
element. If a percentage is used, the percentage is of the containing box.
The following CSS properties can be used to control lists. You can also set
different values for the padding on each side of the box using the following
properties:
The padding-bottomProperty
The padding-bottom property sets the bottom padding (space) of an element.
This can take a value in terms of length of %.
Here is an example:
72
CSS
Here is an example:
The padding-leftProperty
The padding-left property sets the left padding (space) of an element. This can
take a value in terms of length of %.
Here is an example:
73
CSS
</p>
The padding-rightProperty
The padding-right property sets the right padding (space) of an element. This
can take a value in terms of length of %.
Here is an example:
The PaddingProperty
The padding property sets the left, right, top and bottom padding (space) of an
element. This can take a value in terms of length of %.
74
CSS
Here is an example:
</p>
75
16. CURSORS
CSS
The cursor property of CSS allows you to specify the type of cursor that should
be displayed to the user.
One good usage of this property is in using images for submit buttons on forms.
By default, when a cursor hovers over a link, the cursor changes from a pointer
to a hand. However, it does not change form for a submit button on a form.
Therefore, whenever someone hovers over an image that is a submit button, it
provides a visual clue that the image is clickable.
The following table shows the possible values for the cursor property:
Value Description
auto Shape of the cursor depends on the context area it is over. For
example, an ‘I’ over text, a ‘hand’ over a link, and so on.
default An arrow.
76
CSS
help A question mark or balloon, ideal for use over help buttons.
NOTE: You should try to use only these values to add helpful information for
users, and in places, they would expect to see that cursor. For example, using
the crosshair when someone hovers over a link can confuse the visitors.
Here is an example:
<p>Move the mouse over the words to see the cursor change:</p>
<div style="cursor:auto">Auto</div>
<div style="cursor:crosshair">Crosshair</div>
<div style="cursor:default">Default</div>
<div style="cursor:pointer">Pointer</div>
<div style="cursor:move">Move</div>
<div style="cursor:e-resize">e-resize</div>
<div style="cursor:ne-resize">ne-resize</div>
<div style="cursor:nw-resize">nw-resize</div>
77
CSS
<div style="cursor:n-resize">n-resize</div>
<div style="cursor:se-resize">se-resize</div>
<div style="cursor:sw-resize">sw-resize</div>
<div style="cursor:s-resize">s-resize</div>
<div style="cursor:w-resize">w-resize</div>
<div style="cursor:text">text</div>
<div style="cursor:wait">wait</div>
<div style="cursor:help">help</div>
78
17. OUTLINES
CSS
Outlines are very similar to borders, but there are few major differences as well:
• Outline is always the same on all sides; you cannot specify different
values for different sides of an element.
• The outline-style property is used to set the line style for the outline.
• The outline property is used to set all the above three properties in a
single statement.
The outline-widthProperty
The outline-width property specifies the width of the outline to be added to the
box. Its value should be a length or one of the values thin, medium, or thick,
just like the border-width attribute.
Here is an example:
79
CSS
The outline-styleProperty
The outline-style property specifies the style for the line (solid, dotted, or
dashed) that goes around an element. It can take one of the following values:
• inset: Outline makes the box look like it is embedded in the page.
• outset: Outline makes the box look like it is coming out of the canvas.
Here is an example:
80
CSS
The outline-colorProperty
The outline-color property allows you to specify the color of the outline. Its value
should either be a color name, a hex color, or an RGB value, as with the color
and border-color properties.
Here is an example:
81
CSS
<p style="outline-width:5px;outline-style:dotted;
outline-color:rgb(13,33,232)">
This text is having 5x dotted blue outline.
</p>
The OutlineProperty
The outline property is a shorthand property that allows you to specify values for
any of the three properties discussed previously in any order but in a single
statement.
Here is an example:
82
CSS
83
18. DIMENSION
CSS
You have seen the border that surrounds every box i.e. element, the padding
that can appear inside each box, and the margin that can go around them. In
this chapter, we will learn how to change the dimensions of boxes.
We have the following properties that allow you to control the dimensions of a
box.
• The max-height property is used to set a maximum height that a box can be.
• The min-height property is used to set the minimum height that a box can be.
• The max-width property is used to set the maximum width that a box can be.
• The min-width property is used to set the minimum width that a box can be.
Here is an example:
84
CSS
The line-heightProperty
The line-height property allows you to increase the space between lines of text.
The value of the line-height property can be a number, a length, or a
percentage.
Here is an example:
85
CSS
The max-heightProperty
The max-height property allows you to specify the maximum height of a box.
The value of the max-height property can be a number, a length, or a
percentage.
Here is an example:
The min-heightProperty
The min-height property allows you to specify the minimum height of a box. The
value of the min-height property can be a number, a length, or a percentage.
Here is an example:
86
CSS
The max-widthProperty
The max-width property allows you to specify the maximum width of a box. The
value of the max-width property can be a number, a length, or a percentage.
Here is an example:
87
CSS
The min-widthProperty
The min-width property allows you to specify the minimum width of a box. The
value of the min-width property can be a number, a length, or a percentage.
Here is an example:
88
CSS
89
19. SCROLLBARS
CSS
There may be a case when an element's content might be larger than the
amount of space allocated to it. For example, the given width and height
properties do not allow enough room to accommodate the content of the
element.
CSS provides a property called overflow, which tells the browser what to do if the
box's contents is larger than the box itself. This property can take one of the
following values:
Value Description
visible Allows the content to overflow the borders of its containing element.
hidden The content of the nested element is simply cut off at the border of
the containing element and no scrollbars is visible.
scroll The size of the containing element does not change, but the scrollbars
are added to allow the user to scroll to see the content.
auto The purpose is the same as scroll, but the scrollbar will be shown only
if the content does overflow.
Here is an example:
<style type="text/css">
.scroll{
display:block;
border: 1px solid red;
padding:5px;
margin-top:5px;
width:300px;
height:50px;
90
CSS
overflow:scroll;
}
.auto{
display:block;
border: 1px solid red;
padding:5px;
margin-top:5px;
width:300px;
height:50px;
overflow:auto;
}
</style>
<p>Example of scroll value:</p>
<div class="scroll">
I am going to keep lot of content here just to show
you how scrollbars works if there is an overflow in
an element box. This provides your horizontal as well
as vertical scrollbars.
</div>
<br />
<p>Example of auto value:</p>
<div class="auto">
I am going to keep lot of content here just to show
you how scrollbars works if there is an overflow in
an element box. This provides your horizontal as well
as vertical scrollbars.
</div>
91
CSS
92
20. VISIBILITY
CSS
A property called visibility allows you to hide an element from view. You can use
this property along with JavaScript to create very complex menu and very
complex webpage layouts.
You may choose to use the visibility property to hide error messages that are
only displayed if the user needs to see them, or to hide answers to a quiz until
the user selects an option.
NOTE: Remember that the source code will still contain whatever is in the
invisible paragraph, so you should not use this to hide sensitive information such
as credit card details or passwords.
The visibility property can take the values listed in the table that follows:
Value Description
visible The box and its contents are shown to the user.
hidden The box and its content are made invisible, although they still affect
the layout of the page.
collapse This is for use only with dynamic table columns and row effects.
Here is an example:
<p>
This paragraph should be visible in normal way.
</p>
<p style="visibility:hidden;">
This paragraph should not be visible.
</p>
93
CSS
94
21. POSITIONING
CSS
CSS helps you to position your HTML element. You can put any HTML element at
whatever location you like. You can specify whether you want the element
positioned relative to its natural position in the page or absolute based on its
parent element.
Now, we will see all the CSS positioning related properties with examples.
Relative Positioning
Relative positioning changes the position of the HTML element relative to where
it normally appears. So "left:20" adds 20 pixels to the element's LEFT position.
You can use two values top and left along with the position property to move an
HTML element anywhere in an HTML document.
NOTE: You can use the bottom or right values as well in the same way as top
and left.
Here is an example:
<div style="position:relative;left:80px;top:2px;
background-color:yellow;">
This div has relative positioning.
</div>
95
CSS
Absolute Positioning
An element with position: absolute is positioned at the specified coordinates
relative to your screen top-left corner.
You can use two values top and left along with the position property to move an
HTML element anywhere in HTML document.
NOTE: You can use bottom or right values as well in the same way as top and
left.
Here is an example:
<div style="position:absolute;left:80px;top:20px;
background-color:yellow;">
This div has absolute positioning.
</div>
Fixed Positioning
Fixed positioning allows you to fix the position of an element to a particular spot
on the page, regardless of scrolling. Specified coordinates will be relative to the
browser window.
You can use two values top and left along with the position property to move an
HTML element anywhere in the HTML document.
NOTE: You can use bottom or right values as well in the same way as top and
left.
96
CSS
Here is an example:
<div style="position:fixed;left:80px;top:20px;
background-color:yellow;">
This div has fixed positioning.
</div>
97
22. LAYERS
CSS
CSS gives you an opportunity to create layers of various divisions. The CSS
layers refer to applying the z-index property to elements that overlap with each
other.
The z-index property is used along with the position property to create an effect
of layers. You can specify which element should come on top and which element
should come at bottom.
A z-index property can help you to create more complex webpage layouts. The
following example shows how to create layers in CSS.
<div style="background-color:red;
width:300px;
height:100px;
position:relative;
top:10px;
left:80px;
z-index:2">
</div>
<div style="background-color:yellow;
width:300px;
height:100px;
position:relative;
top:-60px;
left:35px;
z-index:1;">
</div>
<div style="background-color:green;
width:300px;
height:100px;
position:relative;
top:-220px;
98
CSS
left:120px;
z-index:3;">
</div>
99
23. PSEUDO CLASSES
CSS
CSS pseudo-classes are used to add special effects to some selectors. You do
not need to use JavaScript or any other script to use those effects. A simple
syntax of pseudo-classes is as follows:
Value Description
:hover Use this class to add special style to an element when you mouse
over it.
:focus Use this class to add special style to an element while the element
has focus.
:first- Use this class to add special style to an element that is the first
child child of some other element.
100
CSS
• a:hover MUST come after a:link and a:visited in the CSS definition in
order to be effective.
• Pseudo-classes are different from CSS classes, but they can be combined.
The :linkpseudo-class
The following example demonstrates how to use :link class to set the link color.
Possible values could be any color name in any valid format.
<style type="text/css">
a:link {color:#000000}
</style>
<a href="/html/index.htm">Black Link</a>
The :visitedpseudo-class
The following example demonstrates how to use :visited class to set the color of
the visited links. Possible values could be any color name in any valid format.
<style type="text/css">
a:visited {color: #006600}
</style>
<a href="/html/index.htm">Click this link</a>
It will produce the following link. Once you click this link, it will change its color
to green.
101
CSS
The :hoverpseudo-class
The following example demonstrates how to use the :hover class to change the
color of links when we bring a mouse pointer over that link. Possible values
could be any color name in any valid format.
<style type="text/css">
a:hover {color: #FFCC00}
</style>
<a href="/html/index.htm">Bring Mouse Here</a>
It will produce the following link. Now you bring your mouse over this link and
you will see that it changes its color to yellow.
The :activepseudo-class
The following example demonstrates how to use the :active class to change the
color of active links. Possible values could be any color name in any valid format.
<style type="text/css">
a:active {color: #FF00CC}
</style>
<a href="/html/index.htm">Click This Link</a>
It will produce the following link. When a user clicks it, the color changes to
pink.
The :focuspseudo-class
The following example demonstrates how to use the :focus class to change the
color of the focused links. Possible values could be any color name in any valid
format.
<style type="text/css">
a:focus {color: #0000FF}
102
CSS
</style>
<a href="/html/index.htm">Click this Link</a>
It will produce the following link. When this link gets focused, its color changes
to orange. The color changes back when it loses focus.
The :first-childpseudo-class
The :first-child pseudo-class matches a specified element that is the first child of
another element and adds special style to that element that is the first child of
some other element.
For example, to indent the first paragraph of all <div> elements, you could use
this definition:
<style type="text/css">
div > p:first-child
{
text-indent: 25px;
}
</style>
<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>
<p>But it will not match the paragraph in this HTML:</p>
<div>
<h3>Heading</h3>
103
CSS
<p>
The first paragraph inside the div.
This paragraph will not be effected.
</p>
</div>
The :langpseudo-class
The language pseudo-class :lang, allows constructing selectors based on the
language setting for specific tags.
This class is useful in documents that must appeal to multiple languages that
have different conventions for certain language constructs. For example, the
French language typically uses angle brackets (< and >) for quoting purposes,
while the English language uses quote marks (' and ').
In a document that needs to address this difference, you can use the :lang
pseudo-class to change the quote marks appropriately. The following code
changes the <blockquote> tag appropriately for the language being used:
<style type="text/css">
/* Two levels of quotes for two languages*/
:lang(en) { quotes: '"' '"' "'" "'"; }
:lang(fr) { quotes: "<<" ">>" "<" ">"; }
</style>
<p>...<q lang="fr">A quote in a paragraph</q>...</p>
104
CSS
The :lang selectors will apply to all the elements in a document. However, not all
elements make use of the quotes property, so the effect will be transparent for
most elements.
105
24. PSEUDO ELEMENTS
CSS
CSS pseudo-elements are used to add special effects to some selectors. You do
not need to use JavaScript or any other script to use those effects. A simple
syntax of pseudo-element is as follows:
Value Description
:first-line Use this element to add special styles to the first line of the text
in a selector.
:first-letter Use this element to add special style to the first letter of the text
in a selector.
The :first-linepseudo-element
The following example demonstrates how to use the :first-line element to add
special effects to the first line of elements in a document.
<style type="text/css">
p:first-line { text-decoration: underline; }
p.noline:first-line { text-decoration: none; }
</style>
<p class="noline"> This line would not have any underline
106
CSS
The :first-letterpseudo-element
The following example demonstrates how to use the :first-letter element to add
special effect to the first letter of elements in the document.
<style type="text/css">
p:first-letter { font-size: 5em; text-color:red; }
p.normal:first-letter { font-size: 10px; }
</style>
<p class="normal"> First character of this paragraph will
be normal and will have font size 10 px;</p>
107
CSS
The :beforepseudo-element
The following example demonstrates how to use :before element to add some
content before any element.
<style type="text/css">
p:before
{
content: url(/images/bullet.gif)
}
</style>
<p> This line will be preceded by a bullet.</p>
<p> This line will be preceded by a bullet.</p>
<p> This line will be preceded by a bullet.</p>
The :afterpseudo-element
The following example demonstrates how to use :after element to add some
content after any element.
<style type="text/css">
108
CSS
p:after
{
content: url(/images/bullet.gif)
}
</style>
<p> This line will be succeeded by a bullet.</p>
<p> This line will be succeeded by a bullet.</p>
<p> This line will be succeeded by a bullet.</p>
109
25. @ RULES
CSS
• The @import: rule imports another style sheet into the current style
sheet.
• The @charset rule indicates the character set the style sheet uses.
• The @font-face rule is used to exhaustively describe a font face for use in
a document.
NOTE: There are other @ rules which we will cover in subsequent chapters.
The @importRule
The @import rule allows you to import styles from another style sheet. It should
appear right at the start of the style sheet before any of the rules, and its value
is a URL.
<style tyle="text/css">
<!--
@import "mystyle.css";
or
@import url("mystyle.css");
.......other CSS rules .....
-->
</style>
The significance of the @import rule is that it allows you to develop your style
sheets with a modular approach. You can create various style sheets and then
include them wherever you need them.
110
CSS
The @charsetRule
If you are writing your document using a character set other than ASCII or ISO-
8859-1 you might want to set the @charset rule at the top of your style sheet to
indicate what character set the style sheet is written in.
The @charset rule must be written right at the beginning of the style sheet
without even a space before it. The value is held in quotes and should be one of
the standard character-sets. For example:
<style tyle="text/css">
<!--
@charset "iso-8859-1"
.......other CSS rules .....
-->
</style>
The @font-faceRule
The @font-face rule is used to exhaustively describe a font face for use in a
document. @font-face may also be used to define the location of a font for
download, although this may run into implementation-specific limits.
Here is an example:
<style tyle="text/css">
<!--
@font-face {
font-family: "Scarborough Light";
src: url("http://www.font.site/s/scarbo-lt");
}
@font-face {
font-family: Santiago;
src: local ("Santiago"),
url("http://www.font.site/s/santiago.tt")
111
CSS
format("truetype");
unicode-range: U+??,U+100-220;
font-size: all;
font-family: sans-serif;
}
-->
</style>
The !importantRule
Cascading Style Sheets cascade. It means that the styles are applied in the
same order as they are read by the browser. The first style is applied and then
the second and so on.
The !important rule provides a way to make your CSS cascade. It also includes
the rules that are to be applied always. A rule having a !important property will
always be applied, no matter where that rule appears in the CSS document.
For example, in the following style sheet, the paragraph text will be black, even
though the first style property applied is red:
<style tyle="text/css">
<!--
p { color: #ff0000; }
p { color: #000000; }
-->
</style>
So, if you wanted to make sure that a property always applied, you would add
the !important property to the tag. So, to make the paragraph text always red,
you should write it as follows:
<style tyle="text/css">
<!--
p { color: #ff0000 !important; }
p { color: #000000; }
-->
112
CSS
</style>
Here you have made p { color: #ff0000 !important; } mandatory, now this rule
will always apply even you have defined another rule p { color: #000000; }.
113
26. FILTERS
CSS
You can use CSS filters to add special effects to text, images and other aspects
of a webpage without using images or other graphics. Filters only work on
Internet Explorer 4.0. If you are developing your site for multiple browsers,
then it may not be a good idea to use CSS filters because there is a possibility
that it would not give any advantage.
In this chapter, we will see the details of each CSS filter. These filters may not
work in your browser.
Alpha Channel
The Alpha Channel filter alters the opacity of the object, which makes it blend
into the background. The following parameters can be used in this filter:
Parameter Description
0 = uniform
1 = linear
2 = radial
3 = rectangular
114
CSS
Example
<p>Image Example:</p>
<p>Text Example:</p>
Motion Blur
Motion Blur is used to create blurred pictures or text with the direction and
strength. The following parameters can be used in this filter:
Parameter Description
add True or false. If true, the image is added to the blurred image;
and if false, the image is not added to the blurred image.
115
CSS
0 = Top
45 = Top right
90 = Right
180 = Bottom
270 = Left
strength The number of pixels the blur will extend. The default is 5 pixels.
Example
<p>Image Example:</p>
<p>Text Example:</p>
116
CSS
Chroma Filter
Chroma Filter is used to make any particular color transparent and usually it is
used with images. You can use it with scrollbars also. The following parameter
can be used in this filter:
Parameter Description
Example
<p>Image Example:</p>
<p>Text Example:</p>
117
CSS
Drop ShadowEffect
Drop Shadow is used to create a shadow of your object at the specified X
(horizontal) and Y (vertical) offset and color.
Parameter Description
offX Number of pixels the drop shadow is offset from the visual
object, along the x-axis. Positive integers move the drop shadow
to the right, negative integers move the drop shadow to the left.
offY Number of pixels the drop shadow is offset from the visual
object, along the y-axis. Positive integers move the drop shadow
down, negative integers move the drop shadow up.
118
CSS
Example
<p>Image Example:</p>
<p>Text Example:</p>
119
CSS
Flip Effect
Flip effect is used to create a mirror image of the object. The following
parameters can be used in this filter:
Parameter Description
Example
<p>Image Example:</p>
<p>Text Example:</p>
120
CSS
Glow Effect
Glow effect is used to create a glow around the object. If it is a transparent
image, then glow is created around the opaque pixels of it. The following
parameters can be used in this filter:
Parameter Description
121
CSS
Example
<p>Image Example:</p>
<p>Text Example:</p>
Grayscale Effect
Grayscale effect is used to convert the colors of the object to 256 shades of
gray. The following parameter is used in this filter:
Parameter Description
122
CSS
Example
<p>Image Example:</p>
<p>Text Example:</p>
Invert Effect
Invert effect is used to map the colors of the object to their opposite values in
the color spectrum, i.e., to create a negative image. The following parameter is
used in this filter:
Parameter Description
Invert Maps the colors of the object to their opposite value in the color
spectrum.
123
CSS
Example
<p>Image Example:</p>
<p>Text Example:</p>
Mask Effect
Mask effect is used to turn transparent pixels to a specified color and makes
opaque pixels transparent. The following parameter is used in this filter:
Parameter Description
124
CSS
Example
<p>Image Example:</p>
<p>Text Example:</p>
Shadow Filter
Shadow filter is used to create an attenuated shadow in the direction and color
specified. This is a filter that lies in between Dropshadow and Glow. The
following parameters can be used in this filter:
Parameter Description
125
CSS
0 = Top
45 = Top right
90 = Right
180 = Bottom
270 = Left
Example
<p>Image Example:</p>
<p>Text Example:</p>
126
CSS
Wave Effect
Wave effect is used to give the object a sine wave distortion to make it look
wavy. The following parameters can be used in this filter:
Parameter Description
Add A value of 1 adds the original image to the waved image, 0 does
not.
phase At what degree the sine wave should start (from 0 to 100).
127
CSS
Example
<p>Image Example:</p>
<p>Text Example:</p>
X-Ray Effect
X- Ray effect grayscales and flattens the color depth. The following parameter is
used in this filter:
Parameter Description
128
CSS
Example
<p>Image Example:</p>
<p>Text Example:</p>
129
27. MEDIA TYPES
CSS
One of the most important features of style sheets is that they specify how a
document is to be presented on different media: on the screen, on paper, with a
speech synthesizer, with a braille device, etc.
We have currently two ways to specify media dependencies for style sheets:
• Specify the target medium from a style sheet with the @media or
@import at-rules.
The @mediarule
The @media rule specifies the target media types (separated by commas) of a
set of rules.
<style tyle="text/css">
<!--
@media print {
body { font-size: 10pt }
}
@media screen {
body { font-size: 12pt }
}
@media screen, print {
body { line-height: 1.2 }
}
-->
</style>
130
CSS
The DocumentLanguage
In HTML 4.0, the media attribute on the LINK element specifies the target media
of an external style sheet.
Following is an example:
<style tyle="text/css">
<!--
<!doctype html public "-//w3c//dtd html 4.0//en">
<html>
<head>
<title>link to a target medium</title>
<link rel="stylesheet" type="text/css"
media="print, handheld" href="foo.css">
</head>
<body>
<p>the body...
</body>
</html>
-->
</style>
Recognized MediaTypes
The names chosen for CSS media types reflect target devices for which the
relevant properties make sense. They give a sense of what device the media
type is meant to refer to. Given below is a list of various media types:
Value Description
131
CSS
print Intended for paged, opaque material and for documents viewed on
screen in print preview mode. Please consult the section on paged
media.
132
28. PAGED MEDIA
CSS
Paged media differ from continuous media in that the content of the document is
split into one or more discrete pages. Paged media includes paper,
transparencies, pages that are displayed on computer screens, etc.
The CSS2 standard introduces some basic pagination control features that let
authors help the browser figure out how to best print their documents.
• The page area: The page area includes the boxes laid out on that page.
The edges of the page area act as the initial containing block for layout
that occurs between page breaks.
You can specify the dimensions, orientation, margins, etc., of a page box within
an @page rule. The dimensions of the page box are set with the 'size' property.
The dimensions of the page area are the dimensions of the page box minus the
margin area.
For example, the following @page rule sets the page box size to 8.5 x 11 inches
and creates '2cm' margin on all sides between the page box edge and the page
area:
<style type="text/css">
<!--
@page { size:8.5in 11in; margin: 2cm }
-->
</style>
133
CSS
You can use the margin, margin-top, margin-bottom, margin-left, and margin-
right properties within the @page rule to set margins for your page.
Finally, the marks property is used within the @page rule to create crop and
registration marks outside the page box on the target sheet. By default, no
marks are printed. You may use one or both of the crop and cross keywords to
create crop marks and registration marks, respectively, on the target print page.
Setting PageSize
The size property specifies the size and orientation of a page box. There are four
values, which can be used for page size:
• auto: The page box will be set to the size and orientation of the target
sheet.
• portrait: Overrides the target's orientation. The page box is the same size
as the target, and the shorter sides are horizontal.
• length: Length values for the 'size' property create an absolute page box.
If only one length value is specified, it sets both the width and height of
the page box. Percentage values are not allowed for the 'size' property.
In the following example, the outer edges of the page box will align with the
target. The percentage value on the 'margin' property is relative to the target
size so if the target sheet dimensions are 21.0cm x 29.7cm (i.e., A4), the
margins are 2.10cm and 2.97cm.
<style type="text/css">
<!--
@page {
size: auto; /* auto is the initial value */
margin: 10%;
}
-->
</style>
The following example sets the width of the page box to be 8.5 inches and the
height to be 11 inches. The page box in this example requires a target sheet size
of 8.5"x11" or larger.
134
CSS
<style type="text/css">
<!--
@page {
size: 8.5in 11in; /* width height */
}
-->
</style>
Once you create a named page layout, you can use it in your document by
adding the page property to a style that is later applied to an element in your
document. For example, this style renders all the tables in your document on
landscape pages:
<style type="text/css">
<!--
@page { size : portrait }
@page rotated { size : landscape }
table { page : rotated }
-->
</style>
Due to the above rule, while printing, if the browser encounters a <table>
element in your document and the current page layout is the default portrait
layout, it starts a new page and prints the table on a landscape page.
<style type="text/css">
<!--
@page :left {
margin-left: 4cm;
margin-right: 3cm;
}
135
CSS
@page :right {
margin-left: 3cm;
margin-right: 4cm;
}
-->
</style>
You can specify the style for the first page of a document with the :first pseudo-
class:
<style type="text/css">
<!--
@page { margin: 2cm } /* All margins set to 2cm */
@page :first {
margin-top: 10cm /* Top margin on first page 10cm */
}
-->
</style>
Controlling Pagination
Unless you specify otherwise, page breaks occur only when the page format
changes or when the content overflows the current page box. To otherwise force
or suppress page breaks, use the page-break-before, page-break-after, and
page-break-inside properties.
The keyword auto is the default, it lets the browser generate page breaks as
needed. The keyword always forces a page break before or after the element,
while avoid suppresses a page break immediately before or after the element.
The left and right keywords force one or two page breaks, so that the element is
rendered on a left-hand or right-hand page.
136
CSS
You'd like each chapter to start on a new, right-hand page, but you don't want
section headers to be split across a page break from the subsequent content.
You can achieve this using the following rule:
<style type="text/css">
<!--
h1 { page-break-before : right }
h2 { page-break-after : avoid }
-->
</style>
Use only the auto and avoid values with the page-break-inside property. If you
prefer that your tables are not be broken across pages if possible, you would
write the rule as follows:
<style type="text/css">
<!--
table { page-break-inside : avoid }
-->
</style>
Here is an example to create 4 lines at the bottom and 3 lines at the top of each
page:
<style type="text/css">
137
CSS
<!--
@page{orphans:4; widows:2;}
-->
</style>
138
29. AURAL MEDIA
CSS
• Learning to read
• Training
• Home entertainment
• Industrial documentation
• Medical documentation
The CSS properties also allow you to vary the quality of synthesized speech
(voice type, frequency, inflection, etc.).
Here is an example:
<style tyle="text/css">
<!--
h1, h2, h3, h4, h5, h6 {
voice-family: paul;
stress: 20;
richness: 90;
cue-before: url("ping.au")
}
p.heidi { azimuth: center-left }
p.peter { azimuth: right }
-->
139
CSS
</style>
It will direct the speech synthesizer to speak headers in a voice (a kind of audio
font) called "paul", on a flat tone, but in a very rich voice. Before speaking the
headers, a sound sample will be played from the given URL.
Paragraphs with class ‘heidi’ will appear to come from front left (if the sound
system is capable of spatial audio), and paragraphs of class ‘peter’ from the
right.
• The azimuth property sets, where the sound should come from
horizontally.
• The elevation property sets, where the sound should come from
vertically.
• The pitch specifies the average pitch (a frequency) of the speaking voice.
• The speak specifies whether the text will be rendered aurally and if so, in
what manner.
140
CSS
• The stress specifies the height of "local peaks" in the intonation contour
of a voice.
The azimuthProperty
The azimuth property sets where the sound should come from horizontally. The
possible values are listed below:
• leftwards: Moves the sound to the left and relative to the current angle.
More precisely, subtracts 20 degrees.
• rightwards: Moves the sound to the right, relative to the current angle.
More precisely, adds 20 degrees.
Here is an example:
<style tyle="text/css">
<!--
h1 { azimuth: 30deg }
td.a { azimuth: far-right } /* 60deg */
#12 { azimuth: behind far-right } /* 120deg */
141
CSS
The elevationProperty
The elevation property sets where the sound should come from vertically. The
possible values are as follows:
Here is an example:
<style tyle="text/css">
<!--
h1 { elevation: above }
tr.a { elevation: 60deg }
tr.b { elevation: 30deg }
tr.c { elevation: level }
-->
</style>
The cue-afterProperty
The cue-after property specifies a sound to be played after speaking an
element's content to delimit it from other. The possible values include:
Here is an example:
<style tyle="text/css">
<!--
a {cue-after: url("dong.wav");}
h1 {cue-after: url("pop.au"); }
-->
</style>
The cue-beforeProperty
This property specifies a sound to be played before speaking an element's
content to delimit it from other. The possible values include:
Here is an example:
<style tyle="text/css">
<!--
a {cue-before: url("bell.aiff");}
h1 {cue-before: url("pop.au"); }
-->
</style>
The cueProperty
The cue property is a shorthand for setting cue-before and cue-after. If two
values are given, the first value is cue-before and the second is cue-after. If only
one value is given, it applies to both properties.
<style tyle="text/css">
<!--
h1 {cue-before: url("pop.au"); cue-after: url("pop.au") }
h1 {cue: url("pop.au") }
143
CSS
-->
</style>
The pause-afterProperty
This property specifies a pause to be observed after speaking an element's
content. The possible values are:
The pause-beforeProperty
This property specifies a pause to be observed before speaking an element's
content. The possible values are:
The pauseProperty
This property is a shorthand for setting pause-before and pause-after. If two
values are given, the first value is pause-before and the second is pause-after.
Here is an example:
<style tyle="text/css">
<!--
/* pause-before: 20ms; pause-after: 20ms */
h1 { pause : 20ms }
/* pause-before: 30ms; pause-after: 40ms */
h2{ pause : 30ms 40ms }
144
CSS
The pitchProperty
This property specifies the average pitch (a frequency) of the speaking voice.
The average pitch of a voice depends on the voice family. For example, the
average pitch for a standard male voice is around 120Hz, but for a female voice,
it's around 210Hz. The possible values are:
The pitch-rangeProperty
This property specifies variation in average pitch. The possible values are:
• number: A value between '0' and '100'. A pitch range of '0' produces a
flat, monotonic voice. A pitch range of 50 produces normal inflection. Pitch
ranges greater than 50 produce animated voices.
The play-duringProperty
This property specifies a sound to be played as a background while an element's
content is spoken. Possible values could be any of the followings:
• mix: When present, this keyword means that the sound inherited from
the parent element's play-during property continues to play and the sound
designated by the uri is mixed with it. If mix is not specified, the element's
background sound replaces the parent's.
• repeat: When present, this keyword means that the sound will repeat if it
is too short to fill the entire duration of the element. Otherwise, the sound
plays once and then stops.
145
CSS
Here is an example:
<style tyle="text/css">
<!--
blockquote.sad { play-during: url("violins.aiff") }
blockquote q { play-during: url("harp.wav") mix }
span.quiet { play-during: none }
-->
</style>
The richnessProperty
This property specifies the richness or brightness of the speaking voice. The
possible values are:
• number: A value between '0' and '100'. The higher the value, the more
the voice will carry. A lower value will produce a soft, mellifluous voice.
The speakProperty
This property specifies whether the text will be rendered aurally and if so, in
what manner. The possible values are:
Note the difference between an element whose 'volume' property has a value of
'silent' and an element whose 'speak' property has the value 'none'. The former
takes up the same time as if it had been spoken, including any pause before and
after the element, but no sound is generated. The latter requires no time and is
not rendered.
146
CSS
The speak-numeralProperty
This property controls how numerals are spoken. The possible values are:
• digits: Speak the numeral as individual digits. Thus, "237" is spoken "Two
Three Seven".
The speak-punctuationProperty
This property specifies how punctuation is spoken. The possible values are:
The speech-rateProperty
This property specifies the speaking rate. Note that both absolute and relative
keyword values are allowed. The possible values are:
• slower: Subtracts 40 words per minutes from the current speech rate.
The stressProperty
This property specifies the height of "local peaks" in the intonation contour of a
voice. English is a stressed language, and different parts of a sentence are
assigned primary, secondary, or tertiary stress. The possible values are:
147
CSS
• number: A value between '0' and '100'. The meaning of values depends
on the language being spoken. For example, a level of '50' for a standard,
English-speaking male voice (average pitch = 122Hz), speaking with
normal intonation and emphasis would have a different meaning than '50'
for an Italian voice.
The voice-familyProperty
The value is a comma-separated, prioritized list of voice family names. It can
have the following values:
Here is an example:
<style tyle="text/css">
<!--
h1 { voice-family: announcer, male }
p.part.romeo { voice-family: romeo, male }
p.part.juliet { voice-family: juliet, female }
-->
</style>
The volumeProperty
Volume refers to the median volume of the voice. It can have the following
values:
• numbers: Any number between '0' and '100'. '0' represents the minimum
audible volume level and 100 corresponds to the maximum comfortable
level.
• silent: No sound at all. The value '0' does not mean the same as 'silent'.
148
CSS
Here is an example:
<style tyle="text/css">
<!--
P.goat { volume: x-soft }
-->
</style>
149
30. PRINTING
CSS
You can use CSS to change the appearance of your web page when it is printed
on a paper. You can specify one font for the screen version and another for the
print version.
You have seen @media rule in the previous chapters. This rule allows you to
specify different styles for different media. So, you can define different rules for
a screen and a printer.
The following example specifies different font families for screen and printer. The
next CSS uses the same font size for both screen as well as printer.
<style tyle="text/css">
<!--
@media screen
{
p.bodyText {font-family:verdana, arial, sans-serif;}
}
@media print
{
p.bodyText {font-family:georgia, times, serif;}
}
@media screen, print
{
p.bodyText {font-size:10pt}
}
-->
</style>
150
CSS
If you are defining your style sheet in a separate file, then you can also use the
media attribute when linking to an external style sheet:
151
31. LAYOUTS
CSS
Hope you are very comfortable with HTML tables and you are efficient in
designing page layouts using HTML Tables. But you know CSS too provides
plenty of controls for positioning elements in a document. Since CSS is the wave
of the future, why not learn and use CSS instead of tables for page layout
purposes?
The following list collects a few pros and cons of both the technologies:
• Most browsers support tables, while CSS support is being slowly adopted.
• Tables are more forgiving when the browser window size changes -
morphing their content and wrapping to accommodate the changes
accordingly. CSS positioning tends to be exact and fairly inflexible.
• Tables are much easier to learn and manipulate than CSS rules.
• Keeping track of nested tables can be a real pain. CSS rules tend to be
well organized, easily read, and easily changed.
Finally, we would suggest you to use whichever technology makes sense to you
and use what you know or what presents your documents in the best way.
CSS also provides table-layout property to make your tables load much faster.
Following is an example:
<table style="table-layout:fixed;width:600px;">
<tr height="30">
<td width="150">CSS table layout cell 1</td>
<td width="200">CSS table layout cell 2</td>
<td width="250">CSS table layout cell 3</td>
</tr>
</table>
152
CSS
You will notice the benefits more on large tables. With traditional HTML, the
browser had to calculate every cell before finally rendering the table. When you
set the table-layout algorithm to fixed, however, it only needs to look at the first
row before rendering the whole table. It means your table will need to have
fixed column widths and row heights.
Sample ColumnLayout
Here are the steps to create a simple Column Layout using CSS:
<style tyle="text/css">
<!--
body {
margin:9px 9px 0 9px;
padding:0;
background:#FFF;
}
-->
</style>
Now, we will define a column with yellow color and later, we will attach this rule
to a <div>:
<style tyle="text/css">
<!--
#level0 {
background:#FC0;
}
-->
</style>
Up to this point, we will have a document with yellow body, so let us now define
another division inside level0:
<style tyle="text/css">
<!--
153
CSS
#level1 {
margin-left:143px;
padding-left:9px;
background:#FFF;
}
-->
</style>
Now, we will nest one more division inside level1, and we will change just the
background color:
<style tyle="text/css">
<!--
#level2 {
background:#FFF3AC;
}
-->
</style>
Finally, we will use the same technique, nest a level3 division inside level2 to get
the visual layout for the right column:
<style tyle="text/css">
<!--
#level3 {
margin-right:143px;
padding-right:9px;
background:#FFF;
}
#main {
background:#CCC;
}
-->
</style>
154
CSS
<style tyle="text/css">
<!--
body {
margin:9px 9px 0 9px;
padding:0;
background:#FFF;}
#level0 {
background:#FC0;}
#level1 {
margin-left:143px;
padding-left:9px;
background:#FFF;}
#level2 {
background:#FFF3AC;}
#level3 {
margin-right:143px;
padding-right:9px;
background:#FFF;}
#main {
background:#CCC;}
-->
</style>
<body>
<div id="level0">
<div id="level1">
<div id="level2">
<div id="level3">
<div id="main">
Final Content goes here...
</div>
155
CSS
</div>
</div>
</div>
</div>
</body>
Similarly, you can add a top navigation bar or an ad bar at the top of the page.
156
32. VALIDATIONS
CSS
Validation is the process of checking something against a rule. When you are a
beginner, it is very common that you will commit many mistakes in writing your
CSS rules. How you will make sure whatever you have written is 100% accurate
and up to the W3 quality standards?
If you use CSS, your code needs to be correct. Improper code may cause
unexpected results in how your page looks or functions.
But if you want to validate your CSS style sheet embedded in an (X)HTML
document, you should first check that the (X)HTML you use is valid.
You can use the following tools to check the validity of your CSS.
The WDG CSS check validator, lets you validate your CSS by
direct input, file upload, and using URI. Errors will be listed by
line and column numbers if you have any. Errors usually come
with links to explain the reason of error.
A CSS validator checks your Cascading Style Sheets to make sure they comply
with the CSS standards set by the W3 Consortium. There are a few validators
which will also tell you which CSS features are supported by which browsers
(since not all browsers are equal in their CSS implementation).
157
CSS
158
33. CSS2 REFERENCE GUIDE
CSS
This is a complete reference guide for web developers where we have listed all
the CSS properties defined in the World Wide Web Consortium's Recommended
Specification for Cascading Style Sheets, Level 2.
• Aural
• Background
• Border
• Classification
• Dimension
• Font
• Generated Content
• List and Marker
• Margin
• Outlines
• Padding
• Positioning
• Table
• Text
• Print
• Pseudo-classes
• Pseudo-elements
Property Description
background-attachment
background-color
background-image
background-position
background-repeat
159
CSS
160
CSS
bottom Used with the position property to place the bottom edge of
an element
161
CSS
font Sets all the font attributes for an element. Value is any of
the values for:
font-style
font-variant
font-weight
font-size
line-height
font-family
font-style Defines the style of the face, either normal or some type of
162
CSS
slanted style
left Used with the position property to place the left edge of an
element
list-style-image
liststyle-position
list-style-type
list-style-type
163
CSS
marks The marks property is used to set crop marks and cross
marks on paged media. This is used with the @page rule.
164
CSS
pitch-range Sets the range of the pitch, from 0 (flat) to 100 (broad);
default is 50
right Used with the position property to place the right edge of
an element.
165
CSS
size The size property is used in paged media to specify the size
of the page.
speak-header Determines if table headers are spoken once for each row
or column or each time a cell is spoken.
speech-rate Sets the rate of speech; a number sets the rate in words
per minute
top Used with the position property to place the top edge of an
element.
166
CSS
:focus Use this class to add special effect to an element while the
element has focus
:hover Use this class to add special effect to an element when you
mouse over it
:first-child Use this class to add special effect to an element that is the
first child of some other element.
:first-letter Use this element to add special effect to the first letter of a text
:first-line Use this element to add special effect to the first line of a text
167
34. COLOR REFERENCES
CSS
The following table shows the 16 color names that were introduced in HTML 3.2,
to support the 16 colors that 8-bit graphics cards offered. Same set of color can
be used in CSS:
168
CSS
There are other colors, which are not part of HTML or XHTML but they are
supported by most of the versions of IE or Netscape. These color names can be
used in CSS as well.
169
CSS
170
CSS
171
CSS
172
CSS
173