Friday, November 27, 2015

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
Most of the new CSS3 properties are implemented in modern browsers.

CSS3 Rounded Corners

CSS3 Rounded Corners

With the CSS3 border-radius property, you can give any element "rounded corners".

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
border-radius5.0
4.0 -webkit-
12.09.04.0
3.0 -moz-
5.0
3.1 -webkit-
10.5

CSS3 border-radius Property

With CSS3, you can give any element "rounded corners", by using the border-radius property.
Here are three examples:
1. Rounded corners for an element with a specified background color:
Rounded corners!
2. Rounded corners for an element with a border:
Rounded corners!
3. Rounded corners for an element with a background image:
Rounded corners!
Here is the code:

Example

#rcorners1 {
    border-radius: 25px;
    background: #73AD21;
    padding: 20px; 
    width: 200px;
    height: 150px; 
}

#rcorners2 {
    border-radius: 25px;
    border: 2px solid #73AD21;
    padding: 20px; 
    width: 200px;
    height: 150px; 
}

#rcorners3 {
    border-radius: 25px;
    background: url(paper.gif);
    background-position: left top;
    background-repeat: repeat;
    padding: 20px; 
    width: 200px;
    height: 150px; 
}
Try it yourself »
NoteTip: The border-radius property is actually a shorthand property for the border-top-left-radius,border-top-right-radius, border-bottom-right-radius and border-bottom-left-radiusproperties.

CSS3 border-radius - Specify Each Corner

If you specify only one value for the border-radius property, this radius will be applied to all 4 corners.
However, you can specify each corner separately if you wish. Here are the rules:
  • Four values: first value applies to top-left, second value applies to top-right, third value applies to bottom-right, and fourth value applies to bottom-left corner
  • Three values: first value applies to top-left, second value applies to top-right and bottom-left, and third value applies to bottom-right
  • Two values: first value applies to top-left and bottom-right corner, and the second value applies to top-right and bottom-left corner
  • One value: all four corners are rounded equally
Here are three examples:
1. Four values - border-radius: 15px 50px 30px 5px:
2. Three values - border-radius: 15px 50px 30px:
3. Two values - border-radius: 15px 50px:
Here is the code:

Example

#rcorners4 {
    border-radius: 15px 50px 30px 5px;
    background: #73AD21;
    padding: 20px; 
    width: 200px;
    height: 150px; 
}

#rcorners5 {
    border-radius: 15px 50px 30px;
    background: #73AD21;
    padding: 20px; 
    width: 200px;
    height: 150px; 
}

#rcorners6 {
    border-radius: 15px 50px;
    background: #73AD21;
    padding: 20px; 
    width: 200px;
    height: 150px; 
}
Try it yourself »
You could also create elliptical corners:

Example

#rcorners7 {
    border-radius: 50px/15px;
    background: #73AD21;
    padding: 20px; 
    width: 200px;
    height: 150px; 
}

#rcorners8 {
    border-radius: 15px/50px;
    background: #73AD21;
    padding: 20px; 
    width: 200px;
    height: 150px; 
}

#rcorners9 {
    border-radius: 50%;
    background: #73AD21;
    padding: 20px; 
    width: 200px;
    height: 150px;
}
Try it yourself »

Test Yourself with Exercises!


CSS3 Rounded Corners Properties

PropertyDescription
border-radiusA shorthand property for setting all the four border-*-*-radius properties
border-top-left-radiusDefines the shape of the border of the top-left corner
border-top-right-radiusDefines the shape of the border of the top-right corner
border-bottom-right-radiusDefines the shape of the border of the bottom-right corner
border-bottom-left-radiusDefines the shape of the border of the bottom-left corner

CSS3 Border Images

CSS3 Border Images

With the CSS3 border-image property, you can set an image to be used as the border around an element.

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-image16.0
4.0 -webkit-
12.011.015.0
3.5 -moz-
6.0
3.1 -webkit-
15.0
11.0 -o-

CSS3 border-image Property

The CSS3 border-image property allows you to specify an image to be used instead of the normal border around an element.
The property has three parts:
  1. The image to use as the border
  2. Where to slice the image
  3. Define whether the middle sections should be repeated or stretched
We will use the following image (called "border.png"):
Border
The border-image property takes the image and slices it into nine sections, like a tic-tac-toe board. It then places the corners at the corners, and the middle sections are repeated or stretched as you specify.
Note: For border-image to work, the element also needs the border property set!
Here, the middle sections of the image are repeated to create the border:
An image as a border!
Here is the code:

Example

#borderimg {
    border: 10px solid transparent;
    padding: 15px;
    -webkit-border-image: url(border.png) 30 round; /* Safari 3.1-5 */
    -o-border-image: url(border.png) 30 round; /* Opera 11-12.1 */
    border-image: url(border.png) 30 round;
}
Try it yourself »
Here, the middle sections of the image are stretched to create the border:
An image as a border!
Here is the code:

Example

#borderimg {
    border: 10px solid transparent;
    padding: 15px;
    -webkit-border-image: url(border.png) 30 stretch; /* Safari 3.1-5 */
    -o-border-image: url(border.png) 30 stretch; /* Opera 11-12.1 */
    border-image: url(border.png) 30 stretch;
}
Try it yourself »
NoteTip: The border-image property is actually a shorthand property for the border-image-source,border-image-slice, border-image-width, border-image-outset and border-image-repeatproperties.

CSS3 border-image - Different Slice Values

Different slice values completely changes the look of the border:
Example 1:
border-image: url(border.png) 50 round;
Example 2:
border-image: url(border.png) 20% round;
Example 3:
border-image: url(border.png) 30% round;
Here is the code:

Example

#borderimg1 {
    border: 10px solid transparent;
    padding: 15px;
    -webkit-border-image: url(border.png) 50 round; /* Safari 3.1-5 */
    -o-border-image: url(border.png) 50 round; /* Opera 11-12.1 */
    border-image: url(border.png) 50 round;
}

#borderimg2 {
    border: 10px solid transparent;
    padding: 15px;
    -webkit-border-image: url(border.png) 20% round; /* Safari 3.1-5 */
    -o-border-image: url(border.png) 20% round; /* Opera 11-12.1 */
    border-image: url(border.png) 20% round;
}

#borderimg3 {
    border: 10px solid transparent;
    padding: 15px;
    -webkit-border-image: url(border.png) 30% round; /* Safari 3.1-5 */
    -o-border-image: url(border.png) 30% round; /* Opera 11-12.1 */
    border-image: url(border.png) 30% round;
}
Try it yourself »

Test Yourself with Exercises!


CSS3 Border Properties

PropertyDescription
border-imageA shorthand property for setting all the border-image-* properties
border-image-sourceSpecifies the path to the image to be used as a border
border-image-sliceSpecifies how to slice the border image
border-image-widthSpecifies the widths of the border image
border-image-outsetSpecifies the amount by which the border image area extends beyond the border box
border-image-repeatSpecifies whether the border image should be repeated, rounded or stretched

CSS3 Backgrounds

CSS3 Backgrounds

CSS3 contains a few new background properties, which allow greater control of the background element.
In this chapter you will learn how to add multiple background images to one element.
You will also learn about the following new CSS3 properties:
  • background-size
  • background-origin
  • background-clip

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-image
(with multiple backgrounds)
4.012.09.03.63.111.5
background-size4.0
1.0 -webkit-
12.09.04.0
3.6 -moz-
4.1
3.0 -webkit-
10.5
10.0 -o-
background-origin1.012.09.04.03.010.5
background-clip4.012.09.04.03.010.5

CSS3 Multiple Backgrounds

CSS3 allows you to add multiple background images for an element, through the background-image property.
The different background images are separated by commas, and the images are stacked on top of each other, where the first image is closest to the viewer.
The following example has two background images, the first image is a flower (aligned to the bottom and right) and the second image is a paper background (aligned to the top-left corner):

Example

#example1 {
    background-image: url(img_flwr.gif), url(paper.gif);
    background-position: right bottom, left top;
    background-repeat: no-repeat, repeat;
}
Try it yourself »
Multiple background images can be specified using either the individual background properties (as above) or thebackground shorthand property.
The following example uses the background shorthand property (same result as example above):

Example

#example1 {
    background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
}
Try it yourself »

CSS3 Background Size

The CSS3 background-size property allows you to specify the size of background images.
Before CSS3, the size of a background image was the actual size of the image. CSS3 allows us to re-use background images in different contexts.
The size can be specified in lengths, percentages, or by using one of the two keywords: contain or cover.
The following example resizes a background image to much smaller than the original image (using pixels):
Original background image:

Lorem Ipsum Dolor

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
Resized background image:

Lorem Ipsum Dolor

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
Here is the code:

Example

#div1 {
    background: url(img_flower.jpg);
    background-size: 100px 80px;
    background-repeat: no-repeat;
}
Try it yourself »
The two other possible values for background-size are contain and cover.
The contain keyword scales the background image to be as large as possible (but both its width and its height must fit inside the content area). As such, depending on the proportions of the background image and the background positioning area, there may be some areas of the background which are not covered by the background image.
The cover keyword scales the background image so that the content area is completely covered by the background image (both its width and height are equal to or exceed the content area). As such, some parts of the background image may not be visible in the background positioning area.
The following example illustrates the use of contain and cover:

Example

#div1 {
    background: url(img_flower.jpg);
    background-size: contain;
    background-repeat: no-repeat;
}
#div2 {
    background: url(img_flower.jpg);
    background-size: cover;
    background-repeat: no-repeat;
}
Try it yourself »

Define Sizes of Multiple Background Images

The background-size property also accepts multiple values for background size (using a comma-separated list), when working with multiple backgrounds.
The following example has three background images specified, with different background-size value for each image:

Example

#example1 {
    background: url(img_flwr.gif) left top no-repeat, url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;
    background-size: 50px, 130px, auto;
}
Try it yourself »

Full Size Background Image

Now we want to have a background image on a website that covers the entire browser window at all times.
The requirements are as follows:
  • Fill the entire page with the image (no white space)
  • Scale image as needed
  • Center image on page
  • Do not cause scrollbars
The following example shows how to do it; Use the html element (the html element is always at least the height of the browser window). Then set a fixed and centered background on it. Then adjust its size with the background-size property:

Example

html {
    background: url(img_flower.jpg) no-repeat center center fixed; 
    background-size: cover;
}
Try it yourself »

CSS3 background-origin Property

The CSS3 background-origin property specifies where the background image is positioned.
The property takes three different values:
  • border-box - the background image starts from the upper left corner of the border
  • padding-box - (default) the background image starts from the upper left corner of the padding edge
  • content-box - the background image starts from the upper left corner of the content
The following example illustrates the background-origin property:

Example

#example1 {
    border: 10px solid black;
    padding:35px;
    background:url(img_flwr.gif);
    background-repeat: no-repeat;
    background-origin: content-box;
}
Try it yourself »

CSS3 background-clip Property

The CSS3 background-clip property specifies the painting area of the background.
The property takes three different values:
  • border-box - (default) the background is painted to the outside edge of the border
  • padding-box - the background is painted to the outside edge of the padding
  • content-box - the background is painted within the content box
The following example illustrates the background-clip property:

Example

#example1 {
    border: 10px dotted black;
    padding:35px;
    background: yellow;
    background-clip: content-box;
}
Try it yourself »

Test Yourself with Exercises!


CSS3 Background Properties

PropertyDescription
backgroundA shorthand property for setting all the background properties in one declaration
background-clipSpecifies the painting area of the background
background-imageSpecifies one or more background images for an element
background-originSpecifies where the background image(s) is/are positioned
background-sizeSpecifies the size of the background image(s)