.

Monday, 12 October 2015

CSS3 2D Transforms


CSS3 Transforms

With CSS3 transform, we can move, scale, turn, spin, and stretch elements.
CSS3 Transforms

How Does it Work?

A transform is an effect that lets an element change shape, size and position.
You can transform your elements using 2D or 3D transformation.

Browser Support

PropertyBrowser Support
transform-moz--webkit--webkit- -o- 
Internet Explorer does not yet support the transform property.
Firefox requires the prefix -moz-.
Chrome and Safari requires the prefix -webkit-.
Opera requires the prefix -o-.

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.
OperaSafariChromeFirefoxInternet Explorer

Example

div
{
transform: rotate(30deg);
-webkit-transform: rotate(30deg); /* Safari and Chrome */
-o-transform: rotate(30deg); /* Opera */
-moz-transform: rotate(30deg); /* Firefox */
}

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

Example

div
{
transform: translate(50px,100px);
-webkit-transform: translate(50px,100px); /* Safari and Chrome */
-o-transform: translate(50px,100px); /* Opera */
-moz-transform: translate(50px,100px); /* Firefox */
}

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.
OperaSafariChromeFirefoxInternet Explorer

Example

div
{
transform: rotate(30deg);
-webkit-transform: rotate(30deg); /* Safari and Chrome */
-o-transform: rotate(30deg); /* Opera */
-moz-transform: rotate(30deg); /* Firefox */
}

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

Example

div
{
transform: scale(2,4);
-webkit-transform: scale(2,4); /* Safari and Chrome */
-o-transform: scale(2,4); /* Opera */
-moz-transform: scale(2,4); /* Firefox */
}

Try it yourself »
The value scale(2,4) transforms the width to be twice its original size, and the height 4 times its original size.

The skew() Method


With the skew() method, the element turns in a given angle, depending on the parameters given for the horizontal (X-axis) and the vertical (Y-axis) lines:
OperaSafariChromeFirefoxInternet Explorer

Example

div
{
transform: skew(30deg,20deg);
-webkit-transform: skew(30deg,20deg); /* Safari and Chrome */
-o-transform: skew(30deg,20deg); /* Opera */
-moz-transform: skew(30deg,20deg); /* Firefox */
}

Try it yourself »
The value skew(30deg,20deg) turns the element 30 degrees around the X-axis, and 20 degrees around the Y-axis.

The matrix() Method


The matrix() method combines all of 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.
OperaSafariChromeFirefoxInternet Explorer

Example

How to rotate a div element 30 degrees, using the matrix method:
div
{
transform:matrix(0.866,0.5,-0.5,0.866,0,0);
-moz-transform:matrix(0.866,0.5,-0.5,0.866,0,0); /* Firefox */
-webkit-transform:matrix(0.866,0.5,-0.5,0.866,0,0); /* Safari and Chrome */
-o-transform:matrix(0.866,0.5,-0.5,0.866,0,0); /* Opera */
}

Try it yourself »


New Transform Properties

The following table lists all the transform properties:
PropertyDescriptionCSS
transformApplies a 2D or 3D transformation to an element3
transform-originAllows you to change the position on transformed elements3

2D Transform Methods

FunctionDescription
matrix(n,n,n,n,n,n)Defines a 2D transformation, using a matrix of six values
translate(x,y)Defines a 2D translation
scale(x,y)Defines a 2D scale transformation
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 Fonts


CSS3 @font-face Rule

Before CSS3, web designers had to use fonts that were already installed on the user's computer.
With CSS3, web designers can use whatever font he/she likes.
When you have found/bought the font you wish to use, simply include the font file in the web site, and it will be downloaded automatically to the user when needed.
You will have to describe your selected font with the new CSS3 @font-face rule.
In the @font-face rule you define a name for the font, and the URL to the font file:

Example

@font-face
{
font-family:myFirstFont;
src:url(Sansation_Light.ttf);
}


Browser Support

PropertyBrowser Support
@font-face
Internet Explorer does not yet support the @font-face rule.
Firefox, Chrome, Safari, and Opera support the @font-face rule.

Using The New Font

To use the new font, add "myFirstFont" as the value of the font-family property:
OperaSafariChromeFirefoxInternet Explorer

Example

div
{
font-family:myFirstFont;
}

Try it yourself »


Using Bold Text

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

Example

@font-face
{
font-family:myFirstFont;
src:url(Sansation_Bold.ttf);
font-weight:bold;
}

Try it yourself »
The file "Sansation_Bold.ttf" 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:
DescriptorValuesDescription
font-familynameRequired. Defines a name for the font
urlURLRequired. Defines the URL to the font file
font-stretchnormal
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-stylenormal
italic
oblique
Optional. Defines how the font should be styled. Default is "normal"
font-weightnormal
bold
100
200
300
400
500
600
700
800
900
Optional. Defines the boldness of the font. Default is "normal"
unicode-rangeunicode-rangeOptional. 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

PropertyBrowser Support
text-shadow
word-wrap
Internet Explorer does not yet support the text-shadow property.
Firefox, Chrome, Safari, and Opera support the text-shadow property.
All major browsers support the word-wrap property.

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

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 vord: thisisaveryveryveryveryveryverylongword. The long word will brake 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 vord: thisisaveryveryveryveryveryverylongword. The long word will brake and wrap to the next line.
The CSS code is as follows:
OperaSafariChromeFirefox

Example

Allow long words to be able to brake and wrap onto the next line:
p {word-wrap:break-word;}

Try it yourself »


New Text Properties

PropertyDescriptionCSS
hanging-punctuationSpecifies whether a punctuation character may be placed outside the line box3
punctuation-trimSpecifies whether a punctuation character should be trimmed3
text-align-lastDescribes 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-emphasisApplies emphasis marks, and the foreground color of the emphasis marks, to the element's text3
text-justifySpecifies the justification method used when text-align is "justify"3
text-outlineSpecifies a text outline3
text-overflowSpecifies what should happen when text overflows the containing element3
text-shadowAdds shadow to text3
text-wrapSpecifies line breaking rules for text3
word-breakSpecifies line breaking rules for non-CJK scripts3
word-wrapAllows long, unbreakable words to be broken and wrap to the next line3

CSS3 Backgrounds


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

PropertyBrowser Support
background-size-moz-
background-origin
Internet Explorer does not yet support the new background properties.
Firefox requires the prefix -moz- to support the background-size property, but does not support the background-origin property.
Chrome, Safari, and Opera support the new background properties.

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.
OperaSafariChromeFirefoxInternet Explorer

Example 1

Resize a background image:
div
{
background:url(img_flwr.gif);
-moz-background-size:80px 60px; /* Firefox */
background-size:80px 60px;
background-repeat:no-repeat;
}

Try it yourself »

OperaSafariChromeFirefoxInternet Explorer

Example 2

Stretch the background image to completely fill the content area:
div
{
background:url(img_flwr.gif);
-moz-background-size:100% 100%; /* Firefox */
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. 


OperaSafariChromeFirefoxInternet Explorer

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.
Note: Internet Explorer does not support multiple background images.
OperaSafariChromeFirefoxInternet Explorer

Example

Set two background images for the body element:
body
{
background-image:url(img_flwr.gif),url(img_tree.gif);
}

Try it yourself »


New Background Properties

PropertyDescriptionCSS
background-clipSpecifies the painting area of the background images3
background-originSpecifies the positioning area of the background images3
background-sizeSpecifies the size of the background images3

.

Popular Posts

Powered by Blogger.