A form or card with several fields can be easier to understand when interaction highlights not only the individual input but the entire container. Users can immediately see which block the active field belongs to, especially when a page contains several forms or cards.
The CSS :focus-within pseudo-class is a convenient way to achieve this. It applies styles to a parent element when any of its child elements receives focus, including an input, textarea, select, link, or button. This approach works not only with mouse interaction but also during keyboard navigation.
What :focus-within Does
The standard :focus pseudo-class styles the element that has received focus. For example, you can change the border of an active input field. The :focus-within pseudo-class works at the parent-element level: when focus is inside a container, the container receives the corresponding state as well.
This is especially useful for forms, login cards, search blocks, filters, accordions, and panels with buttons. When users move between fields with the Tab key, the highlight moves along with the focus.
- An input, textarea, or select inside a container activates :focus-within.
- A button or link inside the container can also trigger this state.
- The effect does not depend on cursor hovering.
- The highlight helps users navigate with a keyboard and on touch devices.
How to Prepare a Container in Elementor
First, open the required container in the Elementor editor and add the CSS class form-card. Enter the class without a period—the period is used only in the CSS selector itself.
The container should include elements that can receive focus, such as form widget fields, a submit button, links, or other interactive elements. If it contains only text and images, :focus-within will not produce a noticeable effect.
You can add the CSS to an Elementor Custom CSS field available in your configuration, or to the site CSS or a child theme. If you add the style directly to an individual Elementor element's Custom CSS field, check that field's syntax: in some contexts, selector is used to refer to the current element. For global CSS, the standard .form-card selector is sufficient.
- Enter form-card in the CSS Classes field.
- Make sure the fields and button are physically located inside this container.
- Do not add a period to the class name in Elementor settings.
- After saving, check the result on the published page, not only in the editor.
.form-card {
border: 1px solid transparent;
transition: border-color .2s ease, box-shadow .2s ease;
}
.form-card:focus-within {
border-color: var(--e-global-color-primary);
box-shadow: 0 0 0 3px color-mix(
in srgb,
var(--e-global-color-primary) 18%,
transparent
);
}
Basic CSS and the Role of Global Colors
The transition property makes the state change smooth. In this example, only border-color and box-shadow are animated, so the effect does not create unnecessary movement across the entire block.
The transparent initial border prevents the container's dimensions from changing when it receives focus. If you add a border only in the active state, the card may shift slightly and move neighboring elements.
The var(--e-global-color-primary) variable lets you use Elementor's primary global color. If this variable is not defined in your theme or project, the color may not be applied. In that case, replace it with a specific value, such as #2563eb, or use your own CSS variable.
- For a dark theme, check that the border remains visible against the dark background.
- The shadow color should be noticeable enough without competing with the primary button.
- If the design does not call for a border, keep only box-shadow, but do not remove the visible focus indicator from the fields themselves.
.form-card:focus-within {
border-color: #2563eb;
box-shadow: 0 0 0 3px rgba(37, 99, 235, .18);
}
How to Preserve Focus Accessibility
Highlighting the container does not replace the focus indicator on the interactive element itself. Users need to see exactly which field is currently active. Therefore, do not use outline: none without an equally clear and visible alternative.
You can highlight the container as an additional cue while keeping the browser's default focus style on the input and button or configuring your own. Test the state with the Tab key: focus should remain visible and move in a logical order.
It is also important to check the contrast of the border and shadow. A color that looks subtle on a white background may be too weak to serve its navigation purpose.
- Go through the form using only the keyboard with Tab and Shift + Tab.
- Make sure the active field is visually distinct from inactive fields.
- Do not rely only on a color change; when necessary, use a border, shadow, or another clearly visible cue.
- Test the effect on both light and dark backgrounds.
.form-card input:focus,
.form-card textarea:focus,
.form-card select:focus,
.form-card button:focus,
.form-card a:focus {
outline: 2px solid var(--e-global-color-primary);
outline-offset: 2px;
}
Compatibility and a Fallback for color-mix
The color-mix function lets you create a semi-transparent shade of a global color without defining a separate rgba value. However, support for modern CSS functions depends on the browser and its version. If compatibility with older browsers is important for your project, provide a fallback.
You can first define box-shadow with rgba and then override it with color-mix. A browser that does not recognize the modern syntax will use the earlier rule.
- Test the page in the main browsers used by your audience.
- If the shadow does not appear, check the CSS variable and the color-mix syntax.
- For the most predictable result, use rgba with a specific color.
.form-card:focus-within {
border-color: var(--e-global-color-primary);
box-shadow: 0 0 0 3px rgba(37, 99, 235, .18);
box-shadow: 0 0 0 3px color-mix(
in srgb,
var(--e-global-color-primary) 18%,
transparent
);
}
Common Mistakes and How to Check the Result
The most common mistake is adding the class to an individual field instead of the container or outer section. In that case, the style may not work as expected. Another possible cause is that the interactive element is outside the container because of an incorrect Elementor section structure.
If the effect does not work, open your browser's developer tools and check whether the form-card class is present in the HTML. Then activate a field and see whether the :focus-within rule is applied. This helps distinguish a CSS problem from an Elementor structure or caching problem.
After making changes, clear the cache for your optimization plugin, CDN, or browser if the site continues to display an older version of the styles.
- Check the class spelling: form-card must match character for character.
- Make sure the CSS is not being overridden by theme or Elementor styles.
- Check whether the container has overflow or other styles that clip the shadow.
- Test the input, textarea, select, button, and link separately.
- Check the mobile version: the shadow should not make the card appear to shift.
Frequently asked questions
Does :focus-within work with Elementor forms?
Yes. The pseudo-class works with any child elements that receive focus, including form fields and buttons. They must actually be located inside the container with the required class.
Do I need to add the form-card class to every field?
No. Add the class to the parent container. All fields, buttons, or links inside it will activate the :focus-within state.
How is :focus-within different from :hover?
:hover primarily responds to cursor movement, while :focus-within responds to focus on a child element. As a result, it works during keyboard navigation and is better suited to accessible forms.
Can I remove the default focus outline if the entire container is highlighted?
You should not remove it without providing a replacement. Container highlighting shows the active group but does not always identify the specific field clearly. Keep the default focus style or create a visible custom style for inputs, selects, textareas, and buttons.
Conclusion
The :focus-within pseudo-class lets you give a container a clear interaction state without JavaScript or complex Elementor logic. Simply assign a class to the container and add transition and active-state styles.
The best result comes when container highlighting complements rather than replaces the focus indicator on the specific field. Check keyboard navigation, contrast, mobile appearance, and CSS function compatibility before publishing.