Friday, 25 March 2016

CSS Colors

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 tells you all possible formats:

Format
Syntax
Example
Hex Code
#RRGGBB
p{color:#FF0000;}
Short Hex Code
#RGB
p{color:#6A7;}
RGB %
rgb(rrr%,ggg%,bbb%)
p{color:rgb(50%,50%,50%);}
RGB Absolute
rgb(rrr,ggg,bbb)
p{color:rgb(0,0,255);}
keyword
aqua, black, etc.
p{color:teal;}
These formats are explained in more detail in the following sections:

CSS Colors - Hex Codes:

A hexadecimal is a 6 digit representation of a color. The first two digits(RR) represent a red value, the next two are a green value(GG), and the last are the blue value(BB).

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.

Color
Color HEX
 
#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.

Color
Color HEX
 
#000
 
#F00
 
#0F0
 
#0FF
 
#FF0
 
#0FF
 
#F0F
 
#FFF
CSS Colors - RGB Values:

This color value is specified using the rgb( ) property. This property takes three values, one each for red, green, and blue. The value can be an integer between 0 and 255 or a percentage.

NOTE: All the browsers does not support rgb() property of color so it is recommended not to use it.

Following is the example to show few colors using RGB values.

Color
Color RGB
 
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.

Browser Safe Colors:

Here is the list of 216 colors which are supposed to be most safe and computer independent colors. These colors very from hexa code 000000 to FFFFFF. These color are safe to use because they ensure that all computers would display the colors correctly when running a 256 color palette:

000000
000033
000066
000099
0000CC
0000FF
003300
003333
003366
003399
0033CC
0033FF
006600
006633
006666
006699
0066CC
0066FF
009900
009933
009966
009999
0099CC
0099FF
00CC00
00CC33
00CC66
00CC99
00CCCC
00CCFF
00FF00
00FF33
00FF66
00FF99
00FFCC
00FFFF
330000
330033
330066
330099
3300CC
3300FF
333300
333333
333366
333399
3333CC
3333FF
336600
336633
336666
336699
3366CC
3366FF
339900
339933
339966
339999
3399CC
3399FF
33CC00
33CC33
33CC66
33CC99
33CCCC
33CCFF
33FF00
33FF33
33FF66
33FF99
33FFCC
33FFFF
660000
660033
660066
660099
6600CC
6600FF
663300
663333
663366
663399
6633CC
6633FF
666600
666633
666666
666699
6666CC
6666FF
669900
669933
669966
669999
6699CC
6699FF
66CC00
66CC33
66CC66
66CC99
66CCCC
66CCFF
66FF00
66FF33
66FF66
66FF99
66FFCC
66FFFF
990000
990033
990066
990099
9900CC
9900FF
993300
993333
993366
993399
9933CC
9933FF
996600
996633
996666
996699
9966CC
9966FF
999900
999933
999966
999999
9999CC
9999FF
99CC00
99CC33
99CC66
99CC99
99CCCC
99CCFF
99FF00
99FF33
99FF66
99FF99
99FFCC
99FFFF
CC0000
CC0033
CC0066
CC0099
CC00CC
CC00FF
CC3300
CC3333
CC3366
CC3399
CC33CC
CC33FF
CC6600
CC6633
CC6666
CC6699
CC66CC
CC66FF
CC9900
CC9933
CC9966
CC9999
CC99CC
CC99FF
CCCC00
CCCC33
CCCC66
CCCC99
CCCCCC
CCCCFF
CCFF00
CCFF33
CCFF66
CCFF99
CCFFCC
CCFFFF
FF0000
FF0033
FF0066
FF0099
FF00CC
FF00FF
FF3300
FF3333
FF3366
FF3399
FF33CC
FF33FF
FF6600
FF6633
FF6666
FF6699
FF66CC
FF66FF
FF9900
FF9933
FF9966
FF9999
FF99CC
FF99FF
FFCC00
FFCC33
FFCC66
FFCC99
FFCCCC
FFCCF

CSS Measurement Units

CSS Measurement Units

Before we start actual exercise, I 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 alogwith proper Examples:

Unit
Description
Example
%
Defines a measurement as a percentage relative to another value, typically an enclosing element.
p {font-size: 16pt; line-height: 125%;}
cm
Defines a measurement in centimeters.
div {margin-bottom: 2cm;}
em
A relative measurement for the height of a font in em spaces. Because an em unit is equivalent to the size of a given font, if you assign a font to 12pt, each "em" unit would be 12pt; thus, 2em would be 24pt.
p {letter-spacing: 7em;}
ex
This value defines a measurement relative to a font's x-height. The x-height is determined by the height of the font's lowercase letter x.
p {font-size: 24pt; line-height: 3ex;}
in
Defines a measurement in inches.
p {word-spacing: .15in;}
mm
Defines a measurement in millimeters.
p {word-spacing: 15mm;}
pc
Defines a measurement in picas. A pica is equivalent to 12 points; thus, there are 6 picas per inch.
p {font-size: 20pc;}
pt
Defines a measurement in points. A point is defined as 1/72nd of an inch.
body {font-size: 18pt;}
px
Defines a measurement in screen pixels.
p {padding

CSS Inclusion

CSS Inclusion Associating Styles

There are four ways to associate styles with your HTML document. Most commonly used methods are inline CSS and External CSS.

Embedded CSS - The <style> Element:

You can put your CSS rules into an HTML document using the <style> element. This tag is placed inside <head>...</head> tags. Rules defined using this syntax will be applied to all the elements available in the document. Here is the generic syntax:

<head>
<style type="text/css" media="...">
Style Rules
............
</style>
</head>
Attributes:

Attributes associated with <style> elements are:

Attribute
Value
Description
type
text/css
Specifies the style sheet language as a content-type (MIME type). This is required attribute.
media
screen
tty
tv
projection
handheld
print
braille
aural
all
Specifies the device the document will be displayed on. Default value is all. This is optional attribute.
Example:

Following is the example of embed CSS based on above syntax:

<head>
<style type="text/css" media="all">
h1{
color: #36C;
}
</style>
</head>
Inline CSS - The style Attribute:

You can use style attribute of any HTML element to define style rules. These rules will be applied to that element only. Here is the generic syntax:

<element style="...style rules....">
Attributes:

Attribute
Value
Description
style
style rules
The value of style attribute is a combination of style declarations separated by semicolon (;).
Example:

Following is the example of inline CSS based on above syntax:

<h1 style ="color:#36C;"> This is inline CSS </h1>
This will produce following result:

This is inline CSS

External CSS - The <link> Element:

The <link> element can be used to include an external stylesheet file in your HTML document.

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.

Here is the generic syntax of including external CSS file:

<head>
<link type="text/css" href="..." media="..." />
</head>
Attributes:

Attributes associated with <style> elements are:

Attribute
Value
Description
type
text/css
Specifies the style sheet language as a content-type (MIME type). This attribute is required.
href
URL
Specifies the style sheet file having Style rules. This attribute is a required.
media
screen
tty
tv
projection
handheld
print
braille
aural
all
Specifies the device the document will be displayed on. Default value is all. This is optional attribute.
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>
CSS Rules Overriding:

We have discussed four ways to include style sheet rules in a an HTML document. Here is the rule to override any Style Sheet Rule.

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.
Handling old Browsers:

There are still many old browsers who do not support CSS. So we should take care while writing our Embedded CSS in an HTML document. The following snippet shows how you can use comment tags to hide CSS from older browsers:

<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 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:

/* This is an external style sheet file */
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}

CSS syntax

CSS Syntax Selectors

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 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 or 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 }
Example: You can define a table border as follows:

table{ border :1px solid #C00; }
Here table is a selector and border is a property and given value 1px solid #C00 is the value of that property.

You can define selectors in various simple ways based on your comfort. Let me put these selectors one by one.

The Type Selectors:

This is the same selector we have seen above. Again one more example to give a color to all level 1 headings :

h1 {
   color: #36CFFF;
}
The Universal Selectors:

Rather than selecting elements of a specific type, the universal selector quite simply matches the name of any element type :

* {
  color: #000000;
}
This rule renders the content of every element in our document in black.

The 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;
}
The Class Selectors:

You can define style rules based on the class attribute of the elements. All the elements having that class will be formatted according to the defined rule.

.black {
  color: #000000;
}
This rule renders the content in black for every element with class attribute set to black in our document. You can make it a bit more particular. For example:

h1.black {
  color: #000000;
}
This rule renders the content in black for only <h1> elements with class attribute set to black.

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 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 only when those headings will lie with in tags having id attribute set to black.

The Child Selectors:

You have seen descendant selectors. There is one more type of selectors which is very similar to descendants but have different functionality. Consider the following example:

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> etc. would not have any effect of this rule.

The Attribute Selectors:

You can also apply styles to HTML elements with particular attributes. The style rule below will match all input elements that has a type attribute with a value of text:

input[type="text"]{
  color: #000000;
}
The advantage to this method is that the <input type="submit" /> element is unaffected, and the color applied only to the desired text fields.

There are following rules applied to attribute selector.

p[lang] - Selects all paragraph elements with a lang attribute.
p[lang="fr"] - Selects all paragraph elements whose lang attribute has a value of exactly "fr".
p[lang~="fr"] - Selects all paragraph elements whose lang attribute contains the word "fr".
p[lang|="en"] - Selects all paragraph elements whose lang attribute contains values that are exactly "en", or begin with "en-".
Multiple Style Rules:

You may need to define multiple style rules for a single element. You can define these rules to combine multiple properties and corresponding values into a single block as defined in the following example:

h1 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
Here all the property and value pairs are separated by a semi colon (;). You can keep them in a ingle 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 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.

You can combine various class selectors together as shown below:

#content, #footer, #supplement {
position: absolute;
left: 510px;
width: 200px;
}

