Monday, 12 October 2015

CSS3 Selectors

Selectors are patterns used to select which element(s) you want to style.
The CSS3 specification contains several new selectors, and many of them have already been implemented in modern browsers.
The following table lists the new CSS3 selectors:


PatternExampleExample descriptionCSS
element1~element2p~ulSelects every ul element that are preceded by a p element3
[attribute^=value]a[src^="https"]Selects every a element whose src attribute value begins with "https"3
[attribute$=value]a[src$=".pdf"]Selects every a element whose src attribute value ends with ".pdf"3
[attribute*=value]a[src*="w3schools"]Selects every a element whose src attribute value contains the substring "w3schools"3
:first-of-typep:first-of-typeSelects every p element that is the first p element of its parent3
:last-of-typep:last-of-typeSelects every p element that is the last p element of its parent3
:only-of-typep:only-of-typeSelects every p element that is the only p element of its parent3
:only-childp:only-childSelects every p element that is the only child of its parent3
:nth-child(n)p:nth-child(2)Selects every p element that is the second child of its parent3
:nth-last-child(n)p:nth-last-child(2)Selects every p element that is the second child of its parent, counting from the last child3
:nth-of-type(n)p:nth-of-type(2)Selects every p element that is the second p element of its parent3
:nth-last-of-type(n)p:nth-last-of-type(2)Selects every p element that is the second p element of its parent, counting from the last child3
:last-childp:last-childSelects every p element that is the last child of its parent3
:root:rootSelects the document’s root element3
:emptyp:emptySelects every p element that has no children (including text nodes)3
:target#news:targetSelects the current active #news element (clicked on a URL containing that anchor name)3
:enabledinput:enabledSelects every enabled input element3
:disabledinput:disabledSelects every disabled input element3
:checkedinput:checkedSelects every checked input element3
:not(selector):not(p)Selects every element that is not a p element3
::selection::selectionSelects the portion of an element that is selected by a user3

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

PropertyBrowser Support
resize
box-sizing-moz--webkit--webkit-
outline-offset
The resize property is only supported in Chrome and Safari.
The box-sizing is supported in Internet Explorer and Opera. Firefox requires the prefix -moz-. Chrome and Safari require the prefix -webkit-.
The outline property is supported in all major browsers, except Internet Explorer.

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 Google Chrome and Safari).
The CSS code is as follows:
SafariChromeInternet Explorer

Example

Specify that a div element should be resizable by the user:
div
{
resize:both;
overflow:auto;
}

Try it yourself »


CSS3 Box Sizing

The box-sizing property allows you to define certain elements to fit an area in a certain way:
OperaSafariChromeFirefox

Example

Specify two bordered boxes side by side:
div
{
box-sizing:border-box;
-moz-box-sizing:border-box; /* Firefox */
-webkit-box-sizing:border-box; /* Safari and Chrome */
width:50%;
float:left;
}

Try it yourself »


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

Example

Specify an outline 15px outside the border edge:
div
{
border:2px solid black;
outline:2px solid red;
outline-offset:15px;
}

Try it yourself »


New User-interface Properties

PropertyDescriptionCSS
appearanceAllows you to make an element look like a standard user interface element3
box-sizingAllows you to define certain elements to fit an area in a certain way3
iconProvides the author the ability to style an element with an iconic equivalent3
nav-downSpecifies where to navigate when using the arrow-down navigation key3
nav-indexSpecifies the tabbing order for an element3
nav-leftSpecifies where to navigate when using the arrow-left navigation key3
nav-rightSpecifies where to navigate when using the arrow-right navigation key3
nav-upSpecifies where to navigate when using the arrow-up navigation key3
outline-offsetOffsets an outline, and draws it beyond the border edge3
resizeSpecifies whether or not an element is resizable by the user3

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

PropertyBrowser Support
column-count-moz--webkit--webkit-
column-gap-moz--webkit--webkit-
column-rule-moz--webkit--webkit-
Internet Explorer and Opera do not yet support the multiple columns properties.
Firefox requires the prefix -moz-.
Chrome and Safari require the prefix -webkit-.

