You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[css-display][css-aam][selectors-4] How elements with display:contents get focused? #2632
In #2355, we clarified that only display:none can affect the element's semantics and interactivity, so element with display: contents should preserve all its interactivity, including the ability to get the :focus (and :focus-visible) state. However, it's not clear how this state should work for this element.
Like the element with no boxes can be in :hover state (see #1141), making it possible to choose its descendants with selectors like .no-boxes-element:hover .something, I believe that the element that would receive focus without display: contents should receive it with display: contents as well. So its descendants should match the selectors like .no-boxes-element:focus .something, and its ancestors should match the :focus-within pseudo-class, making it possible to make the focus change visible despite the element itself is not displayed. And the fact that current browsers (at least, Firefox and Chrome) don't apply focus to the element with display:contents is the bug of these browsers. The element is still there in the DOM, it still can be activated, so there is no reason to skip the focus for it.
However, it's still not very clear how should the :focus state work for the element with display: contents itself. I suppose the easiest way to implement it is to only apply the :focus styles to the element's contents via inheritance. This would mean that the elements with display: contents would not get the default browser "focus ring", but the changes of color, background etc. would be inherited automatically by the element's descendants, and authors would be able to explicitly style the specific descendants of these invisible focused elements.
Another option can be to introduce a new pseudo-element like ::selection that would span all the visible contents of the invisible element and get the default browser focus decoration, so the focus would be always visible, but this approach looks more complicated.
It doesn't make much sense to focus something that doesn't have a box. :hover only works because it's applied to the whole parent chain. That is, comparing :hover and :focus isn't really appropriate. The comparison should be, I think, :hover and :focus-within, which should work with display: contents without issues AFAICT.
Reacted by Vincent Rubinetti, Alexey Bogachev and Arad Alvand
@emilio The intention of display: contents is that it should only affect layout, not functionality. So we need to consider how it applies to interactive elements, not just container elements.
For example, you might want to switch the appearance of a widget from a drop-down list on small screens to a radio-button style layout on larger screens, by making the container disappear and just laying out the options. However, you would still want the widget container (the element that replaces the role of a <select>) to gain focus (and then use arrows to switch between the options).
Or, you might have a link containing both an image and a text block. For layout purposes, you want to lay out the two parts as independent children of the parent layout context, but they are still a single link for accessibility and function.
(Note: current browser implementations of display: contents fail to live up to this promise. But that's another issue.)
The :focus selector would still match the focused element, so any inheritable changes set on it (like color) would propagate to the children. But as currently defined, and box-based styles on the element itself (like outline) would not apply. Which means that default browser focus styles, and most author-defined focus styles, would no longer be rendered.
The only ones which apply in a way different from simply inheriting to child content are outline and background-color. Background color is well supported for selections, so should be straightforward to apply to a multi-box collection equivalent to a display: contents element. Outline would need to be implemented in a way that supports more than a simple rectangle, but this is already true for elements with absolute/fixed position children that extend outside of the parent.
Reacted by Ilya Streltsyn, ExE Boss, Vincent Rubinetti, Raura and Liubov
Whether or not an element is focused has no relation to whether it's in the box tree or not. (It might affect whether some methods of focus-traversal can reach it, but you can always .focus() the element.)
Whether or not the element is display:contents has no effect on how selectors match and properties apply; a focused element will always match :focus if it's focused, and the specified properties will be applied to it. None of them will have any direct effect on the element, because it doesn't generate a box, but inheritance will still work.
We shouldn't try to get extra-smart with this; the element is gone, there's nothing to apply a background or anything else to. (Note, for example, that if you display:contents a child of a grid container, its children are now grid items, and can be moved arbitrarily around the grid; there is nothing remotely related to the "first line" concept to refer to here.)
Reacted by Oriol Brufau, Ilya Streltsyn, Sasha Chudesnov, ExE Boss, abuyoyo and Raura
It doesn't make much sense to focus something that doesn't have a box.
Respectfully disagree. If the element is interactive, can get activated and responds to events as usual, and its activation behavior can have a visual effect on its contents, I see no reasons why this element can't get focus. Making the element and all its subtree completely unfocusable just because if is displayed differently (not hidden/removed!) is a real usability problem and very unlikely matches the authors' intent to just change the element's presentation, but not changing its semantics nor behavior. Having some "virtual" focus state for the invisible element that can make visual change through the element's descendants seems to be much less problematic than having the element and its whole visible subtree completely not focusable.
It might affect whether some methods of focus-traversal can reach it
@tabatkins, could you please show an example? The only way how CSS can prevent the visible and otherwise focusable element from getting focus through click/tap I can quickly imagine is pointer-events: none for all its children, but this seems unrelated to display to me. I agree with you that "we shouldn't try to get extra-smart with this", and the example with grid container's "grandchildren" displayed as separate grid items seems not more complicated to me than, e.g., absolutely positioned children of the visually hidden focused elements. CSS already can handle multiple boxes changing their style because of one element getting focused, Does display:contents really add anything new here?
Making the element and all its subtree completely unfocusable just because if is displayed differently (not hidden/removed!) is a real usability problem and very unlikely matches the authors' intent to just change the element's presentation, but not changing its
FWIW, I agree that making descendants not focusable is a bug. I haven't been able to reproduce it in FF Nighly / FF 58, but Chrome does indeed prevent you to tab to stuff in display: contents subtree. I'd file a Chrome bug for that nor Chrome.
(EDIT: I was going to file myself a Chrome bug and couldn't repro anymore, so probably I wasn't testing what I thought I was testing).
Which UI should the browser show when the display: contents element is focused? Note that it has no box, it's descendants could be anywhere in the layout tree. It seems like a really weird behavior to explain to any user IMO... I could be convinced otherwise I guess.
In any case agreed that we should special-case as little as needed. Special cases = bugs.
it has no box, it's descendants could be anywhere in the layout tree. It seems like a really weird behavior to explain to any user IMO...
Completely agree that it's weird. Exactly like so is the focus behavior of, e.g., an off-screen positioned element with on-screen absolutely positioned children, or the focus behavior of the element with good old outline:none and no replacement for it. Sure, it is bad. But, just like in case of outline:none, it can be fixed (by applying special styles to the element's descendants, in this case). Sure, this possibility (as many others in CSS) can have bad effect on the usability, and the spec probably should warn against misusing/overusing it.
But removing the element from the accessibility tree altogether without any possibility to bring its displayed parts back into it, as browsers currently seem to do, looks way more problematic to me, regardless how good the intent of this behavior was.
I imagine that display: none on an element prevents it from being tabbed to. display:contents would do the same thing - there's nothing on-screen to be tabbed to!
I agree with you that "we shouldn't try to get extra-smart with this", and the example with grid container's "grandchildren" displayed as separate grid items seems not more complicated to me than, e.g., absolutely positioned children of the visually hidden focused elements.
Note - I was responding to the idea that display:contents elements could still have background work on them, like ::first-line.
To me, display: contents and display: none for the interactive element with visible content (e.g. button) are very different. While display: none really removes the element from the presentation, display: contents leaves its children on-screen, they still response to activation via click/tap, and the author most likely wants to preserve all the aspects of their interactivity. The interactive element with display:contents looks very similar to the common pattern for creating custom controls with native functionality: visually-hidden normal <input> + its sibling (usually a <label> or a <span> inside it) that adjusts its styling according to the state of the invisible <input>. The focus state is no different: the <input> elements gets focused by tabbing despite being off-screen, its visible siblings react to this and the user gets the decent UX.
From display:contents, I expect the behavior similar to that. I also expect that the primary use case for applying it to interactive elements would be similar — working around browser limitations of native control styling (e.g. inability to make the <button> non-atomic inline element) while preserving as much native semantics/functionality as possible. It's extremely unlikely that authors expect such elements, which are perfectly visible (through content) and reachable to users, to disappear from the accessibility tree and tabbing sequence.
P.S. Regarding background of these elements, I agree that they shouldn't have it themselves. What I suggested (as a kind of brainstorming-style option) was adding a new pseudo-element (e.g. ::now-focused), which in case of the regular element would match the element's box, and in case of display:contents element would be something ::selection-like that spans all the visible children of such element, and making browsers apply their default focus indication (outline etc.) to this pseudo-element instead of the element. But I admit that this option is not realistic:)
That is not what display: contents does or what it's intended to do. It's meant to remove wrapper elements that are added for semantics or scripting purposes, but mess with styling that depends on direct parent/child relationships (like grid container -> grid item).
This is why it's not good for the case you're talking about. For example, as far as CSS is concerned, button is just a specially-styled span; a <button>foo</button> with display:contents is just raw text, not a button at all anymore. (You can still click on the text, and the click event will bubble up to the button element and trigger a button press, but that's because the DOM and the visual display don't have to have any particular correspondence with each other.) The text is not focusable by tabbing in a normal button, and it doesn't magically become tabbable here either.
The input+label example you bring up doesn't apply either; the input is offscreen, but still present in the page, so it can still be tabbed to and focused by user interaction just fine. You just can't click on it because it's not on screen; this is no different from it just being obscured by another element. This is very different from the element not existing in the rendering structure at all.
Well, display:contents might be not intended for this, but the problem of styling limitations of native controls still exist and any option to strip off all the "dark magic" from these elements is too tempting. Until browsers offer the better way for this (some new appearance value?), authors would likely use all the available valid CSS means to solve this problem — including display:contents.
I'm OK with button:focus { display:contents } not being highlighted, and with its plain text content not highlighted as well. But I'm not OK with the span inside this button not applying the button:focus > span styles. And I'm very not OK with visible and interactive element that can't be keyboard-activated at all just because of its styling. As an author, I see the focus state as a property of the DOM element, not of a box in a render tree. It's the DOM element, not the box, that fires focus and blur events. And if something is interactive, it's expected to be focusable.
Aside from the none value, which also affects the aural/speech output [CSS-SPEECH-1] and interactivity of an element and its descendants, the display property only affects visual layout: its purpose is to allow designers freedom to change the layout behavior of an element without affecting the underlying document semantics.
Doesn't this imply that the interactive element with no box but with visible children is just the element that can't be styled on focus itself, not the element that magically became not focusable?
And I'm very not OK with visible and interactive element that can't be keyboard-activated at all just because of its styling. As an author, I see the focus state as a property of the DOM element, not of a box in a render tree.
Agreed. I would expect the element to be in a focused state, but just not having any visual indication of that state, unless the author picked a descendant element to show an outline (or other distinctive styling).
Reacted by Ilya Streltsyn, Sasha Chudesnov, fantasai, abuyoyo, Raura and Radek Šerý
I think outline should work on display contents elements. Just like it works if an online gets split into lines or bidi or even regions or by a block-in-inline. I think the element should be focusable and tab-focusable too, but only if it is not replaced or a shadow host or none of its descendants in the light tree produce a box. The elements in cases noted above behave as if they were display none and should therefore not be focusable. Besides outline and a few other properties like pointer-event, properties should have no effect on the elements that do generate boxes for their descendants, besides being inherited by default.
An inline getting split across lines is still generating fragments - focusing doesn't need to care whether there are 1 or more fragments, it works similarly in either case.
But zero fragments is a very different case. Where is that outline supposed to go??? There is zero promise of content contiguity here; if you display: contents a child of a grid container, the grandchildren can get placed willy-nilly in the grid, while the child has no position at all.
A display: contents element isn't visible. It isn't interactive. It's nothing at all. It just has contents that might be visible and/or interactive.
Activity
emilio commentedon Apr 30, 2018
It doesn't make much sense to focus something that doesn't have a box.
:hoveronly works because it's applied to the whole parent chain. That is, comparing:hoverand:focusisn't really appropriate. The comparison should be, I think,:hoverand:focus-within, which should work with display: contents without issues AFAICT.AmeliaBR commentedon Apr 30, 2018
@emilio The intention of
display: contentsis that it should only affect layout, not functionality. So we need to consider how it applies to interactive elements, not just container elements.For example, you might want to switch the appearance of a widget from a drop-down list on small screens to a radio-button style layout on larger screens, by making the container disappear and just laying out the options. However, you would still want the widget container (the element that replaces the role of a
<select>) to gain focus (and then use arrows to switch between the options).Or, you might have a link containing both an image and a text block. For layout purposes, you want to lay out the two parts as independent children of the parent layout context, but they are still a single link for accessibility and function.
(Note: current browser implementations of
display: contentsfail to live up to this promise. But that's another issue.)The
:focusselector would still match the focused element, so any inheritable changes set on it (likecolor) would propagate to the children. But as currently defined, and box-based styles on the element itself (likeoutline) would not apply. Which means that default browser focus styles, and most author-defined focus styles, would no longer be rendered.I like the idea of extending the properties that apply to highlight pseudo-elements to also apply to
display: contentselements. The current list of properties in that category are:The only ones which apply in a way different from simply inheriting to child content are
outlineandbackground-color. Background color is well supported for selections, so should be straightforward to apply to a multi-box collection equivalent to adisplay: contentselement. Outline would need to be implemented in a way that supports more than a simple rectangle, but this is already true for elements with absolute/fixed position children that extend outside of the parent.tabatkins commentedon Apr 30, 2018
Whether or not an element is focused has no relation to whether it's in the box tree or not. (It might affect whether some methods of focus-traversal can reach it, but you can always
.focus()the element.)Whether or not the element is
display:contentshas no effect on how selectors match and properties apply; a focused element will always match:focusif it's focused, and the specified properties will be applied to it. None of them will have any direct effect on the element, because it doesn't generate a box, but inheritance will still work.We shouldn't try to get extra-smart with this; the element is gone, there's nothing to apply a background or anything else to. (Note, for example, that if you
display:contentsa child of a grid container, its children are now grid items, and can be moved arbitrarily around the grid; there is nothing remotely related to the "first line" concept to refer to here.)SelenIT commentedon Apr 30, 2018
Respectfully disagree. If the element is interactive, can get activated and responds to events as usual, and its activation behavior can have a visual effect on its contents, I see no reasons why this element can't get focus. Making the element and all its subtree completely unfocusable just because if is displayed differently (not hidden/removed!) is a real usability problem and very unlikely matches the authors' intent to just change the element's presentation, but not changing its semantics nor behavior. Having some "virtual" focus state for the invisible element that can make visual change through the element's descendants seems to be much less problematic than having the element and its whole visible subtree completely not focusable.
@tabatkins, could you please show an example? The only way how CSS can prevent the visible and otherwise focusable element from getting focus through click/tap I can quickly imagine is
pointer-events: nonefor all its children, but this seems unrelated todisplayto me. I agree with you that "we shouldn't try to get extra-smart with this", and the example with grid container's "grandchildren" displayed as separate grid items seems not more complicated to me than, e.g., absolutely positioned children of the visually hidden focused elements. CSS already can handle multiple boxes changing their style because of one element getting focused, Doesdisplay:contentsreally add anything new here?emilio commentedon May 1, 2018
FWIW, I agree that making descendants not focusable is a bug. I haven't been able to reproduce it in FF Nighly / FF 58,
but Chrome does indeed prevent you to tab to stuff in display: contents subtree. I'd file a Chrome bug for thatnor Chrome.(EDIT: I was going to file myself a Chrome bug and couldn't repro anymore, so probably I wasn't testing what I thought I was testing).
emilio commentedon May 1, 2018
It's not clear to me that focusing elements without a box is great in general... It'd be confusing to have to tab twice to get to the
<input>in:Which UI should the browser show when the display: contents element is focused? Note that it has no box, it's descendants could be anywhere in the layout tree. It seems like a really weird behavior to explain to any user IMO... I could be convinced otherwise I guess.
In any case agreed that we should special-case as little as needed. Special cases = bugs.
SelenIT commentedon May 2, 2018
Completely agree that it's weird. Exactly like so is the focus behavior of, e.g., an off-screen positioned element with on-screen absolutely positioned children, or the focus behavior of the element with good old
outline:noneand no replacement for it. Sure, it is bad. But, just like in case ofoutline:none, it can be fixed (by applying special styles to the element's descendants, in this case). Sure, this possibility (as many others in CSS) can have bad effect on the usability, and the spec probably should warn against misusing/overusing it.But removing the element from the accessibility tree altogether without any possibility to bring its displayed parts back into it, as browsers currently seem to do, looks way more problematic to me, regardless how good the intent of this behavior was.
tabatkins commentedon May 2, 2018
I imagine that
display: noneon an element prevents it from being tabbed to.display:contentswould do the same thing - there's nothing on-screen to be tabbed to!Note - I was responding to the idea that
display:contentselements could still havebackgroundwork on them, like::first-line.SelenIT commentedon May 3, 2018
To me,
display: contentsanddisplay: nonefor the interactive element with visible content (e.g.button) are very different. Whiledisplay: nonereally removes the element from the presentation,display: contentsleaves its children on-screen, they still response to activation via click/tap, and the author most likely wants to preserve all the aspects of their interactivity. The interactive element withdisplay:contentslooks very similar to the common pattern for creating custom controls with native functionality: visually-hidden normal<input>+ its sibling (usually a<label>or a<span>inside it) that adjusts its styling according to the state of the invisible<input>. The focus state is no different: the<input>elements gets focused by tabbing despite being off-screen, its visible siblings react to this and the user gets the decent UX.From
display:contents, I expect the behavior similar to that. I also expect that the primary use case for applying it to interactive elements would be similar — working around browser limitations of native control styling (e.g. inability to make the<button>non-atomic inline element) while preserving as much native semantics/functionality as possible. It's extremely unlikely that authors expect such elements, which are perfectly visible (through content) and reachable to users, to disappear from the accessibility tree and tabbing sequence.P.S. Regarding
backgroundof these elements, I agree that they shouldn't have it themselves. What I suggested (as a kind of brainstorming-style option) was adding a new pseudo-element (e.g.::now-focused), which in case of the regular element would match the element's box, and in case ofdisplay:contentselement would be something::selection-like that spans all the visible children of such element, and making browsers apply their default focus indication (outline etc.) to this pseudo-element instead of the element. But I admit that this option is not realistic:)tabatkins commentedon May 3, 2018
That is not what
display: contentsdoes or what it's intended to do. It's meant to remove wrapper elements that are added for semantics or scripting purposes, but mess with styling that depends on direct parent/child relationships (like grid container -> grid item).This is why it's not good for the case you're talking about. For example, as far as CSS is concerned,
buttonis just a specially-styledspan; a<button>foo</button>withdisplay:contentsis just raw text, not a button at all anymore. (You can still click on the text, and the click event will bubble up to thebuttonelement and trigger a button press, but that's because the DOM and the visual display don't have to have any particular correspondence with each other.) The text is not focusable by tabbing in a normalbutton, and it doesn't magically become tabbable here either.The input+label example you bring up doesn't apply either; the input is offscreen, but still present in the page, so it can still be tabbed to and focused by user interaction just fine. You just can't click on it because it's not on screen; this is no different from it just being obscured by another element. This is very different from the element not existing in the rendering structure at all.
SelenIT commentedon May 4, 2018
Well,
display:contentsmight be not intended for this, but the problem of styling limitations of native controls still exist and any option to strip off all the "dark magic" from these elements is too tempting. Until browsers offer the better way for this (some newappearancevalue?), authors would likely use all the available valid CSS means to solve this problem — includingdisplay:contents.I'm OK with
button:focus { display:contents }not being highlighted, and with its plain text content not highlighted as well. But I'm not OK with thespaninside this button not applying thebutton:focus > spanstyles. And I'm very not OK with visible and interactive element that can't be keyboard-activated at all just because of its styling. As an author, I see the focus state as a property of the DOM element, not of a box in a render tree. It's the DOM element, not the box, that firesfocusandblurevents. And if something is interactive, it's expected to be focusable.After all, in #2355 we added the following text:
Doesn't this imply that the interactive element with no box but with visible children is just the element that can't be styled on focus itself, not the element that magically became not focusable?
bradkemper commentedon May 25, 2018
Agreed. I would expect the element to be in a focused state, but just not having any visual indication of that state, unless the author picked a descendant element to show an outline (or other distinctive styling).
FremyCompany commentedon May 30, 2018
I think outline should work on display contents elements. Just like it works if an online gets split into lines or bidi or even regions or by a block-in-inline. I think the element should be focusable and tab-focusable too, but only if it is not replaced or a shadow host or none of its descendants in the light tree produce a box. The elements in cases noted above behave as if they were display none and should therefore not be focusable. Besides outline and a few other properties like pointer-event, properties should have no effect on the elements that do generate boxes for their descendants, besides being inherited by default.
tabatkins commentedon May 31, 2018
An inline getting split across lines is still generating fragments - focusing doesn't need to care whether there are 1 or more fragments, it works similarly in either case.
But zero fragments is a very different case. Where is that outline supposed to go??? There is zero promise of content contiguity here; if you
display: contentsa child of a grid container, the grandchildren can get placed willy-nilly in the grid, while the child has no position at all.A
display: contentselement isn't visible. It isn't interactive. It's nothing at all. It just has contents that might be visible and/or interactive.51 remaining items
Allow elements with display:contents to be focused.
Allow elements with display:contents to be focusable.
Add support for allowing elements with display:contents to be focused.
Add support for allowing elements with display:contents to be focused.
Add support for allowing elements with display:contents to be focused.
Add support for allowing elements with display:contents to be focused.
Add support for allowing elements with display:contents to be focused.
Add support for allowing elements with display:contents to be focused.
Bug 1825055 [wpt PR 39247] - Add support for allowing elements with d…
Allow elements with display:contents to be focusable
Bug 1825055 [wpt PR 39247] - Add support for allowing elements with d…
Bug 1825055 [wpt PR 39247] - Add support for allowing elements with d…
Add support for allowing elements with display:contents to be focused.
Allow elements with display:contents to be focusable
Bug 1825055 [wpt PR 39247] - Add support for allowing elements with d…