Developer Network Home - Help

Yahoo! UI Library: Overlay

Yahoo! UI Library: Overlay

The Overlay control is an extension of Module; its role is to facilitate the creation of modular content that is absolutely positioned above the flow of a page. It adds additional functionality to Module, including methods for positioning, multiple custom events for monitoring internal property changes, and a built-in IFRAME solution for dealing with SELECT element bleed-through in Internet Explorer.

Note: Overlay is a building block for other UI controls and does not in and of itself solve a particular interaction problem; if you're looking for a specific implementation of Overlay, check out the Tooltip, Panel, Dialog and SimpleDialog controls.

Getting Started

To use Overlay, include the following code in your page:

If you are not using Tooltip, Panel, Dialog, or SimpleDialog, you may substitute container-min.js with the smaller container_core-min.js file:

Note that a CSS file is not required if you're only using container_core-min.js, however for the Overlay to operate as designed, it does need to be positioned absolutely. The yui-overlay class applied to all overlays can be used to define your own rule to apply position:absolute for all overlays. Alternatively you could include the container.css file mentioned above, but since it only has a single rule which applies to Overlays, it's overkill if you're aiming to optimize the amount of code required for your page.

YUI dependency configurator.

YUI Dependency Configurator:

Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.

Note: If you wish to include this component via the YUI Loader, its module name is container. (Click here for the full list of module names for YUI Loader.)

Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.

Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.

Using "CSS skins": One of the files listed above contains a CSS "skin" for the Overlay Control. For this skin CSS to work correctly, you will need to apply the CSS class "yui-skin-sam" to an element that is a parent of the element in which the component lives. You can usually accomplish this simply by putting the class on the <body> tag:

<body class="yui-skin-sam">

For more information on skinning YUI components and making use of default skins, see our Understanding YUI Skins article here on the website.

Using Overlay

This section describes common tasks for creating and using Overlay. It contains these sections:

Note that Overlay inherits its constructor and configuration, as well as several other important methods, from Module. See Module for more information on how to utilize Module's features.

Defining Overlay Markup

In its most basic form, the HTML markup for an Overlay looks like this:

Initializing Overlay

To instantiate an Overlay around a pre-existing piece of markup (as exemplified above), simply pass the id of your modular content to the Overlay constructor:

Positioning the Overlay

Overlay has several useful methods that can be used to position the Overlay, by either specifying specific pixel coordinates, or using special methods for contextually aligning or centering the Overlay.

To position an Overlay using pixel coordinates, use the x, y, or xy properties:

To center an Overlay manually, you can call its "center" method:

An alternative to manually centering the Overlay is to set its "fixedcenter" property to always keep the Overlay centered in the viewport:

To align an Overlay to another element on the page contextually, you can set the "context" property:

Rendering the Overlay

Once initialized, an Overlay is rendered in the same way as Module, by invoking the render() method. As with Module, if the Overlay was not created from markup you need to pass to the render method, the parent element to which the Overlay should be added.

Stacking the Overlay

One piece of core functionality provided by Overlay is support for a zIndex configuration property that enables it to be stacked above other elements on the page. The zIndex configuration property is an interface for setting the CSS z-index property, and understanding how the browser handles z-index values is therefore an important part of using the Overlay control and its subclasses.

A commonly asked question when working with Overlays is "Why is my Overlay rendered under DIV X, even though DIV X has a z-index of 10, and I've set the z-index of my Overlay to 9999?". Understanding stacking contexts will help you answer this question.

Understanding z-index and stacking contexts

Relatively and absolutely positioned elements can overlap other elements on the page. In situations where elements overlap, the browser uses the CSS z-index property to determine how overlapping elements are stacked on top of each other along the z-axis: Generally speaking elements with higher z-indices are rendered above elements with lower z-indices.

Consider the following simple markup structure:

In the example above, DIV A1 would be rendered on top of DIV B1 if they were to overlap. DIV A1 and DIV B1 both have the same stacking context, the root or body element, and are stacked relative to each other within this context based on their z-index values.

However, if DIV A and DIV B are either relatively or absolutely positioned, the scenario changes:

DIV B1 (z-index=10) will be rendered on top of DIV A1 (z-index=20), even though it has a lower z-index.

With different, positioned, parent elements, DIV A1 and DIV B1 no longer share a stacking context. As per the W3C layering spec, DIV A and DIV B establish stacking contexts since they are relatively positioned AND have a z-index defined. Therefore, when determining how to stack the elements the browser doesn't compare their z-index values relative to each other, but rather to other elements in the stacking contexts established by their separate parent elements.

Additionally, since DIV A and DIV B share a stacking context (body), they are stacked so that DIV B (z-index=2) is placed above DIV A (z-index=1), based on their z-index values. Furthermore, any elements which are children of DIV A will always be below DIV B and any of its children. No matter how high or low the z-indices of DIV A1 and DIV B1 are set, DIV B1 will always stack above DIV A1, because DIV B is stacked above DIV A.

As mentioned above, positioned elements with a specified z-index will establish new stacking contexts for their child nodes. This is true for all A-Grade browsers. However in IE even positioned elements that lack a defined z-index establish a new stacking context. This is considered to be an IE bug.

To completely avoid the potential impact of ancestor elements establishing new stacking contexts, render Overlays into the document's <body> element. Doing so will gaurantee that all Overlay instances exist within the same stacking context, making it easier to manage how they are stacked above all other elements in the page.

Configuration Properties

Overlay has the following configuration properties, which it passes on to all of its subclasses through inheritance:

Name TypeDefaultDescription
x Number null Sets the element's "left" style property.
y Number null Sets the element's "top" style property.
xy Array null Sets the element's "top" and "left" style properties.
context Array null Allows the Overlay to be anchored to a context element. The format is: [element, elementCorner, contextCorner], where "element" is an element or element id, and the corner parameters are either "tr" (top right), "tl" (top left), "br" (bottom right), or "bl" (bottom left).
effect Object / Object[] none Sets the ContainerEffect (one or many) that should be used when showing and hiding the Overlay.
fixedcenter Boolean false Specifies whether the Overlay should be automatically centered in the viewport on window scroll and resize.
width String null Sets the element's "width" style property.
height String null Sets the element's "height" style property.
zIndex Number null Sets the element's "z-index" style property.
constraintoviewport Boolean false If set to true the Overlay will try to remain inside the confines of the size of viewport.
iframe Boolean false (true by default for IE 6 and below) If set to true the Overlay will have and iframe behind it to prevent other elements with a higher z-index from poking through.

Overlay inherits the following properties:

Name Type Default Description
visible Boolean true Sets whether or not the Overlay is visible on the page (Overlay uses the CSS "visibility" property to control this).
monitorresize Boolean true Configures whether or not to create a hidden off-screen element that can be used to monitor for text size changes in the DOM.

Support & Community

The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.

In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.

Container Family Examples:

Other YUI Examples That Make Use of the Container Family:

More Reading about the YUI Overlay Control:

YUI Overlay on del.icio.us:

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings