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 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:
The CSS3 specification is still under development by W3C.
However, many of the new CSS3 properties have been implemented in modern browsers.
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:
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 |
|
|
|
|
|
border-radius |
9.0 |
5.0 |
4.0 |
5.0 |
10.5 |
box-shadow |
9.0 |
10.0 |
4.0 |
5.1 |
10.5 |
border-image |
11.0 |
16.0 |
15.0 |
6.0 |
15.0 |
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!
Add rounded corners to a div element:
div {
border: 2px solid;
border-radius: 25px;
}
In CSS3, the box-shadow property is used to add shadow to boxes:
Add a box-shadow to a div element:
div {
box-shadow: 10px 10px 5px #888888;
}
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:
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;
}
Property |
Description |
CSS |
A shorthand property for setting all the border-image-* properties |
3 |
|
A shorthand property for setting all the four border-*-radius properties |
3 |
|
Attaches one or more drop-shadows to the box |
3 |
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:
You will also learn how to use multiple background images.
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 |
|
|
|
|
|
background-size |
9.0 |
4.0 |
4.0 |
4.1 |
10.5 |
background-origin |
9.0 |
1.0 |
4.0 |
3.0 |
10.5 |
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.
Resize a background image:
div {
background: url(img_flwr.gif);
background-size: 80px 60px;
background-repeat: no-repeat;
}
Stretch the background image to completely fill the content area:
div {
background: url(img_flwr.gif);
background-size: 100% 100%;
background-repeat: no-repeat;
}
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.
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. |
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;
}
Property |
Description |
CSS |
Specifies the painting area of the background images |
3 |
|
Specifies the positioning area of the background images |
3 |
|
Specifies the size of the background images |
3 |
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:
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 |
|
|
|
|
|
linear-gradient |
10.0 |
26.0 |
16.0 |
6.1 |
12.1 |
radial-gradient |
10.0 |
26.0 |
16.0 |
6.1 |
12.1 |
repeating-linear-gradient |
10.0 |
26.0 |
16.0 |
6.1 |
12.1 |
repeating-radial-gradient |
10.0 |
26.0 |
16.0 |
6.1 |
12.1 |
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:
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:
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:
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:
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 */
}
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.).
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:
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 */
}
The following example shows how to set multiple color stops:
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:
#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);
}
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:
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*/
}
The repeating-linear-gradient() function is used to repeat linear gradients:
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%);
}
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:
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)
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
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 */
}
The shape parameter defines the shape. It can take the value circle or ellipse. The default value is ellipse.
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 */
}
The size parameter defines the size of the gradient. It can take four values:
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);
}
The repeating-radial-gradient() function is used to repeat radial gradients:
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 contains several new text features.
In this chapter you will learn about the following text properties:
The numbers in the table specify the first browser version that fully supports the property.
Property |
|
|
|
|
|
text-shadow |
10.0 |
4.0 |
3.5 |
4.0 |
9.5 |
word-wrap |
5.5 |
23.0 |
3.5 |
6.1 |
12.1 |
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:
Add a shadow to a header:
h1 {
text-shadow: 5px 5px 5px #FF0000;
}
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:
Allow long words to be able to break and wrap onto the next line:
p {
word-wrap: break-word;
}
Property |
Description |
CSS |
Specifies whether a punctuation character may be placed outside the line box |
3 |
|
Specifies whether a punctuation character should be trimmed |
3 |
|
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 |
|
Applies emphasis marks, and the foreground color of the emphasis marks, to the element's text |
3 |
|
Specifies the justification method used when text-align is "justify" |
3 |
|
Specifies a text outline |
3 |
|
Specifies what should happen when text overflows the containing element |
3 |
|
Adds shadow to text |
3 |
|
Specifies line breaking rules for text |
3 |
|
Specifies line breaking rules for non-CJK scripts |
3 |
|
Allows long, unbreakable words to be broken and wrap to the next line |
3 |
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.
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 |
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.
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.
The numbers in the table specifies the first browser version that fully supports the font format.
Font format |
|
|
|
|
|
TTF/OTF fonts |
9.0* |
4.0 |
3.5 |
3.1 |
10.0 |
WOFF fonts |
9.0 |
5.0 |
3.6 |
5.1 |
11.1 |
SVG fonts |
Not supported |
4.0 |
Not supported |
3.2 |
9.0 |
EOT fonts |
6.0 |
Not supported |
Not supported |
Not supported |
Not supported |
*The font format only works when set to be "installable".
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:
@font-face {
font-family: myFirstFont;
src: url(sansation_light.woff);
}
div {
font-family: myFirstFont;
}
You must add another @font-face rule containing descriptors for bold text:
@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.
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 |
Optional. Defines how the font should be stretched. Default is "normal" |
font-style |
normal |
Optional. Defines how the font should be styled. Default is "normal" |
font-weight |
normal |
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" |
With CSS3 transform, we can move, scale, turn, spin, and stretch elements.
A transformation is an effect that lets an element change shape, size and position.
You can transform your elements using 2D or 3D transformation.
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 |
|
|
|
|
|
transform |
10.0 |
36.0 |
16.0 |
3.2 -webkit- |
23.0 |
transform-origin |
10.0 |
36.0 |
16.0 |
3.2 -webkit- |
23.0 |
In this chapter you will learn about the 2d transform methods:
You will learn about 3D transforms in the next chapter.
div {
-ms-transform: rotate(30deg); /* IE 9 */
-webkit-transform: rotate(30deg); /* Chrome, Safari, Opera */
transform: rotate(30deg);
}
With the translate() method, the element moves from its current position, depending on the parameters given for the left (X-axis) and the top (Y-axis) position:
div {
-ms-transform: translate(50px,100px); /* IE 9 */
-webkit-transform: translate(50px,100px); /* Chrome, Safari, Opera */
transform: translate(50px,100px);
}
The value translate(50px,100px) moves the element 50 pixels from the left, and 100 pixels from the top.
With the rotate() method, the element rotates clockwise at a given degree. Negative values are allowed and rotates the element counter-clockwise.
div {
-ms-transform: rotate(30deg); /* IE 9 */
-webkit-transform: rotate(30deg); /* Chrome, Safari, Opera */
transform: rotate(30deg);
}
The value rotate(30deg) rotates the element clockwise 30 degrees.
With the scale() method, the element increases or decreases the size, depending on the parameters given for the width (X-axis) and the height (Y-axis):
div {
-ms-transform: scale(2,4); /* IE 9 */
-webkit-transform: scale(2,4); /* Chrome, Safari, Opera */
transform: scale(2,4);
}
The&nb