CSS Intro

CSS Introduction

Before your begin:

Before you begin, it's important that you know Windows or Unix. A working knowledge of Windows or Unix makes it much easier to learn HTML.

You should be familiar with:

Basic word processing using any text editor.
How to create directories and files.
How to navigate through different directories.
Basic understanding on internet browsing using a browser like Internet Explorer or Firefox etc.
Basic understanding on developing simple Web Pages using HTML or XHTML.
If you are new to HTML and XHTML then I would suggest you to go through our HTML Tutorial or XHTML Tutorial. Anyone of HTML or XHTML is enough to proceed.

What is CSS?

Cascading Style Sheets, fondly referred to as CSS, is a simple design language intended to simplify the process of making web pages presentable.

CSS handles the look and feel part of a web page. Using CSS, you can control the color of the text, the style of fonts, the spacing between paragraphs, how columns are sized and laid out, what background images or colors are used, as well as a variety of other effects.

CSS is easy to learn and understand but it provides powerful control over the presentation of an HTML document. Most commonly, CSS is combined with the markup languages HTML or XHTML.

Advantages of CSS:

CSS saves time - You can write CSS once and then reuse 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 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 far better look to your HTML page in comparison of 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.
Who Creates and Maintains CSS?

CSS is created and maintained through a group of people within the W3C called the CSS Working Group. The CSS Working Group creates documents called specifications. When a specification has been discussed and officially ratified by W3C members, it becomes a recommendation.

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.

About this Tutorial:

This tutorial covers both versions CSS1 and CSS2 and teaches you CSS starting from basic concepts to advanced concepts. So now start from next chapter and finish it till end to become master in CSS

CSS

CSS Home

CSS is used to control the style of a web document in a simple and easy way.

CSS stands for Cascading Style Sheet.

This tutorial gives complete understanding on CSS.

CSS3 Responsive

CSS3 Responsive Web Design

What is Responsive Web Design?

Responsive web design is to create web sites that look good on all devices.

Responsive web design is not a program or a JavaScript.

Responsive web design is a must for mobile devices.

Creating Your Own Responsive Design

One way to create a responsive design, is to create it yourself, with simple CSS:

<!DOCTYPE html>
<html lang="en-US">
<head>
<style>
.article {
  float: left;
  margin: 5px;
  padding: 5px;
  width: 300px;
  height: 300px;
  border: 1px solid black;
}
</style>
</head>
<body>

<h1>W3Schools</h1>

<div class="article">
  <h3>About W3Schools</h3>
  <p>W3Schools is a web developer's site.
  It provides tutorials and references covering
  many aspects of web programming,
  including HTML, CSS, JavaScript, XML, SQL, PHP, ASP, etc.</p>
  <p>W3schools contains thousands of code examples.
  By using an online editor,
  readers can edit examples experimentally.</p>
</div>

<div class="article">
  <h3>Most Wanted Tutorials</h3>
  <p>HTML5 Tutorial</p>
  <p>How to build your web site with HTML5</p>
  <p>CSS3 Tutoral</p>
  <p>JavaScript Tutorial</p>
</div>

<div class="article">
  <h3>About This Demo</h3>
  <p>This demo is about responsive design.</p>
  <p>Try to make the page smaller or wider,
  to see responsive design in action.</p>
</div>

</body>
</html>

 

Using Bootstrap

Another way to create a responsive design, is to use an already existing CSS framework.

Bootstrap is the most popular HTML, CSS, and JavaScript framework for responsive web design.

Bootstrap helps you develop sites that look nice at any size; screen, laptop, tablet, or phone:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>

<body>
<div class="container">

<div class="jumbotron">
  <h1>W3Schools Demo</h1>
  <p>Resize this responsive page!</p>
</div>

<div class="row">
  <div class="col-md-4">
    <h2>London</h2>
    <p>London is the capital city of England.</p>
    <p>It is the most populous city in the United Kingdom,
    with a metropolitan area of over 13 million inhabitants.</p>
  </div>
  <div class="col-md-4">
    <h2>Paris</h2>
    <p>Paris is the capital and most populous city of France.</p>
  </div>
  <div class="col-md-4">
    <h2>Tokyo</h2>
    <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
    and the most populous metropolitan area in the world.</p>
  </div>
</div>

</div>
</body>
</html>

To learn more about Bootstrap read our Bootstrap Tutorial

CSS3 User Interface

CSS3 User Interface

In CSS3, some of the new user interface features are resizing elements, box sizing, and outlining.

In this chapter you will learn about the following user interface properties:

resize
box-sizing
outline-offset
Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -webkit- or -moz- specify the first version that worked with a prefix.

Property
IE
Chrome
Firebox
Safari
Opera
resize
Not supported
4.0
5.0
4.0 -moz-
4.0
15.0
box-sizing
Partial from 8.0
10.0
4.0 -webkit-
29.0
2.0 -moz-
5.1
3.1 -webkit-
9.5
outline-offset
Not supported
4.0
5.0
4.0 -moz-
4.0
9.5
CSS3 Resizing

In CSS3, the resize property specifies whether or not an element should be resizable by the user.

This div element is resizable by the user (in Chrome, Firefox, Safari and Opera 15+).
The CSS code is as follows:

Example

Specify that a <div> element should be resizable by the user:

div {
    resize: both;
    overflow: auto;
}
CSS3 Box Sizing

The box-sizing property is used to tell the browser what the sizing properties (width and height) should include.

Should they include the border-box or just the content-box which is the default value of the width and height properties.

For example, if you want two bordered boxes side by side, it can be achieved through setting box-sizing to "border-box". This forces the browser to render the box with the specified width and height, and place the border and padding inside the box.

Example

Specify two bordered boxes side by side:

div {
    -moz-box-sizing: border-box; /* Firefox */
    box-sizing: border-box;
    width: 50%;
    float: left;
}
CSS3 Outline Offset

The outline-offset property offsets an outline, and draws it beyond the border edge.

Outlines differ from borders in two ways:

Outlines do not take up space
Outlines may be non-rectangular
This div has an outline 15px outside the border edge.
The CSS code is as follows:

Example

Specify an outline 15px outside the border edge:

div {
    border: 2px solid black;
    outline: 2px solid red;
    outline-offset: 15px;
}
CSS3 User-interface Properties

The following table lists all the user-interface properties:

Property
Description
CSS
appearance
Allows you to make an element look like a standard user interface element
3
box-sizing
Allows you to define certain elements to fit an area in a certain way
3
icon
Provides the author the ability to style an element with an iconic equivalent
3
nav-down
Specifies where to navigate when using the arrow-down navigation key
3
nav-index
Specifies the tabbing order for an element
3
nav-left
Specifies where to navigate when using the arrow-left navigation key
3
nav-right
Specifies where to navigate when using the arrow-right navigation key
3
nav-up
Specifies where to navigate when using the arrow-up navigation key
3
outline-offset
Offsets an outline, and draws it beyond the border edge
3
resize
Specifies whether or not an element is resizable by the user
3

CSS3 Multiple Columns

CSS3 Multiple Columns

With CSS3, you can create multiple columns for laying out text - like in newspapers!

In this chapter you will learn about the following multiple column properties:

column-count
column-gap
column-rule
Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -webkit- or -moz- specify the first version that worked with a prefix.

Property
IE
Chrome
Firebox
Safari
Opera
column-count
10.0
4.0 -webkit-
2.0 -moz-
3.1 -webkit-
15.0 -webkit-
11.1
column-gap
10.0
4.0 -webkit-
2.0 -moz-
3.1 -webkit-
15.0 -webkit-
11.1
column-rule
10.0
4.0 -webkit-
2.0 -moz-
3.1 -webkit-
15.0 -webkit-
11.1
CSS3 Create Multiple Columns

The column-count property specifies the number of columns an element should be divided into:

Example

Divide the text in a <div> element into three columns:

div {
    -webkit-column-count: 3; /* Chrome, Safari, Opera */
    -moz-column-count: 3; /* Firefox */
    column-count: 3;
}
CSS3 Specify the Gap Between Columns

The column-gap property specifies the gap between the columns:

Example

Specify a 40 pixels gap between the columns:

div {
    -webkit-column-gap: 40px; /* Chrome, Safari, Opera */
    -moz-column-gap: 40px; /* Firefox */
    column-gap: 40px;
}
 
CSS3 Column Rules
The column-rule property sets the width, style, and color of the rule between columns.

Example

Specify the width, style and color of the rule between columns:

div {
    -webkit-column-rule: 3px outset #ff00ff; /* Chrome, Safari, Opera */
    -moz-column-rule: 3px outset #ff00ff; /* Firefox */
    column-rule: 3px outset #ff00ff;
}
CSS3 Multiple Columns Properties

The following table lists all the multiple columns properties: 

Property
Description
CSS
column-count
Specifies the number of columns an element should be divided into
3
column-fill
Specifies how to fill columns
3
column-gap
Specifies the gap between the columns
3
column-rule
A shorthand property for setting all the column-rule-* properties
3
column-rule-color
Specifies the color of the rule between columns
3
column-rule-style
Specifies the style of the rule between columns
3
column-rule-width
Specifies the width of the rule between columns
3
column-span
Specifies how many columns an element should span across
3
column-width
Specifies the width of the columns
3
columns
A shorthand property for setting column-width and column-count

CSS3 animation

CSS3 Animations

 CSS3 animations can replace animations created by Flash and JavaScript in existing web pages.

CSS3
Animation
Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.

Property
IDE
Chrome
Firebox
Safari
Opera
@keyframes
10.0
4.0 -webkit-
16.0
5.0 -moz-
4.0 -webkit-
15.0 -webkit-
12.1
12.0 -o-
animation
10.0
4.0 -webkit-
16.0
5.0 -moz-
4.0 -webkit-
15.0 -webkit-
12.1
12.0 -o-
 

CSS3 @keyframes Rule

The @keyframes rule is where the animation is created.

Specify a CSS style inside the @keyframes rule and the animation will gradually change from the current style to the new style.

CSS3 Animation

When an animation is created in the @keyframe rule, you must bind it to a selector, otherwise the animation will have no effect.

Bind the animation to a selector (element) by specifying at least these two properties:

 the name of the animation
 the duration of the animation
Example

Bind the "myfirst" animation to the <div> element. Animation duration: 5 seconds:

div {
    -webkit-animation: myfirst 5s; /* Chrome, Safari, Opera */
    animation: myfirst 5s;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes myfirst {
    from {background: red;}
    to {background: yellow;}
}

/* Standard syntax */
@keyframes myfirst {
    from {background: red;}
    to {background: yellow;}
}
Note: If the duration part is not specified, the animation will have no effect, because the default value is 0. 

What Are CSS3 Animations?

An animation lets an element gradually change from one style to another.

You can change as many properties you want, as many times you want.

You can specify when the change will happen in percent, or you can use the keywords "from" and "to" (which represents 0% and 100%).

0% represents the start of the animation, 100% is when the animation is complete.

Example

Change the background color when the animation is 25%, and 50%, and again when the animation is 100% complete:

/* Chrome, Safari, Opera */
@-webkit-keyframes myfirst {
    0%   {background: red;}
    25%  {background: yellow;}
    50%  {background: blue;}
    100% {background: green;}
}

/* Standard syntax */
@keyframes myfirst {
    0%   {background: red;}
    25%  {background: yellow;}
    50%  {background: blue;}
    100% {background: green;}
}
Example

Change the background color and the position when the animation is 25%, 50%, 75%, and again when the animation is 100% complete:

/* Chrome, Safari, Opera */
@-webkit-keyframes myfirst {
    0%   {background: red; left:0px; top:0px;}
    25%  {background: yellow; left:200px; top:0px;}
    50%  {background: blue; left:200px; top:200px;}
    75%  {background: green; left:0px; top:200px;}
    100% {background: red; left:0px; top:0px;}
}

/* Standard syntax */
@keyframes myfirst {
    0%   {background: red; left:0px; top:0px;}
    25%  {background: yellow; left:200px; top:0px;}
    50%  {background: blue; left:200px; top:200px;}
    75%  {background: green; left:0px; top:200px;}
    100% {background: red; left:0px; top:0px;}
}
More Animation Examples

The example below uses seven of the animation properties:

Example

div {
    /* Chrome, Safari, Opera */
    -webkit-animation-name: myfirst;
    -webkit-animation-duration: 5s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-delay: 2s;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-direction: alternate;
    -webkit-animation-play-state: running;
    /* Standard syntax */
    animation-name: myfirst;
    animation-duration: 5s;
    animation-timing-function: linear;
    animation-delay: 2s;
    animation-iteration-count: infinite;
    animation-direction: alternate;
    animation-play-state: running;
}
The same animation effect as the example above (except the animation-play-state property). However, here we are using the shorthand animation property:

Example

div {
    -webkit-animation: myfirst 5s linear 2s infinite alternate; /* Chrome, Safari, Opera */
    animation: myfirst 5s linear 2s infinite alternate; /* Standard syntax */
}
 
CSS3 Animation Properties
The following table lists the @keyframes rule and all the animation properties:

Property
Description
@keyframes
Specifies the animation
animation
A shorthand property for setting all the animation properties, except the animation-play-state and the animation-fill-mode property
animation-delay
Specifies when the animation will start
animation-direction
Specifies whether or not the animation should play in reverse on alternate cycles
animation-duration
Specifies how many seconds or milliseconds an animation takes to complete one cycle
animation-fill-mode
Specifies what styles will apply for the element when the animation is not playing (when it is finished, or when it has a "delay")
animation-iteration-count
Specifies the number of times an animation should be played
animation-name
Specifies the name of the @keyframes animation
animation-play-state
Specifies whether the animation is running or paused
animation-timing-function
Specifies the speed curve of the animation

CSS3 transitions

CSS3 Transitions

CSS3 transitions allows you to change property values smoothly (from one value to another), over a given duration.

Example: Hover over the element below to see a CSS3 transition effect

CSS3
Transition
Browser Support for Transitions

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.

Property
IE
Chrome
Firebox
Safari
Opera
transition
10.0
26.0
4.0 -webkit-
16.0
4.0 -moz-
6.1
3.1 -webkit-
12.1
10.5 -o-
transition-delay
10.0
26.0
4.0 -webkit-
16.0
4.0 -moz-
6.1
3.1 -webkit-
12.1
10.5 -o-
transition-duration
10.0
26.0
4.0 -webkit-
16.0
4.0 -moz-
6.1
3.1 -webkit-
12.1
10.5 -o-
transition-property
10.0
26.0
4.0 -webkit-
16.0
4.0 -moz-
6.1
3.1 -webkit-
12.1
10.5 -o-
transition-timing-function
10.0
26.0
4.0 -webkit-
16.0
4.0 -moz-
6.1
3.1 -webkit-
12.1
10.5 -o-
 

How Does it Work?

To create a transition effect, you must specify two things:

the CSS property you want to add an effect to
the duration of the effect
Note: If the duration part is not specified, the transition will have no effect, because the default value is 0.

The following example shows a 100px * 100px red <div> element. The <div> element has also specified a transition effect for the width property, with a duration of 2 seconds:

Example

div {
    width: 100px;
    height: 100px;
    background: red;
    -webkit-transition: width 2s; /* Safari */
    transition: width 2s;
}
 
The transition effect will start when the specified CSS property (width) changes value.

Now, let us specify a new value for the width property when a user mouses over the <div> element:

Example

div:hover {
    width: 300px;
}
Notice that when the cursor mouses out of the element, it will gradually change back to its original style.

Change Several Property Values

The following example adds a transition effect for both the width and height property, with a duration of 2 seconds for the width and 4 seconds for the height:

Example

div {
    -webkit-transition: width 2s, height 4s; /* Safari */
    transition: width 2s, height 4s;
}
Specify the Speed Curve of the Transition

The transition-timing-function property specifies the speed curve of the transition effect.

The transition-timing-function property can have the following values:

ease - specifies a transition effect with a slow start, then fast, then end slowly (this is default)
linear - specifies a transition effect with the same speed from start to end
ease-in - specifies a transition effect with a slow start
ease-out - specifies a transition effect with a slow end
ease-in-out - specifies a transition effect with a slow start and end
cubic-bezier(n,n,n,n) - lets you define your own values in a cubic-bezier function
The following example shows the some of the different speed curves that can be used:

Example

#div1 {transition-timing-function: linear;}
#div2 {transition-timing-function: ease;}
#div3 {transition-timing-function: ease-in;}
#div4 {transition-timing-function: ease-out;}
#div5 {transition-timing-function: ease-in-out;}

Delay the Transition Effect

The transition-delay property specifies a delay (in seconds) for the transition effect.

The following example has a 1 second delay before starting:

Example

div {
    -webkit-transition-delay: 1s; /* Safari */
    transition-delay: 1s;
}
Transition + Transformation

The following example also adds a transformation to the transition effect:

Example

div {
    -webkit-transition: width 2s, height 2s, -webkit-transform 2s; /* Safari */
    transition: width 2s, height 2s, transform 2s;
}
More Transition Examples

The CSS3 transition properties can be specified one by one, like this:

Example

div {
    transition-property: width;
    transition-duration: 2s;
    transition-timing-function: linear;
    transition-delay: 1s;
}
or by using the shorthand property transition:

Example

div {
    transition: width 2s linear 1s;
}
 
CSS3 Transition Properties

The following table lists all the transition properties:

Property
Description
transition
A shorthand property for setting the four transition properties into a single property
transition-delay
Specifies when the transition effect will start
transition-duration
Specifies how many seconds or milliseconds a transition effect takes to complete
transition-property
Specifies the name of the CSS property the transition effect is for
transition-timing-function
Specifies the speed curve of the transition effect

CSS3 3D transform

CSS3 3D Transforms

CSS3 allows you to format your elements using 3D transformations.

Click on the elements below to see the difference between a 2D transformation and a 3D transformation:

2D rotate
3D rotate
Browser Support for 3D Transforms

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.

Property
IDE
Chrome
Firebox
Safari
Opera
transform
10.0
36.0
12.0 -webkit-
16.0
10.0 -moz-
4.0 -webkit-
23.0
15.0 -webkit-
transform-origin
(three-value syntax)
10.0
36.0
12.0 -webkit-
16.0
10.0 -moz-
4.0 -webkit-
23.0
15.0 -webkit-
transform-style
11.0
36.0
12.0 -webkit-
16.0
10.0 -moz-
4.0 -webkit-
23.0
15.0 -webkit-
perspective
10.0
36.0
12.0 -webkit-
16.0
10.0 -moz-
4.0 -webkit-
23.0
15.0 -webkit-
perspective-origin
10.0
36.0
12.0 -webkit-
16.0
10.0 -moz-
4.0 -webkit-
23.0
15.0 -webkit-
backface-visibility
10.0
36.0
12.0 -webkit-
16.0
10.0 -moz-
4.0 -webkit-
23.0
15.0 -webkit-
CSS3 3D Transforms

In this chapter you will learn about the following 3D transformation methods:

rotateX()
rotateY()
rotateZ()
The rotateX() Method

The rotateX() method rotates an element around its X-axis at a given degree:

Example

div {
    -webkit-transform: rotateX(150deg); /* Safari */
    transform: rotateX(150deg);
}
 
The rotateY() Method

The rotateY() method rotates an element around its Y-axis at a given degree:

Example

div {
    -webkit-transform: rotateY(130deg); /* Safari */
    transform: rotateY(130deg);
}
The rotateZ() Method

The rotateZ() method rotates an element around its Z-axis at a given degree:

Example

div {
    -webkit-transform: rotateZ(90deg); /* Safari */
    transform: rotateZ(90deg);
}
 
CSS3 Transform Properties

The following table lists all the 3D transform properties:

Property
Description
transform
Applies a 2D or 3D transformation to an element
transform-origin
Allows you to change the position on transformed elements
transform-style
Specifies how nested elements are rendered in 3D space
perspective
Specifies the perspective on how 3D elements are viewed
perspective-origin
Specifies the bottom position of 3D elements
backface-visibility
Defines whether or not an element should be visible when not facing the screen
3D Transform Methods

Function
Description
matrix3d
(n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n)
Defines a 3D transformation, using a 4x4 matrix of 16 values
translate3d(x,y,z)
Defines a 3D translation
translateX(x)
Defines a 3D translation, using only the value for the X-axis
translateY(y)
Defines a 3D translation, using only the value for the Y-axis
translateZ(z)
Defines a 3D translation, using only the value for the Z-axis
scale3d(x,y,z)
Defines a 3D scale transformation
scaleX(x)
Defines a 3D scale transformation by giving a value for the X-axis
scaleY(y)
Defines a 3D scale transformation by giving a value for the Y-axis
scaleZ(z)
Defines a 3D scale transformation by giving a value for the Z-axis
rotate3d(x,y,z,angle)
Defines a 3D rotation
rotateX(angle)
Defines a 3D rotation along the X-axis
rotateY(angle)
Defines a 3D rotation along the Y-axis
rotateZ(angle)
Defines a 3D rotation along the Z-axis
perspective(n)
Defines a perspective view for a 3D

CSS3 2d transforms

CSS3 2D Transforms

CSS3 Transforms

CSS3 transforms allow you to translate, rotate, scale, and skew elements.

A transformation is an effect that lets an element change shape, size and position.

CSS3 supports 2D and 3D transformations.

Browser Support for 2D Transforms

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -ms-, -webkit-, -moz-, or -o- specify the first version that worked with a prefix.

Property
IDE
Chrome
Firebox
Safari
Opera
transform
10.0
9.0 -ms-
36.0
4.0 -webkit-
16.0
3.5 -moz-
3.2 -webkit-
23.0
15.0 -webkit-
12.1
10.5 -o-
transform-origin
(two-value syntax)
10.0
9.0 -ms-
36.0
4.0 -webkit-
16.0
3.5 -moz-
3.2 -webkit-
23.0
15.0 -webkit-
12.1
10.5 -o-
 

CSS3 2D Transforms

In this chapter you will learn about the following 2D transformation methods:

translate()
rotate()
scale()
skewX()
skewY()
matrix()

Tip: You will learn about 3D transformations in the next chapter.
The translate() Method

The translate() method moves an element from its current position (according to the parameters given for the X-axis and the Y-axis).

The following example moves the <div> element 50 pixels to the right, and 100 pixels down from its current position:

Example

div {
    -ms-transform: translate(50px,100px); /* IE 9 */
    -webkit-transform: translate(50px,100px); /* Safari */
    transform: translate(50px,100px);
}
The rotate() Method

The rotate() method rotates an element clockwise or counter-clockwise according to a given degree.

The following example rotates the <div> element clockwise with 20 degrees:

Example

div {
    -ms-transform: rotate(20deg); /* IE 9 */
    -webkit-transform: rotate(20deg); /* Safari */
    transform: rotate(20deg);
}
Using negative values will rotate the element counter-clockwise.

The following example rotates the <div> element counter-clockwise with 20 degrees:

Example

div {
    -ms-transform: rotate(-20deg); /* IE 9 */
    -webkit-transform: rotate(-20deg); /* Safari */
    transform: rotate(-20deg);
}
 
The scale() Method

The scale() method increases or decreases the size of an element (according to the parameters given for the width and height).

The following example increases the <div> element to be two times of its original width, and three times of its original height: 

Example

div {
    -ms-transform: scale(2,3); /* IE 9 */
    -webkit-transform: scale(2,3); /* Safari */
    transform: scale(2,3);
}
The following example decreases the <div> element to be half of its original width and height: 

Example

div {
    -ms-transform: scale(0.5,0.5); /* IE 9 */
    -webkit-transform: scale(0.5,0.5); /* Safari */
    transform: scale(0.5,0.5);
}
The skewX() Method

The skewX() method skews an element along the X-axis by the given angle.

The following example skews the <div> element 20 degrees along the X-axis:

Example

div {
    -ms-transform: skewX(20deg); /* IE 9 */
    -webkit-transform: skewX(20deg); /* Safari */
    transform: skewX(20deg);
}
The skewY() Method

The skewY() method skews an element along the Y-axis by the given angle.

The following example skews the <div> element 20 degrees along the Y-axis:

Example

div {
    -ms-transform: skewY(20deg); /* IE 9 */
    -webkit-transform: skewY(20deg); /* Safari */
    transform: skewY(20deg);
}
The skew() Method

The skew() method skews an element along the X and Y-axis by the given angles.

The following example skews the <div> element 20 degrees along the X-axis, and 10 degrees along the Y-axis:

Example

div {
    -ms-transform: skew(20deg, 10deg); /* IE 9 */
    -webkit-transform: skew(20deg, 10deg); /* Safari */
    transform: skew(20deg, 10deg);
}
If the second parameter is not specified, it has a zero value. So, the following example skews the <div> element 20 degrees along the X-axis:

Example

div {
    -ms-transform: skew(20deg); /* IE 9 */
    -webkit-transform: skew(20deg); /* Safari */
    transform: skew(20deg);
}
The matrix() Method

The matrix() method combines all the 2D transform methods into one.

The matrix() method take six parameters, containing mathematic functions, which allows you to rotate, scale, move (translate), and skew elements:

Example

div {
    -ms-transform: matrix(1, -0.3, 0, 1, 0, 0); /* IE 9 */
    -webkit-transform: matrix(1, -0.3, 0, 1, 0, 0); /* Safari */
    transform: matrix(1, -0.3, 0, 1, 0, 0);
}
CSS3 Transform Properties

The following table lists all the 2D transform properties:

Property
Description
transform
Applies a 2D or 3D transformation to an element
transform-origin
Allows you to change the position on transformed elements
2D Transform Methods

Function
Description
matrix(n,n,n,n,n,n)
Defines a 2D transformation, using a matrix of six values
translate(x,y)
Defines a 2D translation, moving the element along the X- and the Y-axis
translateX(n)
Defines a 2D translation, moving the element along the X-axis
translateY(n)
Defines a 2D translation, moving the element along the Y-axis
scale(x,y)
Defines a 2D scale transformation, changing the elements width and height
scaleX(n)
Defines a 2D scale transformation, changing the element's width
scaleY(n)
Defines a 2D scale transformation, changing the element's height
rotate(angle)
Defines a 2D rotation, the angle is specified in the parameter
skew(x-angle,y-angle)
Defines a 2D skew transformation along the X- and the Y-axis
skewX(angle)
Defines a 2D skew transformation along the X-axis
skewY(angle)
Defines a 2D skew transformation along the Y-axis

CSS3 web fonts

CSS3 Web Fonts


CSS3 Web Fonts - The @font-face Rule

Web fonts allow Web designers to use fonts that are not installed on the user's computer.

When you have found/bought the font you wish to use, just include the font file on your web server, and it will be automatically downloaded to the user when needed.

Your "own" fonts are defined within the CSS3 @font-face rule.

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Property
@font-face
9.0
4.0
3.5
3.2
10.0
Different Font Formats

TrueType Fonts (TTF)

TrueType is a font standard developed in the late 1980s, by Apple and Microsoft. TrueType is the most common font format for both the Mac OS and Microsoft Windows operating systems.

OpenType Fonts (OTF)

OpenType is a format for scalable computer fonts. It was built on TrueType, and is a registered trademark of Microsoft. OpenType fonts are used commonly today on the major computer platforms.

The Web Open Font Format (WOFF)

WOFF is a font format for use in web pages. It was developed in 2009, and is now a W3C Recommendation. WOFF is essentially OpenType or TrueType with compression and additional metadata. The goal is to support font distribution from a server to a client over a network with bandwidth constraints.

The Web Open Font Format (WOFF 2.0)

TrueType/OpenType font that provides better compression than WOFF 1.0.

SVG Fonts/Shapes

SVG fonts allow SVG to be used as glyphs when displaying text. The SVG 1.1 specification define a font module that allows the creation of fonts within an SVG document. You can also apply CSS to SVG documents, and the @font-face rule can be applied to text in SVG documents.

Embedded OpenType Fonts (EOT)

EOT fonts are a compact form of OpenType fonts designed by Microsoft for use as embedded fonts on web pages.

Browser Support for Font Formats

The numbers in the table specifies the first browser version that fully supports the font format.

Font format
IE
Chrome
Firebox
Safari
Opera
TTF/OTF
9.0*
4.0
3.5
3.1
10.0
WOFF
9.0
5.0
3.6
5.1
11.1
WOFF2
Not supported
36.0
35.0*
Not supported
26.0
SVG
Not supported
4.0
Not supported
3.2
9.0
EOT
6.0
Not supported
Not supported
Not supported
Not supported
 

*IE: The font format only works when set to be "installable".

*Firefox: Not supported by default, but can be enabled (need to set a flag to "true" to use WOFF2).

Using The Font You Want

In the CSS3 @font-face rule you must first define a name for the font (e.g. myFirstFont), and then point to the font file.

Tip: Always use lowercase letters for the font URL. Uppercase letters can give unexpected results in IE.
To use the font for an HTML element, refer to the name of the font (myFirstFont) through the font-family property:

Example

@font-face {
    font-family: myFirstFont;
    src: url(sansation_light.woff);
}

div {
    font-family: myFirstFont;
}
Using Bold Text

You must add another @font-face rule containing descriptors for bold text:

Example

@font-face {
    font-family: myFirstFont;
    src: url(sansation_bold.woff);
    font-weight: bold;
}
The file "sansation_bold.woff" is another font file, that contains the bold characters for the Sansation font.

Browsers will use this whenever a piece of text with the font-family "myFirstFont" should render as bold.

This way you can have many @font-face rules for the same font.

CSS3 Font Descriptors

The following table lists all the font descriptors that can be defined inside the @font-face rule:

Descriptor
Values
Description
font-family
name
Required. Defines a name for the font
src
URL
Required. Defines the URL of the font file
font-stretch
normal
condensed
ultra-condensed
extra-condensed
semi-condensed
expanded
semi-expanded
extra-expanded
ultra-expanded
Optional. Defines how the font should be stretched. Default is "normal"
font-style
normal
italic
oblique
Optional. Defines how the font should be styled. Default is "normal"
font-weight
normal
bold
100
200
300
400
500
600
700
800
900
Optional. Defines the boldness of the font. Default is "normal"
unicode-range
unicode-range
Optional. Defines the range of UNICODE characters the font supports. Default is "U+0-10FFFF

CSS3 text effects

CSS3 Text Effects

CSS3 contains several new text features.

In this chapter you will learn about the following text properties:

text-shadow
word-wrap
Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Property
IE
Chrome
Firebox
Safari
Opera
text-shadow
10.0
4.0
3.5
4.0
9.5
word-wrap
5.5
23.0
3.5
6.1
12.1
 

CSS3 Text Shadow

In CSS3, the text-shadow property applies shadow to text.

You specify the horizontal shadow, the vertical shadow, the blur distance, and the color of the shadow:

Example

Add a shadow to a <h1> element:

h1 {
    text-shadow: 5px 5px 5px #FF0000;
}
 
CSS3 Word Wrapping

If a word is too long to fit within an area, it expands outside:

This paragraph contains a very long word: thisisaveryveryveryveryveryverylongword. The long word will break and wrap to the next line.

In CSS3, the word-wrap property allows you to force the text to wrap - even if it means splitting it in the middle of a word:

This paragraph contains a very long word: thisisaveryveryveryveryveryverylongword. The long word will break and wrap to the next line.

The CSS code is as follows:

Example

Allow long words to be able to break and wrap onto the next line:

p {
    word-wrap: break-word;
}
 
CSS3 Text Properties

Property
Description
CSS
hanging-punctuation
Specifies whether a punctuation character may be placed outside the line box
3
punctuation-trim
Specifies whether a punctuation character should be trimmed
3
text-align-last
Describes how the last line of a block or a line right before a forced line break is aligned when text-align is "justify"
3
text-emphasis
Applies emphasis marks, and the foreground color of the emphasis marks, to the element's text
3
text-justify
Specifies the justification method used when text-align is "justify"
3
text-outline
Specifies a text outline
3
text-overflow
Specifies what should happen when text overflows the containing element
3
text-shadow
Adds shadow to text
3
text-wrap
Specifies line breaking rules for text
3
word-break
Specifies line breaking rules for non-CJK scripts
3
word-wrap
Allows long, unbreakable words to be broken and wrap to the next line
3

CSS3 gradients

CSS3 Gradients

CSS3 gradients let you display smooth transitions between two or more specified colors.

Earlier, you had to use images for these effects. However, by using CSS3 gradients you can reduce download time and bandwidth usage. In addition, elements with gradients look better when zoomed, because the gradient is generated by the browser.

CSS3 defines two types of gradients:

Linear Gradients (goes down/up/left/right/diagonally)
Radial Gradients (defined by their center)
Browser Support

The numbers in the table specify the first browser version that fully supports the property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.
 
Property
IE
Chrome
Firebox
Safari
Opera
linear-gradient
10.0
26.0
10.0 -webkit-
16.0
3.6 -moz-
6.1
5.1 -webkit-
12.1
11.1 -o-
radial-gradient
10.0
26.0
10.0 -webkit-
16.0
3.6 -moz-
6.1
5.1 -webkit-
12.1
11.6 -o-
repeating-linear-gradient
10.0
26.0
10.0 -webkit-
16.0
3.6 -moz-
6.1
5.1 -webkit-
12.1
11.1 -o-
repeating-radial-gradient
10.0
26.0
10.0 -webkit-
16.0
3.6 -moz-
6.1
5.1 -webkit-
12.1
11.6 -o-
CSS3 Linear Gradients

To create a linear gradient you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

Example of Linear Gradient:

Syntax

background: linear-gradient(direction, color-stop1, color-stop2, ...);
Linear Gradient - Top to Bottom (this is default)

The following example shows a linear gradient that starts at the top. It starts red, transitioning to blue:

Example

A linear gradient from top to bottom:

#grad {
  background: -webkit-linear-gradient(red, blue); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(red, blue); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(red, blue); /* For Firefox 3.6 to 15 */
  background: linear-gradient(red, blue); /* Standard syntax */
}
Linear Gradient - Left to Right

