|
| 1 | +<script>{ |
| 2 | + "title": "Stacking Elements", |
| 3 | + "termSlugs": { |
| 4 | + "category": [ "theming" ] |
| 5 | + } |
| 6 | +}</script> |
| 7 | + |
| 8 | +Widgets that stack, or move in front of other elements, often present challenges |
| 9 | +when placed into real world pages. It's usually easy to either change the `z-index` |
| 10 | +or parent of the stacked element to avoid any collisions on the page. However, |
| 11 | +jQuery UI needs a generic solution that doesn't require manually playing with |
| 12 | +`z-index` values. This is accomplished via the `ui-front` class, and usually an |
| 13 | +accompanying `appendTo` option on stacking widgets. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## The `ui-front` class |
| 18 | + |
| 19 | +The `ui-front` class is very basic. It just sets a static `z-index` value on the |
| 20 | +element. However, the existence of the class is used to indicate where stacking |
| 21 | +elements should be appended. This allows us to take advantage of nested stacking |
| 22 | +contexts, resulting in a default DOM position that works for most use cases. |
| 23 | + |
| 24 | +*Note: When using `ui-front`, you must also set `position` to `relative`, |
| 25 | +`absolute` or `fixed` in order for the `z-index` to be applied.* |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +## The stacking technique |
| 30 | + |
| 31 | +Any widget that appends a stacking element to the page must use the `ui-front` |
| 32 | +class, and in many cases should have an `appendTo` option. The following logic |
| 33 | +should be applied to the stacking element: |
| 34 | + |
| 35 | +* If a value is provided for the `appendTo` option, then append the stacking |
| 36 | +element to the specified element. |
| 37 | +* If the `appendTo` option is set to `null` (default), then the widget should |
| 38 | +walk up the DOM from the associated element. For example, when the autocomplete |
| 39 | +menu is appended to the DOM, the walking starts from the associated input element. |
| 40 | + * If an element with the `ui-front` class is found, append to that element. |
| 41 | + * If no element with the `ui-front` class is found, append to the body. |
| 42 | +* The stacking element must also have `position` set to `relative`, `absolute`, |
| 43 | +or `fixed` in order for the `z-index` from the `ui-front` class to be applied. |
| 44 | +Using [.position()](/position/) will automatically set `position`. |
0 commit comments