CSS allows animation of HTML elements without using JavaScript or Flash!
CSS Animations
About Hover.css. All Hover.css effects make use of a single element (with the help of some pseudo-elements where necessary), are self contained so you can easily copy and paste them, and come in CSS, Sass, and LESS flavours. Many effects use CSS3 features such as transitions, transforms and animations.
In this chapter you will learn about the following properties:
Browser Support for Animations
The numbers in the table specify the first browser version that fully supports the property.
Browser Specific Prefixes
Some older browsers need specific prefixes (-webkit-) to understand the animation properties:
Example
div { Try it Yourself »
width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; } @keyframes example { from {background-color: red;} to {background-color: yellow;} } What are CSS Animations?
An animation lets an element gradually change from one style to another.
You can change as many CSS properties you want, as many times you want.
To use CSS animation, you must first specify some keyframes for the animation.
Keyframes hold what styles the element will have at certain times.
The @keyframes Rule
When you specify CSS styles inside the
@keyframes rule, the animation will gradually change from the current style to the new style at certain times.
To get an animation to work, you must bind the animation to an element.
The following example binds the 'example' animation to the <div> element. The animation will last for 4 seconds, and it will gradually change the background-color of the <div> element from 'red' to 'yellow':
Example
/* The animation code */ Try it Yourself »
@keyframes example { from {background-color: red;} to {background-color: yellow;} } /* The element to apply the animation to */ div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; }
Note: The
animation-duration property defines how long time an animation should take to complete. If the animation-duration property is not specified, no animation will occur, because the default value is 0s (0 seconds).
In the example above we have specified when the style will change by using the keywords 'from' and 'to' (which represents 0% (start) and 100% (complete)).
It is also possible to use percent. By using percent, you can add as many style changes as you like.
The following example will change the background-color of the <div> element when the animation is 25% complete, 50% complete, and again when the animation is 100% complete:
Example
/* The animation code */ Try it Yourself »
@keyframes example { 0% {background-color: red;} 25% {background-color: yellow;} 50% {background-color: blue;} 100% {background-color: green;} } /* The element to apply the animation to */ div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; }
The following example will change both the background-color and the position of the <div> element when the animation is 25% complete, 50% complete, and again when the animation is 100% complete:
Example
/* The animation code */ Try it Yourself »
@keyframes example { 0% {background-color:red; left:0px; top:0px;} 25% {background-color:yellow; left:200px; top:0px;} 50% {background-color:blue; left:200px; top:200px;} 75% {background-color:green; left:0px; top:200px;} 100% {background-color:red; left:0px; top:0px;} } /* The element to apply the animation to */ div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; } Delay an Animation
The
animation-delay property specifies a delay for the start of an animation.
The following example has a 2 seconds delay before starting the animation:
Example
div { Try it Yourself »
width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-delay: 2s; }
Negative values are also allowed. If using negative values, the animation will start as if it had already been playing for N seconds.
In the following example, the animation will start as if it had already been playing for 2 seconds:
Example
div { Try it Yourself »
width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-delay: -2s; } Set How Many Times an Animation Should Run
The
animation-iteration-count property specifies the number of times an animation should run.
The following example will run the animation 3 times before it stops:
Example
div { Try it Yourself »
width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 3; }
The following example uses the value 'infinite' to make the animation continue for ever:
Example
div { Try it Yourself »
width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: infinite; } Run Animation in Reverse Direction or Alternate Cycles
The
animation-direction property specifies whether an animation should be played forwards, backwards or in alternate cycles.
The animation-direction property can have the following values:
The following example will run the animation in reverse direction (backwards):
Example
div { Try it Yourself »
width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-direction: reverse; }
The following example uses the value 'alternate' to make the animation run forwards first, then backwards:
Example
div { Try it Yourself »
width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 2; animation-direction: alternate; }
The following example uses the value 'alternate-reverse' to make the animation run backwards first, then forwards:
Example
div { Try it Yourself »
width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 2; animation-direction: alternate-reverse; } Specify the Speed Curve of the Animation
The
animation-timing-function property specifies the speed curve of the animation.
The animation-timing-function property can have the following values:
The following example shows the some of the different speed curves that can be used:
Example
#div1 {animation-timing-function: linear;} Try it Yourself »
#div2 {animation-timing-function: ease;} #div3 {animation-timing-function: ease-in;} #div4 {animation-timing-function: ease-out;} #div5 {animation-timing-function: ease-in-out;} Specify the fill-mode For an Animation
CSS animations do not affect an element before the first keyframe is played or after the last keyframe is played. The animation-fill-mode property can override this behavior.
The
animation-fill-mode property specifies a style for the target element when the animation is not playing (before it starts, after it ends, or both).
The animation-fill-mode property can have the following values:
The following example lets the <div> element retain the style values from the last keyframe when the animation ends:
Example
div { Try it Yourself »
width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-fill-mode: forwards; }
The following example lets the <div> element get the style values set by the first keyframe before the animation starts (during the animation-delay period):
Example
div { Try it Yourself »
width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: backwards; }
The following example lets the <div> element get the style values set by the first keyframe before the animation starts, and retain the style values from the last keyframe when the animation ends:
Example
div { Try it Yourself »
width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: both; } Animation Shorthand Property
The example below uses six of the animation properties:
Example
div { Try it Yourself »
animation-name: example; animation-duration: 5s; animation-timing-function: linear; animation-delay: 2s; animation-iteration-count: infinite; animation-direction: alternate; }
The same animation effect as above can be achieved by using the shorthand
animation property:
Example
div { Try it Yourself »
animation: example 5s linear 2s infinite alternate; } Test Yourself with Exercises!CSS Animation Properties
The following table lists the @keyframes rule and all the CSS animation properties:
50 CSS3 button examples with effects & animationsAre you looking for some CSS3 buttons, which are good enough to be used in real-world website projects? here are some CSS3 buttons you might like. Since my last post about codepen buttons, I've been searching sites like codepen, cssdeck and jsfiddle for some good practical buttons and I'm amazed to find so many cool CSS3 buttons with nice effects and animations. Here are some wonderful buttons I've picked, which I think are cool and practical and can be used in your web projects. Some buttons here do use jQuery, but not too lavishly to mess-up everything.CSS3 Button GeneratorCheckout Sanwebe CSS3 button generator.CSS3 Pink Button with Pacifico fontNice 3D pink rectangle button uses CSS3 transition effects. Other than small jQuery code to play the sound and change inner text, it's entirely made using pure CSS3.Collection of CSS3 3D Web ButtonsCollection of 3D buttons, created only using CSS3. The buttons were inspired by freebie PSD Chunky 3D buttons.CSS3 Social 3D buttonsNicely done social buttons only using CSS3 and icon font.CSS3 Animated buttonsCircle animated buttons with CSS3 background patterns. Might not work in Firefox 3.6 and IE10.CSS3 Circle Animated buttonsAnother circle animated buttons, but this time, the text rotation on mouse hover gives really nice effect.Clean Circle buttonsAnother example of circle CSS3 buttons.Pure CSS3 Toggle ButtonsNicely created Toggle buttons, entirely created using CSS3 and icon font.CSS3 Animated ButtonA nice looking 3d button created using CSS3 and Google fonts.CSS3 Button EffectsButtons demonstrate cool animations using different CSS3 properties.Shiny CSS3 ButtonsWe are now absolutely sure that we can easily create shiny buttons only using CSS3, no more photoshop.3D CSS3 ButtonsIt's pretty amazing what you can do with CSS3 pseudo elements :before and :after. Checkout amazingly done 3d buttons.CSS3 SwitchA pure-CSS3 button switch example, no javascript used.3D flip button effect3D flip button effect on click, created only using CSS3.Brand ButtonsNo effects or animation, just collection of popular brand buttons using CSS and bootstrap icons.Dark circled buttonA circle button inspired by this dribbble shot example. Small jQuery code to give blob effect on click.CSS3 SwitchA Switch inspired by dribbble shot, created only using CSS3.Beautiful Flat ButtonsA collection of flat buttons for all occasions, no animation or effects.Social Media Sharing ButtonsMuch nicer version of social share buttons, only nonfunctional. As per author it should work in IE7+.Christmas ButtonCSS Christmas button using data:urls, a method to embed the image data directly into the document.Soft ButtonA round shiny button using CSS3.Soft ButtonButtons only uses unicode symbols, you can also use text or icon fonts.Chunky 3D pure CSS3 AnimatedAnother set of 3D animated buttons for websites. Animation is done using the animation and keyframes properties.CSS3 Metal UI buttonsCollection of CSS3 metal button, the symbols were created with the help of 'pictos' font by using @font-face. Box-shadow and linear-gradient properties were used to create metal look.CSS3 rounded buttonsAnother collection of rounded animated button created using CSS3.Pressable CSS3 Social ButtonsButtons uses simple CSS3 properties like gradients, box-shadows, text-shadows etc. Hover and active states are also included in this set.CSS3 Simple Push buttonsNice CSS3 buttons. The HTML and CSS codes are much more usable in real world projects.CSS3 3D download buttonThis 3D download button uses perspective transform to make the button look 3D which works only in webkit browsers.Amazing CSS3 Social ButtonsThese social buttons use liner-gradient as the base background, box-shadow for 3D effect, the icons are the result of data:URI.Big ButtonA big fat shiny CSS3 button with a shadow effect beneath it. The button uses font called 'Sansita One' from Google font.Simple ButtonsJust some simple CSS buttons for the refinement.CSS3 Social ButtonsAnother set of Social buttons created using CSS3, it uses :before and :after to create the button itself. The icons are from fontawesome.io.Simple CSS buttonsA series of simple CSS buttons. They are easy to customize and use. Can easily be integrated with Font-Awesome or other icons library to bring it out more.Poker Chip ButtonSimple poker chip button example using CSS3, Can also be used as a button with hover effect.Slidey ButtonSlidey CSS3 button concept, based on Dribble shot by @buatoom.Admin Menu ButtonsAdministration bar (or menu or navigation) with css3 and fontawesome. When toggle on button class active is added using jQuery.Stitched ButtonA simple button with stitched look, demonstrating the possibility of CSS3, no background image used.Spin ButtonCircular button with spinning border for hover indication.Pure CSS3 ButtonA clean soft button created only using CSS3, the button was inspired by this Dribble example.Slide Out Card CSS3 ButtonThese buttons look like cards that slide out of a sleeve. Useful for teasing a user or for any info that needs to remain hidden until the user chooses.Candybar CSS3 Button animationA Pure CSS candybar button animation to denote loading state.CSS3 On/Off SwitchesNicely created CSS3 On/Off Switches created using CSS3, uses small jQuery code to toggle class.Glossy CSS3 ButtonsSet of glossy css3 buttons, uses various CSS3 properties to give it 3D glossy look.3D buttons with multi-color button edgePure CSS3 buttons with multi color button edge.8-bit HoversCheckout these brilliant 8-bit Hovers buttons.Sullivan ButtonsSimple and clean buttons made using pure CSS. Icons from FontAwesome used.Sexy SCSS buttons in HSBSome CSS buttons with FontAwesome icons.Clean Button SetSome regular clean button set for your web applications.Facebook ButtonsThe easiest way to make people think: 'Wow, this app must be as good as Facebook!'.Button SwitchBootstrap button toggle switch.Animated CSS3 ButtonsA collection of CSS3 animated buttons to spice up your siteAColorful CSS ButtonsA collection of various types of CSS buttons in various colors.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |