CSS Material
CSS Material
What is CSS?
CSS stands for Cascading Style Sheets, is a simple design language used to control
the style of a web pages in a simple and easy way.
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, and layout designs, in
display for different devices and screen sizes as well as a variety of other effects.
Advantages of CSS
CSS saves time − you can write CSS once and then reuse same sheet in
multiple HTML pages. You can define a style for each HTML element and apply it
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.
Easy maintenance − To make a global change, simply change the style, and
all elements in all the web pages will be updated automatically.
Multiple Device Compatibility − Style sheets allow content to be optimized
for more than one type of device.
Offline Browsing − CSS can store web applications locally with the help of an
offline [Link] of this, we can view offline [Link] cache also ensures
faster loading and better overall performance of the website.
Platform Independence − The Script offer consistent platform independence
and can support latest browsers as well.
CSS - Syntax
1
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 −
Selector − A selector is an HTML tag at which a style will be applied. This could
be any tag like <h1> or <table> etc.
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
value either red or #F1F1F1 etc.
selector{property: value }
Here table is a selector and border is a property and 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.
h1 {
2
CSS
color:#36CFFF;
}
*{
color:#000000;
}
ulem{
color:#000000;
}
.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:
<pclass="center bold">
This para will be styled by the classescenter and bold.
3
CSS
</p>
The ID Selectors
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;
}
Body > p {
color:#000000;
}
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.
4
CSS
Multiple Style Rules
You may need to define multiple style rules for a single element. You can define these
rules to combine multiple properties and corresponding values into a single block as
defined in the following example −
h1 {
color:#36C;
font-weight: normal;
letter-spacing:.4em;
margin-bottom:1em;
text-transform: lowercase;
}
Here all the property and value pairs are separated by a semi colon (;). You can keep
them in a single line or multiple lines. For better readability we keep them into separate
lines.
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.
5
CSS
#content, #footer, #supplement {
position: absolute;
left:510px;
width:200px;
}
CSS - Inclusion
There are four ways to associate styles with your HTML document. Most commonly
used methods are inline CSS and External CSS.
<!DOCTYPE html>
<html>
<head>
<styletype="text/css"media="all">
body{
background-color: linen;
}
h1 {
color: maroon;
margin-left:40px;
}
</style>
</head>
6
CSS
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Attributes
Attributes associated with <style> elements are −
<elementstyle="...style rules....">
Attributes
Attribut Value Description
e
<html>
<head>
7
CSS
</head>
<body>
<h1style="color:#36C;"> This is inline CSS </h1>
</body>
</html>
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.
h1, h2, h3 {
color:#36C;
font-weight: normal;
8
CSS
letter-spacing:.4em;
margin-bottom:1em;
text-transform: lowercase;
}
Now you can include this file [Link] in any HTML document as follows −
<head>
<linktype="text/css"href="[Link]"media=" all"/>
</head>
Any inline style sheet takes highest priority. So, it will override any rule defined
in <style>...</style> tags or rules defined in any external style sheet file.
Any rule defined in <style>...</style> tags will override rules defined in any
external style sheet file.
Any rule defined in external style sheet file takes lowest priority, and rules
defined in this file will be applied only when above two rules are not applicable.
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 style sheet. You can simple put your comments
inside /*.....this is a comment in style sheet.....*/.
You can use /* ....*/ to comment multi-line blocks in similar way you do in C and C++
programming languages.
<!DOCTYPE html>
<html>
<head>
<style>
9
CSS
p{
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is a multi-line comment */
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
We have listed out all the CSS Measurement Units along with proper Examples −
10
CSS
would be 16px; thus, 2em would be 32px.
(emphemeral unit)
CSS - Colors
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 else 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 −
11
CSS
Each hexadecimal code will be preceded by a pound or hash sign '#'. Following are the
examples to use Hexadecimal notation.
Each hexadecimal code will be preceded by a pound or hash sign '#'. Following are the
examples to use Hexadecimal notation.
CSS - Background
How to set backgrounds of various HTML elements. You can set the following
background properties of an element −
<html>
12
CSS
<head>
<body>
<pstyle="background-color:yellow;">
This text has a yellow background color.</p>
</body>
</head>
<html>
<html>
<head>
<style>
body {
background-image:url("/css/images/[Link]");
background-color:#cccccc;
}
</style>
<body>
<h1>Hello World!</h1>
</body>
</head>
<html>
<html>
13
CSS
<head>
<style>
body{
background-image:url("/css/images/[Link]");
background-repeat: repeat;
}
</style>
</head>
<body>
<p>ELearnInfotech</p>
</body>
</html>
The following example which demonstrates how to repeat the background image
vertically.
<html>
<head>
<style>
body{
background-image:url("/css/images/[Link]");
background-repeat: repeat-y;
}
</style>
</head>
<body>
<p>ELearnInfotech</>
</body>
</html>
The following example demonstrates how to repeat the background image horizontally.
<html>
<head>
14
CSS
<style>
body{
background-image:url("/css/images/[Link]");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<p>ELearnInfotech</>
</body>
</html>
<html>
<head>
<style>
body{
background-image:url("/css/images/[Link]");
background-position:100px;
}
</style>
</head>
<body>
<p>ELearnInfotech</>
</body>
</html>
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.
15
CSS
<html>
<head>
<style>
body{
background-image:url("/css/images/[Link]");
background-position:100px200px;
}
</style>
</head>
<body>
<p>ELearnInfotech</>
</body>
</html>
The following example demonstrates how to set the fixed background image.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image:url('/css/images/[Link]');
background-repeat:no-repeat;
background-attachment:fixed;
}
</style>
16
CSS
</head>
<body>
The following example demonstrates how to set the scrolling background image.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image:url('/css/images/[Link]');
background-repeat:no-repeat;
background-attachment:fixed;
background-attachment:scroll;
}.
</style>
</head>
<body>
17
CSS
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
</body>
</html>
CSS - Fonts
This chapter teaches you how to set fonts of a content, available in an HTML element.
You can set following font properties of an element −
<html>
<head>
</head>
<body>
<pstyle="font-family:georgia,garamond,serif;">
This text is rendered in either georgia, garamond, or the default serif font
18
CSS
depending on which font you have at your system.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="font-style:italic;">
This text will be rendered in italic style
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="font-variant:small-caps;">
This text will be rendered as small caps
</p>
19
CSS
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="font-weight:bold;">This font is bold.</p>
<pstyle="font-weight:bolder;">This font is bolder.</p>
<pstyle="font-weight:500;">This font is 500 weight.</p>
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="font-size:20px;">This font size is 20 pixels</p>
<pstyle="font-size:small;">This font size is small</p>
20
CSS
<pstyle="font-size:large;">This font size is large</p>
</body>
</html>
CSS - Text
This chapter teaches you how to manipulate text using CSS properties. You can set
following text properties of an element −
<html>
<head>
</head>
<body>
<pstyle="color:red;">
This text will be written in red.
</p>
21
CSS
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="direction:rtl;">
This text will be renedered from right to left
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="letter-spacing:5px;">
This text is having space between letters.
</p>
</body>
22
CSS
</html>
<html>
<head>
</head>
<body>
<pstyle="word-spacing:5px;">
This text is having space between words.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="text-align:right;">
This will be right aligned.
</p>
<pstyle="text-align:center;">
This will be center aligned.
23
CSS
</p>
<pstyle="text-align:left;">
This will be left aligned.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="text-decoration:underline;">
This will be underlined
</p>
<pstyle="text-decoration:line-through;">
This will be striked through.
</p>
<pstyle="text-decoration:overline;">
This will have a over line.
</p>
<pstyle="text-decoration:blink;">
This text will have blinking effect
</p>
24
CSS
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="text-transform:capitalize;">
This will be capitalized
</p>
<pstyle="text-transform:uppercase;">
This will be in uppercase
</p>
<pstyle="text-transform:lowercase;">
This will be in lowercase
</p>
</body>
</html>
<html>
<head>
25
CSS
</head>
<body>
<pstyle="white-space:pre;">
This text has a line break and the white-space pre setting tells the browser to
honor
it just like the HTML pre tag.</p>
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="text-shadow:4px4px8px blue;">
If your browser supports the CSS text-shadow property, this text will have a
blue shadow.
</p>
</body>
</html>
CSS plays a good role to control image display. You can set the following image
properties using CSS.
26
CSS
The width property is used to set the width of an image.
The -moz-opacity property is used to set the opacity of an image.
<html>
<head>
</head>
<body>
<imgstyle="border:0px;"src="/css/images/[Link]"/>
<br/>
<imgstyle="border:3px dashed red;"src="/css/images/[Link]"/>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<imgstyle="border:1px solid red; height:100px;"src="/css/images/[Link]"/>
<br/>
27
CSS
<imgstyle="border:1px solid red; height:50%;"src="/css/images/[Link]"/>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<imgstyle="border:1px solid red; width:150px;"src="/css/images/[Link]"/>
<br/>
<imgstyle="border:1px solid red; width:100%;"src="/css/images/[Link]"/>
</body>
</html>
CSS - Links
This chapter teaches you how to set different properties of a hyper link using CSS. You
can set following properties of a hyper link −
We will revisit the same properties when we will discuss Pseudo-Classes of CSS.
28
CSS
<style type="text/css">
</style>
CSS - Tables
This tutorial will teach you how to set different properties of an HTML table using CSS.
You can set following properties of a table −
If you provide one value, it will 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">
[Link] {border-spacing:10px;}
29
CSS
[Link] {border-spacing:10px; 15px;}
</style>
Now let's modify the previous example and see the effect −
</html>
<html>
<head>
<styletype="text/css">
[Link]{
table-layout:auto
}
[Link]{
table-layout:fixed
}
</style>
</head>
<body>
<tableclass="auto"border="1"width="100%">
<tr>
<tdwidth="20%">1000000000000000000000000000</td>
<tdwidth="40%">10000000</td>
30
CSS
<tdwidth="40%">100</td>
</tr>
</table>
<br/>
<tableclass="fixed"border="1"width="100%">
<tr>
<tdwidth="20%">1000000000000000000000000000</td>
<tdwidth="40%">10000000</td>
<tdwidth="40%">100</td>
</tr>
</table>
</body>
</html>
CSS - 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:
31
CSS
border-left-color changes the color of left border.
border-right-color changes the color of right border.
<html>
<head>
<styletype="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;
border-color:#009900;/* Green */
}
</style>
</head>
<body>
<pclass="example1">
This example is showing all borders in different colors.
</p>
<pclass="example2">
This example is showing all borders in green color only.
</p>
32
CSS
</body>
</html>
You can individually change the style of the bottom, left, top, and right
borders of an element using the following properties −
<html>
<head>
</head>
<body>.
<pstyle="border-width:4px;border-style:none;">
This is a border with none width.
33
CSS
</p>
<pstyle="border-width:4px;border-style:solid;">
This is a solid border.
</p>
<pstyle="border-width:4px;border-style:dashed;">
This is a dahsed border.
</p>
<pstyle="border-width:4px;border-style:double;">
This is a double border.
</p>
<pstyle="border-width:4px;border-style:groove;">
This is a groove border.
</p>
<pstyle="border-width:4px;border-style:ridge">
This is aridge border.
</p>
<pstyle="border-width:4px;border-style:inset;">
This is a inset border.
</p>
<pstyle="border-width:4px;border-style:outset;">
This is a outset border.
</p>
<pstyle="border-width:4px;border-style:hidden;">
This is a hidden border.
34
CSS
</p>
<pstyle="border-width:4px;border-top-style:solid;
border-bottom-style:dashed;border-left-style:groove;border-right-style:double;">
This is aa border with four different styles.
</p>
</body>
</html>
You can individually change the width of the bottom, top, left, and right
borders of an element using the following properties −
<html>
<head>
</head>
<body>
<pstyle="border-width:4px;border-style:solid;">
This is a solid border whose width is 4px.
</p>
<pstyle="border-width:4pt;border-style:solid;">
35
CSS
This is a solid border whose width is 4pt.
</p>
<pstyle="border-width:thin;border-style:solid;">
This is a solid border whose width is thin.
</p>
<pstyle="border-width:medium;border-style:solid;">
This is a solid border whose width is medium;
</p>
<pstyle="border-width:thick;border-style:solid;">
This is a solid border whose width is thick.
</p>
<pstyle="border-bottom-width:4px;border-top-width:10px;
border-left-width:2px;border-right-width:15px;border-style:solid;">
This is aa border with four different width.
</p>
</body>
</html>
CSS - Margins
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.
36
CSS
We have the following properties to set an element margin.
Here is an example −
<html>
<head>
</head>
<body>
<pstyle="margin:15px; border:1px solid black;">
all four margins will be 15px
</p>
37
CSS
</p>
</html>
CSS - Lists
Lists are very helpful in conveying a set of either numbered or bullet 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:
Here are the values which can be used for an unordered list −
Value Description
none NA
38
CSS
Here are the values, which can be used for an ordered list −
39
CSS
Here is an example −
<html>
<head>
</head>
<body>
<ulstyle="list-style-type:circle;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ulstyle="list-style-type:square;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<olstyle="list-style-type:decimal;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<olstyle="list-style-type:lower-alpha;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<olstyle="list-style-type:lower-roman;">
<li>Maths</li>
40
CSS
<li>Social Science</li>
<li>Physics</li>
</ol>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<ulstyle="list-style: inside square;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
41
CSS
</html>
CSS - Paddings
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 −
Here is an example −
<html>
<head>
</head>
<body>
<pstyle="padding-bottom:15px; border:1px solid black;">
42
CSS
This is a paragraph with a specified bottom padding
</p>
</html>
CSS - Cursors
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
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
43
CSS
text The I bar.
help A question mark or balloon, ideal for use over help buttons.
<html>
<head>
</head>
<body>
<p>Move the mouse over the words to see the cursor change:</p>
<divstyle="cursor:auto">Auto</div>
<divstyle="cursor:crosshair">Crosshair</div>
<divstyle="cursor:default">Default</div>
<divstyle="cursor:pointer">Pointer</div>
<divstyle="cursor:move">Move</div>
<divstyle="cursor:e-resize">e-resize</div>
<divstyle="cursor:ne-resize">ne-resize</div>
<divstyle="cursor:nw-resize">nw-resize</div>
<divstyle="cursor:n-resize">n-resize</div>
<divstyle="cursor:se-resize">se-resize</div>
<divstyle="cursor:sw-resize">sw-resize</div>
<divstyle="cursor:s-resize">s-resize</div>
<divstyle="cursor:w-resize">w-resize</div>
<divstyle="cursor:text">text</div>
<divstyle="cursor:wait">wait</div>
<divstyle="cursor:help">help</div>
44
CSS
</body>
</html>
CSS - Outlines
Outlines are very similar to borders, but there are few major differences as well −
Here is an example −
<html>
<head>
</head>
<body>
<pstyle="outline-width:thin;outline-style:solid;">
45
CSS
This text is having thin outline.
</p>
<br/>
<pstyle="outline-width:thick;outline-style:solid;">
This text is having thick outline.
</p>
<br/>
<pstyle="outline-width:5px;outline-style:solid;">
This text is having 5x outline.
</p>
</body>
</html>
Here is an example −
<html>
46
CSS
<head>
</head>
<body>
<pstyle="outline-width:thin;outline-style:solid;">
This text is having thin solid outline.
</p>
<br/>
<pstyle="outline-width:thick;outline-style:dashed;">
This text is having thick dashed outline.
</p>
<br/>
<pstyle="outline-width:5px;outline-style:dotted;">
This text is having 5x dotted outline.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="outline-width:thin;outline-style:solid;outline-color:red">
47
CSS
This text is having thin solid red outline.
</p>
<br/>
<pstyle="outline-width:thick;outline-style:dashed;outline-color:#009900">
This text is having thick dashed green outline.
</p>
<br/>
<pstyle="outline-width:5px;outline-style:dotted;outline-color:rgb(13,33,232)">
This text is having 5x dotted blue outline.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<pstyle="outline:thin solid red;">
This text is having thin solid red outline.
</p>
<br/>
48
CSS
This text is having thick dashed green outline.
</p>
<br/>
</html>
CSS - Dimension
You have seen the border that surrounds every box ie. element, the padding that can
appear inside each box and the margin that can go around them. In this tutorial we will
learn how we can change the dimensions of boxes.
We have the following properties that allow you to control the dimensions of a box.
<html>
<head>
</head>
49
CSS
<body>
<pstyle="width:400px; height:100px; border:1px solid red; padding:5px; margin:10px;">
This paragraph is 400pixels wide and 100 pixels high
</p>
</body>
</html>
CSS - Scrollbars
There may be a case when an element's content might be larger than the amount of
space allocated to it. For example, 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.
<html>
<head>
</head>
<styletype="text/css">
.scroll{
50
CSS
display:block;
border:1px solid red;
padding:5px;
margin-top:5px;
width:300px;
height:50px;
overflow:scroll;
}
</style>
<body>
</body>
</html>
CSS - Visibility
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.
51
CSS
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.
<html>
<head>
</head>
<body>
<p>
This paragraph should be visible in normal way.
</p>
<pstyle="visibility:hidden;">
This paragraph should not be visible.
</p>
</body>
</html>
CSS - Positioning
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 −
52
CSS
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.
Absolute Positioning
An element with position: absolute is positioned at the specified coordinates relative
to your screen top-left corner.
53
CSS
<html>
<head>
</head>
<body>
<divstyle="position:absolute; left:80px; top:20px;background-color:yellow;">
This div has absolute positioning.
</div>
</body>
</html>
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.
54
CSS
CSS - Layers
CSS gives you opportunity to create layers of various divisions. The CSS layers refer
to applying the z-index property to elements that overlap with each other.
A z-index property can help you to create more complex webpage layouts. Following is
the example which shows how to create layers in CSS.
<html>
<head>
</head>
<body>
<divstyle="background-color:red; width:300px; height:100px;position:relative;
top:10px; left:80px; z-index:2">
</div>
selector:pseudo-class{property: value}
55
CSS
CSS classes can also be used with pseudo-classes −
[Link]:pseudo-class{property: value}
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 of
child some other element.
a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective.
a:active MUST come after a:hover in the CSS definition in order to be effective.
Pseudo-class names are not case-sensitive.
Pseudo-class are different from CSS classes but they can be combined.
56
CSS
The :link pseudo-class
The following example demonstrates how to use the :link class to set the
link color. Possible values could be any color name in any valid format.
<html>
<head>
<styletype="text/css">
a:link {color:#000000}
</style>
</head>
<body>
<ahref="">Black Link</a>
</body>
</html>
<html>
<head>
<styletype="text/css">
a:visited {color:#006600}
</style>
</head>
<body>
<ahref="">Click this link</a>
</body>
</html>
This will produce following link. Once you will click this link, it will change its
color to green.
57
CSS
The :hover pseudo-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.
<html>
<head>
<styletype="text/css">
a:hover {color:#FFCC00}
</style>
</head>
<body>
<ahref="">Bring Mouse Here</a>
</body>
</html>
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.
<html>
<head>
<styletype="text/css">
a:active {color:#FF00CC}
</style>
</head>
<body>
<ahref="">Click This Link</a>
</body>
</html>
It will produce the following link. When a user clicks it, the color changes to pink.
58
CSS
The :focus pseudo-class
The following example demonstrates how to use the :focus class to change the color of
focused links. Possible values could be any color name in any valid format.
<html>
<head>
<styletype="text/css">
a:focus {color:#0000FF}
</style>
</head>
<body>
<ahref="">Click this Link</a>
</body>
</html>
It will produce the following link. When this link gets focused, its color changes to
orange. The color changes back when it loses focus.
For example, to indent the first paragraph of all <div> elements, you could use this
definition −
<html>
<head>
<styletype="text/css">
div> p:first-child
{
59
CSS
text-indent:25px;
}
</style>
</head>
<body>
<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>
<p>The first paragraph inside the div. This paragraph will not be effected.</p>
</div>
</body>
</html>
selector:pseudo-element{property: value}
[Link]:pseudo-element{property: value}
60
CSS
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.
<html>
<head>
<styletype="text/css">
p:first-line { text-decoration: underline;}
[Link]:first-line{ text-decoration: none;}
</style>
</head>
<body>
<pclass="noline"> This line would not have any underline because this belongs to
nline class.</p>
<p>The first line of this paragraph will be underlined as defined in the CSS rule
above. Rest of the lines in this paragraph will remain normal. This example shows how
to use :first-line pseduo element to give effect to the first line of any HTML
element.</p>
</body>
61
CSS
</html>
<html>
<head>
<styletype="text/css">
p:first-letter { font-size:5em;}
[Link]:first-letter{ font-size:10px;}
</style>
</head>
<body>
<pclass="normal"> First character of this paragraph will be normal and will have font
size 10 px;</p>
<p>The first character of this paragraph will be 5em big as defined in the CSS rule
above. Rest of the characters in this paragraph will remain normal. This example
shows how to use :first-letter pseduo element to give effect to the first characters
of any HTML element.</p>
</body>
</html>
<html>
<head>
<styletype="text/css">
p:before
{
62
CSS
content:url(/https/www.scribd.com/images/[Link])
}
</style>
</head>
<body>
<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>
</body>
</html>
<html>
<head>
<styletype="text/css">
p:after
{
content:url(/https/www.scribd.com/images/[Link])
}
</style>
</head>
<body>
<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>
</body>
</html>
63
CSS
CSS - Layouts
Hope you are very comfortable with HTML tables and you are efficient in designing
page layouts using HTML Tables. But you know CSS also 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.
CSS is pivotal to the future of Web documents and will be supported by most
browsers.
CSS is more exact than tables, allowing your document to be viewed as you
intended, regardless of the browser window.
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.
<tablestyle="table-layout:fixed;width:600px;">
<trheight="30">
<tdwidth="150">CSS table layout cell 1</td>
<tdwidth="200">CSS table layout cell 2</td>
<tdwidth="250">CSS table layout cell 3</td>
</tr>
64
CSS
</table>
<stylestyle="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>:
<stylestyle="text/css">
<!--
#level0 {
background:#FC0;
}
-->
</style>
Upto this point, we will have a document with yellow body, so let us now define another
division inside level0 −
<style style="text/css">
<!--
#level1 {
margin-left:143px;
padding-left:9px;
background:#FFF;
65
CSS
}
-->
</style>
Now, we will nest one more division inside level1, and we will change just background
color −
<stylestyle="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 −
<stylestyle="text/css">
<!--
#level3 {
margin-right:143px;
padding-right:9px;
background:#FFF;
}
#main {
background:#CCC;
}
-->
</style>
<stylestyle="text/css">
body{
margin:9px9px09px;
padding:0;
66
CSS
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>
<divid="level0">
<divid="level1">
<divid="level2">
<divid="level3">
<divid="main">
Final Content goes here...
</div>
</div>
</div>
</div>
</div>
67
CSS
</body>
Similarly, you can add a top navigation bar or an ad bar at the top of the
page.
68