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

The CSS3 specification is still under development by W3C.

However, many of the new CSS3 properties have been implemented in modern browsers.

 

 

 

CSS3 Borders

« Previous

Next Chapter »

 

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

 

 

 

 

 

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;


Try it yourself » 

 

 

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;


Try it yourself » 

 

 

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;


Try it yourself » 

 

 

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

3

 

 

 

CSS3 Backgrounds

« Previous

Next Chapter »

 

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

 

 

 

 

 

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;


Try it yourself » 

 

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;


Try it yourself » 

 

 

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;


Try it yourself » 

 

 

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;


Try it yourself » 

 

 

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

3

 

 

 

CSS3 Gradients

« Previous

Next Chapter »

 

 

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

 

 

 

 

 

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 */


Try it yourself » 

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 */


Try it yourself » 

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 */


Try it yourself » 

 

 

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 */


Try it yourself » 

 

 

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 */


Try it yourself » 

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); 


Try it yourself » 

 

 

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*/


Try it yourself » 

 

 

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%);


Try it yourself » 

 

 

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 */


Try it yourself » 

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 */


Try it yourself » 

 

 

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 */


Try it yourself » 

 

 

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);


Try it yourself » 

 

 

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 Text Effects

« Previous

Next Chapter »

 

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

 

 

 

 

 

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

h1 {
    text-shadow: 5px 5px 5px #FF0000;


Try it yourself » 

 

 

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;


Try it yourself » 

 

 

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 Web Fonts

« Previous

Next Chapter »

 

 

 

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.

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

 

 

 

 

 

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".

 

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;


Try it yourself » 

 

 

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;
}


Try it yourself » 

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 2D Transforms

« Previous

Next Chapter »

 

 

 

CSS3 Transforms

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.

 

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

 

 

 

 

 

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 2d transform methods:

  • translate()
  • rotate()
  • scale()
  • skew()
  • matrix()

You will learn about 3D transforms in the next chapter.

Example

div {
    -ms-transform: rotate(30deg); /* IE 9 */
    -webkit-transform: rotate(30deg); /* Chrome, Safari, Opera */
    transform: rotate(30deg);


Try it yourself » 

 

 

The translate() Method

 

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:

Example

div {
    -ms-transform: translate(50px,100px); /* IE 9 */
    -webkit-transform: translate(50px,100px); /* Chrome, Safari, Opera */
    transform: translate(50px,100px);


Try it yourself » 

The value translate(50px,100px) moves the element 50 pixels from the left, and 100 pixels from the top.

 

The rotate() Method

 

With the rotate() method, the element rotates clockwise at a given degree. Negative values are allowed and rotates the element counter-clockwise.

Example

div {
    -ms-transform: rotate(30deg); /* IE 9 */
    -webkit-transform: rotate(30deg); /* Chrome, Safari, Opera */
    transform: rotate(30deg);


Try it yourself » 

The value rotate(30deg) rotates the element clockwise 30 degrees.

 

The scale() Method

 

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

Example

div {
    -ms-transform: scale(2,4); /* IE 9 */
    -webkit-transform: scale(2,4); /* Chrome, Safari, Opera */
    transform: scale(2,4);


Try it yourself » 

The&nb