Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center / Flex Test Drive /

Flex Test Drive: Change the appearance of your application

by Adobe

Adobe logo

Modified

27 June 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flash Builder Flex RIA

Video | Code | Tutorial | Links

Create a style sheet

 

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the TestDrive solution files (ZIP, 14 MB)

Code

FlexWebTestDrive.mxml

<?xml version="1.0" encoding="utf-8"?> <s:Application ...> <fx:Style source="assets/FlexWebTestDrive.css"/> (...) <s:Label id="xyzLabel" x="50" y="50" text="XYZ Corporation Directory"/> (...) <s:Button id="addBtn" styleName="actionButton" .../> (...) <s:ToggleButton id="toggleBtn" styleName="actionButton" .../> (...) </s:Application>

assets/FlexWebTestDrive.css

@namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; global { font-family: Arial; font-size: 12; chrome-color: #494949; symbolColor: #FFFFFF; } s|Application { backgroundColor: #E6E6E6; } s|Button, s|ToggleButton { color: #FFFFFF; cornerRadius: 5; fontWeight: bold; } s|Button:disabled { color: #000000; } .actionButton { chromeColor: #7091B9; } #xyzLabel { color:#FFFFFF; fontSize:24; fontWeight:bold; }

Tutorial

In the previous four modules, you learned to create, debug, and deploy a Flex application. In this module, you learn how to change the appearance of your application using styling and skinning.

With styling, you set component styles inline in MXML (as you already have) or preferably, in a style sheet using selectors (style rules). Each component has a limited number of styles defined for it. For example, for a Label you can set styles including font-size, font-family, and color. For a Button, you can also set a corner-radius style. If you want to change the appearance of a component more drastically then possible with a component's styles, you need to create or edit the associated component skin—the file specifying what the component should look like.

There are two families of components in the Flex framework: Spark and MX. The tags that start with s, like <s:Button>, are Spark components introduced in Flex 4 or later. The tags that start with mx, like <mx:Button> and <mx:PieChart> (which you will use in the next module), are the older Flex components. You set the appearance of MX components primarily using styling. The Spark components have been rearchitected to primarily use a skinning (rather than styling) model in which each component's associated skin file manages everything related to a component's appearance, including its graphics, its layout, and its states. Many but not all of the MX components have been rearchitected as Spark components. When both Spark and MX versions of a component exist, use the newer Spark version of the component.

In this tutorial, you learn to create a style sheet and define style rules that you apply to your application. In the following tutorials, you create and use component skins.

Step 1: Create a style sheet and a CSS global selector.

Return to FlexWebTestDrive.mxml in Design mode. Navigate to the Appearance view and change font and color styles (see Figure 1).

If you don't want to choose your own values, here are some you can use:
font-family: Verdana, font-size: 10, chrome-color: #494949.

Set global styles in the Appearance view.
Figure 1.Set global styles in the Appearance view.

Your components in the design area will reflect these new styles. Even though the global font-size is set to 10 pixels, the XYZ label is still large because it has a font-size style set inline in its MXML tag. Closer, or more specific style values take precedence.

Switch to Source mode. You will see a new Style tag below the Application tag.

<fx:Style source="FlexWebTestDrive.css"/>

In the Package Explorer, you should see a new file, FlexWebTestDrive.css. Open it. You will see a global CSS selector whose styles will be applied to all components.

/* CSS file */ @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; global { font-family: Verdana; font-size: 10; chrome-color: #494949; }

Run the application and see the different fonts and colors.

View the new font and color styles.
Figure 2. View the new font and color styles.

Step 2: Move the style sheet into an assets folder.

In the Package Explorer, create a new assets package (if it does not already exist). Drag FlexWebTestDrive.css and drop it in the assets folder. In the Move dialog box that appears, click OK. Return to FlexWebTestDrive.mxml and change the Style tag to use the new location.

Your code should appear as shown here:

<fx:Style source="assets/FlexWebTestDrive.css"/>

Step 3: Modify a CSS selector.

In FlexWebTestDrive.css,change the font-family to Arial, the font-size to 12, and add a symbol-color of white (#FFFFFF).

When typing styles, press Ctrl+spacebar to force Content Assist to pop up so you can select styles from this list (see Figure 3).

Use Content Assist when editing style sheets.
Figure 3.Use Content Assist when editing style sheets.

Save the file and then run the application. The font has changed again and the scroll arrows are now white.

Step 4: Create a CSS type selector.

In Design mode for FlexWebTestDrive.mxml, select one of the buttons and in the Properties view set its text to bold and white and change its corner radius to 5.Look at the generated code. Return to Design mode and click the Convert to CSS button (see Figure 4). In the New Style Rule dialog box, select Specific component (see Figure 5).

When you set styles in the Properties view, styles are set inline for the selected state.

<s:Button id="empBtn" color.Employees="#FFFFFF" cornerRadius.Employees="5" fontWeight.Employees="bold" .../>

To move the styles from the component tag to a style sheet, make sure the button component is selected and then click the Convert to CSS button in the Properties view (see Figure 4).

Convert inline styles to CSS.
Figure 4. Convert inline styles to CSS.

When you choose to create a selector for a specific component, a CSS type selector is created whose styles will automatically be applied to all component instances of this type—in this case, all Button controls.

Create a CSS type selector.
Figure 5. Create a CSS type selector.

Return to FlexWebTestDrive.css. You will see a new CSS type selector:

s|Button { color: #FFFFFF; cornerRadius: 5; fontWeight: bold; }

The s in front of Button is to specify that this is the style for Spark Buttons, not MX Buttons.

Return to FlexWebTestDrive.mxml in Design mode or run the application. All the buttons (except the Bigger Text ToggleButton) should now have rounded corners and bold, white text, but the disabled buttons are now difficult to read (see Figure 6).

See that disabled buttons are unreadable.
Figure 6. See that disabled buttons are unreadable.

Step 5: Create a CSS pseudo selector.

In FlexWebTestDrive.css, create a pseudo selector for the disabled state of Button components and set its color to black, #000000.

s|Button:disabled { color:#000000; }

Look at a component's API to find its defined states that can be used in the pseudo selectors (see Figure 7). Remember you can open a component's API by selecting Help > Dynamic Help, clicking a tag in MXML, and then clicking the API link in the Help view.

 Locate the states of a Button component in its API.
Figure 7. Locate the states of a Button component in its API.

Return to FlexWebTestDrive.mxml in Design mode or run the application. You should now be able to read disabled buttons (see Figure 8).

 Make the disabled buttons readable.
Figure 8. Make the disabled buttons readable.

Step 6: Style the ToggleButton controls.

In FlexWebTestDrive.css, use a comma-delimited list to use the same style rule for Button and ToggleButton instances.

The selector should appear as shown here:

s|Button, s|ToggleButton { color: #FFFFFF; cornerRadius: 5; fontWeight: bold; }

The BiggerText ToggleButton should now also have bold, white text and rounded corners (see Figure 9).

Style the ToggleButton controls.
Figure 9. Style the ToggleButton controls.

Step 7: Create a CSS class selector.

In Design mode, select the Add button and in the Properties view change its chrome-color to a blue color, like #7091B9 (see Figure 10). Click the Convert to CSS button. In the New Style Rule dialog box, select All components with style name and name the style actionButton (see Figure 11). Make this the style used by the Add button in all the application states.

 Use a class selector to style the Add button.
Figure 10. Use a class selector to style the Add button.
Create a CSS class selector called actionButton.
Figure 11. Create a CSS class selector called actionButton.

The new selector in FlexWebTestDrive.css should appear as shown here:

.actionButton { chromeColor: #7091B9; }

This class selector can be selectively applied to any component.

Return to Source mode in FlexWebTestDrive.mxml and locate the addBtn button. It now has a styleName property (for one state) set equal to the name of the class selector you just defined.

<s:Button id="addBtn" styleName.Employees="actionButton" .../>

To set it for all states, modify the source code to styleName="actionButton".

Step 8: Assign a CSS class selector to another component.

In Design mode, select the Bigger Text button in the Departments state. Select the actionButton style in the Properties view (see Figure 12).

Any styles that can be applied to that component will appear in the drop-down list.

 Assign a CSS class selector to the Bigger Text button.
Figure 12. Assign a CSS class selector to the Bigger Text button.

The ToggleButton tag should appear as shown here:

<s:ToggleButton id="toggleBtn" label="Bigger Text" styleName="actionButton" .../>

The actionButton style was selectively applied to this second button, which should now also be blue (see Figure 13).

 Use a class selector to style the Bigger Text button.
Figure 13. Use a class selector to style the Bigger Text button.

Step 9: Set the Application background color.

In FlexWebTestDrive.css, create a Spark Application selector and set the backgroundColor to light gray, #E6E6E6.

The selector should appear as shown here:

s|Application { backgroundColor: #E6E6E6; }

Save the file and run the application. The background color should now be light gray when the application loads and when it is displayed.

Step 10: Create a CSS ID selector.

In Source mode of FlexWebTestDrive.mxml, give the XYZ Label an id of xyzLabel and remove its color, fontWeight, and fontSize styles. In FlexWebTestDrive.css, create an ID selector for the Label using #xyzLabel and set the color to #FFFFFF, the font-weight to bold, and the font-size to 24.

Your Label should appear as shown here:

<s:Label id="xyzLabel" x="50" y="50" text="XYZ Corporation Directory"/>

 Your CSS ID selector should appear as shown here:

#xyzLabel { color: #FFFFFF; fontSize: 24; fontWeight: bold; }

Return to FlexWebTestDrive.mxml in Design mode or run the application and make sure the XYZ Label is large, white, and bold (see Figure 14).

 View the styled application.
Figure 14. View the styled application.

In this tutorial, you learned to create a style sheet and define CSS global, type, class, pseudo, and ID selectors. In addition to these selectors, you can also create component specific class selectors and descendant selectors. In the next tutorial, you will learn to change the appearance of a component more drastically by creating and using skins.

Learn more

Documentation: Using Flex 4.5

  • Styles and themes
  • Using Cascading Style Sheets
  • About style value formats
  • Applying styles
  • About style inheritance
  • Using the StyleManager class
  • Loading style sheets at runtime
  • Applying themes with Flash Builder

Flex Developer Center

  • UI design and RIA workflows

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Modify the database
  • Flex Test Drive: Add charts and graphs
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Change the appearance of your application
  • Flex Test Drive: Change the appearance of your application
  • Flex Test Drive: Add charts and graphs
  • Flex Test Drive: Modify the database

Tutorials and samples

Tutorials

  • Flex mobile performance checklist
  • Flex and Maven with Flexmojos – Part 3: Journeyman
  • Migrating Flex 3 applications to Flex 4.5 – Part 4

Samples

  • Twitter Trends
  • Flex 4.5 reference applications
  • Mobile Trader Flex app on Android Market

Flex user forum

More
07/25/2011 Flash Player Debug Issues - Safari 5.1 & Chrome 13
04/22/2012 Loader png - wrong color values in BitmapData
04/22/2012 HTTPService and crossdomain.xml doesn't work as expected
04/23/2012 Memory related crashes in Flex application

Flex Cookbook

More
04/06/2012 How to detect screen resize with a SkinnableComponent
02/29/2012 Embed Stage3D content inside Flex application components
02/15/2012 Custom WorkFlow Component
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement