tags, each with its own unique ID. Say you have the site’s navigation bar inside a tag that has a class named .navbar attached to it. To hide the nav bar when someone prints the page, create another class style named .navbar in your print style sheet. In the CSS Rule Definition window, click the Block category and then choose “None” from the Display property menu. (In Figure 8-13, right, for example, the banner and both sidebars don’t appear in the printed version of the page.)
Chapter 8: Advanced CSS
403
Styling for Print
• Adjust margins and widths. To make a website look more elegant, you might increase the margins around the edges of a page. But this extra space only wastes paper when printed. In the print style sheet, remove any margins you applied to the body tag. In addition, if you hide parts of a page when it’s printed, it’s possible that the remaining page elements won’t fill the printed page. In this case, add a style to the print style sheet that changes the widths of the printed elements. For example, if you have a two-column design—a sidebar with links and other site-specific info and a main column filled with all the useful info that should appear on a printed page—and you hide one column (the sidebar), you’d then set the width of the remaining column to 100% and remove any margins on its left and right sides. That way, the printed information fills the width of the page. • Take advantage of !important. As mentioned earlier, sometimes the print style sheet needs to override certain CSS properties from another style sheet. Thanks to the cascade (page 391), a style must have greater “specificity” to overrule another style. If you’re trying to override, say, the font color used for a descendent selector named body #wrapper #maincontent p, you have to add the same long-winded style name to your print style sheet. Fortunately, CSS provides a simpler method: the !important directive. Adding !important to a property in a CSS style lets that property overrule any conflicting property values from other styles, even if those other styles are more specific. Unfortunately, Dreamweaver doesn’t give you a way to easily add this option. You have to manually edit the style sheet in Code view. Say you want the text of all paragraph tags to print black. Create a p tag style in the print style sheet and set the color property to “black.” Then open the print style sheet in Code view and add !important after the color value and before the semicolon. Here’s what that would look like in Code view: p { color: #000000 !important; }
When you print the page, this style overrides any color settings for any paragraph tags in a competing style sheet—even a much more specific style.
404
Dreamweaver CS6: The Missing Manual
CSS Transitions Gem in the Rough
A Time to Design A Dreamweaver feature called Design Time style sheets lets you quickly try different CSS style sheets as you develop web pages. With it, you can hide (external) style sheets you’ve attached to a web page and substitute new ones.
The Design-Time Style Sheets window appears. Click the top + button to select an external style sheet. Note that clicking this button doesn’t attach the style sheet to the page; it merely lets you access the properties of a .css file as you work on a page.
Design Time style sheets come in handy when you work on HTML that, later on, you intend to make part of a complete web page. Dreamweaver Library items are a good example; this feature (discussed in Chapter 18) lets you create a chunk of HTML that any number of pages on your site can use. When you update the Library item, Dreamweaver updates every page that uses it. A timesaving feature, for sure, but since a Library item is only part of a page, it doesn’t include the portion needed to either store styles or attach an external style sheet. So when designing a Library item, you’re working in the dark (or at least without any style). By using Design Time style sheets, you can access all the styles in an external style sheet and even preview the effects directly in Design view.
To properly view your page with this new style sheet, you may need to get an attached external style sheet out of the way. To do that, use the bottom + button to add it to the Hide list. You can only use Design Time style sheets when you work in Dreamweaver. They have no effect on how a page looks in a web browser. That’s both the good news and the bad news. Although Dreamweaver lets you apply class styles from a Design Time style sheet to your web page, it doesn’t actually attach the sheet to the page. For example, if you use a Design Time style sheet to design a Library item, Dreamweaver doesn’t guarantee that the web page using the Library item has the style sheet you’re using attached to it. You have to attach it yourself, or else your visitors will never see your intended result.
You can apply a Design Time style sheet by clicking the Design Time style sheet button in the Style Rendering toolbar (see Figure 8-15) or by choosing Format→CSS Styles→Design-Time.
CSS Transitions In Chapter 4 you learned about the :hover pseudo-class, which lets you change the style of a link as a visitor mouses over it. For example, the background color of a link in a navigation bar might be deep blue, but when a visitor mouses over that link, the background changes to bright orange. That change, from blue to orange, is instantaneous, but it might feel a bit abrupt to some. What if you could animate the change so that the link starts out blue and then morphs, moving through a range of colors on the way to turning solid orange. When you mouse off the link, the color would animate back from orange to blue. Now that would be cool…and, thanks to CSS3’s new CSS Transitions property, it’s easy to achieve.
Chapter 8: Advanced CSS
405
CSS Transitions
In a nutshell, a CSS transition is an animation between one or more CSS properties to another set of CSS properties. Web browsers handle the animation: You only need to supply the starting point (the blue background of a link, for instance) and the ending point (the orange background). You also need to add the CSS Transition property, which, unfortunately, requires a fair amount of code. However, Dreamweaver CS6 provides a simple, straightforward tool for creating that code: the new CSS Transitions panel. Here’s how you use it: Note CSS transitions are fun and look great, but they’re relatively new, so not all browsers support them. While Safari, Firefox, and Chrome understand CSS transitions, only version 10 of Internet Explorer (which isn’t even shipping at the time of this writing) does. That means that over half of the web-surfing world won’t be able to enjoy your finely crafted CSS transitions…yet. But since CSS transitions are really just eye candy anyway, visitors coming to your site with IE 7, 8, or 9 won’t know what they’re missing.
1. Create a style for an element on your page. That element can be a class, tag, ID, or any CSS selector style. The element’s style represents the beginning of the animation, and you can use a wide range of CSS properties, including background-color; background-position (page 264); border-color and border-width (page 260); color (page 157); font-size (page 154); font-weight (page 161); margins (page 449); padding (page 449); width and height (page 448); the left, top, bottom and right position settings (page 470); word-spacing (page 164); and more. (You can find a complete list of valid, animate-able properties at http://tinyurl.com/dxjbdhd, though not all browsers support all the properties.) If you’re creating a style for a nav button, for example, you could start with a simple class (.nav) that you apply to each link you want to animate. Or, if all the nav buttons are together inside a single div tag or unordered list, you could add a class name to the div or list tag (
406
Dreamweaver CS6: The Missing Manual
CSS Transitions
Create new transition
Figure 8-16
The CSS Transitions panel lets you add, edit, and delete animations between CSS properties. It lists all the CSS transitions defined in the style sheet (or style sheets) for the currently open web page.
3. Click the New Transition button (the + sign). The New Transition window appears (see Figure 8-17).
Figure 8-17
The New Transition window looks a bit overwhelming at first, but it’s really an easy way to create a CSS transition: identify a page element you want to animate using the Target Rule menu, specify a trigger for the animation (the “Transition on” option), choose a CSS property you want to animate, and then set some timing options.
Chapter 8: Advanced CSS
407
CSS Transitions
4. From the Target Rule menu, select the style you created in step 1. This menu lists all the styles in the page’s style sheet, including tag styles, class styles, IDs, and so on. It also lists any IDs applied to page elements, even if you never created an accompanying ID style, which comes in handy if you simply want to animate an element without first creating an ID style for it. 5. From the Transition On menu, select the trigger for the animation. Unfortunately, you can’t just animate an element whenever you like—you’re limited to a handful of events related to CSS selectors. The :hover pseudo-class is the most well-known, but there are others you can tap into: • active refers to the moment you click on a link. It’s the same as the :active pseudo-class discussed on page 206. Browsers also apply an active state to any time you click on an element and hold the mouse button down. In other words, you could create a CSS animation so that when a visitor clicks on a particular
element and holds the mouse button down, that div’s background color changes, its width increases, and its font size grows. • checked applies to checkboxes (page 576) and radio buttons. However, there aren’t a lot of CSS properties that apply to checkboxes and radio buttons that are worth animating. • disabled and enabled apply to form elements visitors can’t select or change. Since you can’t disable or enable a form element without JavaScript, you probably won’t ever need to use either of these settings. • focus refers to text fields in web forms. When a visitor either clicks into or tabs to a text field, that field receives what’s called “focus.” You can apply a separate set of styles to a field when it’s focused and animate the change between the look of the field as it normally appears and as it appears when a guest clicks into it to type. For example, you could set the height of a multi-line text field (also called a textarea, as described on page 575) to 100 pixels, and then create a CSS transition that changes the field’s height to 500 pixels. When a visitor clicks into that text box, it grows to 500 pixels high, and when the visitor tabs or clicks out of the box, it shrinks back to 100 pixels. Likewise, you could set the background color of a text field to white, but when a visitor clicks into it, the background slowly changes to bright yellow. • hover is the most commonly used option. It applies when a visitor mouses over an element. While you’ll commonly use the hover transition with links, you can apply it to any page element: paragraphs, div tags, headlines, and so on. • You probably won’t ever use the indeterminate setting. It applies only to checkboxes and radio buttons, and refers to a checkbox or radio button that is neither turned on or off. Sounds very Zen, but it’s also very useless. You can only specify the indeterminate setting using JavaScript, and since
408
Dreamweaver CS6: The Missing Manual
you probably won’t ever need to use the state, there’s no need to create an animation for it, either.
CSS Transitions
• target applies to named anchors, described on page 202, and it applies when a visitor clicks a link that jumps to that anchor. For example, say you add a link at the top of a page that jumps down the page to a named anchor. You could add a background color and a CSS transition to that named anchor so that when a visitor clicks the link and jumps to that spot on the page, the color fades into view. Note For some ideas on what you can do with the CSS :target pseudo-class check out http://tinyurl
.com/6mxqbz7 and http://css-tricks.com/on-target/.
6. Choose either “Use the same transition for all properties” or “Use a different transition for each property.” As you’ll see in steps 7-9, you can set a duration for the animation (how long it takes), a delay (how long before it begins), and a “timing function” (the rate of change during the animation). You can apply the same settings to all CSS properties or set different timings for each property. For example, if, when a visitor mouses over a navigation button, you want that button to change from blue to orange, have its text grow from 16 to 25 pixels, and enlarge its border from 1 pixel to 10 pixels in width, you have a choice. You can have all three animations occur at the same rate, or set different rates for each. For example, you could have the background color change quickly, make the border change more slowly, and wait for a second before the text increases in size; to do this, you’d choose the “Use a different transition for each property” option. Depending on which selection you make, the next steps will vary. If you selected the “Use the same transition” option, continue to the next step. If you selected “Use a different transition,” skip to steps 10 and 11 to add a CSS property to animate, and then jump back to steps 7– 9 to set the duration, delay, and timing function for that property. 7. Type a number in the Duration box and select either “s” (for seconds) or “ms” (for milliseconds) from the menu to the right. The duration specifies how long the animation takes. While it may seem cool to slowly change the background color over the course of 10 seconds, most web surfers are impatient and don’t want to wait to savor your carefully choreographed animation. A good value is .25 seconds (250 milliseconds)—that speed is fast enough for anxious visitors, but slow enough so they can see it. However, you may want to try .5 seconds (500 milliseconds) or longer to see how the effect looks. As mentioned in step 6, you can set different timings for each property, so you might want one animation to happen really quickly (like a change in the text size), but another (like a change in background color) to happen more slowly.
Chapter 8: Advanced CSS
409
CSS Transitions
Note Don’t leave the Duration box blank. Otherwise, the transition takes place immediately, with no
animation.
8. Type a number in the Delay box and select either “s” (for seconds) or “ms” (for milliseconds) from the menu to the right. The delay value indicates how long the browser should wait before starting the animation. In general, it’s best to type 0, since waiting for any significant amount of time before starting the animation might confuse visitors. In addition, an animated transition reverses itself the moment the “trigger” stops: In other words, if a visitor mouses over a link, the animation begins, but when he moves the mouse off the link, the animation stops (even if the animation hasn’t ended) and goes back to the original CSS property. If you add a long delay, then, your visitors may never see the animation. 9. Choose an animation speed from the Timing Function drop-down menu. While the duration setting indicates how long the animation takes from start to finish, the timing function (also known as an “easing” function) controls the rate of change in an animation. Normally, a CSS transition doesn’t progress at the same speed for the entire animation. That is, if you’re changing the background of a link from blue to orange, the background color will start moving toward orange slowly at first; more quickly in the middle, reaching a more orangish color rapidly; and then slowly turning solid orange at the end. Different timing functions specify different rates of change. You can get some interesting and more “natural”-looking animations with different options: • The cubic Bezier setting is the most complex. It requires four numbers that plot four “control points” along a line. Each point is a number from 0 to 1. In reality, all the timing functions follow what’s called a Bezier curve that plots the amount of visual change over time (if you’ve worked with a drawing program, you’ve probably used Bezier “handles” to add curves to lines). The math isn’t worth explaining (that’s just our way of saying it’s too confusing even for us), so you’re better off using an online tool to create and test different settings. The “CSS cubic-bezier Builder” tool at http:// tinyurl.com/bljzsxl is particularly good. It lets you drag the control handles around and test how the animation changes over time. Once you find a curve you like, just note the four numbers and type them into the Timing Function box: for example, cubic-bezier(.17, 1, .78, .42). • The ease option is the normal way a transition animates: slowly at first, rapidly in the middle, and then slowly at the end. • The ease-in setting begins slowly at first, then picks up speed to finish the animation rapidly.
410
Dreamweaver CS6: The Missing Manual
• The ease-in-out option begins very slowly, proceeds very rapidly in the middle and ends very slowly.
CSS Transitions
• The ease-out option begins very quickly, and then ends slowly. • The linear option provides a smooth animation, where every step in the show represents the same amount of progress. There’s a reason that this setting isn’t the default for animations…it’s just boring. Tip The best way to pick a timing function is simply by experimenting. Try different settings and see which you like best. You can always go back and edit the setting, as described in Figure 8-18.
Remove selected transition Edit selected transition
Figure 8-18
To edit a transition, simply select the triggering event (hover, active, and so on) in the CSS Transitions panel, and then click the edit button. Deleting a transition is a bit more complicated, as explained next.
10. From the Property menu (Figure 8-17) pick the CSS Property you want to animate. While the list is very long, there are a few properties that work particularly well. Any property that uses color, like the background-color, border-color, and color properties, are good candidates; positioning properties like left, right, top, and bottom (used with absolutely positioned elements, described in Chapter 9) also look great when animated, as do the width and height properties. Some animated properties don’t look very good or don’t work at all in most browsers. For example, while Chrome can animate a change in background-image, no other browsers can, and animating font-weight (from normal to bold, for example) doesn’t work in any browser.
Chapter 8: Advanced CSS
411
CSS Transitions
You should select a CSS property that you specified in the original selector you chose for step 4. For example, if you want to animate the background color of an element, make sure you first give the element a background color before you add a CSS transition to it. While it’s possible to animate a property without first adding that property to the beginning style (the Target selector), it sometimes produces weird results. 11. Set the value for the property in the End Value box. What you add to this box depends on what property you selected. If you pick the background-color property, for example, Dreamweaver displays a color picker box; either select a color from it or type a color value into the box (see page 160 for more on CSS colors). If you pick a property that requires a numeric value, like width, height, margin-left, and so on, Dreamweaver displays two options: a box to enter a value and a menu for selecting a measurement unit, like px (pixels), em, or % (see page 154 for more on CSS measurement units). If, in step 6, you chose to set a different transition for each property, go back to steps 7– 9 to set the timing options for this particular property. 12. Continue to add additional properties following steps 10 and 11. You don’t need to animate every CSS property applied to an element. Trying to add too many visual changes at once not only slows down the browser, it also probably distracts your audience. One or two animated changes is often enough to capture people’s attention. 13. From the “Choose Where to Create the Transition” menu, select which style sheet you want to add the transition information to. CSS transitions are really just CSS styles, so you store them in either an internal or external style sheet. This is the same choice you make when creating any CSS style, as described on page 128. In general, it’s best to store the style in the same external style sheet you use for all the styles in your site. 14. Click Create Transition. Dreamweaver closes the New Transition window, adds the required CSS to the page’s style sheet, and lists the animation in the CSS Transitions panel (see Figure 8-18). Note If you choose to create a CSS transition using a class style as the target (step 4 on page 408), Dream-
weaver adds the class name to the HTML element currently selected (or where your cursor is currently positioned). This can be a bit disorienting since, if you aren’t paying attention and simply create a CSS transition, Dreamweaver will apply that class to the current element, possibly significantly altering the look of that tag. Simply remove the unwanted class from the tag (see page 137); this won’t remove the CSS transition, so it will still work for the elements you intended.
412
Dreamweaver CS6: The Missing Manual
Understanding the CSS Transitions Panel Dreamweaver’s CSS Transitions panel isn’t completely straightforward. There are three parts to each transition listed in the panel: the target selector, the triggering event, and a list of matched elements. On the far left are the CSS selectors—they represent the selectors the transition targets, and they’re the same as the targets you selected in step 4 on page 408, when you created the transition. For example, in Figure 8-18 you can see .imageRight, .nav a, and .footer. Those are three selectors: two class selectors and a descendent selector (.nav a), which targets links inside another element that has the class .nav applied to it.
CSS Transitions
Next, the action that triggers the transition appears indented underneath each selector. For example, the .imageRight class has a hover transition, as do all the links inside another page element with the class .nav. You can have more than one trigger listed for a target. For example, you could create a transition that appears when a visitor mouses over an element and a different transition when a visitor clicks the link (or makes it active): That’s the case for the .footer element in Figure 8-18. Indented below the trigger event is a list of the elements that match the target selector. While the list of target selectors and triggers are defined in the style sheet the page uses, the elements list is specific to each page. For example, in Figure 8-18, Dreamweaver lists the hover transition for the .imageRight class selector as “[Not applied].” That means that Dreamweaver won’t apply the transition to any element on the page because no HTML tag has the .imageRight class applied to it; there may be a tag with that class applied to it on another page that uses the same style sheet, but not on this page. The same transition may also apply to more than one element on the page. This is common when you apply a CSS transition to each button in a navigation bar. In Figure 8-18, for example, the panel states that there are “[3 instances]” of the button transition on the page, meaning three links on this page use the same transition. Likewise, you see td.footer and div.footer listed under both the active and hover triggers of the .footer selector. This means that there is one
tag and one tag with the .footer class applied to them, and both of those elements have a CSS transition for hover and active states. If you apply the .footer class to additional elements on the page, Dreamweaver lists those as well.
Deleting a CSS Transition Deleting a CSS transition is more complicated than you might think. As discussed in the box on page 416, Dreamweaver acts on two styles each time you add a transition: the initial target selector (the style before the animation begins) and the finished state (a pseudo-class like :hover or :active [see page 206]). So when you delete a CSS transition, Dreamweaver can immediately tell which styles you wish to edit.
Chapter 8: Advanced CSS
413
CSS Transitions
To remove a CSS transition: 1. Select the target selector (for example, .imageRight , .nav a , or .footer) and click the minus-sign icon (a.k.a. the Remove Transition button). Dreamweaver opens the Remove Transition window (Figure 8-19). If you have more than one trigger event (like hover and active) on the same element, you can select the trigger to remove just that transition (active, for instance), and leave the other one alone (the hover transition, for example). If there are two trigger events and you choose the target selector, you can delete all the transitions for that selector.
Figure 8-19
The trick to removing a transition is figuring out exactly what you want to remove.
2. In the Remove Transition window, turn on the checkboxes that tell Dreamweaver what you’d like to remove. Here’s the confusing part about deleting a transition. Dreamweaver gives you several options, which have different effects. • The Remove Target rule option relates to the style that controls the appearance of the target element—that is, the style that defines the look of the element before the transition begins. As mentioned in the box on page 416, that initial style also contains the CSS transition property—the CSS property that tells a web browser that you want to animate a change in the CSS. Choose the “Transition properties” radio button if you simply want to remove the CSS transition property but leave the rest of the style untouched. You’ll usually select this option, since the style will also contain properties
414
Dreamweaver CS6: The Missing Manual
you’d like to preserve, like the font, font-size, background-color, margin, padding, and so on. However, if you really want to completely remove a style, formatting and all, turn on the “Entire rule” radio button. Just keep in mind that this removes all the formatting applied by that style to the target element.
CSS Transitions
• Turn on the Remove Transition On rule(s) checkbox to delete the pseudoclass style (:hover, :active, and so on) that Dreamweaver added to your style sheet. This removes the end result of the animation—the style that formats the element when a visitor interacts with it (by hovering over the element, for example). Leave this checkbox turned off if you simply want to remove the animation. For example, you may find that you like the abrupt change in background when a visitor mouses over a link and you prefer to not have a smooth animated change. If that’s the case, turn on the “Remove Target rule” checkbox, and select the “Transition properties” button, but leave the “Remove Transition On rule(s)” and the “Remove class from element(s)” checkboxes turned off. • The Remove class from element(s) checkbox appears only if you apply a transition to a class selector (.imageRight, for example). Turning on this checkbox simply removes that class from any HTML tag on the page, so use this checkbox when you want to completely remove all traces of a transition, including the styles and classes used in it. However, if you’re keeping the initial class style and its formatting (but not its transition properties), don’t turn on this checkbox—that way, you’ll leave the class on the HTML tag, and the remaining styling information will still apply to that element. So, in a nutshell, if all you want to do is remove an animation, turn on the “Remove Target rule” checkbox, turn on the “Transition properties” radio button, and leave all the other checkboxes turned off. If you want to erase everything Dreamweaver added, turn on the “Remove Target rule” checkbox, select the “Transition properties” button, and turn on the “Remove Transition On rule(s)” checkbox. 3. Click Remove. The Remove Transition window closes and Dreamweaver makes the requested changes, altering styles and the HTML where needed. Because Dreamweaver might make several changes at this point, you can’t simply choose Edit→Undo to reverse the Remove Transition action, so be careful when you delete a transition.
Chapter 8: Advanced CSS
415
CSS Transitions Power Users’ Clinic
Behind the Scenes of CSS Transitions Dreamweaver CS6’s CSS Transitions panel makes it easy to create complex animated transitions. Behind the scenes, however, Dreamweaver generates quite a bit of CSS. As you’ve read, a CSS transition is an animation between changes in CSS properties. You define those properties in two separate styles, the first defines the beginning of the animation, and the second specifies the final set of properties at the end of the animation. For example, creating an a tag style for the tag sets the style for links on a page, while an a:hover style sets the appearance of links when a visitor mouses over them. When you create a CSS transition and set the Transition On property to “hover” (see step 5 on page 408), Dreamweaver creates a new style with the :hover pseudo-class for whatever selector you chose in step 4 on page 408. For example, say you had an image on the page with the class .imgBorder applied to it. You want to add a cool animation that changes the border color around that image when a visitor mouses over it. By following the steps starting on page 406, you add a new CSS transition. Dreamweaver creates a new style named .imgBorder:hover with the new border color you specified. The CSS for that would look something like this: .imgBorder:hover { border-color: #457348; }
By itself, this new rule doesn’t have any special CSS transition magic at all. It simply changes the border color when someone mouses over that image. To get the animation effect, Dreamweaver has to add another CSS property to the original style—in this example, to the .imgBorder class style. That’s
how CSS transitions work: You add the transition property to the starting point for the animation, the style that formats the element when the page first loads. That way, a web browser knows that it needs to animate the changes from this style to the :hover style. Unfortunately, because the transition property hasn’t yet been finalized by the W3C (the group that determines how CSS works), each browser requires its own special version of the transition property, which is preceded with a special prefix, -ms for Internet Explorer, -webkit (Safari and Chrome), -moz (Firefox), and -o (Opera). That means that to add a CSS transition, Dreamweaver actually has to write 5 lines of CSS, not just one: .imgBorder { border: 10px solid #758837; -webkit-transition: border-color .5s ease-out; -moz-transition: border-color .5s easeout; -ms-transition: border-color .5s easeout; -o-transition: border-color .5s easeout; transition: border-color .5s ease-out; }
Thankfully, Dreamweaver takes care of all this coding for you.
Another Way to Create CSS Transitions Dreamweaver CS6 adds yet another way to create CSS transitions, though this method requires that you know how CSS transitions really work (to find out, read the box above). To animate CSS transitions, you use the CSS3 Transition property. Dreamweaver CS6 lets you add that property when you create a style using the CSS Rule Definition window (see Figure 8-20).
416
Dreamweaver CS6: The Missing Manual
CSS Transitions
Figure 8-20
You can use the CSS Rule Definition window to add a CSS transition property to any element. You’ll also need to create another style that triggers the transition, like a :hover pseudo-class style.
To create a transition using the Transition property: 1. Create a style to format the element whose transition you want to animate. For example, a link in a navigation bar, a form field, and so on. This style represents the beginning of the animation. (See page 125 for a recap on how to create a style.) 2. Make sure the Rule Definition window is open for that style and select the Transition category. You can add a transition after you create a style—just edit the existing style by double-clicking its name in the CSS Styles panel. 3. Choose which property you wish to animate by turning off the “All animatable properties” checkbox, clicking the + button, and then selecting a CSS property (like background color or height). Of course, instead of picking which property to animate, you can leave the “All animatable properties” checkbox turned on. This tells a browser to animate any changes in style properties. This may be what you’re after, but in many cases you’ll only want one property to change. For example, say you have a link with a background color, a font color, and a 1-pixel border. You then create a :hover pseudo-class style that sets different background and font colors and a different size border. If you want the browser to animate both color and border changes, then you’d leave the “All animatable properties” box turned on. But if you want only the background color animated, you’d turn off the checkbox, click the + button, and then select background-color from the menu.
Chapter 8: Advanced CSS
417
Using CSS3
4. Type a Duration and Delay amount, and pick a timing function. These settings are described in steps 7–9 on page 409–410. 5. Repeat steps 3–4 to add more CSS properties, and then click OK to complete the style. Dreamweaver writes the necessary CSS code to add a transition to the style—but you’re not done yet. There won’t be any animation until you also add a style that defines the formatting for the end of the transition. Dreamweaver’s New Transition window (Figure 8-17) handles this part automatically, but when you create a transition using the Rule Definition window, you need to define this style yourself. 6. Click the New CSS Rule button on the CSS Styles panel. The New CSS Rule window appears, and you’re ready to create a second style. 7. In the New CSS Rule window, choose Compound from the Selector Type menu, and type a name with a pseudo-class in the Selector Name box. Since CSS transitions only animate a change in styling, you must use one of the handful of CSS pseudo-classes that dynamically change an element. These include the same options mentioned on step 5 on page 408, hover, active, focus, target, checked, disabled, enabled, and indeterminate. Hover is the most common. The name of the selector should match the one for the style you created in step 1 above. Say, for example, the original style is for a descendent selector for all the links inside an element that has the class .nav applied to it. That style would be named .nav a. To create a style to format those links when the mouse hovers over them then you’d create a style named .nav a:hover. (Note that there’s no space between the “a” and the “:hover.”) To create a style for the active state of those same links, you’d name it .nav a:active. 8. Add the properties you want using the Rule Definition window and then click OK to complete the style. Now you have two styles—the starting style that has the CSS transition property, and the hover (or active or focus, and so on) style. You’re done!
Using CSS3 The web continually evolves—not only do new websites appear (Facebook, YouTube, Twitter), but the fundamental technologies used to build websites change and grow. Web designers used to use HTML tables to lay out pages; now we use CSS. The web design world today is abuzz with the next incarnation of the CSS standard: CSS3. CSS3 adds amazing new design effects to websites, such as drop shadows for text or page elements, animated effects, rounded corners on images, gradient backgrounds, and more (see a list of the most popular CSS3 properties on page 421). 418
Dreamweaver CS6: The Missing Manual
But there are drawbacks to CSS3. First, it’s a work in progress; the W3C is still hammering out the list of CSS3 properties and how they work. As a result, browser support for CSS3 varies significantly. Internet Explorer 6 was created long before CSS3, and even Internet Explorer 9 doesn’t understand many aspects of the standard. Some browsers don’t support any of the properties proposed in CSS3. In addition, to get some CSS3 properties to work in the browsers that do support them, you have to use special “browser vendor prefixes.” For example, the box-shadow property adds a drop shadow to page elements (like a sidebar, div, or headline). You’d use this CSS3 code to add a box shadow:
Using CSS3
box-shadow: 2px 3px 4px #000;
But to get a box shadow in Safari and Chrome browsers, you have to write that same code like this: -webkit-box-shadow: 2px 3px 4px #000;
And for Firefox, you’d write this: -moz-box-shadow: 2px 3px 4px #000;
In other words, to add a drop shadow to an element, you have to write three lines of code, and even then it won’t work in Internet Explorer 8 or earlier! Because the CSS3 standard isn’t complete and doesn’t work universally across browsers, Dreamweaver doesn’t support it in the same way that it supports the W3Csanctioned CSS 2.1 standard. CSS 2.1 has wide support among web browsers—even Internet Explorer 6 understands most of its properties—and Dreamweaver’s CSS Rule Definition window reflects this. You can find most of the CSS 2.1 properties in that window, but you won’t find any of the new CSS3 properties there. That’s not to say that Dreamweaver CS6 doesn’t support some of those properties. You already used two of them, web fonts and CSS transitions. Other CSS3 properties are available in the Properties pane; see the next section for details. Tip If you want to know which browsers support which CSS properties, visit the “When can I use…” website at http://caniuse.com/. It’s an invaluable resource for web designers.
The Properties Pane As you read on page 382, you can add CSS properties to a style using the Properties pane: Click the Add Property link and select a property from the drop-down list. Dreamweaver CS6 includes CSS3 properties in this list. As with CSS 2.1 properties, you simply select a CSS3 property from the list, and then type a proper value in the box to the right. Unfortunately, you need to know what the proper value is, and for many CSS3 properties, Dreamweaver doesn’t help you out (see the end of this section for links to resources on CSS3). However, for some of the most commonly used CSS3 properties, Dreamweaver provides a bit more help in the form of pop-up dialog boxes. For example, if you add the text-shadow property, a small icon (circled
Chapter 8: Advanced CSS
419
Using CSS3
in Figure 8-21) appears in the Value column; click it and a dialog box appears. Fill out the three boxes to define the placement and appearance of the drop shadow, and to pick a color for it. Dreamweaver provides similar functionality for the box-shadow, border-image, and border-radius properties.
Figure 8-21
While Dreamweaver doesn’t provide much assistance in using most CSS3 properties, it does give you an easy-to-use and -understand dialog box for a handful of CSS3 properties, like text-shadow, pictured here.
Code Hinting Dreamweaver also includes CSS3 code hints. You read about code-hinting in the last chapter, but in a nutshell, it’s a helpful aid for designers who like to type their HTML, CSS, and JavaScript by hand. As you type a CSS property into a style sheet, Dreamweaver lists properties that match as you type away. For example, type co and Dreamweaver lists color, column-count, and other CSS properties that begin with co. In Figure 8-22, for instance, after typing -webkit-, Dreamweaver lists all WebKit-specific CSS3 properties. You can click on the property with your mouse or, even better, use the up and down arrows on your keyboard to select the desired property; hitting Return makes Dreamweaver type the rest of the property for you. That’s a great way to cut down on typing and increase your productivity. Unfortunately, code-hinting is really a power user’s tool: You need to know how the CSS3 property works to correctly fill out the required value.
420
Dreamweaver CS6: The Missing Manual
Using CSS3
Figure 8-22
Dreamweaver’s code-hinting feature is a time- and finger-saver. As you type a few letters of a CSS property, Dreamweaver opens a window that lists matching properties. You can select a property from the list and let Dreamweaver type the rest. This feature works with HTML tags and JavaScript programming as well.
Common CSS3 Properties Although CSS3 is still in development, a range of browsers support a limited set of its properties. Many web designers have begun testing the CSS3 waters and include judicious amounts of CSS3 in their pages. Some of the most exciting (and supported) properties are: • Text-shadow. Adds a drop shadow to headlines or any other text. Unfortunately, no version of Internet Explorer (even IE 9) supports this property, but it works in Safari, Firefox, Chrome, and Opera without a special vendor-specific prefix (like -webkit or -moz). You can use Dreamweaver’s Properties pane to add textshadow to a style (see Figure 8-21). For more on the text-shadow property and a useful tool for previewing its effect, visit http://tinyurl.com/cxxhavg. • Box-shadow. Adds a drop shadow to any HTML element, like a sidebar, banner, or any box on a page. You can even add a rectangular shadow around headlines and paragraphs of text. All major browsers (except IE 8 and earlier) support this property. Unfortunately, like many CSS3 properties, you’ll need to use special prefixes to make this work in some browsers (for example, you need to specify
Chapter 8: Advanced CSS
421
Advanced CSS Tutorial
-webkit-box-shadow instead of box-shadow for version 5 of Safari and early versions of Safari for iOS). Visit this useful tool for previewing box-shadow effects: http://tinyurl.com/c2h2rrv. • Border-radius. If you’ve ever struggled trying to create rounded corners using multiple background images, this CSS3 property is for you. The border-radius property lets you round the corners of any HTML element—you can even round just one, two, or three corners, or provide different radii for each. Again, IE 8 and earlier can’t take advantage of this fun property, and you need to provide additional properties for this to work in Safari, Firefox, and Chrome. Visit the Hands-On CSS Border Radius Generator at http://tinyurl.com/bpkamka. • Gradient. CSS3 lets you add a gradient to the background of any element, so you could, for example, create a background that fades from white to dark gray without using graphics. You can create either linear (top to bottom or left to right) or radial gradients (circular from the center outward). Internet Explorer doesn’t support this property, and the other major browsers require different property names for it. For a great tool to help you preview and write the required CSS, visit http://www.colorzilla.com/gradient-editor/. • Transforms. Have you ever wanted to rotate an element on a page—perhaps make a tag look like a piece of paper that’s been set down askew on a table top? Well, even if you haven’t, you can! Thanks to CSS3 you can rotate, skew, and scale an element with the Transform property. You’ll need vendor-specific prefixes for Internet Explorer 9 (-ms-transform), Safari and Chrome (-webkittransform), Firefox (-moz-transform), and Opera (-o-tranform). Of course, IE 8 and earlier are left out of the party. To learn how to use this property and test it out, visit http://tinyurl.com/d8dle98.
Advanced CSS Tutorial In this tutorial, you’ll create a descendent selector, and use Dreamweaver CS6’s new CSS Transitions tool. Tip You need to download files from www.sawmac.com/dwcs6 to complete this tutorial. See the Note on
page 54 for details.
Once you download the tutorial files and open Dreamweaver, set up a new site as described on page 40. In a nutshell, choose Site→New Site. In the Site Setup window that appears, type Adv CSS Tutorial into the Site Name field. Click the folder icon next to the Local Site Folder field, navigate to and select the Chapter08 folder inside the MM_DWCS6 folder, and then click Choose (Select). Finally, click OK.
Creating a Descendent Selector Once again, you’ll be working on a page for the good people who run Cafe Soylent Green. 422
Dreamweaver CS6: The Missing Manual
Advanced CSS Tutorial
1. Choose File➞Open. You’ll work on a page similar to the ones you built in Chapter 5. 2. Navigate to the Chapter08 folder and double-click the file index.html. You can also double-click the index.html file in the Files panel to open it. You’ll notice a simple navigation bar in the upper-right corner of the page. These links would look a lot better as buttons, so you’ll add a style to make that happen. 3. Make sure you have the CSS Styles panel open (Window➞CSS Styles), and then click the New Rule button in the bottom-right of the panel. The New CSS Rule window opens (see Figure 8-23).
Figure 8-23
You can create any type of CSS selector, like the descendent selector—.nav a—pictured here by first selecting “Compound” from the Selector Type menu and then typing a selector name. You can even use this option to create basic tag, class, and ID styles.
You follow the same process you do to create any style (see page 125 if you need a recap). You want to style these links (navigation links in this example), but creating an tag style would affect every link on the page (in this case, the links at the bottom of the page, and those associated with the two images). A better option is a descendent selector that targets just the navigation links (About Us, Menu, and Home). In this case, the links are actually bulleted list items that have a bit of styling already. The outer tag has a class applied to it: . This class name is all you need to create a style that affects just the links inside the bulleted list.
Chapter 8: Advanced CSS
423
Advanced CSS Tutorial
4. Choose Compound from the Selector Type menu, type .nav a in the Selector Name box, and than choose “styles.css” from the bottom menu. The New CSS Rule window should look like Figure 8-23. The selector you just typed—.nav a—is a descendent selector. The element on the far right of the selector, “a”, is the target. The selector to the left, .nav, is an element that wraps around the tag. Time to create the sty |