Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to style <details>/<summary> to be open (e.g. for print styles) #2084

Open
patrickhlauke opened this issue Dec 5, 2017 · 27 comments
Labels
css-ui-4 Current Work

Comments

@patrickhlauke
Copy link
Member

(Not quite sure which spec this would relate to specifically, sorry)

If a page uses <details>/<summary> expand/collapse blocks, there currently appears to be no way to open these via CSS. This poses problems for print styling: if I wanted to have these blocks appear open when printed, it seems that the only solution is to first run JS over the document and add the open attribute on the <details>.

@patrickhlauke
Copy link
Member Author

/cc @tabatkins @fantasai

@Heydon
Copy link

Heydon commented Dec 5, 2017

Would be great it was specified to be open for print media by default (which I guess is what you mean?)

@patrickhlauke
Copy link
Member Author

patrickhlauke commented Dec 5, 2017

Either that, or have some way to control it (like you'd want to control any other aspects when making decisions on how something should appear when printed). Currently, it's "style-resistant"...

@Heydon
Copy link

Heydon commented Dec 5, 2017

Aye, probably both.

@iandevlin
Copy link

Nice idea.

@patrickhlauke patrickhlauke changed the title Ability to style <details>/<summary> to be open (for print styles) Ability to style <details>/<summary> to be open (e.g. for print styles) Dec 5, 2017
@upsuper
Copy link
Member

upsuper commented Dec 7, 2017

I'm thinking about whether it is possible to describe the behavior of <details>/<summary> in CSS rather than having them being HTML rendering magic.

The most straightforward approach might be defining a new pseudo-element (something like ::details-content) for wrapping content in <details> other than the effective <summary>. But generally, browser vendors would be against adding this kind of pseudo-element wrapper, because they can bring lots of unnecessary complexity.

Maybe we can have that pseudo-element, but only allow display property with none or contents. This should make it easy to implement, I suppose.

@tabatkins tabatkins added the css-ui-4 Current Work label Dec 12, 2017
@tabatkins
Copy link
Member

I'm down with that approach. The UA stylesheet is then extremely trivial:

details::details-content { display: none; }
details[open]::details-content { display: contents; }

And overriding it is super easy. 👍 from me.

@AmeliaBR
Copy link
Contributor

AmeliaBR commented Dec 12, 2017

+1 to any strategy that makes it possible to describe the behavior of <details>/<summary> in standard CSS. It would make it much more useful in responsive design (e.g., for a collapsible menu) if you could auto-expand (and optionally hide the toggle <summary> button) when there is room for the full content.

Another way to approach this would be to have a generic selector for anonymous text nodes.

So the default style rule for <details>/<summary> would look something like:

details:not([open]) > *:not(summary),
details:not([open]) > *text* {
  /* any direct child elements other than summary, or any direct child text nodes */
  display: none;
}

There are other cases where it could be useful for authors to override default HTML renderings for anonymous text nodes, like <meter>/<progress>, where the anonymous text content inside is a formatted representation of the value, but is currently never rendered in supporting browsers.

A *text* selector could also simplify other browser styles, e.g. the way MS Edge adds a solid-color background to all anonymous text spans in high-contrast mode.

@js-choi
Copy link

js-choi commented Dec 13, 2017

@AmeliaBR I had been planning to propose pseudo-elements for anonymous text boxes, first to the WICG then eventually here. For instance, a pseudo-element for anonymous block boxes would be generally useful in other cases as an approximate analogue to HTML’s paragraphs, e.g., anonymous-block “paragraphs” such as those in <h1>First block</h1> This is the <em>second</em> block in this example. <p>This is the third.</p> <ul> <li>Fourth.</li> <li> <p>Fifth.</p> <p>Sixth.</p> </li> </ul>. Such pseudo-elements, however, might have broad ramifications for rendering performance, so I had planned on courting authors in the WICG before the CSSWG to bolster my case. This is the first mention of this idea I have yet seen elsewhere on the web, so it’s good to have another use case.

Having said all that, it is precisely the broadness in scope of such an anonymous-box selector that may make a ::details-content pseudo-element more feasible in the short term. An anonymous-box selector also may not actually match all of the non-summary content in a <details> element: <details> <summary>A</summary> <p> B <p> C </details>. I still do plan to make a formal proposal for anonymous-box selectors in general, unless someone else beats me to it.

chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Aug 3, 2018
Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
@jonjohnjohnson
Copy link

Big fan of @tabatkins idea...

details::details-content { display: none; }
details[open]::details-content { display: contents; }

But @Loirooriol, any idea how you could make this work with your ::contents proposal? Even if the summary is a child of the details element? I know there are cases where the tree is shuffled around for layout, like display: run-in?

As an aside, could we also get a ::details-marker pseudo element in the spec at the same time? I know webkit already has their own version, but it would be great to more seamlessly style the summary (or lack thereof) taking control over the generated contents arrow/marker. Could it be possible to steal the list items marker box ::marker pseudo element for reuse? Since it seems that...

Fully standards-compliant implementations automatically apply the CSS display: list-item to the summary element

But make the ::marker available even when details has no summary?

MDN Ref - ::-webkit-details-marker

@AmeliaBR
Copy link
Contributor

AmeliaBR commented Aug 4, 2018

As an aside, could we also get a ::details-marker pseudo element

The regular ::marker on the <summary> element will apply to it.

But make the ::marker available even when details has no summary?

In that case, it makes more sense to define a pseudoelement for the auto-generated summary toggle, and allow it to have stacked psuedoelements on it:

details::details-summary::marker,
details summary::marker {
  /* styles for the disclosure marker */
}

@jonjohnjohnson
Copy link

@AmeliaBR as well as something like this?

details::details-summary {
  content: 'Details';
  display: list-item;
}

@AmeliaBR
Copy link
Contributor

AmeliaBR commented Aug 5, 2018

@jonjohnjohnson Exactly! As I said earlier in the thread, I would very much like to get to the point where all of details/summary layout & rendering can be defined with a CSS model & customized by media queries. I hadn't been thinking about auto-generated summary toggles, but they definitely should be part of that.

@Loirooriol
Copy link
Contributor

@jonjohnjohnson I don't think this would be a good fit for my ::contents proposal. Here you want to select only some children of the <details> but not the <summary>. The ::wrap proposal (#588) allows this bit doesn't seem a feasible feature, since it allows authors to produce non-tree DAGs.

there are cases where the tree is shuffled around for layout, like display: run-in

But this reparenting happens in the box tree. I think ::contents should live in the element tree just like ::before and ::after, so that things stay well-defined.

Then I think new pseudo-elements for detail parts would be better.

chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Sep 6, 2018
Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
aarongable pushed a commit to chromium/chromium that referenced this issue Sep 7, 2018
Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
Reviewed-on: https://chromium-review.googlesource.com/1153651
Commit-Queue: David Grogan <dgrogan@chromium.org>
Reviewed-by: Christian Biesinger <cbiesinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589388}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Sep 7, 2018
Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
Reviewed-on: https://chromium-review.googlesource.com/1153651
Commit-Queue: David Grogan <dgrogan@chromium.org>
Reviewed-by: Christian Biesinger <cbiesinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589388}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Sep 7, 2018
Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
Reviewed-on: https://chromium-review.googlesource.com/1153651
Commit-Queue: David Grogan <dgrogan@chromium.org>
Reviewed-by: Christian Biesinger <cbiesinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589388}
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Sep 12, 2018
…of <details> is ignored, a=testonly

Automatic update from web-platform-testsAdd test asserting the display property of <details> is ignored

Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
Reviewed-on: https://chromium-review.googlesource.com/1153651
Commit-Queue: David Grogan <dgrogan@chromium.org>
Reviewed-by: Christian Biesinger <cbiesinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589388}

--

wpt-commits: 9d5d9aa80785d9726ed0a5eaab1a8d144fd4b560
wpt-pr: 12218
jankeromnes pushed a commit to jankeromnes/gecko that referenced this issue Sep 12, 2018
…of <details> is ignored, a=testonly

Automatic update from web-platform-testsAdd test asserting the display property of <details> is ignored

Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
Reviewed-on: https://chromium-review.googlesource.com/1153651
Commit-Queue: David Grogan <dgrogan@chromium.org>
Reviewed-by: Christian Biesinger <cbiesinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589388}

--

wpt-commits: 9d5d9aa80785d9726ed0a5eaab1a8d144fd4b560
wpt-pr: 12218
@NickColley
Copy link

I hope it's OK to chip in with an example of where this has stopped us from using details:

At GDS we were considering creating our new accordion component using details elements as it would allow for better functionality when JavaScript is not available, we're deciding not to use the element for now but I think if this were possible to style when printed we'd likely use it.

@tabatkins
Copy link
Member

See also #3126, where Anne relays some requests for the ability to put ::before/::after in the contents of a details (so ::before comes after the summary, and they're both hidden when the details is closed); providing a ::details-content pseudo-element that allows ::before/::after on itself would also solve that problem.

@chbndrhnns
Copy link

I am making heavy use of these twisties for hiding diagrams or code snippets on markdown documentation pages (that get rendered to HTML). I have some styles that are activated when printing the document and for that purpose, I would also welcome a way to set <details> to open via pure CSS.

gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this issue Oct 3, 2019
…of <details> is ignored, a=testonly

Automatic update from web-platform-testsAdd test asserting the display property of <details> is ignored

Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
Reviewed-on: https://chromium-review.googlesource.com/1153651
Commit-Queue: David Grogan <dgroganchromium.org>
Reviewed-by: Christian Biesinger <cbiesingerchromium.org>
Cr-Commit-Position: refs/heads/master{#589388}

--

wpt-commits: 9d5d9aa80785d9726ed0a5eaab1a8d144fd4b560
wpt-pr: 12218

UltraBlame original commit: 3e84907d855160d165c2989d9cdd7f53f9ab16d7
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this issue Oct 3, 2019
…of <details> is ignored, a=testonly

Automatic update from web-platform-testsAdd test asserting the display property of <details> is ignored

Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
Reviewed-on: https://chromium-review.googlesource.com/1153651
Commit-Queue: David Grogan <dgroganchromium.org>
Reviewed-by: Christian Biesinger <cbiesingerchromium.org>
Cr-Commit-Position: refs/heads/master{#589388}

--

wpt-commits: 9d5d9aa80785d9726ed0a5eaab1a8d144fd4b560
wpt-pr: 12218

UltraBlame original commit: 3e84907d855160d165c2989d9cdd7f53f9ab16d7
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this issue Oct 3, 2019
…of <details> is ignored, a=testonly

Automatic update from web-platform-testsAdd test asserting the display property of <details> is ignored

Chrome, Safari, and Firefox pass. Edge hasn't implemented yet.

The test still holds for the proposed new spec text in
whatwg/html#3686

This scenario is discussed at the end of
whatwg/html#603 but the conclusion drawn
appears (to me) to be opposite what Chrome, Safari, and Firefox do.

Related other issues:
w3c/csswg-drafts#2084
whatwg/html#1839

Bug: 635282
Change-Id: Ice95225b3c9d5d90b80c657643d036490fd2e1b2
Reviewed-on: https://chromium-review.googlesource.com/1153651
Commit-Queue: David Grogan <dgroganchromium.org>
Reviewed-by: Christian Biesinger <cbiesingerchromium.org>
Cr-Commit-Position: refs/heads/master{#589388}

--

wpt-commits: 9d5d9aa80785d9726ed0a5eaab1a8d144fd4b560
wpt-pr: 12218

UltraBlame original commit: 3e84907d855160d165c2989d9cdd7f53f9ab16d7
@radogado
Copy link

Hi, please add the ability to display by CSS, because I can't use the element in a responsive situation where it should always be open. Thanks.

@toastal
Copy link

toastal commented Jul 29, 2022

I was hoping appearance: none would have helped me, but it seems like I need to reach for a JS solution. 😕

@samuelbradshaw
Copy link

samuelbradshaw commented Oct 30, 2022

+1. Another use case is to allow the open/closed state of the details element to be controlled by a preceding checkbox, using the sibling selector – something like

#agree-checkbox:checked + details {
  toggle-state: open;
}

Or a more general toggle to expand or collapse all details elements on the page:

#expand-all-checkbox:checked ~ details {
  toggle-state: open;
}

@jonathantneal
Copy link
Contributor

With the recent additions of :open and :closed, I believe usage of the proposed pseudo-elements (for the shadow slot for details summary and for the shadow slot for details content) would look like this:

::details-content {
  /* styles the shadow slot for details content */
}

::details-summary {
  /* styles the shadow slot for details summary */
}

:open::details-summary {
  /* styles the shadow slot for details summary when it is open */
}

:closed::details-summary {
  /* style the shadow slot for details summary when it is closed */
}

:open::details-content {
  /* styles the shadow slot for details content when it is open */
}

:closed::details-content {
  /* styles the shadow slot for details content when it is closed */
}

The :open & :closed pseudo-states would select LightDOM elements (like <details>) matching either state, while the ::details-summary & ::details-content pseudo-elements would then cross the shadow boundary and select the shadow elements matching either term within <details>.

@edent
Copy link
Member

edent commented Nov 15, 2022

Adding my voice to this. There's another issue with this - if a details box is closed, there's no way to navigate to its contents.

Here's a quick example:

<a href="#answer">What's in the box?</a>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<details>
   <summary>The box</summary>
   <div id="answer">A lovely surprise!</div>
</details>

I naïvely expected my browser to open the details and scroll me down to #answer.

Again, this would be particularly useful for answer accordions etc.

@Boldewyn
Copy link

I’ve detailed the use case from @edent’s comment a bit more in whatwg/html#8509.

@mariusGundersen
Copy link

@tabatkins any progress on this?

@yisibl
Copy link
Contributor

yisibl commented May 24, 2024

#9879 (comment)

RESOLVED: Use ::details-content as the name for the pseudo-element for the part of <details> that opens and closes

@Boldewyn
Copy link

Thank you for the pointer!

After reading the other issue (and two other linked ones) I’m still not sure how I would use ::details-content. Would it be something like this?

details ::details-content:has(:target) {
  display: block /* or inline, or use visibility? */;
}

@yisibl
Copy link
Contributor

yisibl commented May 24, 2024

@Boldewyn I wrote a demo for ::details-content https://x.com/yisibl/status/1791452140663345300

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css-ui-4 Current Work
Projects
None yet
Development

No branches or pull requests