Technique C44:Using CSS z-index to un-obscure focused content
About this Technique
This technique is not referenced from any Understanding document.
This technique applies to use this technique as a last-resort to un-obscure focusable user interface components. This technique will typically be useful when content (typically flow content) is obscured by pop-over elements.
Examples of persistent elements which may partially obscure content include:
- cookie consent banners
- expanded menus
- quick escape safety features
- chatbot windows
This technique can be used for all technologies that support Cascading Style Sheets (CSS).
Description
The objective of this technique is to ensure that focusable user interface components (such as links, buttons, and form fields) are always fully visible when focused. This includes cases where, when not receiving focus, the same user interface component is partially or completely obscured by other in-page elements.
This un-obscuring of focused components is achieved by applying a high CSS z-index value to the user interface component when it receives focus. The applied z-index value must be higher than the z-index values of all elements that might partially or fully obscure the focused user interface component.
Setting the CSS properties
This technique requires careful selection of which portions of content should receive the z-index property. If there is a chatbot button that can only obscure content in a sidebar, then only apply this technique to that sidebar.
Most scenarios can be solved by setting three CSS properties on the sections of the page which are overlapped by a pop-over element:
z-indexposition, tobackground-color, to improve readability
The z-index property must be set. The position properly only needs to be set for focusable components in the section which do not already have position values. The background-color property is only necessary for focusable elements which lack an existing background.
Set the z-index to a value higher than all other elements which may obscure the focusable components.
Examples
Here are examples of four different use-cases, paired with four variations of CSS property needs.
Example 1: focusable content with CSS background and position properties
This example is modelled on a "quick escape" tool which can at times obscure a sidebar navigation area. It sets a z-index value for HTML <a> links within the navigation sidebar.
For this example, we will say the "quick escape" safety feature has a z-index value of "1", and that it's a controlled value that is unlikely to change. Because of the stability of the z-index value, a relatively low value of "2" is enough to achieve the desired effect and un-obscure focused content.
.sidebar-navigation a:focus {
z-index: 2;
}
Example 2: Focusable content which lacks a CSS background property
This example is modelled on a chatbot window which pops over body content, potentially obscuring multiple focusable user interface components. The body content area has a white background. It expands on Example 1, demonstrating how to un-obscure focusable content when some of the content may lack a pre-set background. A distinct background property which provides good contrast with any text or image within the focusable content is essential to the success of this technique, in order to ensure the focused content is perceivable once unobscured.
For this example, we will say the current highest z-index value on the page is "9999", but that it's an uncontrolled value introduced by an external tool or plugin (in this case, a third-party chatbot pop-over). Because of the instability of the z-index values on the page, a universally-high value of "infinity" is one method to consistently achieve the desired effect.
.body-content :focus {
z-index: calc(infinity);
background-color: white;
}
Example 3: Focusable content which lacks a CSS position property
This example is modelled on a cookie consent banner which, at times, covers up portions of the footer. The footer content area has a "white" background.
This expands on "Example 1", demonstrating how to un-obscure focusable content when some of the content may lack a CSS position. Great care should be taken when setting a position value for an entire section of content, such as for all elements within a footer. This may override existing position properties. Setting a position value is essential to the success of this technique, because z-index only affects positioned elements.
For this example, we will say the current highest z-index value on the page is "1000000", and that it's an uncontrolled value introduced by an external tool or plugin (in this case, a third-party cookie consent banner). Because of the instability of the z-index values on the page, a universally-high value of "infinity" is one method to consistently achieve the desired effect. Additionally, in this example, some of the footer's focusable content already has set z-index values, which will need to be overridden.
footer :focus {
z-index: calc(infinity) !important;
position: relative;
}
Example 4: Focusable content which lacks consistent CSS background and position properties
This example is modelled on a large menu that doesn't close when unfocused. The menu pops over header links, as well as body and sidebar content. It can potentially obscure a large variety of focusable user interface components. The obscured content areas include several elements which lack a position or background property, as well as some elements with specific background and z-index values.
For this example, we will say the menu pop-over has a z-index value of "80", and that it's a controlled value that is unlikely to change. However, since the focusable content in this example has a variety of background and z-index values, care must be taken when setting those properties.
header a:focus {
z-index: 81 !important;
position: relative;
background-color: green;
}
.body-content :focus {
z-index: 81 !important;
position: relative;
background-color: white;
}
.sidebar-navigation :focus {
z-index: 81 !important;
position: relative;
background-color: pink;
}
Related Resources
No endorsement implied.
Tests
Procedure
This test procedure was written for HTML content displayed within browsers. It may not reflect the test procedure for all technologies that support Cascading Style Sheets (CSS).
- Use keyboard tabbing to navigate through and visually examine all focusable user interface components.
- Identify areas of focusable content which, in certain circumstances, are partially or fully obscured by pop-over elements.
- Use browser inspection tools or examine stylesheets to determine the highest z-index value of elements that are obscuring the focusable content.
- Examine all obscured focusable user interface components to determine if they have pre-set z-index,
positionorbackgroundproperties. - Create CSS selectors to carefully target the obscured focusable content elements and areas.
- Add CSS z-index,
positionandbackground(includingbackground-color) properties, to achieve the desired effect, with a reasonable level of precision and minimal overrides. - Implement the new CSS.
- Check the results by using keyboard tabbing to navigate through and visually examining all focusable user interface components.
- Ensure that all focusable user interface components are now fully unobscured when focused.
- Ensure that new bugs or visual issues are introduced by the added
backgroundandpositionproperties.
Expected Results
- When unfocused, there are no visual changes compared the initial starting point before new CSS was added.
- When focused, all focusable user interface components are fully visible and unobscured by pop-over elements.
- When focused, no elements experience layout shift due to a changed
positionproperty. - When focused, no elements experience a decrease in readability due to changes in contrast between text or images and their backgrounds.