Dreamweaver II | CSS Transitions


 

CSS Transitions

CSS transitions create a gradual change from the start state (the black background) to the end state (the blue background).

No matter how pretty your web page is, no one is going to stop and stare at it for long. What users do is interact. And when they interact, you want that pretty page to come to life. Links that light up on rollover, submenus that unfold, images that swap, accordions that open and close. A live web page is not a static design comp; a live page moves and breathes.

The possibilities to animate a web design are pretty cool, but these interactions often feel jerky or abrupt. When you mouse over a link, the color changes in an instant. First black, then blue. Bang!

Using CSS transitions, you can control the timing. You can make your colors gradually shift from black to blue, creating a smoother, more natural effect. In this lecture, we'll try out transitions of all shapes and forms.

In this lecture, you can expect to:

Learn about CSS transition properties.
Explore the different timing functions.
Experiment with CSS transforms.
Apply CSS transitions to form elements.
Study practical examples of transitions in use.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Use Matthew Lein's CSS Easing Animation tool to try out different timing functions.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Transitions are triggered by user events—most often, a mouseover.

 

 

 

 

 

 

 

 

 

 

CSS Transition Properties

 

Mouse over the buttons below:

These buttons are styled purely with CSS.

Whoa, where did that blue come from? This effect is made with just a few lines of CSS. The link background-color is set to black.

During the "hover" state, the background-color changes to blue.

While the blue is nice, it's a little abrupt. For a different approach, mouse over the buttons below. Notice the difference? The background-color "eases" to blue more smoothly:

CSS transitions are supported by the latest browsers.

When the page first loads, the background-color is black (the start state). When the user mouses over a link, the background-color turns blue (the end state). The transition declaration is applied to the start state.

The transition declaration feeds the browser three pieces of information:

 
 
  • transition-property: which CSS property will be transformed (in the example above, the background-color)

  • transition-duration: how long the the transition will last (1 second)

  • transition-time-function: how the transition should animate (ease)

    "Ease" is the default timing function, but there are several to choose from:

    • ease: starts slow, speeds up, and slows at end

    • ease-in: starts slow, then speeds up

    • ease-in-out: similar to ease, but slower at each end

    • ease-out: starts fast, and slows towards the end

    • linear: constant speed throughout

    • cubic-bezier(x1,y1,x2,y2): specifies a cubic-bézier curve to control the speed

 
 

You can write the transition declaration in longhand, with each property on a separate line:


Or in shorthand, with all the properties on a single line:


Writing your transitions in shorthand results in fewer lines of code. It's cleaner and more efficient.

In fact, you can make Dreamweaver default to shorthand transitions! Go to Dreamweaver > Preferences (or Edit > Preferences on a PC). Choose the CSS Styles category, and select "Use shorthand for transition".

So, what can you do with transitions? Lots! To get an idea, check out Lea Verou's animatable.

Lea Verou's animatable offers a smorgasbord of exciting CSS animation effects, all achieved—as she says in the header!—with any two values of (almost) any property.

You'll notice that almost all the CSS properties are represented here. Properties like "font-family" cannot be transitioned, since it would be impossible to for the browser to determine the in-between states transitioning from one font to another.

In order to apply a transition, the browser needs to compute a mathematical value. If you tell the browser to transition a font-size of 10 pixels to a font-size of 20 pixels, it's done. Colors can also be transitioned, since they are represented by RGB values. Here's a list of CSS properties which can be transitioned:

 
 
  • background: background-color, background-position, background-size

  • borders: border-color, border-width, border-radius, border-spacing, outline-color, outline-width

  • typography: font-size, color, font-weight, letter-spacing, word-spacing text-indent, text-shadow, vertical-align

  • size: height, width, max-height, max-width, min-height, min-width

  • positioning: left, right, top, bottom, z-index, clip

  • margins: margin-bottom, margin-left, margin-right, margin-top

  • padding: padding-bottom, padding-left, padding-right, padding-top

  • opacity
 
 

You're probably familiar with most of the CSS properties listed above, but you might not have used the new CSS transform properties yet. Combining transitions and transforms, you can create all kinds of animated effects:

 
 
  • rotate: rotates the element clockwise

  • scale: increases or decreases the size of the element

  • skew: slants an element to the side

  • translate: moves an element to the right or left, or up or down
 
 

The CSS Transitions Panel

Ready to give transitions a try? Download the transitions folder from the course downloads area and move it into your dw2projects folder. Expand the transform folder and open transform.html.

You can write your transitions by hand in Code View, but Dreamweaver has a handy panel called "CSS Transitions" which only works in Live View. Toggle to Live View to try it out.

Square 1: Rotate

First, tell Dreamweaver which page element should be transitioned. Select the first square in the document window.

Go to Window > CSS Transitions and click the + sign to create a new transition.

The New Transition dialog will appear.

1. Type .rotate into the Target Rule field (Dreamweaver will create a new class selector called .rotate and apply it to the div).

2. Choose "hover" from the Transition On menu (the transition will occur when you mouse over the .rotate div).

3. Choose "Use a different transition for each property". This will allow you to specify exactly which CSS property should receive the transition.

Choosing "Use the same transition for all properties" slows performance a bit, since the browser has to check each CSS property to see if a change has occurred. If you only want to apply the transition to a single property, save the browser the work.

4. Click the + sign beneath the Property pane, selecting "transform" from the list.

5. Type "2" into the Duration menu (the transition will occur for 2 seconds).

6. Leave the Delay field blank (it will default to a delay of 0 seconds). The transition-delay setting isn't often used, since you'll usually want your transition to start playing right away.

7. Choose "ease-out" from the Timing Function menu.

8. Type "rotate(45deg)" into the End Value field.

9. Save the transition in the transform.css file, and click Create Transition.

Mouse over the first square to see the transition in action.

Toggle to the transform.css file to see how this magic works. On hover, the square is set to rotate 45 degrees. This transition is applied to the "transform" property. It takes place over a two second time period, and eases out.

Dreamweaver also provides specific browser prefixes for Safari and Opera.

Toggle back to Live View. Select div.rotate in the CSS Transitions panel, then click the pencil icon to edit.

Let's speed up the rotation and change how the transition is timed.

1. Change the duration to "0.2" seconds.

2. Click the menu to the right of the Timing Function field and choose "linear".

Save the transition and preview in a browser. Mouse over the first square, noticing the fast, steady rotation speed.

Square 2: Spin

Is spinning the same as rotation? Not quite.

Select square #2, which will receive the next transition.

Dreamweaver will apply the new transition to whatever element is selected on the page.

Click the + sign in the CSS Transitions panel to create a new transition.

1. Type .spin into the Target Rule field.

2. Choose "active" from the Transition On menu (the transition will occur when you click the .rotate div).

3. Choose "Use a different transition for each property".

4. Click the + sign beneath the Property pane, selecting "transform" from the list.

5. Type "4" into the Duration menu. Leave the Delay field blank, and choose "ease-in-out" from the Timing Function menu.

6. Type "rotate(720deg) rotateY(0)" into the End Value field.

7. Save the transition in the transform.css file, and click Create Transition.

When you mouse over the second square, nothing will happen. Click and hold to see the square spin. The "active" transition occurs as the element is being activated.

Square 3: Scale and Color

Feeling dizzy? Don't worry, we'll stop rotating for now and try out some scale and color transitions. Select square #3.

Dreamweaver will apply the new transition to whatever element is selected on the page.

Click the + sign in the CSS Transitions panel to create a new transition.

1. Type .grow into the Target Rule field.

2. Choose "hover" from the Transition On menu.

3. We are going to apply this transition to more than one property here, so choose "Use the same transition for all properties".

4. Click the + sign beneath the Property pane, selecting "transform" from the list.

5. Type "2" into the Duration menu and choose "ease-in" from the Timing Function menu. Leave the Delay field blank.

6. Type "scale(2, 4)" into the End Value field. This will make the square twice as wide and four times as tall—you'll see!

Don't click Create Transition yet, there's another property to add.

7. Click the + sign beneath the Property pane, selecting "background-color" from the list.

8. Type "#093" as the End Value.

9. Save the transition in the transform.css file, and click Create Transition.

When you mouse over the third square, it will grow in size as it turns green. Pretty neat!

Square 4: Skew

OK, time to get a little skewed! Select square #4.

Dreamweaver will apply the new transition to whatever element is selected on the page.

Click the + sign in the CSS Transitions panel to create a new transition.

1. Type .skew into the Target Rule field.

2. Choose "hover" from the Transition On menu.

3. Choose "Use a different transition for each property".

4. Click the + sign beneath the Property pane, selecting "transform" from the list.

5. Type "1" into the Duration menu and choose "linear" from the Timing Function menu. Leave the Delay field blank.

6. Type "skewX(20deg)" into the End Value field. This will skew the square 20 degrees horizontally. If you wanted to skew the square vertically, you would use a value of "skewY(20deg)".

7. Save the transition in the transform.css file, and click Create Transition.

Mouse over the fourth square to see it slant.

Square 5: Translate

Translate, in this case, is a fancy word for "move." Select square #5.

Dreamweaver will apply the new transition to whatever element is selected on the page.

Click the + sign in the CSS Transitions panel to create a new transition.

1. Type .translate into the Target Rule field.

2. Choose "hover" from the Transition On menu.

3. Choose "Use a different transition for each property".

4. Click the + sign beneath the Property pane, selecting "transform" from the list.

5. Type "1" into the Duration menu and choose "linear" from the Timing Function menu. Leave the Delay field blank.

6. Type "translateX(50px)" into the End Value field. This will move the square 50 pixels to the right. If you wanted to move the square to the left, you would use a negative value, "translateX(-50px)". To move the square down, use "translateY(50px)". Or up: "translateY(-50px)".

7. Save the transition in the transform.css file, and click Create Transition.

When you mouse over the fifth square, it will move to the right.

Opacity

We put those boxes through a lot, didn't we? They might be feeling a little worn out, fading a little. Seems like the right time to talk about opacity transitions! Open the opacity.html file from the opacity folder. Expand the CSS Designer panel. Toggle to opacity.css and find the ul.gallery li style. Add a new property to set the opacity to "0.6".

Toggle back to Live View. Click the + sign in the CSS Transitions panel to create a new transition.

1. Click the menu to the right of the Target Rule field, and choose ul.gallery li.

2. Choose "hover" from the Transition On menu.

3. Choose "Use a different transition for each property".

4. Click the + sign beneath the Property pane, selecting "opacity" from the list.

5. Type "1" into the Duration menu, and choose a value of "ease" from the Timing Function menu. Leave the Delay field blank.

6. Type "1.0" into the End Value field.

7. Save the transition in the opacity.css file, and click Create Transition.

Mousing over an image will bring it to full opacity.

Jump Links

There's a specific method of CSS transition that does a great job making links feel more vibrant. Open target.html from the target folder. This document contains several summaries of Jane Austen novels, indexed by jump links at the top. Clicking a jump link takes you to the appropriate heading in the document.

The "target" transition can be used to animate the headings as they are targeted. Click the + sign in your CSS Transitions panel to create a new transition.

1. Click the menu to the right of the Target Rule field, and choose h2.

2. Choose "target" from the Transition On menu.

3. Choose "Use a different transition for each property".

4. Click the + sign beneath the Property pane, selecting "color" from the list.

5. Type "1" into the Duration menu. Leave the Delay field blank. Set the timing-function to "linear".

6. Type "#660000" into the End Value field.

7. Save the transition in the target.css file, and click Create Transition.

When you click a link, the heading will glow red.

Forms

Expand the form folder and open form.html. Click the + sign to create a new transition.

1. Type "input" into the Target rule field (the input tag selector will apply to <input> form elements, like text boxes).

2. Choose "focus" from the Transition On menu (this transition will occur when you insert your cursor into a form field).

In case you're wondering, the checked, disabled, enabled, and indeterminate transitions also apply to form elements:

 
 
  • checked: occurs when a checkbox is checked

  • disabled/enabled: occurs when a form element becomes disabled or enabled

  • indeterminate: occurs when a checkbox's indeterminate DOM property is set to "true" by JavaScript
 
 

3. Choose "Use the same transition for all properties".

4. Type "1" into the Duration menu. Leave the Delay field blank. Choose "ease-in" from the timing-function menu.

5. Click the + sign beneath the Property pane, selecting "background-color" from the list.

6. Type "rgba(176,247,255,1)" into the End Value field.

7. Save the transition in the form.css file, and click Create Transition.

Click inside an input field and watch as it glows blue.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If CSS transitions aren't supported by a particular browser, the normal hover effect (with no transition) will display instead.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Fine-tune the timing of hover state changes to create a smooth, elegant visual experience.

 

Case Studies

You've learned a lot about different nifty transitions, but since you're a sharp designer, you might be thinking something like: "sure, these look cool, but are they useful?"

To finish up this lesson, we'll take a look at how transitions are used around the Web to enhance designs. I've collected a few in this lecture's downloads for you to check out.

Vertical Navigation

First up, an example of how transitions in vertical navigation menus can help the user experience feel more active. Expand the side-nav folder and open side-nav.html. Mouse over the left nav links, and you'll notice a subtle shift to the right.

Toggle to view the side-nav.css file.

On hover, the left-padding shifts from 15 pixels to 20 pixels. The bottom border transitions from solid gray to solid black, and the font color darkens from gray to black. This transition occurs over a time period of 0.2 seconds, in a linear fashion.

Horizontal Navigation

Now for an example of a similar idea with a horizontal navigation bar. Buttons that get "ready" for users to click them help make your site feel like it's really responding to input. They also ensure that your visitor is aware they've moused over a clickable item, which means you don't have to make that item stand out from the rest of the design until the user is ready to select it.

Expand the top-nav folder and open top-nav.html. Make your workspace as large as possible so you'll see the horizontal version of the navigation bar. As you mouse over the top nav bar, a semi-transparent white background will highlight each link.

Back in Dreamweaver, take a look at the top-nav.css file.

On hover, the white background color shifts from rgba(255,255,255,0) to rgba(255,255,255,0.2).

Buttons

Open the buttons.html file from the buttons folder. Mouse over the "visit site" button and watch it grow on hover. This subtle change is suggestive to the user, encouraging them to "make the click."

Toggle to view the CSS. Several properties are transitioned on hover: the padding increases, the background color is darkened, and an inset box-shadow is applied. The changes ease in and out, over a time period of 150 milliseconds.

External Links

External links on the Web Designer Depot website are animated using CSS transitions. Beyond impressing their users, these links also stand out from internal links, providing an extra signal to the user that they're about to leave the site.

Open the external-links.html file. Mouse-over the "Flaticon" link.

Take a look at the source code to see how this is put together. Each external link is wrapped in a span tag with a data-title attribute.

Toggle to the external-links.css file. Two different transitions are appearing simultaneously. The span attribute animates the pale red rectangle (the top of the spinning box). The span:after attribute animates the darker red rectangle (the bottom of the spinning box).

Captions

Expand the caption folder and open caption1.html. Mouse over each image to reveal the caption. This image gallery was created by Mary Lou for Codrops. There are seven variations in total, each using a different combination of CSS transitions and CSS transforms. Study the accompanying CSS files to see how they work.

http://tympanus.net/Tutorials/CaptionHoverEffects/

I hope you've enjoyed learning about CSS transitions. In the exercise, you'll have a chance to apply transitions to a design of your own making.

 

   
 
Learn about front end and back end form design.
Learn how to create various form elements such as text fields, radio buttons, checkboxes, and submit buttons.
Experiment with new HTML5 form elements.
Learn about the GET and POST methods used for submitting form data and the Enctype attribute.
Explore ways of implementing different types of form handlers.
Learn to troubleshoot common form problems.
 

Discussion
Share your thoughts and opinions with your fellow students in the Discussion area.

Exercise
Build a website for a Napa Valley winery.