CSS3 Create Multiple Columns

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

Example

Divide the text in a div element into three columns:
div
{
-moz-column-count:3; /* Firefox */
-webkit-column-count:3; /* Safari and Chrome */
column-count:3;
}

Try it yourself »


CSS3 Specify the Gap Between Columns

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

Example

Specify a 40 pixels gap between the columns:
div
{
-moz-column-gap:40px; /* Firefox */
-webkit-column-gap:40px; /* Safari and Chrome */
column-gap:40px;
}

Try it yourself »


CSS3 Column Rules

The column-rule property sets the width, style, and color of the rule between columns.
SafariChromeFirefoxInternet Explorer

Example

Specify the width, style and color of the rule between columns:
div
{
-moz-column-rule:3px outset #ff00ff; /* Firefox */
-webkit-column-rule:3px outset #ff00ff; /* Safari and Chrome */
column-rule:3px outset #ff00ff;
}

Try it yourself »


New Multiple Columns Properties

PropertyDescriptionCSS
column-countSpecifies the number of columns an element should be divided into3
column-fillSpecifies how to fill columns3
column-gapSpecifies the gap between the columns3
column-ruleA shorthand property for setting all the column-rule-* properties3
column-rule-colorSpecifies the color of the rule between columns3
column-rule-styleSpecifies the style of the rule between columns3
column-rule-widthSpecifies the width of the rule between columns3
column-spanSpecifies how many columns an element should span across3
column-widthSpecifies the width of the columns3
columnsA shorthand property for setting column-width and column-count3

CSS3 Animations

CSS3 Animations

With CSS3, we can create animations, which can replace animated images, Flash animations, and JavaScripts in many web pages.

CSS3
Animation

CSS3 @keyframes Rule

To create animations in CSS3, you will have to learn about the @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.

Browser Support

PropertyBrowser Support
@keyframes-webkit--webkit-
animation-webkit--webkit-
Internet Explorer, Firefox, and Opera does not yet support the @keyframes rule or the animation property.
Chrome and Safari requires the prefix -webkit-.



SafariChromeInternet Explorer

Example

@keyframes myfirst
{
from {background: red;}
to {background: yellow;}
}

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


CSS3 animation

When the animation is created in the @keyframe, bind it to a selector, otherwise the animation will have no effect.
Bind the animation to a selector by specifying at least these two CSS3 animation properties:
  • Specify the name of the animation
  • Specify the duration of the animation
SafariChromeInternet Explorer

Example

Binding the "myfirst" animation to a div element, duration: 5 seconds:
div
{
animation: myfirst 5s;
-webkit-animation: myfirst 5s; /* Safari and Chrome */
}

Try it yourself »
Note: You must define the name and the duration of the animation. If duration is omitted, the animation will not run, because the default value is 0.

What are Animations in CSS3?

An animation is an effect that lets an element gradually change from one style to another.
You can change as many styles you want, as many times you want.
Specify when the change will happen in percent, or the keywords "from" and "to", which is the same as 0% and 100%.
0% is the beginning of the animation, 100% is when the animation is complete.
For best browser support, you should always define both the 0% and the 100% selectors.
SafariChromeInternet Explorer

Example

Change the background color when the animation is 25%, 50%, and again when the animation is 100% complete:
@keyframes myfirst
{
0%   {background: red;}
25%  {background: yellow;}
50%  {background: blue;}
100% {background: green;}
}

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

Try it yourself »

SafariChromeInternet Explorer

Example

Change the background color and position:
@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;}
}

@-webkit-keyframes myfirst /*Safari and Chrome */
{
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;}
}

Try it yourself »


CSS3 Animation Properties

The following table lists the @keyframes rule and all the animation properties:
PropertyDescriptionCSS
@keyframesSpecifies the animation3
animationA shorthand property for all the the animation properties, except the animation-play-state property3
animation-nameSpecifies the name of the @keyframes animation3
animation-durationSpecifies how many seconds or milliseconds an animation takes to complete one cycle. Default 03
animation-timing-functionDescribes how the animation will progress over one cycle of its duration. Default "ease"3
animation-delaySpecifies when the animation will start. Default 03
animation-iteration-countSpecifies the number of times an animation is played. Default 13
animation-directionSpecifies whether or not the animation should play in reverse on alternate cycles. Default "normal"3
animation-play-stateSpecifies whether the animation is running or paused. Default "running"3
The two examples below sets all the animation properties:
SafariChromeInternet Explorer

Example

Run an animation called myfirst, with all the animation properties set:
div
{
animation-name: myfirst;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-play-state-: running;
/*Safari and Chrome: */
-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;
}

Try it yourself »

SafariChromeInternet Explorer

Example

The same animation as above, using the shorthand animation property:
div
{
animation: myfirst 5s linear 2s infinite alternate;
/*Safari and Chrome: */
-webkit-animation: myfirst 5s linear 2s infinite alternate;
}

Try it yourself »

CSS3 Transitions

CSS3 Transitions

With CSS3, we can add an effect when changing from one style to another, without using Flash animations or JavaScripts.

Mouse over the element below:
CSS3
Transition

Browser Support

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

How does it work?

CSS3 transitions are effects that let an element gradually change from one style to another.
To do this, you must specify two things:
  • Specify the CSS property you want to add an effect to
  • Specify the duration of the effect.
OperaSafariChromeFirefoxInternet Explorer

Example

Transition effect on the width property, duration: 2 seconds:
div
{
transition: width 2s;
-webkit-transition: width 2s; /* Safari and Chrome: */
-o-transition: width 2s; /* Opera: */
}
Note: If the duration is not specified, the transition will have no effect, because default value is 0.
The effect will start when the specified CSS property changes value. A typical CSS property change would be when a user mouse-over an element:
OperaSafariChromeInternet Explorer

Example

Specify :hover for <div> elements:
div:hover
{
width:300px;
}

Try it yourself »
Note: When the cursor mouse out of the element, it gradually changes back to it's original style.

Multiple changes

To add a transitional effect for more than one style, add more properties, separated by commas:
OperaSafariChromeInternet Explorer

Example

Add effects on the width, height, and the transformation:
div
{
transition: width 2s, height 2s, transform 2s;
-webkit-transition: width 2s, height 2s, -webkit-transform 2s;
-o-transition: width 2s, height 2s,-o-transform 2s;
}

Try it yourself »


Transition Properties

The following table lists all the transition properties:
PropertyDescriptionCSS
transitionA shorthand property for setting the four transition properties into a single property3
transition-propertySpecifies the name of the CSS property to which the transition is applied3
transition-durationDefines the length of time that a transition takes. Default 03
transition-timing-functionDescribes how the speed during a transition will be calculated. Default "ease"3
transition-delayDefines when the transition will start. Default 03
The two examples below sets all transition properties:
OperaSafariChromeInternet Explorer

Example

Use all transition properties in one example:
div
{
transition-property: width;
transition-duration: 1s;
transition-timing-function: linear;
transition-delay: 2s;
/*Safari and Chrome: */
-webkit-transition-property: width;
-webkit-transition-duration: 1s;
-webkit-transition-timing-function: linear;
-webkit-transition-delay: 2s;
/*Opera: */
-o-transition-property: width;
-o-transition-duration: 1s;
-o-transition-timing-function: linear;
-o-transition-delay: 2s;
}

Try it yourself »

OperaSafariChromeInternet Explorer

Example

The same transition effects as above, using the shorthand transition property:
div
{
transition: width 1s linear 2s;
-webkit-transition: width 1s linear 2s; /*Safari and Chrome: */
-o-transition: width 1s linear 2s; /*Opera: */
}

Try it yourself »