Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
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 Quick Starts /

Styling components

by Adobe

Adobe logo

Created

22 March 2010

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flex

Requirements

User level

All

Required products

  • Flex (Download trial)

Styles are useful for defining the look and feel (appearance) of your Adobe Flex applications. You can use them to change the appearance of a single component, or apply them across all components.

There are many ways to apply styles in Flex. Some provide more granular control and can be performed programmatically. Others are not as flexible, but can require less computation. In Flex, you can apply styles to controls in the following ways:

  • Using local style definitions
  • Using external style sheets
  • Using the setStyle() method

Flex does not support controlling all visual aspects of a component with Cascading Style Sheets (CSS). Properties such as x, y, width and height are properties, not styles, of the UIComponent class and, therefore, cannot be set in CSS. You must also be aware of which properties your theme supports. The default theme in Flex does not support all style properties.

A theme is a set of styles that define the look and feel of a Flex application. A theme can define something as simple as the color scheme or common font for an application, or it can be a complete reskinning of all the components used by the application. Themes usually take the form of a SWC file. However, themes can also be a CSS file and embedded graphical resources, such as symbols from a SWF file.

Note: For more information on supported style properties in themes see "About themes" in the Flex 4 documentation. To determine the styles that a specific visual component supports, see the styles section of the component in ActionScript 3 Reference for the Adobe Flash Platform.

Using local style definitions

You can create local style definitions in your MXML files using the <fx:Style> tag. This tag contains style sheet definitions that adhere to the CSS 2.0 syntax. These definitions apply to the current document and all children of the current document.

The following example creates two class selectors, .fontStyle and .backgroundColor. The BorderContainer , Button, and one of the Labels apply the styles using their styleName properties. As this example shows, using class selectors lets you give different styles to instances of the same component.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- StylesStyleTag --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="150" height="140"> <s:layout> <s:VerticalLayout paddingTop="10"/> </s:layout> <fx:Style> @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; .fontStyle { font-family : Arial, Helvetica, sans-serif; font-weight : bold; color: maroon; } .backgroundColor { background-color: #ffffcc; } </fx:Style> <s:BorderContainer styleName="backgroundColor" width="100%" height="100%"> <s:layout> <s:VerticalLayout gap="10" horizontalAlign="center" paddingTop="10"/> </s:layout> <s:Label text="Unstyled Text"/> <s:Label text="Styled Text" styleName="fontStyle"/> <s:Button styleName="fontStyle" label="Submit"/> </s:BorderContainer> </s:Application>

Tip: If you want all instances of a certain component to share the same style, you can use a CSS type selector. For example, by using the following type selector, all BorderContainer instances in your application will have a background color of blue.

BorderContainer{ backgroundColor: #ccffff; }

Result

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.

Back to top

Using external style sheets

Flex supports external CSS style sheets. To apply a style sheet to the current document and its child documents, use the source property of the <fx:Style> tag. External style sheet files should be in the folder that contains your MXML source files. By default, this is the src folder in your MXML project.

Note: You should try to limit the number of style sheets used in an application, and set the style sheet only at the top-level document in the application (the document that contains the <s:Application> tag). If you set a style sheet in a child document, unexpected results can occur.

The following example defines two CSS class selectors in an external CSS file called external.css. You use an external CSS file in a Flex application by specifying its path and file name in the source property of the <fx:Style> tag.

Example:

External CSS file

/* CSS file */ @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; .fontStyle { font-family : Arial, Helvetica, sans-serif; font-weight : bold; color: blue; } .backgroundColor { background-color: #ccffff; }

MXML file

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="150" height="140"> <!-- StylesExternal.mxml --> <s:layout> <s:VerticalLayout paddingTop="10"/> </s:layout> <fx:Style source="styles/external.css"/> <s:BorderContainer styleName="backgroundColor" width="100%" height="100%"> <s:layout> <s:VerticalLayout gap="10" horizontalAlign="center" paddingTop="10"/> </s:layout> <s:Label text="Unstyled Text"/> <s:Label text="Styled Text" styleName="fontStyle"/> <s:Button styleName="fontStyle" label="Submit"/> </s:BorderContainer> </s:Application>

Result

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.

Back to top

Using the setStyle() method

Use the setStyle() method to manipulate style properties on instances of controls in ActionScript. This method of applying styles requires a greater amount of processing power on the client than style sheets. However setStyle() provides more granular control over how styles are applied.

The setStyle() method takes two arguments: the style name and the style value.

Tip: When you are instantiating an object and setting the styles for the first time, you should try to apply style sheets rather than use the setStyle() method, which is computationally expensive. You should use this method only when you are changing an object's styles during run time.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- StylesSetStyle.mxml --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="160" height="160" creationComplete="application_creationCompleteHandler(event)"> <s:layout> <s:VerticalLayout paddingTop="10"/> </s:layout> <fx:Script> <![CDATA[ public function application_creationCompleteHandler(e:Event):void { styleManager.getStyleDeclaration("global").setStyle("fontSize", 14); styleManager.getStyleDeclaration("global").setStyle("color", "blue"); styleManager.getStyleDeclaration("spark.components.BorderContainer").setStyle("backgroundColor", "#ffddff"); } ]]> </fx:Script> <s:BorderContainer width="100%" height="100%"> <s:layout> <s:VerticalLayout gap="10" horizontalAlign="center" paddingTop="10"/> </s:layout> <s:Label id="myLabel" text="Styled Text"/> <s:Button id="myButton" label="Submit"/> </s:BorderContainer> </s:Application>

Result

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.

Back to top

For more information

  • Styles and Themes

Back to top


This work is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License.

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps

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