The following example shows a linear gradient that starts from the left. It starts red, transitioning to blue:

Example

A linear gradient from left to right:

#grad {
  background: -webkit-linear-gradient(left, red , blue); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(right, red, blue); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(right, red, blue); /* For Firefox 3.6 to 15 */
  background: linear-gradient(to right, red , blue); /* Standard syntax */
}
Linear Gradient - Diagonal

You can make a gradient diagonally by specifying both the horizontal and vertical starting positions.

The following example shows a linear gradient that starts at top left (and goes to bottom right). It starts red, transitioning to blue:

Example

A linear gradient that starts at top left (and goes to bottom right):

#grad {
  background: -webkit-linear-gradient(left top, red , blue); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(bottom right, red, blue); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(bottom right, red, blue); /* For Firefox 3.6 to 15 */
  background: linear-gradient(to bottom right, red , blue); /* Standard syntax */
}
Using Angles

If you want more control over the direction of the gradient, you can define an angle, instead of the predefined directions (to bottom, to top, to right, to left, to bottom right, etc.).

Syntax

background: linear-gradient(angle, color-stop1, color-stop2);
The angle is specified as an angle between a horizontal line and the gradient line, going counter-clockwise. In other words, 0deg creates a bottom to top gradient, while 90deg generates a left to right gradient.

The following example shows how to use angles on linear gradients:

Example

A linear gradient with a specified angle:

#grad {
  background: -webkit-linear-gradient(180deg, red, blue); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(180deg, red, blue); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(180deg, red, blue); /* For Firefox 3.6 to 15 */
  background: linear-gradient(180deg, red, blue); /* Standard syntax */
}
Using Multiple Color Stops

The following example shows how to set multiple color stops:

Example

A linear gradient from top to bottom with multiple color stops:

#grad {
  background: -webkit-linear-gradient(red, green, blue); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(red, green, blue); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(red, green, blue); /* For Firefox 3.6 to 15 */
  background: linear-gradient(red, green, blue); /* Standard syntax */
}
The following example shows how to create a linear gradient with the color of the rainbow and some text:

Example

#grad {
  /* For Safari 5.1 to 6.0 */
  background: -webkit-linear-gradient(left,red,orange,yellow,green,blue,indigo,violet);
  /* For Opera 11.1 to 12.0 */
  background: -o-linear-gradient(left,red,orange,yellow,green,blue,indigo,violet);
  /* For Fx 3.6 to 15 */
  background: -moz-linear-gradient(left,red,orange,yellow,green,blue,indigo,violet);
  /* Standard syntax */
  background: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
}
Using Transparency

CSS3 gradients also support transparency, which can be used to create fading effects.

To add transparency, we use the rgba() function to define the color stops. The last parameter in the rgba() function can be a value from 0 to 1, and it defines the transparency of the color: 0 indicates full transparency, 1 indicates full color (no transparency).

The following example shows a linear gradient that starts from the left. It starts fully transparent, transitioning to full color red:

Example

A linear gradient from left to right, with transparency:

#grad {
  background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /*Safari 5.1-6*/
  background: -o-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /*Opera 11.1-12*/
  background: -moz-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /*Fx 3.6-15*/
  background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1)); /*Standard*/
}
Repeating a linear-gradient

The repeating-linear-gradient() function is used to repeat linear gradients:

Example

A repeating linear gradient:

#grad {
  /* Safari 5.1 to 6.0 */
  background: -webkit-repeating-linear-gradient(red, yellow 10%, green 20%);
  /* Opera 11.1 to 12.0 */
  background: -o-repeating-linear-gradient(red, yellow 10%, green 20%);
  /* Firefox 3.6 to 15 */
  background: -moz-repeating-linear-gradient(red, yellow 10%, green 20%);
  /* Standard syntax */
  background: repeating-linear-gradient(red, yellow 10%, green 20%);
}
CSS3 Radial Gradients

A radial gradient is defined by its center.

To create a radial gradient you must also define at least two color stops.

Example of Radial Gradient:

Syntax

background:
 
radial-gradient(shape size at position, start-color, ..., last-color);
By default, shape is ellipse, size is farthest-corner, and position is center.

Radial Gradient - Evenly Spaced Color Stops (this is default)

Example

A radial gradient with evenly spaced color stops:

#grad {
  background: -webkit-radial-gradient(red, green, blue); /* Safari 5.1 to 6.0 */
  background: -o-radial-gradient(red, green, blue); /* For Opera 11.6 to 12.0 */
  background: -moz-radial-gradient(red, green, blue); /* For Firefox 3.6 to 15 */
  background: radial-gradient(red, green, blue); /* Standard syntax */
}
Radial Gradient - Differently Spaced Color Stops

Example

A radial gradient with differently spaced color stops:

#grad {
  background: -webkit-radial-gradient(red 5%, green 15%, blue 60%); /* Safari 5.1-6.0 */
  background: -o-radial-gradient(red 5%, green 15%, blue 60%); /* For Opera 11.6-12.0 */
  background: -moz-radial-gradient(red 5%, green 15%, blue 60%); /* For Firefox 3.6-15 */
  background: radial-gradient(red 5%, green 15%, blue 60%); /* Standard syntax */
}
Set Shape

The shape parameter defines the shape. It can take the value circle or ellipse. The default value is ellipse.

Example

A radial gradient with the shape of a circle:

#grad {
  background: -webkit-radial-gradient(circle, red, yellow, green); /* Safari */
  background: -o-radial-gradient(circle, red, yellow, green); /* Opera 11.6 to 12.0 */
  background: -moz-radial-gradient(circle, red, yellow, green); /* Firefox 3.6 to 15 */
  background: radial-gradient(circle, red, yellow, green); /* Standard syntax */
}
Use of Different Size Keywords

The size parameter defines the size of the gradient. It can take four values:

closest-side
farthest-side
closest-corner
farthest-corner
Example

A radial gradient with different size keywords:

#grad1 {
  /* Safari 5.1 to 6.0 */
  background: -webkit-radial-gradient(60% 55%, closest-side,blue,green,yellow,black);
  /* For Opera 11.6 to 12.0 */
  background: -o-radial-gradient(60% 55%, closest-side,blue,green,yellow,black);
  /* For Firefox 3.6 to 15 */
  background: -moz-radial-gradient(60% 55%, closest-side,blue,green,yellow,black);
  /* Standard syntax */
  background: radial-gradient(closest-side at 60% 55%,blue,green,yellow,black);
}

#grad2 {
  /* Safari 5.1 to 6.0 */
  background: -webkit-radial-gradient(60% 55%, farthest-side,blue,green,yellow,black);
  /* Opera 11.6 to 12.0 */
  background: -o-radial-gradient(60% 55%, farthest-side,blue,green,yellow,black);
  /* For Firefox 3.6 to 15 */
  background: -moz-radial-gradient(60% 55%, farthest-side,blue,green,yellow,black);
  /* Standard syntax */
  background: radial-gradient(farthest-side at 60% 55%,blue,green,yellow,black);
}
Repeating a radial-gradient

The repeating-radial-gradient() function is used to repeat radial gradients:

Example

A repeating radial gradient:

#grad {
  /* For Safari 5.1 to 6.0 */
  background: -webkit-repeating-radial-gradient(red, yellow 10%, green 15%);
  /* For Opera 11.6 to 12.0 */
  background: -o-repeating-radial-gradient(red, yellow 10%, green 15%);
  /* For Firefox 3.6 to 15 */
  background: -moz-repeating-radial-gradient(red, yellow 10%, green 15%);
  /* Standard syntax */
  background: repeating-radial-gradient(red, yellow 10%, green 15%);

CSS3 background

CSS3 Backgrounds

CSS3 contains several new background properties,
which allow greater control of the background element.

In this chapter you will learn about the following background properties:

background-size
background-origin
You will also learn how to use multiple background images.

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.

Property
IDE
Chrome
firebox
Safari
Opera
background-size
9.0
4.0
1.0 -webkit-
4.0
3.6 -moz-
4.1
3.0 -webkit-
10.5
10.0 -o-
background-origin
9.0
1.0
4.0
3.0
10.5
 

CSS3 The background-size Property

The background-size property specifies the size of the background image.

Before CSS3, the background image size was determined by the actual size of the image. In CSS3 it is possible to specify the size of the background image, which allows us to re-use background images in different contexts.

You can specify the size in pixels or in percentages. If you specify the size as a percentage, the size is relative to the width and height of the parent element.

Example 1

Resize a background image:

div {
    background: url(img_flwr.gif);
    background-size: 80px 60px;
    background-repeat: no-repeat;
}
 
Example 2
Stretch the background image to completely fill the content area:

div {
    background: url(img_flwr.gif);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}
CSS3 The background-origin Property

The background-origin property specifies the positioning area of the background images.

The background image can be placed within the content-box, padding-box, or border-box area.

Example

Position the background image within the content-box:

div {
    background: url(img_flwr.gif);
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-origin: content-box;
}
 
CSS3 Multiple Background Images

CSS3 allows you to use several background images for an element.

Example

Set two background images for the <body> element:

body {
    background: url(img_tree.gif), url(img_flwr.gif);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}
CSS3 Background Properties

Property
Description
CSS
background-clip
Specifies the painting area of the background images
3
background-origin
Specifies the positioning area of the background images
3
background-size
Specifies the size of the background images

CSS3 borders

CSS3 Borders

With CSS3, you can create rounded borders, add shadow to boxes, and use an image as a border - without using a design program, like Photoshop.

In this chapter you will learn about the following border properties:

border-radius
box-shadow
border-image
Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.

Property
IE
Chrome
Firebox
Safari
Opera
border-radius
9.0
5.0
4.0 -webkit-
4.0
3.0 -moz-
5.0
3.1 -webkit-
10.5
box-shadow
9.0
10.0
4.0 -webkit-
4.0
3.5 -moz-
5.1
3.1 -webkit-
10.5
border-image
11.0
16.0
4.0 -webkit-
15.0
3.5 -moz-
6.0
3.1 -webkit-
15.0
11.0 -o-
 

CSS3 The border-radius Property - Rounded Corners

Adding rounded corners in CSS2 was tricky. We had to use different images for each corner.

In CSS3, creating rounded corners is easy.

In CSS3, the border-radius property is used to create rounded corners:

This box has rounded corners!
 
Example

Add rounded corners to a <div> element:

div {
    border: 2px solid;
    border-radius: 25px;
}
CSS3 The box-shadow Property

In CSS3, the box-shadow property is used to add shadow to boxes:

 
Example

Add a box-shadow to a <div> element:

div {
    box-shadow: 10px 10px 5px #888888;
}
CSS3 The border-image Property

With the CSS3 border-image property you can use an image to create a border:

The border-image property allows you to specify an image as a border!
The original image used to create the border above:

Example

Use an image to create a border around a <div> element:

div {
    -webkit-border-image: url(border.png) 30 30 round; /* Safari 3.1-5 */
    -o-border-image: url(border.png) 30 30 round; /* Opera 11-12.1 */
    border-image: url(border.png) 30 30 round;
}
CSS3 Border Properties

Property
Description
CSS
border-image
A shorthand property for setting all the border-image-* properties
3
border-radius
A shorthand property for setting all the four border-*-radius properties
3
box-shadow
Attaches one or more drop-shadows to the box

CSS3

CSS3 Introduction

CSS3 is the latest standard for CSS.

CSS3 is completely backwards-compatible with earlier versions of CSS.

This section teaches you about the new features in CSS3!

CSS3 Modules

CSS3 has been split into "modules". It contains the "old CSS specification" (which has been split into smaller pieces). In addition, new modules are added.

Some of the most important CSS3 modules are:

Selectors
Box Model
Backgrounds and Borders
Image Values and Replaced Content
Text Effects
2D/3D Transformations
Animations
Multiple Column Layout
User Interface
CSS3 Recommendation

Most of the CSS3 Modules are W3C Recommendations, and CSS3 properties are implemented in all modern browsers

PHP for pearl developers

PHP for PERL Developers

This chapter will list out major similarities and differences in between PHP and PERL. This will help PERL developers to understand PHP very quickly and avoid common mistakes.

Similarities:

Compiled scripting languages: Both Perl and PHP are scripting languages.This means that they are not used to produce native standalone executables in advance of execution.
Syntax: PHP's basic syntax is very close to Perl's, and both share a lot of syntactic features with C. Code is insensitive to whitespace, statements are terminated by semicolons, and curly braces organize multiple statements into a single block. Function calls start with the name of the function, followed by the actual arguments enclosed in parentheses and separated by commas.
Dollar-sign variables: All variables in PHP look like scalar variables in Perl: a name with a dollar sign ($) in front of it.
No declaration of variables: As in Perl, you don.t need to declare the type of a PHP variable before using it.
Loose typing of variables: As in Perl, variables in PHP have no intrinsic type other than the value they currently hold. You can store iether number or string in same type of variable.
Strings and variable interpolation: Both PHP and Perl do more interpretation of double-quoted strings ("string") than of singlequoted strings ('string').
Differences:

PHP is HTML-embedded: Although it is possible to use PHP for arbitrary tasks by running it from the command line, it is more typically connected to a Web server and used for producing Web pages. If you are used to writing CGI scripts in Perl, the main difference in PHP is that you no longer need to explicitly print large blocks of static HTML using print or heredoc statements and instead can simply write the HTML itself outside of the PHP code block.
No @ or % variables: PHP has one only kind of variable, which starts with a dollar sign ($). Any of the datatypes in the language can be stored in such variables, whether scalar or compound.
Arrays versus hashes: PHP has a single datatype called an array that plays the role of both hashes and arrays/lists in Perl.
Specifying arguments to functions: Function calls in PHP look pretty much like subroutine calls in Perl. Function definitions in PHP, on the other hand, typically require some kind of list of formal arguments as in C or Java which is not the csse in PERL.
Variable scoping in functions: In Perl, the default scope for variables is global. This means that top-level variables are visible inside subroutines. Often, this leads to promiscuous use of globals across functions. In PHP, the scope of variables within function definitions is local by default.
No module system as such: In PHP there is no real distinction between normal code files and code files used as imported libraries.
Break and continue rather than next and last: PHP is more like C langauge and uses break and continue instead of next and last statement.
No elsif: A minor spelling difference: Perl's elsif is PHP's elseif.
More kinds of comments: In addition to Perl-style (#) single-line comments, PHP offers C-style multiline comments (/* comment */ ) and Java-style single-line comments (// comment).
Regular expressions: PHP does not have a built-in syntax specific to regular expressions, but has most of the same functionality in its "Perl-compatible" regular expression functions

PHP for c developers

PHP for C Developers

The simplest way to think of PHP is as interpreted C that you can embed in HTML documents. The language itself is a lot like C, except with untyped variables, a whole lot of Web-specific libraries built in, and everything hooked up directly to your favorite Web server.

The syntax of statements and function definitions should be familiar, except that variables are always preceded by $, and functions do not require separate prototypes.

Here we will put some similarities and diferences in PHP and C

Similarities:

Syntax: Broadly speaking, PHP syntax is the same as in C: Code is blank insensitive, statements are terminated with semicolons, function calls have the same structure (my_function(expression1, expression2)), and curly braces ({ and }) make statements into blocks. PHP supports C and C++-style comments (/* */ as well as //), and also Perl and shell-script style (#).
Operators: The assignment operators (=, +=, *=, and so on), the Boolean operators (&&, ||, !), the comparison operators (<,>, <=, >=, ==, !=), and the basic arithmetic operators (+, -, *, /, %) all behave in PHP as they do in C.
Control structures: The basic control structures (if, switch, while, for) behave as they do in C, including supporting break and continue. One notable difference is that switch in PHP can accept strings as case identifiers.
Function names: As you peruse the documentation, you.ll see many function names that seem identical to C functions.
Differences:

Dollar signs: All variables are denoted with a leading $. Variables do not need to be declared in advance of assignment, and they have no intrinsic type.
Types: PHP has only two numerical types: integer (corresponding to a long in C) and double (corresponding to a double in C). Strings are of arbitrary length. There is no separate character type.
Type conversion: Types are not checked at compile time, and type errors do not typically occur at runtime either. Instead, variables and values are automatically converted across types as needed.
Arrays: Arrays have a syntax superficially similar to C's array syntax, but they are implemented completely differently. They are actually associative arrays or hashes, and the index can be either a number or a string. They do not need to be declared or allocated in advance.
No structure type: There is no struct in PHP, partly because the array and object types together make it unnecessary. The elements of a PHP array need not be of a consistent type.
No pointers: There are no pointers available in PHP, although the typeless variables play a similar role. PHP does support variable references. You can also emulate function pointers to some extent, in that function names can be stored in variables and called by using the variable rather than a literal name.
No prototypes: Functions do not need to be declared before their implementation is defined, as long as the function definition can be found somewhere in the current code file or included files.
Memory management: The PHP engine is effectively a garbage-collected environment (reference-counted), and in small scripts there is no need to do any deallocation. You should freely allocate new structures - such as new strings and object instances. IN PHP5, it is possible to define destructors for objects, but there is no free or delete. Destructors are called when the last reference to an object goes away, before the memory is reclaimed.
Compilation and linking: There is no separate compilation step for PHP scripts.
Permissiveness: As a general matter, PHP is more forgiving than C (especially in its type system) and so will let you get away with new kinds of mistakes. Unexpected results are more common than errors

PHP object oriented

Object Oriented Programming in PHP

We can imagine our universe made of different objects like sun, earth, moon etc. Similarly we can imagine our car made of different objects like wheel, steering, gear etc. Same way there is object oriented programming concepts which assume everything as an object and implement a software using different objects.

Object Oriented Concepts:

Before we go in detail, lets define important terms related to Object Oriented Programming.

Class: This is a programmer-defined datatype, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.
Object: An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.
Member Variable: These are the variables defined inside a class. This data will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created.
Member function: These are the function defined inside a class and are used to access object data.
Inheritance: When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.
Parent class: A class that is inherited from by another class. This is also called a base class or super class.
Child Class: A class that inherits from another class. This is also called a subclass or derived class.
Polymorphism: This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it make take different number of arguments and can do different task.
Overloading: a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.
Data Abstraction: Any representation of data in which the implementation details are hidden (abstracted).
Encapsulation: refers to a concept where we encapsulate all the data and member functions together to form an object.
Constructor: refers to a special type of function which will be called automatically whenever there is an object formation from a class.
Destructors: refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.
Defining PHP Classes:

The general form for defining a new class in PHP is as follows:

<?php
class phpClass{
   var $var1;
   var $var2 = "constant string";
   function myfunc ($arg1, $arg2) {
      [..]
   }
   [..]
}
?>
Here is the description of each line:

The special form class, followed by the name of the class that you want to define.
A set of braces enclosing any number of variable declarations and function definitions.
Variable declarations start with the special form var, which is followed by a conventional $ variable name; they may also have an initial assignment to a constant value.
Function definitions look much like standalone PHP functions but are local to the class and will be used to set and access object data.
Example:

Here is an example which defines a class of Books type:

<?php
class  Books{
    /* Member variables */
    var $price;
    var $title;
    /* Member functions */
    function setPrice($par){
       $this->price = $par;
    }
    function getPrice(){
       echo $this->price ."<br/>";
    }
    function setTitle($par){
       $this->title = $par;
    }
    function getTitle(){
       echo $this->title ." <br/>";
    }
}
?>
The variable $this is a special variable and it refers to the same object ie. itself.

Creating Objects in PHP

Once you defined your class, then you can create as many objects as you like of that class type. Following is an example of how to create object using new operator.

   $physics = new Books;
   $maths = new Books;
   $chemistry = new Books;
Here we have created three objects and these objects are independent of each other and they will have their existance separately. Next we will see how to access member function and process member variables.

Calling Member Functions

After creating your objects, you will be able to call member functions related to that object. One member function will be able to process member variable of related object only.

Following example shows how to set title and prices for the three books by calling member functions.

   $physics->setTitle( "Physics for High School" );
   $chemistry->setTitle( "Advanced Chemistry" );
   $maths->setTitle( "Algebra" );
 
   $physics->setPrice( 10 );
   $chemistry->setPrice( 15 );
   $maths->setPrice( 7 );
Now you call another member functions to get the values set by in above example:

   $physics->getTitle();
   $chemistry->getTitle();
   $maths->getTitle();
   $physics->getPrice();
   $chemistry->getPrice();
   $maths->getPrice();
This will produce follwoing result:

  Physics for High School
  Advanced Chemistry
  Algebra
  10
  15
  7
Constructor Functions:

Constructor Functions are special type of functions which are called automatically whenever an object is created. So we take full advantage of this behaviour, by initializing many things through constructor functions.

PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.

Following example will create one constructor for Books class and it will initialize price and title for the book at the time of object creation.

function __construct( $par1, $par2 ){
   $this->price = $par1;
   $this->title = $par2;
}
Now we don't need to call set function separately to set price and title. We can initialize these two member variables at the time of object creation only. Check following example below:

   $physics = new Books( "Physics for High School", 10 );
   $maths = new Books ( "Advanced Chemistry", 15 );
   $chemistry = new Books ("Algebra", 7 );
 
   /* Get those set values */
   $physics->getTitle();
   $chemistry->getTitle();
   $maths->getTitle();
 
   $physics->getPrice();
   $chemistry->getPrice();
   $maths->getPrice();
This will produce following result:

  Physics for High School
  Advanced Chemistry
  Algebra
  10
  15
  7
Destructor:

Like a constructor function you can define a destructor function using function __destruct(). You can release all the resourceses with-in a destructor.

Inheritance:

PHP class definitions can optionally inherit from a parent class definition by using the extends clause. The syntax is as follows:

  class Child extends Parent {
     <definition body>
  }
The effect of inheritance is that the child class (or subclass or derived class) has the following characteristics:

Automatically has all the member variable declarations of the parent class.
Automatically has all the same member functions as the parent, which (by default) will work the same way as those functions do in the parent.
Following example inherit Books class and adds more functionality based on the requirement.

class Novel extends Books{
   var publisher;
   function setPublisher($par){
     $this->publisher = $par;
   }
   function getPublisher(){
     echo $this->publisher. "<br />";
   }
}
Now apart from inherited functions, class Novel keeps two additional member functions.

Function Overriding:

Function definitions in child classes override definitions with the same name in parent classes. In a child class, we can modify the definition of a function inherited from parent class.

In the follwoing example getPrice and getTitle functions are overriden to retrun some values.

    function getPrice(){
       echo $this->price . "<br/>";
       return $this->price;
    }
    function getTitle(){
       echo $this->title . "<br/>";
       return $this->title;
    }
Public Members:

Unless you specify otherwise, properties and methods of a class are public. That is to say, they may be accessed in three possible situations:

From outside the class in which it is declared
From within the class in which it is declared
From within another class that implements the class in which it is declared
Till now we have seen all members as public members. If you wish to limit the accessibility of the members of a class then you define class members as private or protected.

Private members:

By designating a member private, you limit its accessibility to the class in which it is declared. The private member cannot be referred to from classes that inherit the class in which it is declared and cannot be accessed from outside the class.

A class member can be made private by using private keyword infront of the member.

class MyClass {
   private $car = "skoda";
   $driver = "SRK";
 
   function __construct($par) {
      // Statements here run every time
      // an instance of the class
      // is created.
   }
   function myPublicFunction() {
      return("I'm visible!");
   }
   private function myPrivateFunction() {
      return("I'm  not visible outside!");
   }
}
When MyClass class is inherited by another class using extends, myPublicFunction() will be visible, as will $driver. The extending class will not have any awareness of or access to myPrivateFunction and $car, because they are declared private.

Protected members:

A protected property or method is accessible in the class in which it is declared, as well as in classes that extend that class. Protected members are not available outside of those two kinds of classes. A class member can be made protected by using protected keyword infront of the member.

Here is different version of MyClass:

class MyClass {
   protected $car = "skoda";
   $driver = "SRK";
 
   function __construct($par) {
      // Statements here run every time
      // an instance of the class
      // is created.
   }
   function myPublicFunction() {
      return("I'm visible!");
   }
   protected function myPrivateFunction() {
      return("I'm  visible in child class!");
   }
}
Interfaces:

Interfaces are defined to provide a common function names to the implementors. Different implementors can implement those interfaces according to theri requirements. You can say, interfaces are skeltons which are implemented by developers.

As of PHP5, it is possible to define an interface, like this:

interface Mail {
   public function sendMail();
}
Then, if another class implemented that interface, like this:

class Report implements Mail {
   // sendMail() Definition goes here
}
Constants:

A constant is somewhat like a variable, in that it holds a value, but is really more like a function because a constant is immutable. Once you declare a constant, it does not change.

Declaring one constant is easy, as is done in this version of MyClass:

class MyClass {
   const requiredMargin = 1.7;
   function __construct($incomingValue) {
      // Statements here run every time
      // an instance of the class
      // is created.
   }
}
In this class, requiredMargin is a constant. It is declared with the keyword const, and under no circumstances can it be changed to anything other than 1.7. Note that the constant's name does not have a leading $, as variable names do.

Abstract Classes:

An abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract, like this:

When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same visibillity.

abstract class MyAbstractClass {
   abstract function myAbstractFunction() {
   }
}
Note that function definitions inside an abstract class must also be preceded by the keyword abstract. It is not legal to have abstract function definitions inside a non-abstract class.

Static Keyword:

Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).

Try out following example:

<?php
class Foo
{
    public static $my_static = 'foo';
 
    public function staticValue() {
        return self::$my_static;
    }
}
print Foo::$my_static . "\n";
$foo = new Foo();
print $foo->staticValue() . "\n";
Final Keyword:

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Following example results in Fatal error: Cannot override final method BaseClass::moreTesting()

<?php
class BaseClass {
   public function test() {
       echo "BaseClass::test() called<br>";
   }
 
   final public function moreTesting() {
       echo "BaseClass::moreTesting() called<br>";
   }
}
 
class ChildClass extends BaseClass {
   public function moreTesting() {
       echo "ChildClass::moreTesting() called<br>";
   }
}
?>
Calling parent constructors:

Instead of writing an entirely new constructor for the subclass, let's write it by calling the parent's constructor explicitly and then doing whatever is necessary in addition for instantiation of the subclass. Here's a simple example:

class Name
{
   var $_firstName;
   var $_lastName;
   function Name($first_name, $last_name)
   {
     $this->_firstName = $first_name;
     $this->_lastName = $last_name;
   }
   function toString() {
     return($this->_lastName .", " .$this->_firstName);
   }
}
class NameSub1 extends Name
{
   var $_middleInitial;
   function NameSub1($first_name, $middle_initial, $last_name) {
       Name::Name($first_name, $last_name);
       $this->_middleInitial = $middle_initial;
   }
   function toString() {
       return(Name::toString() . " " . $this->_middleInitial);
   }
}
In this example, we have a parent class (Name), which has a two-argument constructor, and a subclass (NameSub1), which has a three-argument constructor. The constructor of NameSub1 functions by calling its parent constructor explicitly using the :: syntax (passing two of its arguments along) and then setting an additional field. Similarly, NameSub1 defines its nonconstructor toString() function in terms of the parent function that it overrides.

NOTE: A constructor can be defined with the same name as the name of a class. It is defined in above example