Understanding CSS: Basics and Benefits
Understanding CSS: Basics and Benefits
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.
● Easy maintenance − To make a global change, simply change
the style, and all elements in all the web pages will be updated
automatically.
● 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.
● Multiple Device Compatibility − Style sheets allow content to
be optimized for more than one type of device. By using the
same HTML document, different versions of a website can be
presented for handheld devices such as PDAs and cell phones or
for printing.
● 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.
CSS Versions
Cascading Style Sheets level 1 (CSS1) came out of W3C as a
recommendation in December 1996. This version describes the CSS
language as well as a simple visual formatting model for all the HTML tags.
CSS2 became a W3C recommendation in May 1998 and builds on CSS1. This
version adds support for media-specific style sheets e.g. printers and aural
devices, downloadable fonts, element positioning and tables.
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 −
● 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.
You can put CSS Style Rule Syntax as follows −
selector { property: value }
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
to black in our document. You can make it a bit more particular. For example
−
h1#black {
color: #000000;
}
This rule renders the content in black for only <h1> elements
with id attribute set to black.
The true power of id selectors is when they are used as the foundation for
descendant selectors, For example −
#black h2 {
color: #000000;
}
In this example all level 2 headings will be displayed in black color when
those headings will lie with in tags having id attribute set to black.
Attributes
Attributes associated with <style> elements are −
Attributes
Attribu Value Description
te
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>
Attributes
Attributes associated with <style> elements are −
Example
Consider a simple style sheet file with a name [Link] 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 [Link] in any HTML document as follows −
<head>
<link type = "text/css" href = "[Link]" media = " all" />
</head>
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>
CSS - Measurement Units
Before we start the actual exercise, we would like to give a brief idea about
the CSS Measurement Units. CSS supports a number of measurements
including absolute units such as inches, centimeters, points, and so on, as
well as relative measures such as percentages and em units. You need these
values while specifying various measurements in your Style rules
e.g. border = "1px solid red".
We have listed out all the CSS Measurement Units along with proper
Examples −
#000000
#FF0000
#00FF00
#0000FF
#FFFF00
#00FFFF
#FF00FF
#C0C0C0
#FFFFFF
#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)
Building Color Codes
You can build millions of color codes using our Color Code Builder. Check
our HTML Color Code Builder. To use this tool, you would need a Java
Enabled Browser.
<body>
<p style = "background-color:yellow;">
This text has a yellow background color.
</p>
</body>
</html>
<body>
<h1>Hello World!</h1>
</body>
<html>
<body>
<p>Tutorials point</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>Tutorials point</p>
</body>
</html>
<body>
<p>Tutorials point</p>
</body>
</html>
<body>
<p>Tutorials point</p>
</body>
</html>
<body>
<p>Tutorials point</p>
</body>
</html>
<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>
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>
<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 −
<p style = "
This parapgraph has fixed repeated background image.
</p>
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 −
● The font-family property is used to change the face of a font.
● The font-style property is used to make a font italic or oblique.
● The font-variant property is used to create a small-caps effect.
● The font-weight property is used to increase or decrease how
bold or light a font appears.
● The font-size property is used to increase or decrease the size of
a font.
● The font property is used as shorthand to specify a number of
other font properties.
<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>
<body>
<p style = "font-style:italic;">
This text will be rendered in italic style
</p>
</body>
</html>
<body>
<p style = "font-variant:small-caps;">
This text will be rendered as small caps
</p>
</body>
</html>
<body>
<p style = "font-weight:bold;">
This font is bold.
</p>
<body>
<p style = "font-size:20px;">
This font size is 20 pixels
</p>
<body>
<p style = "font-size-adjust:0.61;">
This text is using a font-size-adjust value.
</p>
</body>
</html>
<body>
<p style = "font-stretch:ultra-expanded;">
If this doesn't appear to work, it is likely that your computer
doesn't have a <br>condensed or expanded version of the font being
used.
</p>
</body>
</html>
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 −
● The color property is used to set the color of a text.
● The direction property is used to set the text direction.
● The letter-spacing property is used to add or subtract space
between the letters that make up a word.
● The word-spacing property is used to add or subtract space
between the words of a sentence.
● The text-indent property is used to indent the text of a
paragraph.
● The text-align property is used to align the text of a document.
● The text-decoration property is used to underline, overline, and
strikethrough text.
● The text-transform property is used to capitalize text or convert
text to uppercase or lowercase letters.
● The white-space property is used to control the flow and
formatting of text.
● The text-shadow property is used to set the text shadow around
a text.
<body>
<p style = "color:red;">
This text will be written in red.
</p>
</body>
</html>
<body>
<p style = "direction:rtl;">
This text will be rendered from right to left
</p>
</body>
</html>
<body>
<p style = "letter-spacing:5px;">
This text is having space between letters.
</p>
</body>
</html>
<body>
<p style = "word-spacing:5px;">
This text is having space between words.
</p>
</body>
</html>
Set the Text Indent
The following example demonstrates how to indent the first line of a
paragraph. Possible values are % or a number specifying indent space.
<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>
<body>
<p style = "text-align:right;">
This will be right aligned.
</p>
<body>
<p style = "text-decoration:underline;">
This will be underlined
</p>
<body>
<p style = "text-transform:capitalize;">
This will be capitalized
</p>
<p style = "text-transform:uppercase;">
This will be in uppercase
</p>
<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>
<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>
CSS - Using Images
Images play an important role in any webpage. Though it is not
recommended to include a lot of images, but it is still important to use good
images wherever required.
CSS plays a good role to control image display. You can set the following
image properties using CSS.
● The border property is used to set the width of an image border.
● The height property is used to set the height of an image.
● 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.
<body>
<img style = "border:0px;" src = "/css/images/[Link]" />
<br />
<img style = "border:3px dashed red;" src = "/css/images/[Link]" />
</body>
</html>
<body>
<img style = "border:1px solid red; width:150px;" src =
"/css/images/[Link]" />
<br />
<img style = "border:1px solid red; width:100%;" src =
"/css/images/[Link]" />
</body>
</html>
<body>
<img style = "border:1px solid red; -moz-opacity:0.4;
filter:alpha(opacity=40);" 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.
● The :link signifies unvisited hyperlinks.
● The :visited signifies visited hyperlinks.
● The :hover signifies an element that currently has the user's
mouse pointer hovering over it.
● The :active signifies an element on which the user is currently
clicking.
Usually, all these properties are kept in the header part of the HTML
document.
Remember a:hover MUST come after a:link and a:visited in the CSS definition
in order to be effective. Also, a:active MUST come after a:hover in the CSS
definition as follows −
<style type = "text/css">
a:link {color: #000000}
a:visited {color: #006600}
a:hover {color: #FFCC00}
a:active {color: #FF00CC}
</style>
Now, we will see how to use these properties to give different effects to
hyperlinks.
<body>
<a href = "">Link</a>
</body>
</html>
<body>
<a href = ""> link</a>
</body>
</html>
<body>
<a href = "">Link</a>
</body>
</html>
<body>
<a href = "">Link</a>
</body>
</html>
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 −
● The border-collapse specifies whether the browser should
control the appearance of the adjacent borders that touch each
other or whether each cell should maintain its style.
● The border-spacing specifies the width that should appear
between table cells.
● The caption-side captions are presented in the <caption>
element. By default, these are rendered above the table in the
document. You use the caption-side property to control the
placement of the table caption.
● The empty-cells specifies whether the border should be shown if
a cell is empty.
● The table-layout allows browsers to speed up layout of a table
by using the first width properties it comes across for the rest of
a column rather than having to load the whole table before
rendering it.
Now, we will see how to use these properties with examples.
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 />
<body>
</body>
</html>
<body>
</body>
</html>
<body>
<tr>
<th>Row Title</th>
<td class = "empty">value</td>
<td class = "empty"></td>
</tr>
</table>
</body>
</html>
<body>
</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 −
● The border-color specifies the color of a border.
● The border-style specifies whether a border should be solid,
dashed line, double line, or one of the other possible values.
● The border-width specifies the width of a border.
<body>
<p class = "example1">
This example is showing all borders in different colors.
</p>
<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>
<body>
<p style = "border-width:4px; border-style:solid;">
This is a solid border whose width is 4px.
</p>
<body>
<p style = "border:4px solid red;">
This example is showing shorthand property for border.
</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.
We have the following properties to set an element margin.
● The margin specifies a shorthand property for setting the margin
properties in one declaration.
● The margin-bottom specifies the bottom margin of an element.
● The margin-top specifies the top margin of an element.
● The margin-left specifies the left margin of an element.
● The margin-right specifies the right margin of an element.
Now, we will see how to use these properties with examples.
<body>
<p style = "margin: 15px; border:1px solid black;">
all four margins will be 15px
</p>
<body>
<p style = "margin-bottom: 15px; border:1px solid black;">
This is a paragraph with a specified bottom margin
</p>
<body>
<p style = "margin-left: 15px; border:1px solid black;">
This is a paragraph with a specified left margin
</p>
1
none
NA
2
disc (default)
A filled-in circle
3
circle
An empty circle
4
square
A filled-in square
Here are the values, which can be used for an ordered list −
alpha, beta,
lower-greek The marker is lower-greek
gamma
A, I, U, E, O, KA,
katakana The marker is katakana
KI
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>
1
none
NA
2
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.
3
outside
If the text goes onto a second line, the text will be aligned with the
start of the first line (to the right of the bullet).
Here is an example −
<html>
<head>
</head>
<body>
<ul style = "list-style-type:circle; list-stlye-position:outside;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<body>
<ul>
<li style = "list-style-image: url(/images/[Link]);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol>
<li style = "list-style-image: url(/images/[Link]);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
</body>
</html>
<body>
<ul style = "list-style: inside square;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<body>
<ul style = "list-style: inside square; marker-offset:2em;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<body>
<p style = "padding-bottom: 15px; border:1px solid black;">
This is a paragraph with a specified bottom padding
</p>
<body>
<p style = "padding-top: 15px; border:1px solid black;">
This is a paragraph with a specified top padding
</p>
<body>
<p style = "padding-left: 15px; border:1px solid black;">
This is a paragraph with a specified left padding
</p>
<body>
<p style = "padding-right: 15px; border:1px solid black;">
This is a paragraph with a specified right padding
</p>
<body>
<p style = "padding: 15px; border:1px solid black;">
all four padding will be 15px
</p>
1
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...
2
crosshair
A crosshair or plus sign
3
default
An arrow
4
pointer
A pointing hand (in IE 4 this value is hand)
5
move
The I bar
6
e-resize
The cursor indicates that an edge of a box is to be moved right
(east)
7
ne-resize
The cursor indicates that an edge of a box is to be moved up and
right (north/east)
8
nw-resize
The cursor indicates that an edge of a box is to be moved up and
left (north/west)
9
n-resize
The cursor indicates that an edge of a box is to be moved up
(north)
10
se-resize
The cursor indicates that an edge of a box is to be moved down
and right (south/east)
11
sw-resize
The cursor indicates that an edge of a box is to be moved down
and left (south/west)
12
s-resize
The cursor indicates that an edge of a box is to be moved down
(south)
13
w-resize
The cursor indicates that an edge of a box is to be moved left
(west)
14
text
The I bar
15
wait
An hour glass
16
help
A question mark or balloon, ideal for use over help buttons
17
<url>
The source of a cursor image file
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>
CSS - Outlines
Outlines are very similar to borders, but there are few major differences as
well −
● An outline does not take up space.
● Outlines do not have to be rectangular.
● Outline is always the same on all sides; you cannot specify
different values for different sides of an element.
NOTE − The outline properties are not supported by IE 6 or Netscape 7.
You can set the following outline properties using CSS.
● The outline-width property is used to set the width of the
outline.
● The outline-style property is used to set the line style for the
outline.
● The outline-color property is used to set the color of the outline.
● The outline property is used to set all the above three properties
in a single statement.
<body>
<p style = "outline-width:thin; outline-style:solid;">
This text is having thin outline.
</p>
<br />
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 />
<body>
<p style = "outline-width:thin; outline-style:solid;outline-color:red">
This text is having thin solid red outline.
</p>
<br />
<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>
The line-height Property
The line-height property allows you to increase the space between lines of
text. The value of the line-height property can be a number, a length, or a
percentage.
Here is an example −
<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>
<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/[Link]" width = "95" height = "84"
/>
</body>
</html>
<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
This paragraph is 200px high and max width is 100px
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 = "/images/[Link]" width = "95" height = "84" />
</body>
</html>
<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
</p>
<img alt = "logo" src = "/css/images/[Link]" width = "95" height = "84" />
</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 −
1
visible
Allows the content to overflow the borders of its containing
element.
2
hidden
The content of the nested element is simply cut off at the border of
the containing element and no scrollbars is visible.
3
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.
4
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>
<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>
</head>
<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 />
1
border-radius
Use this element for setting four boarder radius property
2
border-top-left-radius
Use this element for setting the boarder of top left corner
3
border-top-right-radius
Use this element for setting the boarder of top right corner
4
border-bottom-right-radius
Use this element for setting the boarder of bottom right corner
5
border-bottom-left-radius
Use this element for setting the boarder of bottom left corner
Example
This property can have three values. The following example uses both the
values −
<html>
<head>
<style>
#rcorners1 {
border-radius: 25px;
background: #8AC007;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners2 {
border-radius: 25px;
border: 2px solid #8AC007;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners3 {
border-radius: 25px;
background: url(/css/images/[Link]);
background-position: left top;
background-repeat: repeat;
padding: 20px;
width: 200px;
height: 150px;
}
</style>
</head>
<body>
<p id = "rcorners1">Rounded corners!</p>
<p id = "rcorners2">Rounded corners!</p>
<p id = "rcorners3">Rounded corners!</p>
</body>
</html>
<body>
<p id = "rcorners1"></p>
<p id = "rcorners2"></p>
<p id = "rcorners3"></p>
</body>
<body>
CSS3 - Border Image
CSS Border image property is used to add image boarder to some
[Link] don't need to use any HTML code to call boarder image.A
sample syntax of boarder image is as follows −
#borderimg {
border: 10px solid transparent;
padding: 15px;
}
The most commonly used values are shown below −
1
border-image-source
Used to set the image path
2
border-image-slice
Used to slice the boarder image
3
border-image-width
Used to set the boarder image width
4
border-image-repeat
Used to set the boarder image as rounded, repeated and stretched
Example
Following is the example which demonstrates to set image as a border for
elements.
<html>
<head>
<style>
#borderimg1 {
border: 10px solid transparent;
padding: 15px;
border-image-source: url(/css/images/[Link]);
border-image-repeat: round;
border-image-slice: 30;
border-image-width: 10px;
}
#borderimg2 {
border: 10px solid transparent;
padding: 15px;
border-image-source: url(/css/images/[Link]);
border-image-repeat: round;
border-image-slice: 30;
border-image-width: 20px;
}
#borderimg3 {
border: 10px solid transparent;
padding: 15px;
border-image-source: url(/css/images/[Link]);
border-image-repeat: round;
border-image-slice: 30;
border-image-width: 30px;
}
</style>
</head>
<body>
<p id = "borderimg1">This is image boarder example.</p>
<p id = "borderimg2">This is image boarder example.</p>
<p id = "borderimg3">This is image boarder example.</p>
</body>
</html>
CSS3 - Multi Background
Multi Background
CSS Multi background property is used to add one or more images at a time
without HTML code, We can add images as per our requirement.A sample
syntax of multi background images is as follows −
#multibackground {
background-image: url(/css/images/[Link]), url(/css/images/[Link]);
background-position: left top, left top;
background-repeat: no-repeat, repeat;
padding: 75px;
}
The most commonly used values are shown below −
1
background
Used to setting all the background image properties in one section
2
background-clip
Used to declare the painting area of the background
3
background-image
Used to specify the background image
4
background-origin
Used to specify position of the background images
5
background-size
Used to specify size of the background images
Example
Following is the example which demonstrate the multi background images.
<html>
<head>
<style>
#multibackground {
background-image: url(/css/images/[Link]),
url(/css/images/[Link]);
background-position: left top, left top;
background-repeat: no-repeat, repeat;
padding: 75px;
}
</style>
</head>
<body>
<div id = "multibackground">
<h1>[Link]</h1>
<p>
Tutorials Point originated from the idea that there exists a class of
readers who respond better to online content and prefer to learn new
skills at their own pace from the comforts of their drawing rooms.
The journey commenced with a single tutorial on HTML in 2006 and
elated
by the response it generated, we worked our way to adding fresh
tutorials
to our repository which now proudly flaunts a wealth of tutorials and
allied articles on topics ranging from programming languages to web
designing
to academics and much more..
</p>
</div>
</body>
</html>
RGBA colors
HSL colors
HSLA colors
Opacity
RGBA stands for Red Green Blue [Link] is an extension of CSS2,Alpha
specifies the opacity of a color and parameter number is a numerical
between 0.0 to 1.0. A Sample syntax of RGBA as shown below −
#d1 {background-color: rgba(255, 0, 0, 0.5);}
#d2 {background-color: rgba(0, 255, 0, 0.5);}
#d3 {background-color: rgba(0, 0, 255, 0.5);}
HSL stands for hue, saturation, [Link] Huge is a degree on the
color wheel, saturation and lightness are percentage values between 0 to
100%. A Sample syntax of HSL as shown below −
#g1 {background-color: hsl(120, 100%, 50%);}
#g2 {background-color: hsl(120, 100%, 75%);}
#g3 {background-color: hsl(120, 100%, 25%);}
HSLA stands for hue, saturation, lightness and alpha. Alpha value
specifies the opacity as shown RGBA. A Sample syntax of HSLA as shown
below −
#g1 {background-color: hsla(120, 100%, 50%, 0.3);}
#g2 {background-color: hsla(120, 100%, 75%, 0.3);}
#g3 {background-color: hsla(120, 100%, 25%, 0.3);}
opacity is a thinner paints need black added to increase opacity. A sample
syntax of opacity is as shown below −
#g1 {background-color:rgb(255,0,0);opacity:0.6;}
#g2 {background-color:rgb(0,255,0);opacity:0.6;}
#g3 {background-color:rgb(0,0,255);opacity:0.6;}
The following example shows rgba color property.
<html>
<head>
<style>
#p1 {background-color:rgba(255,0,0,0.3);}
#p2 {background-color:rgba(0,255,0,0.3);}
#p3 {background-color:rgba(0,0,255,0.3);}
</style>
</head>
<body>
<p>RGBA colors:</p>
<p id = "p1">Red</p>
<p id = "p2">Green</p>
<p id = "p3">Blue</p>
</body>
</html>
<body>
<p>HSL colors:</p>
<p id = "g1">Green</p>
<p id = "g2">Normal Green</p>
<p id = "g3">Dark Green</p>
</body>
</html>
<body>
<p>HSLA colors:</p>
<p id = "d1">Less opacity green</p>
<p id = "d2">Green</p>
<p id = "d3">Green</p>
</body>
</html>
The following example shows Opacity property.
<html>
<head>
<style>
#m1 {background-color:rgb(255,0,0);opacity:0.6;}
#m2 {background-color:rgb(0,255,0);opacity:0.6;}
#m3 {background-color:rgb(0,0,255);opacity:0.6;}
</style>
</head>
<body>
<p>HSLA colors:</p>
<p id = "m1">Red</p>
<p id = "m2">Green</p>
<p id = "m3">Blue</p>
</body>
</html>
CSS3 - Gradients
What is Gradients?
Gradients displays the combination of two or more colors as shown below −
Types of gradients
Linear Gradients(down/up/left/right/diagonally)
Radial Gradients
Linear gradients
Linear gradients are used to arrange two or more colors in linear formats like
top to bottom.
Top to bottom
<html>
<head>
<style>
#grad1 {
height: 100px;
background: -webkit-linear-gradient(pink,green);
background: -o-linear-gradient(pink,green);
background: -moz-linear-gradient(pink,green);
background: linear-gradient(pink,green);
}
</style>
</head>
<body>
<div id = "grad1"></div>
</body>
</html>
Left to right
<html>
<head>
<style>
#grad1 {
height: 100px;
background: -webkit-linear-gradient(left, red , blue);
background: -o-linear-gradient(right, red, blue);
background: -moz-linear-gradient(right, red, blue);
background: linear-gradient(to right, red , blue);
}
</style>
</head>
<body>
<div id = "grad1"></div>
</body>
</html>
Diagonal
Diagonal starts at top left and right button.
<html>
<head>
<style>
#grad1 {
height: 100px;
background: -webkit-linear-gradient(left top, red , blue);
background: -o-linear-gradient(bottom right, red, blue);
background: -moz-linear-gradient(bottom right, red, blue);
background: linear-gradient(to bottom right, red , blue);
}
</style>
</head>
<body>
<div id = "grad1"></div>
</body>
</html>
Multi color
<html>
<head>
<style>
#grad2 {
height: 100px;
background: -webkit-linear-gradient(red, orange, yellow, red, blue,
green,pink);
background: -o-linear-gradient(red, orange, yellow, red, blue,
green,pink);
background: -moz-linear-gradient(red, orange, yellow, red, blue,
green,pink);
background: linear-gradient(red, orange, yellow, red, blue, green,pink);
}
</style>
</head>
<body>
<div id = "grad2"></div>
</body>
</html>
<body>
<div id = "grad1"></div>
</body>
</html>
<body>
<div id = "grad1"></div>
</body>
</html>
CSS3 - Shadow
CSS3 supported to add shadow to text or [Link] property has
divided as follows −
Text shadow
Box Shadow
Text shadow
CSS3 supported to add shadow effects to text. Following is the example to
add shadow effects to text −
<html>
<head>
<style>
h1 {
text-shadow: 2px 2px;
}
h2 {
text-shadow: 2px 2px red;
}
h3 {
text-shadow: 2px 2px 5px red;
}
h4 {
color: white;
text-shadow: 2px 2px 4px #000000;
}
h5 {
text-shadow: 0 0 3px #FF0000;
}
h6 {
text-shadow: 0 0 3px #FF0000, 0 0 5px #0000FF;
}
p{
color: white;
text-shadow: 1px 1px 2px black, 0 0 25px blue, 0 0 5px darkblue;
}
</style>
</head>
<body>
<h1>[Link]</h1>
<h2>[Link]</h2>
<h3>[Link]</h3>
<h4>[Link]</h4>
<h5>[Link]</h5>
<h6>[Link]</h6>
<p>[Link]</p>
</body>
</html>
box shadow
Used to add shadow effects to elements, Following is the example to add
shadow effects to element.
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
padding: 15px;
background-color: red;
box-shadow: 10px 10px;
}
</style>
</head>
<body>
<div>This is a div element with a box-shadow</div>
</body>
</html>
CSS3 - Text
CSS3 contained several extra features, which is added later on.
text-overflow
word-wrap
word-break
There are following most commonly used property in CSS3 −
1
text-align-last
Used to align the last line of the text
2
text-emphasis
Used to emphasis text and color
3
text-overflow
used to determines how overflowed content that is not displayed is
signaled to users
4
word-break
Used to break the line based on word
5
word-wrap
Used to break the line and wrap onto next line
Text-overflow
The text-overflow property determines how overflowed content that is not
displayed is signaled to users. the sample example of text overflow is shown
as follows −
<html>
<head>
<style>
p.text1 {
white-space: nowrap;
width: 500px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: clip;
}
p.text2 {
white-space: nowrap;
width: 500px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<b>Original Text:</b>
<p>
Tutorials Point originated from the idea that there exists a class of
readers who respond better to online content and prefer to learn new
skills at their own pace from the comforts of their drawing rooms.
</p>
<b>Text overflow:clip:</b>
<b>Text overflow:ellipsis</b>
</body>
</html>
CSS3 Word Breaking
Used to break the line, following code shows the sample code of word
breaking.
<html>
<head>
<style>
p.text1 {
width: 140px;
border: 1px solid #000000;
word-break: keep-all;
}
p.text2 {
width: 140px;
border: 1px solid #000000;
word-break: break-all;
}
</style>
</head>
<body>
</body>
</html>
CSS word wrapping
Word wrapping is used to break the line and wrap onto next [Link]
following code will have sample syntax −
p{
word-wrap: break-word;
}
CSS3 - Web Fonts
Web fonts are used to allows the fonts in CSS, which are not installed on
local system.
1
TrueType Fonts (TTF)
TrueType is an outline font standard developed by Apple and
Microsoft in the late 1980s, It became most common fonts for both
windows and MAC operating systems.
2
OpenType Fonts (OTF)
OpenType is a format for scalable computer fonts and developed by
Microsoft
3
The Web Open Font Format (WOFF)
WOFF is used for develop web page and developed in the year of
2009. Now it is using by W3C recommendation.
4
SVG Fonts/Shapes
SVG allow SVG fonts within SVG documentation. We can also apply
CSS to SVG with font face property.
5
Embedded OpenType Fonts (EOT)
EOT is used to develop the web pages and it has embedded in
webpages so no need to allow 3rd party fonts
<body>
<div>This is the example of font face with CSS3.</div>
<p><b>Original Text :</b>This is the example of font face with CSS3.</p>
</body>
</html>
Fonts description
The following list contained all the fonts description which are placed in the
@font-face rule −
1
font-family
Used to defines the name of font
2
src
Used to defines the URL
3
font-stretch
Used to find, how font should be stretched
4
font-style
Used to defines the fonts style
5
font-weight
Used to defines the font weight(boldness)
CSS3 - 2d Transforms
2D transforms are used to re-change the element structure as translate,
rotate, scale, and skew.
The following table has contained common values which are used in 2D
transforms −
1
matrix(n,n,n,n,n,n)
Used to defines matrix transforms with six values
2
translate(x,y)
Used to transforms the element along with x-axis and y-axis
3
translateX(n)
Used to transforms the element along with x-axis
4
translateY(n)
Used to transforms the element along with y-axis
5
scale(x,y)
Used to change the width and height of element
6
scaleX(n)
Used to change the width of element
7
scaleY(n)
Used to change the height of element
8
rotate(angle)
Used to rotate the element based on an angle
9
skewX(angle)
Used to defines skew transforms along with x axis
10
skewY(angle)
Used to defines skew transforms along with y axis
Rotate 20 degrees
Box rotation with 20 degrees angle as shown below −
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: pink;
border: 1px solid black;
}
div#myDiv {
/* IE 9 */
-ms-transform: rotate(20deg);
/* Safari */
-webkit-transform: rotate(20deg);
/* Standard syntax */
transform: rotate(20deg);
}
</style>
</head>
<body>
<div>
Tutorials [Link].
</div>
<div id = "myDiv">
Tutorials [Link]
</div>
</body>
</html>
Rotate -20 degrees
Box rotation with -20 degrees angle as shown below −
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: pink;
border: 1px solid black;
}
div#myDiv {
/* IE 9 */
-ms-transform: rotate(-20deg);
/* Safari */
-webkit-transform: rotate(-20deg);
/* Standard syntax */
transform: rotate(-20deg);
}
</style>
</head>
<body>
<div>
Tutorials [Link].
</div>
<div id = "myDiv">
Tutorials [Link]
</div>
</body>
</html>
Skew X axis
Box rotation with skew x-axis as shown below −
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: pink;
border: 1px solid black;
}
div#skewDiv {
/* IE 9 */
-ms-transform: skewX(20deg);
/* Safari */
-webkit-transform: skewX(20deg);
/* Standard syntax */
transform: skewX(20deg);
}
</style>
</head>
<body>
<div>
Tutorials [Link].
</div>
<div id = "skewDiv">
Tutorials [Link]
</div>
</body>
</html>
Skew Y axis
Box rotation with skew y-axis as shown below −
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: pink;
border: 1px solid black;
}
div#skewDiv {
/* IE 9 */
-ms-transform: skewY(20deg);
/* Safari */
-webkit-transform: skewY(20deg);
/* Standard syntax */
transform: skewY(20deg);
}
</style>
</head>
<body>
<div>
Tutorials [Link].
</div>
<div id = "skewDiv">
Tutorials [Link]
</div>
</body>
</html>
Matrix transform
Box rotation with Matrix transforms as shown below −
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: pink;
border: 1px solid black;
}
div#myDiv1 {
/* IE 9 */
-ms-transform: matrix(1, -0.3, 0, 1, 0, 0);
/* Safari */
-webkit-transform: matrix(1, -0.3, 0, 1, 0, 0);
/* Standard syntax */
transform: matrix(1, -0.3, 0, 1, 0, 0);
}
</style>
</head>
<body>
<div>
Tutorials [Link].
</div>
<div id = "myDiv1">
Tutorials [Link]
</div>
</body>
</html>
/* Safari */
-webkit-transform: matrix(1, 0, 0.5, 1, 150, 0);
/* Standard syntax */
transform: matrix(1, 0, 0.5, 1, 150, 0);
}
</style>
</head>
<body>
<div>
Tutorials [Link].
</div>
<div id = "myDiv2">
Tutorials [Link]
</div>
</body>
</html>
CSS3 - 3D Transforms
Using with 3d transforms, we can move element to x-axis, y-axis and z-axis,
Below example clearly specifies how the element will rotate.
Methods of 3D transforms
Below methods are used to call 3D transforms −
1
matrix3d(n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n)
Used to transforms the element by using 16 values of matrix
2
translate3d(x,y,z)
Used to transforms the element by using x-axis,y-axis and z-axis
3
translateX(x)
Used to transforms the element by using x-axis
4
translateY(y)
Used to transforms the element by using y-axis
5
translateZ(z)
Used to transforms the element by using y-axis
6
scaleX(x)
Used to scale transforms the element by using x-axis
7
scaleY(y)
Used to scale transforms the element by using y-axis
8
scaleY(y)
Used to transforms the element by using z-axis
9
rotateX(angle)
Used to rotate transforms the element by using x-axis
10
rotateY(angle)
Used to rotate transforms the element by using y-axis
11
rotateZ(angle)
Used to rotate transforms the element by using z-axis
X-axis 3D transforms
The following an example shows the x-axis 3D transforms.
<html>
<head>
<style>
div {
width: 200px;
height: 100px;
background-color: pink;
border: 1px solid black;
}
div#myDiv {
-webkit-transform: rotateX(150deg);
/* Safari */
transform: rotateX(150deg);
/* Standard syntax */
}
</style>
</head>
<body>
<div>
tutorials [Link]
</div>
<p>Rotate X-axis</p>
<div id = "myDiv">
tutorials [Link].
</div>
</body>
</html>
Y-axis 3D transforms
The following an example shows the y-axis 3D transforms −
<html>
<head>
<style>
div {
width: 200px;
height: 100px;
background-color: pink;
border: 1px solid black;
}
div#yDiv {
-webkit-transform: rotateY(150deg);
/* Safari */
transform: rotateY(150deg);
/* Standard syntax */
}
</style>
</head>
<body>
<div>
tutorials [Link]
</div>
<p>Rotate Y axis</p>
<div id = "yDiv">
tutorials [Link].
</div>
</body>
</html>
Z-axis 3D transforms
The following an example shows the Z-axis 3D transforms −
<html>
<head>
<style>
div {
width: 200px;
height: 100px;
background-color: pink;
border: 1px solid black;
}
div#zDiv {
-webkit-transform: rotateZ(90deg);
/* Safari */
transform: rotateZ(90deg);
/* Standard syntax */
}
</style>
</head>
<body>
<div>
tutorials [Link]
</div>
<p>rotate Z axis</p>
<div id = "zDiv">
tutorials [Link].
</div>
</body>
</html>
CSS3 - Animation
Animation is process of making shape changes and creating motions with
elements.
@keyframes
Keyframes will control the intermediate animation steps in CSS3.
<body>
<h1>Tutorials Point</h1>
<p>this is an example of moving left animation .</p>
<button onclick = "myFunction()">Reload page</button>
<script>
function myFunction() {
[Link]();
}
</script>
</body>
</html>
<body>
<h1>Tutorials Point</h1>
1
column-count
Used to count the number of columns that element should be
divided.
2
column-fill
Used to decide, how to fill the columns.
3
column-gap
Used to decide the gap between the columns.
4
column-rule
Used to specifies the number of rules.
5
rule-color
Used to specifies the column rule color.
6
rule-style
Used to specifies the style rule for column.
7
rule-width
Used to specifies the width.
8
column-span
Used to specifies the span between columns.
Example
Below example shows the arrangement of text as new paper structure.
<html>
<head>
<style>
.multi {
/* Column count property */
-webkit-column-count: 4;
-moz-column-count: 4;
column-count: 4;
<body>
</body>
</html>
For suppose, if user wants to make text as new paper without line, we can do
this by removing style syntax as shown below −
.multi {
/* Column count property */
-webkit-column-count: 4;
-moz-column-count: 4;
column-count: 4;
1
appearance
Used to allow the user to make elements as user interface elements.
2
box-sizing
Allows to users to fix elements on area in clear way.
3
icon
Used to provide the icon on area.
4
resize
Used to resize elements which are on area.
5
outline-offset
Used to draw the behind the outline.
6
nav-down
Used to move down when you have pressed on down arrow button in
keypad.
7
nav-left
Used to move left when you have pressed on left arrow button in
keypad.
8
nav-right
Used to move right when you have pressed on right arrow button in
keypad.
9
nav-up
Used to move up when you have pressed on up arrow button in
keypad.
horizontal
vertical
both
Using of both value in resize property in css3 user interface −
<html>
<head>
<style>
div {
border: 2px solid;
padding: 20px;
width: 300px;
resize: both ;
overflow: auto;
}
</style>
</head>
<body>
<div>[Link]</div>
</body>
</html>
<body>
<div>TutorialsPoint</div>
</body>
</html>
CSS3 - Box Sizing
Box sizing property is using to change the height and width of element.
Since css2, the box property has worked like as shown below −
width + padding + border = actual visible/rendered width of an element's
box
height + padding + border = actual visible/rendered height of an element's
box
Means when you set the height and width, it appears little bit bigger then
given size cause element boarder and padding added to the element height
and width.
<body>
<div class = "div1">[Link]</div><br />
<div class = "div2">[Link]</div>
</body>
</html>
Above image is having same width and height of two element but giving
result is different, cause second one is included padding property.
<body>
<div class = "div1">[Link]</div><br />
<div class = "div2">[Link]</div>
</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 −
1
visible
The box and its contents are shown to the user.
2
hidden
The box and its content are made invisible, although they still affect
the layout of the page.
3
collapse
This is for use only with dynamic table columns and row effects.
Here is an example −
<html>
<head>
</head>
<body>
<p>
This paragraph should be visible in normal way.
</p>
Relative Positioning
Relative positioning changes the position of the HTML element relative to
where it normally appears. So "left:20" adds 20 pixels to the element's LEFT
position.
You can use two values top and left along with the position property to move
an HTML element anywhere in the HTML document.
<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.
You can use two values top and left along with the position property to move
an HTML element anywhere in the HTML document.
<body>
<div style = "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.
You can use two values top and left along with the position property to move
an HTML element anywhere in the HTML document.
<body>
<div style = "background-color:red;
width:300px;
height:100px;
position:relative;
top:10px;
left:80px;
z-index:2">
</div>
1
:link
Use this class to add special style to an unvisited link.
2
:visited
Use this class to add special style to a visited link.
3
:hover
Use this class to add special style to an element when you mouse
over it.
4
:active
Use this class to add special style to an active element.
5
:focus
Use this class to add special style to an element while the element
has focus.
6
:first-child
Use this class to add special style to an element that is the first child
of some other element.
7
:lang
Use this class to specify a language to use in a specified element.
<body>
<a href = "">Black Link</a>
</body>
</html>
<body>
<a href = "">Click this link</a>
</body>
</html>
<body>
<a href = "">Bring Mouse Here</a>
</body>
</html>
<body>
<a href = "">Click This Link</a>
</body>
</html>
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>
<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>
<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
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}
CSS classes can also be used with pseudo-elements −
[Link]:pseudo-element {property: value}
The most commonly used pseudo-elements are as follows −
1
:first-line
Use this element to add special styles to the first line of the text in a
selector.
2
:first-letter
Use this element to add special style to the first letter of the text in a
selector.
3
:before
Use this element to insert some content before an element.
4
:after
Use this element to insert some content after an element.
<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>
<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>
The :before pseudo-element
The following example demonstrates how to use the :before element to add
some content before any element.
<html>
<head>
<style type = "text/css">
p:before {
content: url(/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>
<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 Filters - Text and Image Effects
You can use CSS filters to add special effects to text, images and other
aspects of a webpage without using images or other graphics. Filters only
work on Internet Explorer 4.0. If you are developing your site for multiple
browsers, then it may not be a good idea to use CSS filters because there is
a possibility that it would not give any advantage.
In this chapter, we will see the details of each CSS filter. These filters may
not work in your browser.
Alpha Channel
The Alpha Channel filter alters the opacity of the object, which makes it
blend into the background. The following parameters can be used in this
filter −
1
opacity
Level of the opacity. 0 is fully transparent, 100 is fully opaque.
2
finishopacity
Level of the opacity at the other end of the object.
3
style
The shape of the opacity gradient.
0 = uniform
1 = linear
2 = radial
3 = rectangular
4
startX
X coordinate for opacity gradient to begin.
5
startY
Y coordinate for opacity gradient to begin.
6
finishX
X coordinate for opacity gradient to end.
7
finishY
Y coordinate for opacity gradient to end.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
Motion Blur
Motion Blur is used to create blurred pictures or text with the direction and
strength. The following parameters can be used in this filter −
Sr.N Parameter & Description
o.
1
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.
2
direction
The direction of the blur, going clockwise, rounded to 45-degree
increments. The default value is 270 (left).
0 = Top
45 = Top right
90 = Right
135 = Bottom right
180 = Bottom
225 = Bottom left
270 = Left
315 = Top left
3
strength
The number of pixels the blur will extend. The default is 5 pixels.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
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 −
1
color
The color that you'd like to be transparent.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
1
color
The color, in #RRGGBB format, of the dropshadow.
2
offX
Number of pixels the drop shadow is offset from the visual object,
along the x-axis. Positive integers move the drop shadow to the right,
negative integers move the drop shadow to the left.
3
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.
4
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>
Flip Effect
Flip effect is used to create a mirror image of the object. The following
parameters can be used in this filter −
1
FlipH
Creates a horizontal mirror image
2
FlipV
Creates a vertical mirror image
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
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 −
1
color
The color you want the glow to be.
2
strength
The intensity of the glow (from 1 to 255).
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
Grayscale Effect
Grayscale effect is used to convert the colors of the object to 256 shades of
gray. The following parameter is used in this filter −
1
grayscale
Converts the colors of the object to 256 shades of gray.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
Invert Effect
Invert effect is used to map the colors of the object to their opposite values
in the color spectrum, i.e., to create a negative image. The following
parameter is used in this filter −
1
Invert
Maps the colors of the object to their opposite value in the color
spectrum.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
Mask Effect
Mask effect is used to turn transparent pixels to a specified color and makes
opaque pixels transparent. The following parameter is used in this filter −
1
color
The color that the transparent areas will become.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
Shadow Filter
Shadow filter is used to create an attenuated shadow in the direction and
color specified. This is a filter that lies in between Dropshadow and Glow. The
following parameters can be used in this filter −
1
color
The color that you want the shadow to be.
2
direction
The direction of the blur, going clockwise, rounded to 45-degree
increments. The default value is 270 (left).
0 = Top
45 = Top right
90 = Right
135 = Bottom right
180 = Bottom
225 = Bottom left
270 = Left
315 = Top left
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
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 −
1
add
A value of 1 adds the original image to the waved image, 0 does not.
2
freq
The number of waves.
3
light
The strength of the light on the wave (from 0 to 100).
4
phase
At what degree the sine wave should start (from 0 to 100).
5
strength
The intensity of the wave effect.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
1
xray
Grayscales and flattens the color depth.
Example
<html>
<head>
</head>
<body>
<p>Image Example:</p>
<p>Text Example:</p>
Possible Values
left − The element is floated to the left side of its parent
element's content area.
right − The element is floated to the right side of its parent
element's content area.
none − The element is not floated.
Applies to
All the elements but positioned elements and generated content.
DOM Syntax
[Link] = "left";
Example
Here is the example showing usage of this property −
<h1 style = "float: left;">
CSS float Example
</h1>
<p>
If your browser supports the CSS float Property, this text will be flowing
around the heading.
</p>
Floating Elements with CSS
The CSS float property is used for positioning or formatting a box or content.
Developer can position element towards left or right with CSS float.
The float property can have one of the following values −
Example
Let’s see an example of CSS Float property −
<!DOCTYPE html>
<html>
<head>
<title>CSS Float</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
input[type="button"] {
border-radius: 10px;
}
#container {
display: flex;
flex-direction: column-reverse;
justify-content: center;
align-items: center;
}
.child{
height: 40px;
width: 40px;
color: white;
border: 4px solid black;
}
.orange{
background-color: #FF8A00;
}
.red{
background-color: #F44336;
}
.violet{
background-color: #C303C3;
}
.green{
background-color: #4CAF50;
}
.blue{
background-color: #03A9F4;
}
.yellow{
background-color: #FEDC11;
}
#left{
display: flex;
float: left;
}
#right{
display: flex;
float: right;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>CSS-Float</legend>
<div id="container">
<div class="child orange"></div><div class="child red"></div><div
class="child violet"></div><div class="child green"></div><div class="child
blue"></div><div class="child yellow"></div>
</div><br>
<input type="button" value="float-->left" onclick="floatDecider('left')">
<input type="button" value="float-->right" onclick="floatDecider('right')">
<div><div id="left"></div><div id="right"></div></div>
</fieldset>
</form>
<script>
var left = [Link]('left');
var right = [Link]('right');
function floatDecider(direction){
var allChildren = [Link]('child');
if(direction === 'left')
[Link]('beforeend',allChildren[0]);
else
[Link]('afterbegin',allChildren[0]);
}
</script>
</body>
</html>