What Is CSS?
What Is CSS?
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, layout designs,
and variations in display for different devices and screen sizes as well as a variety
of other effects.
CSS is easy to learn and understand but it provides powerful control over the
presentation of an HTML document. Most commonly, CSS is combined with the
mark-up languages HTML or XHTML.
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 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.
· Superior styles to HTML − CSS has a much wider array of attributes than
HTML, so you can give a far better look to your HTML page in comparison
to HTML attributes.
· Global web standards − Now HTML attributes are being deprecated and it
is being recommended to use CSS. So its a good idea to start using CSS in
all the HTML pages to make them compatible to future browsers.
· Offline Browsing − CSS can store web applications locally with the help of
an offline catche.Using of this, we can view offline websites.The cache also
ensures faster loading and better overall performance of the website.
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) was 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 was 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.
CSS3 was became a W3C recommendation in June 1999 and builds on older
versions CSS. it has divided into documentations is called as Modules and here
each module having new extension features defined in CSS2.
CSS3 Modules
CSS3 Modules are having old CSS specifications as well as extension features.
Selectors
Box Model
Backgrounds and Borders
Image Values and Replaced Content
Text Effects
2D/3D Transformations
Animations
Multiple Column Layout
User Interface
CSS Tutorial
CSS is used to control the style of a web document in a simple and easy way.
CSS is the acronym for "Cascading Style Sheet". This tutorial covers both the
versions CSS1,CSS2 and CSS3, and gives a complete understanding of CSS,
starting from its basics to advanced concepts.
CSS Syntax
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
becolor, border etc.
· Value - Values are assigned to properties. For example, color property can
have value either red or #F1F1F1 etc.
selector { property: value }
table{ border :1px solid #C00; }
Here table is a selector and border is a property and given value 1px solid #C00is
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 {
color: #36CFFF;
}
* {
color: #000000;
}
This rule renders the content of every element in our document in black.
The Descendant Selectors
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, style rule will apply
to <em> element only when it lies inside <ul> tag.
ul em {
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:
h1.black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with class attribute
set to black.
You can apply more than one class selectors to given element. Consider the
following example:
<p class="center bold">
This para will be styled by the classes center and bold.
</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;
}
This rule renders the content in black for every element with id attribute set
toblack 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 with in tags having id attribute set to black.
body > p {
color: #000000;
}
This rule will render all the paragraphs in black if they are direct child of <body>
element. Other paragraphs put inside other elements like <div> or <td> would not
have any effect of this rule.
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;
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.
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 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.
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>
<style type = "text/css" media = "all">
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Attributes
Attributes associated with <style> elements are −
media screen Specifies the device the document will be displayed on.
Default value is all. This is an optional attribute.
tty
tv
projection
handheld
braille
aural
all
<element style = "...style rules....">
Attributes
Example
Following is the example of inline CSS based on the above syntax −
<html>
<head>
</head>
<body>
<h1 style = "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>
Attributes
Attributes associated with <style> elements are −
href URL Specifies the style sheet file having Style rules. This
attribute is a required.
media screen
tty
tv
projection
braille
aural
all
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 HTML
document −
<head>
@import "mystyle.css";
</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.
<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 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.
Example
<!DOCTYPE html>
<html>
<head>
<style>
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>
Hello World
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>
<style type = "text/css" media = "all">
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Attributes
Attributes associated with <style> elements are −
media screen
tty
tv
projection
braille
aural
all
<element style = "...style rules....">
Attributes
Attribut Value Description
e
Example
Following is the example of inline CSS based on the above syntax −
<html>
<head>
</head>
<body>
<h1 style = "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>
</head>
Attributes
Attributes associated with <style> elements are −
href URL Specifies the style sheet file having Style rules. This
attribute is a required.
media screen
tty
tv
projection
braille
aural
all
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>
Imported CSS - @import Rule
@import is used to import an external stylesheet in a manner similar to the <link>
element. Here is the generic syntax of @import rule.
<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 HTML
document −
<head>
@import "mystyle.css";
</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.
<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 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.
Example
<!DOCTYPE html>
<html>
<head>
<style>
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>
It will produce the following result −
Hello World
We have listed out all the CSS Measurement Units along with proper Examples −
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 −
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.
#000000
#FF0000
#00FF00
#0000FF
#FFFF00
#00FFFF
#FF00FF
#C0C0C0
#FFFFFF
CSS Colors - Short Hex Codes
This is a shorter form of the six-digit notation. In this format, each digit is
replicated to arrive at an equivalent six-digit value. For example: #6A7 becomes
#66AA77.
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.
#000
#F00
#0F0
#0FF
#FF0
#0FF
#F0F
#FFF
rgb(0,0,0)
rgb(255,0,0)
rgb(0,255,0)
rgb(0,0,255)
rgb(255,255,0)
rgb(0,255,255)
rgb(255,0,255)
rgb(192,192,192)
rgb(255,255,255)
CSS Background
This chapter teaches you how to set backgrounds of various HTML elements. You
can set the following background properties of an element −
<html>
<head>
<body>
<p style = "background-color:yellow;">
This text has a yellow background color.</p>
</body>
</head>
<html>
It will produce the following result −
The following example which demonstrates how to repeat the background image
vertically.
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat-y;
}
</style>
</head>
<body>
<p>Tutorials point</>
</body>
</html>
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<p>Tutorials point</>
</body>
</html>
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-position:100px 200px;
}
</style>
</head>
<body>
<p>Tutorials point</>
</body>
</html>
The following example demonstrates how to set the scrolling background image.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url('/css/images/css.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-attachment:scroll;
}.
</style>
</head>
<body>
<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>
<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>
Shorthand Property
You can use the background property to set all the background
properties at once. For example −
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>
<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>
</body>
</html>
This text is rendered in either georgia, garamond, or the default serif font
depending on which font you have at your system.
<html>
<head>
</head>
<body>
<p style="font-style:italic;">
This text will be rendered in italic style
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<p style="font-variant:small-caps;">
This text will be rendered as small caps
</p>
</body>
</html>
The following example demonstrates how to set the font weight of an element. The
font-weight property provides the functionality to specify how bold a font is.
Possible values could be normal, bold, bolder, lighter, 100, 200, 300, 400, 500, 600,
700, 800, 900.
<html>
<head>
</head>
<body>
<p style="font-weight:bold;">This font is bold.</p>
<p style="font-weight:bolder;">This font is bolder.</p>
<p style="font-weight:500;">This font is 500 weight.</p>
</body>
</html>
It will produce the following result –
<html>
<head>
</head>
<body>
<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>
</body>
</html>
The following example demonstrates how to set the font size adjust of an element.
This property enables you to adjust the x-height to make fonts more legible.
Possible value could be any number.
<html>
<head>
</head>
<body>
<p style="font-size-adjust:0.61;">
This text is using a font-size-adjust value.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<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>
</body>
</html>
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.
Shorthand Property
You can use the font property to set all the font properties at once. For example −
<html>
<head>
</head>
<body>
<p style="font:italic small-caps bold 15px georgia;">
Applying all the properties on the text at once.
</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>
<p style="direction:rtl;">
This text will be renedered from right to left
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<p style="letter-spacing:5px;">
This text is having space between letters.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<p style="word-spacing:5px;">
This text is having space between words.
</p>
</body>
</html>
<html>
<head>
</head>
<body>
<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>
</body>
</html>
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.
<html>
<head>
</head>
<body>
<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>
</body>
</html>
<html>
<head>
</head>
<body>
<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>
</body>
</html>
<html>
<head>
</head>
<body>
<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>
</body>
</html>
<html>
<head>
</head>
<body>
<p style="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>
This text has a line break and the white-space pre setting tells the browser to
honor
<html>
<head>
</head>
<body>
<p style="text-shadow:4px 4px 8px blue;">
If your browser supports the CSS text-shadow property, this text will have a
blue shadow.
</p>
</body>
</html>
If your browser supports the CSS text-shadow property, this text will have a blue
shadow.
CSS plays a good role to control image display. You can set the following image
properties using CSS.
<html>
<head>
</head>
<body>
<img style="border:0px;" src="/css/images/logo.png" />
<br />
<img style="border:3px dashed red;" src="/css/images/logo.png" />
</body>
</html>
The Image Height Property
The height property of an image is used to set the height of an image. This
property can have a value in length or in %. While giving value in %, it applies it in
respect of the box in which an image is available.
Here is an example −
<html>
<head>
</head>
<body>
<img style="border:1px solid
red; height:100px;" src="/css/images/logo.png" />
<br />
<img style="border:1px solid red; height:50%;" src="/css/images/logo.png" />
</body>
</html>
The Image Width Property
The width property of an image is used to set the width of an image. This property
can have a value in length or in %. While giving value in %, it applies it in respect
of the box in which an image is available.
Here is an example −
<html>
<head>
</head>
<body>
<img style="border:1px solid red; width:150px;" src="/css/images/logo.png" />
<br />
<img style="border:1px solid red; width:100%;" src="/css/images/logo.png" />
</body>
</html>
In Mozilla (-moz-opacity:x) x can be a value from 0.0 - 1.0. A lower value makes
the element more transparent (The same things goes for the CSS3-valid syntax
opacity:x).
Here is an example −
<html>
<head>
</head>
<body>
<img style="border:1px solid red;-moz-
opacity:0.4;filter:alpha(opacity=40);" src="/css/images/logo.png" />
</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.
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">
</style>
Now, we will see how to use these properties to give different effects to hyperlinks.
<html>
<head>
<style type="text/css">
a:link {color:#000000}
</style>
</head>
<body>
<a href="">Link</a>
</body>
</html>
<html>
<head>
<style type="text/css">
a:visited {color: #006600}
</style>
</head>
<body>
<a href=""> link</a>
</body>
</html>
It will produce the following link. Once you will click this link, it will change its
color to green.
<html>
<head>
<style type="text/css">
a:hover {color: #FFCC00}
</style>
</head>
<body>
<a href="">Link</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>
<style type="text/css">
a:active {color: #FF00CC}
</style>
</head>
<body>
<a href="">Link</a>
</body>
</html>
It will produce the following link. It will change its color to pink when the user
clicks it.
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 −
<html>
<head>
<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;
}
td.b {
border-style:solid;
border-width:3px;
border-color:#333333;
padding:10px;
}
</style>
</head>
<body>
<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>
</body>
</html>
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">
table.example {border-spacing:10px;}
</style>
Now let's modify the previous example and see the effect −
<html>
<head>
<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>
</head>
<body>
<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>
</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>
</body>
</html>
This property can have one of the four values top, bottom, left or right. The
following example uses each value.
NOTE: These properties may not work with your IE Browser.
<html>
<head>
<style type="text/css">
caption.top {caption-side:top}
caption.bottom {caption-side:bottom}
caption.left {caption-side:left}
caption.right {caption-side:right}
</style>
</head>
<body>
<table style="width:400px; border:1px solid black;">
<caption class="top">
This caption will appear at the top
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
<br />
<table style="width:400px; border:1px solid black;">
<caption class="bottom">
This caption will appear at the bottom
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
<br />
<table style="width:400px; border:1px solid black;">
<caption class="left">
This caption will appear at the left
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
<br />
<table style="width:400px; border:1px solid black;">
<caption class="right">
This caption will appear at the right
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
</body>
</html>
Cell A
Cell B
Cell A
Cell B
Cell A
Cell B
Cell A
Cell B
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.
<html>
<head>
<style type="text/css">
table.empty{
width:350px;
border-collapse:separate;
empty-cells:hide;
}
td.empty{
padding:5px;
border-style:solid;
border-width:1px;
border-color:#999999;
}
</style>
</head>
<body>
<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>
</body>
</html>
NOTE − This property is not supported by many browsers so do not rely on this
property.
<html>
<head>
<style type="text/css">
table.auto {
table-layout: auto
}
table.fixed{
table-layout: fixed
}
</style>
</head>
<body>
<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 />
<table class="fixed" border="1" width="100%">
<tr>
<td width="20%">1000000000000000000000000000</td>
<td width="40%">10000000</td>
<td width="40%">100</td>
</tr>
</table>
</body>
</html>
It will produce the following result −
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:
<html>
<head>
<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;
border-color:#009900; /* Green */
}
</style>
</head>
<body>
<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>
</body>
</html>
· 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 −
<html>
<head>
</head>
<body>.
<p style="border-width:4px; border-style:none;">
This is a border with none width.
</p>
<p style="border-width:4px; border-style:solid;">
This is a solid border.
</p>
<p style="border-width:4px; border-style:dashed;">
This is a dahsed border.
</p>
<p style="border-width:4px; border-style:double;">
This is a double border.
</p>
<p style="border-width:4px; border-style:groove;">
This is a groove border.
</p>
<p style="border-width:4px; border-style:ridge">
This is aridge border.
</p>
<p style="border-width:4px; border-style:inset;">
This is a inset border.
</p>
<p style="border-width:4px; border-style:outset;">
This is a outset border.
</p>
<p style="border-width:4px; border-style:hidden;">
This is a hidden border.
</p>
<p style="border-width:4px;border-top-style:solid;
border-bottom-style:dashed; border-left-style:groove; border-right-
style:double;">
This is a a 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>
<p style="border-width:4px; border-style:solid;">
This is a solid border whose width is 4px.
</p>
<p style="border-width:4pt; border-style:solid;">
This is a solid border whose width is 4pt.
</p>
<p style="border-width:thin; border-style:solid;">
This is a solid border whose width is thin.
</p>
<p style="border-width:medium; border-style:solid;">
This is a solid border whose width is medium;
</p>
<p style="border-width:thick; border-style:solid;">
This is a solid border whose width is thick.
</p>
<p style="border-bottom-width:4px;border-top-width:10px;
border-left-width: 2px;border-right-width:15px;border-style:solid;">
This is a a border with four different width.
</p>
</body>
</html>
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.
<html>
<head>
</head>
<body>
<p style="border:4px solid red;">
This example is showing shorthand property for border.
</p>
</body>
</html>
This example is showing shorthand property for border(this text red color border).
CSS Margins
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.
We have the following properties to set an element margin.
Here is an example −
<html>
<head>
</head>
<body>
<p style="margin: 15px; border:1px solid black;">
all four margins will be 15px
</p>
<p style="margin:10px 2%; border:1px solid black;">
top and bottom margin will be 10px, left and right margin will be 2% of the
total width of the document.
</p>
<p style="margin: 10px 2% -10px; border:1px solid black;">
top margin will be 10px, left and right margin will be 2% of the total width of
the document, bottom margin will be -10px
</p>
<p style="margin: 10px 2% -10px auto; border:1px solid black;">
top margin will be 10px, right margin will be 2% of the total width of the
document, bottom margin will be -10px, left margin will be set by the browser
</p>
</body>
</html>
top and bottom margin will be 10px, left and right margin will be 2% of the
total width of the document.
top margin will be 10px, left and right margin will be 2% of the total width of
the document, bottom margin will be -10px
top margin will be 10px, right margin will be 2% of the total width of the
document, bottom margin will be -10px, left margin will be set by the
browser
Here is an example −
<html>
<head>
</head>
<body>
<p style="margin-bottom: 15px; border:1px solid black;">
This is a paragraph with a specified bottom margin.
</p>
<p style="margin-bottom: 5%; border:1px solid black;">
This is another paragraph with a specified bottom margin in percent
</p>
</body>
</html>
It will produce the following result –
Here is an example −
<html>
<head>
</head>
<body>
<p style="margin-top: 15px; border:1px solid black;">
This is a paragraph with a specified top margin
</p>
<p style="margin-top: 5%; border:1px solid black;">
This is another paragraph with a specified top margin in percent
</p>
</body>
</html>
Here is an example:
<html>
<head>
</head>
<body>
<p style="margin-left: 15px; border:1px solid black;">
This is a paragraph with a specified left margin
</p>
<p style="margin-left: 5%; border:1px solid black;">
This is another paragraph with a specified top margin in percent
</p>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<p style="margin-right: 15px; border:1px solid black;">
This is a paragraph with a specified right margin
</p>
<p style="margin-right: 5%; border:1px solid black;">
This is another paragraph with a specified right margin in percent
</p>
</body>
</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
disc (default) A filled-in circle
Here are the values, which can be used for an ordered list −
Here is an example −
<html>
<head>
</head>
<body>
<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>
</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>
</body>
</html>
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.
outsid If the text goes onto a second line, the text will be aligned with the start
e of the first line (to the right of the bullet).
Here is an example −
<html>
<head>
</head>
<body>
<ul style="list-style-type:circle; list-style-position:outside;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<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-style-position:outside;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<ol style="list-style-type:lower-alpha;list-style-position:inside;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
</body>
</html>
o Maths
o Social Science
o Physics
Maths
Social Science
Physics
1. Maths
2. Social Science
3. Physics
a. Maths
b. Social Science
c. Physics
Here is an example −
<html>
<head>
</head>
<body>
<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>
</ol>
</body>
</html>
Maths
Social Science
Physics
1. Maths
2. Social Science
3. Physics
Here is an example −
<html>
<head>
</head>
<body>
<ul style="list-style: inside square;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol style="list-style: outside upper-alpha;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
</body>
</html>
Maths
Social Science
Physics
A. Maths
B. Social Science
C. Physics
Here is an example −
<html>
<head>
</head>
<body>
<ul style="list-style: inside square; marker-offset:2em;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol style="list-style: outside upper-alpha; marker-offset:2cm;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
</body>
</html>
Maths
Social Science
Physics
A. Maths
B. Social Science
C. Physics
CSS Paddings
The padding property allows you to specify how much space should appear
between the content of an element and its border −
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-bottom specifies the bottom padding of an element.
Here is an example −
<html>
<head>
</head>
<body>
<p style="padding-bottom: 15px; border:1px solid black;">
This is a paragraph with a specified bottom padding
</p>
<p style="padding-bottom: 5%; border:1px solid black;">
This is another paragraph with a specified bottom padding in percent
</p>
</body>
</html>
The padding-top Property
The padding-top property sets the top padding (space) of an element. This can take
a value in terms of length of %.
Here is an example −
<html>
<head>
</head>
<body>
<p style="padding-top: 15px; border:1px solid black;">
This is a paragraph with a specified top padding
</p>
<p style="padding-top: 5%; border:1px solid black;">
This is another paragraph with a specified top padding in percent
</p>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<p style="padding-left: 15px; border:1px solid black;">
This is a paragraph with a specified left padding
</p>
<p style="padding-left: 15%; border:1px solid black;">
This is another paragraph with a specified left padding in percent
</p>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<p style="padding-right: 15px; border:1px solid black;">
This is a paragraph with a specified right padding
</p>
<p style="padding-right: 5%; border:1px solid black;">
This is another paragraph with a specified right padding in percent
</p>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<p style="padding: 15px; border:1px solid black;">
all four padding will be 15px
</p>
<p style="padding:10px 2%; border:1px solid black;">
top and bottom padding will be 10px, left and right
padding will be 2% of the total width of the document.
</p>
<p style="padding: 10px 2% 10px; border:1px solid black;">
top padding will be 10px, left and right padding will
be 2% of the total width of the document, bottom padding will be 10px
</p>
<p style="padding: 10px 2% 10px 10px; border:1px solid black;">
top padding will be 10px, right padding will be 2% of
the total width of the document, bottom padding and top padding will be 10px
</p>
</body>
</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.
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
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 visitors.
Here is an example −
<html>
<head>
</head>
<body>
<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>
<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>
</body>
</html>
Move the mouse over the words to see the cursor change:
Auto
Crosshair
Default
Pointer
Move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help
CSS Outlines
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 property is used to set all the above three properties in a single statement.
Here is an example −
<html>
<head>
</head>
<body>
<p style="outline-width:thin; outline-style:solid;">
This text is having thin outline.
</p>
<br />
<p style="outline-width:thick; outline-style:solid;">
This text is having thick outline.
</p>
<br />
<p style="outline-width:5px; outline-style:solid;">
This text is having 5x outline.
</p>
</body>
</html>
· outset: Outline makes the box look like it is coming out of the canvas.
· hidden: Same as none.
Here is an example −
<html>
<head>
</head>
<body>
<p style="outline-width:thin; outline-style:solid;">
This text is having thin solid outline.
</p>
<br />
<p style="outline-width:thick; outline-style:dashed;">
This text is having thick dashed outline.
</p>
<br />
<p style="outline-width:5px;outline-style:dotted;">
This text is having 5x dotted outline.
</p>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<p style="outline-width:thin; outline-style:solid;outline-color:red">
This text is having thin solid red outline.
</p>
<br />
<p style="outline-width:thick; outline-style:dashed;outline-color:#009900">
This text is having thick dashed green outline.
</p>
<br />
<p style="outline-width:5px;outline-style:dotted;outline-color:rgb(13,33,232)">
This text is having 5x dotted blue outline.
</p>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<p style="outline:thin solid red;">
This text is having thin solid red outline.
</p>
<br />
<p style="outline:thick dashed #009900;">
This text is having thick dashed green outline.
</p>
<br />
<p style="outline:5px dotted rgb(13,33,232);">
This text is having 5x dotted blue outline.
</p>
</body>
</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 how we can change the dimensions of boxes.
We have the following properties that allow you to control the dimensions of a box.
Here is an example −
<html>
<head>
</head>
<body>
<p style="width:400px; height:100px; border:1px solid
red; padding:5px; margin:10px;">
This paragraph is 400pixels wide and 100 pixels high
</p>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<p style="width:400px; height:100px; border:1px solid
red; padding:5px; margin:10px; line-height:30px;">
This paragraph is 400pixels wide and 100 pixels high and here line height is
30pixels.
This paragraph is 400 pixels wide and 100 pixels high and here line height is
30pixels.
</p>
</body>
</html>
This paragraph is 400pixels wide and 100 pixels high and here line height is
30pixels.This paragraph is 400 pixels wide and 100 pixels high and here line
height is 30pixels.
Here is an example −
<html>
<head>
</head>
<body>
<p style="width:400px; max-height:10px; border:1px solid
red; padding:5px; margin:10px;">
This paragraph is 400px wide and max height is 10px
This paragraph is 400px wide and max height is 10px
This paragraph is 400px wide and max height is 10px
This paragraph is 400px wide and max height is 10px
</p>
<br>
<br>
<br>
<img alt="logo" src="/css/images/logo.png" width="195" height="84" />
</body>
</html>
his paragraph is 400px wide and max height is 10px This paragraph is 400px wide
and max height is 10px This paragraph is 400px wide and max height is 10px This
paragraph is 400px wide and max height is 10px
Image here
Here is an example −
<html>
<head>
</head>
<body>
<p style="width:400px; min-height:200px; border:1px solid red; padding:5px;
margin:10px;">
This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px
This paragraph is 400px wide and min height is 200px
</p>
<img alt="logo" src="/css/images/logo.png" width="95" height="84" />
</body>
</html>
This paragraph is 400px wide and min height is 200px This paragraph is 400px
wide and min height is 200px This paragraph is 400px wide and min height is
200px This paragraph is 400px wide and min height is 200px
Image here
Here is an example −
<html>
<head>
</head>
<body>
<p style="max-width:100px; height:200px; border:1px solid red; padding:5px;
margin:10px;">
This paragraph is 200px high and max width is 100px
This paragraph is 200px high and max width is 100px
</p>
<img alt="logo" src="/css/images/logo.png" width="95" height="84" />
</body>
</html>
It will produce the following result –
This paragraph is 200px high and max width is 100px This paragraph is 200px
high and max width is 100px
Image here
Here is an example −
<html>
<head>
</head>
<body>
<p style="min-width:400px; height:100px; border:1px solid red; padding:5px;
margin:10px;">
This paragraph is 100px high and min width is 400px
This paragraph is 100px high and min width is 400px
<img alt="logo" src="/css/images/css.gif" width="95" height="84" />
</body>
</html>
This paragraph is 100px high and min width is 400px This paragraph is 100px
high and min width is 400px
Image here
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.
visible Allows the content to overflow the borders of its containing element.
hidde The content of the nested element is simply cut off at the border of the
n 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 −
<html>
<head>
</head>
<style type="text/css">
.scroll{
display:block;
border: 1px solid red;
padding:5px;
margin-top:5px;
width:300px;
height:50px;
overflow:scroll;
}
.auto{
display:block;
border: 1px solid red;
padding:5px;
margin-top:5px;
width:300px;
height:50px;
overflow:auto;
}
</style>
<body>
<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>
</body>
</html>
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.
collaps This is for use only with dynamic table columns and row effects.
e
Here is an example −
<html>
<head>
</head>
<body>
<p>
This paragraph should be visible in normal way.
</p>
<p style="visibility:hidden;">
This paragraph should not be visible.
</p>
</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.
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.
collaps This is for use only with dynamic table columns and row effects.
e
Here is an example −
<html>
<head>
</head>
<body>
<p>
This paragraph should be visible in normal way.
</p>
<p style="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 −
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.
<html>
<head>
</head>
<body>
<div style="position:relative;left:80px;top:2px;background-color:yellow;">
This div has relative positioning.
</div>
</body>
</html>
Absolute Positioning
An element with position: absolute is positioned at the specified coordinates
relative to your screen top-left corner.
Here is an example −
<html>
<head>
</head>
<body>
<div style="position:absolute; left:80px; top:20px; background-
color:yellow;">
This div has absolute positioning.
</div>
</body>
</html>
Here is an example −
<html>
<head>
</head>
<body>
<div style="position:fixed; left:80px; top:20px; background-color:yellow;">
This div has fixed positioning.
</div>
</body>
</html>
This div has fixed positioning.
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>
<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; positio
n:relative; top:-60px; left:35px; z-index:1;">
</div>
<div style="background-color:green; width:300px; height:100px; positio
n:relative; top:-220px; left:120px; z-index:3;">
</div>
</body>
</html>
Image here
selector:pseudo-class {property: value}
selector.class:pseudo-class {property: value}
Value Description
: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.
· a:hover MUST come after a:link and a:visited in the CSS definition in order
to be effective.
· Pseudo-class are different from CSS classes but they can be combined.
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>
<style type="text/css">
a:link {color:#000000}
</style>
</head>
<body>
<a href="">Black Link</a>
</body>
</html>
Black Link
The :visited pseudo-class
The following is the example which demonstrates how to use the :visited class to
set the color of visited links. Possible values could be any color name in any valid
format.
<html>
<head>
<style type="text/css">
a:visited {color: #006600}
</style>
</head>
<body>
<a href="">Click this link</a>
</body>
</html>
This will produce following link. Once you will click this link, it will change its
color to green.
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>
<style type="text/css">
a:hover {color: #FFCC00}
</style>
</head>
<body>
<a href="">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.
The :active pseudo-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.
<html>
<head>
<style type="text/css">
a:active {color: #FF00CC}
</style>
</head>
<body>
<a href="">Click This Link</a>
</body>
</html>
It will produce the following link. When a user clicks it, the color changes to pink.
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>
<style type="text/css">
a:focus {color: #0000FF}
</style>
</head>
<body>
<a href="">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>
<style type="text/css">
div > p:first-child
{
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>
Heading
The first paragraph inside the div. This paragraph will not be
effected.
The :lang pseudo-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 ').
<html>
<head>
<style type="text/css">
/* Two levels of quotes for two languages*/
:lang(en) { quotes: '"' '"' "'" "'"; }
:lang(fr) { quotes: "<<" ">>" "<" ">"; }
</style>
</head>
<body>
<p>...<q lang="fr">A quote in a paragraph</q>...</p>
</body>
</html>
The :lang selectors will apply to all the elements in the document. However, not all
elements make use of the quotes property, so the effect will be transparent for
most elements.
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 −
selector:pseudo-element {property: value}
selector.class:pseudo-element {property: value}
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>
<style type="text/css">
p:first-line { text-decoration: underline; }
p.noline:first-line { text-decoration: none; }
</style>
</head>
<body>
<p class="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>
</html>
This line would not have any underline because this belongs to
nline class.
<html>
<head>
<style type="text/css">
p:first-letter { font-size: 5em; }
p.normal:first-letter { font-size: 10px; }
</style>
</head>
<body>
<p class="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>
First character of this paragraph will be normal and will have font
size 10 px;
The first character of this paragraph will be 5em big and in red
color 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.
<html>
<head>
<style type="text/css">
p:after
{
content: url(/images/bullet.gif)
}
</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>
CSS @ Rules
This chapter will cover the following important @ rules −
· The @import: rule imports another style sheet into the current style sheet.
NOTE − There are other @ rules which we will cover in subsequent chapters.
<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.
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>
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")
format("truetype");
unicode-range: U+??,U+100-220;
font-size: all;
font-family: sans-serif;
}
-->
</style>
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 −
<html>
<head>
<style tyle="text/css">
p { color: #ff0000 !important; }
p { color: #000000; }
</style>
</head>
<body>
<p>example.com</>
</body>
</html>
Here you have made p { color: #ff0000 !important; } mandatory, now this rule will
always apply even you have defined another rule p { color: #000000; }
example.com
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
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png" alt="CSS Logo"
style="Filter: Alpha(Opacity=100,
FinishOpacity=0,
Style=2,
StartX=20,
StartY=40,
FinishX=0,
FinishY=0)" />
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: blue;
Filter: Alpha(Opacity=100, FinishOpacity=0, Style=1, StartX=0, StartY=0, Fin
ishX=580, FinishY=0)">CSS Tutorials</div>
</body>
</html>
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.
0 = Top
45 = Top right
90 = Right
180 = Bottom
270 = Left
315 = Top left
strength The number of pixels the blur will extend. The default is 5 pixels.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png" alt="CSS Logo"
style="Filter: Blur(Add = 0, Direction = 225, Strength = 10)">
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: blue;
Filter: Blur(Add = 1, Direction = 225, Strength = 10)">CSS Tutorials</div>
</body>
</html>
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
color The color that you'd like to be transparent.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/images/css.gif"
alt="CSS Logo" style="Filter: Chroma(Color = #FFFFFF)">
<p>Text Example:</p>
<div style="width: 580;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: #3300FF;
Filter: Chroma(Color = #3300FF)">CSS Tutorials</div>
</body>
</html>
Parameter Description
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.
positive If true, all opaque pixels of the object have a dropshadow. If false, all
transparent pixels have a dropshadow. The default is true.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png"
alt="CSS Logo"
style="Filter: Chroma(Color = #000000)
DropShadow(Color=#FF0000,
OffX=2,
OffY=2, Positive=1)">
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: red;
Filter: DropShadow(Color=#000000, OffX=2, OffY=2, Positive=1)">CSS
Tutorials</div>
</body>
</html>
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
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png"
alt="CSS Logo"
style="Filter: FlipH">
<img src="/css/images/logo.png" alt="CSS Logo" style="Filter: FlipV">
<p>Text Example:</p>
<div style="width: 300;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: red;
Filter: FlipV">CSS Tutorials</div>
</body>
</html>
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
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png"
alt="CSS Logo"
style="Filter: Chroma(Color = #000000) Glow(Color=#00FF00, Strength=20)">
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: red;
Filter: Glow(Color=#00FF00, Strength=20)">CSS Tutorials</div>
</body>
</html>
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
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png"
alt="CSS Logo"
style="Filter: Gray">
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: red;
Filter: Gray">CSS Tutorials</div>
</body>
</html>
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.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/images/css.gif"
alt="CSS Logo"
style="Filter: invert">
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: red;
Filter: invert">CSS Tutorials</div>
</body>
</html>
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
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png"
alt="CSS Logo"
style="FILTER: Chroma(Color = #000000) Mask(Color=#00FF00)">
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: red;
Filter: Mask(Color=#00FF00)">CSS Tutorials</div>
</body>
</html>
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
0 = Top
45 = Top right
90 = Right
180 = Bottom
270 = Left
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png"
alt="CSS Logo"
style="FILTER: Chroma(Color = #000000) Shadow(Color=#00FF00,
Direction=225)">
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family:
Arial Black;
color: red;
Filter: Shadow(Color=#0000FF, Direction=225)">CSS Tutorials</div>
</body>
</html>
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).
strength The intensity of the wave effect.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png"
alt="CSS Logo"
style="FILTER: Chroma(Color = #000000)
Wave(Add=0, Freq=1, LightStrength=10, Phase=220, Strength=10)">
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: red;
Filter: Wave(Add=0, Freq=1, LightStrength=10, Phase=20, Strength=20)">CS
S Tutorials</div>
</body>
</html>
X-Ray Effect
X-Ray effect grayscales and flattens the color depth. The following parameter is
used in this filter:
Parameter Description
xray Grayscales and flattens the color depth.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<img src="/css/images/logo.png"
alt="CSS Logo"
style="Filter: Xray"">
<p>Text Example:</p>
<div style="width: 357;
height: 50;
font-size: 30pt;
font-family: Arial Black;
color: red;
style="Filter: Xray">CSS Tutorials</div>
</body>
</html>
**Note all result are not displaying because its effect on application.
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.
<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>
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>
Value Description
print Intended for paged, opaque material and for documents viewed on
screen in print preview mode. Please consult the section on paged
media.
The CSS2 standard introduces some basic pagination control features that let
authors help the browser figure out how to best print their documents.
The CSS2 page model specifies how a document is formatted within a rectangular
area -- the page box -- that has a finite width and height. These features fall into
two groups −
· 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 × 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>
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.
· auto − The page box will be set to the size and orientation of the target
sheet.
· landscape − Overrides the target's orientation. The page box is the same
size as the target, and the longer sides are horizontal.
· 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 × 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" × 11" or larger.
<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">
<!--
-->
</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;
}
@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, andpage-
break-inside properties.
Both the page-break-before and page-break-after accept the auto, always, avoid,
left, and right keywords.
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.
<style type="text/css">
<!--
h1 { page-break-before : right }
h2 { page-break-after : avoid }
-->
</style>
<style type="text/css">
<!--
table { page-break-inside : avoid }
-->
</style>
Here is the example to create 4 lines at the bottom and 3 lines at the top of each
page −
<style type="text/css">
<!--
@page{orphans:4; widows:2;}
-->
</style>
Aural rendering of documents is mainly used by the visually impaired. Some of the
situations in which a document can be accessed by means of aural rendering
rather than visual rendering are the following.
Learning to read
Training
Web access in vehicles
Home entertainment
Industrial documentation
Medical documentation
When using aural properties, the canvas consists of a three-dimensional physical
space (sound surrounds) and a temporal space (one may specify sounds before,
during, and after other sounds).
The CSS properties also allow you to vary the quality of synthesized speech (voice
type, frequency, inflection, etc.)
Here is an example −
<html>
<head>
<style tyle="text/css">
h1, h2, h3, h4, h5, h6 {
voice-family: paul;
stress: 20;
richness: 90;
cue-before: url("../audio/pop.au");
}
p{
azimuth:center-right;
}
</style>
</head>
<body>
<h1>Example.com</h1>
<h2>Example.com</h2>
<h3>Example.com</h3>
<h4>Example.com</h4>
<h5>Example.com</h5>
<h6>Example.com</h6>
<p>Example.com</p>
</body>
</html>
Example.com
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.
· 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 */
p.comment { azimuth: behind } /* 180deg */
-->
</style>
Here is an example −
<style tyle="text/css">
<!--
h1 { elevation: above }
tr.a { elevation: 60deg }
tr.b { elevation: 30deg }
tr.c { elevation: level }
-->
</style>
Here is an example −
<style tyle="text/css">
<!--
a {cue-after: url("dong.wav");}
h1 {cue-after: url("pop.au"); }
-->
</style>
Here is an example −
<style tyle="text/css">
<!--
a {cue-before: url("bell.aiff");}
h1 {cue-before: url("pop.au"); }
-->
</style>
<style tyle="text/css">
<!--
h1 {cue-before: url("pop.au"); cue-after: url("pop.au") }
h1 {cue: url("pop.au") }
-->
</style>
<style tyle="text/css">
<!--
/* pause-before: 20ms; pause-after: 20ms */
h1 { pause : 20ms }
/* pause-before: 30ms; pause-after: 40ms */
h2{ pause : 30ms 40ms }
/* pause-before: ?; pause-after: 10ms */
h3 { pause-after : 10ms }
-->
</style>
· frequency - Specifies the average pitch of the speaking voice in hertz (Hz).
· x-low, low, medium, high, x-high - These values do not map to absolute
frequencies since these values depend on the voice family.
· 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.
· 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.
Here is an example −
<style tyle="text/css">
<!--
-->
</style>
· 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.
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.
· digits − Speak the numeral as individual digits. Thus, "237" is spoken "Two
Three Seven".
· 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.
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>
· numbers − Any number between '0' and '100'. '0' represents the minimum
audible volume level and 100 corresponds to the maximum comfortable
level.
· percentage − These values are calculated relative to the inherited value,
and are then clipped to the range '0' to '100'.
· silent − No sound at all. The value '0' does not mean the same as 'silent'.
Here is an example −
<style tyle="text/css">
<!--
P.goat { volume: x-soft }
-->
</style>
You have seen @media rule in previous chapters. This rule allows you to specify
different style for different media. So, you can define different rules for screen and
a printer.
The example below specifies different font families for screen and print. 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
{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 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.
<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>
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.
<style style="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 style="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;
}
-->
</style>
Now, we will nest one more division inside level1, and we will change just
background color −
<style style="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 style="text/css">
<!--
#level3 {
margin-right:143px;
padding-right:9px;
background:#FFF;
}
#main {
background:#CCC;
}
-->
</style>
Similarly, you can add a top navigation bar or an ad bar at the top of the page.
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 −
<link rel="stylesheet" type="text/css" media="print" href="mystyle.css">
CSS Validations
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.
You can use the following tools to check the validity of your CSS.
1 W3C CSS Validator (World Wide Web Consortium), This validator checks
your css by either file upload, direct input, or using URI - one page at a time.
This validator helps you to locate all the errors in your CSS.
2 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 that 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).