大网数据广告活动
Web 标准与前端开发

`::details-content` CSS pseudo-element

· 52CE 我爱测

The **`::details-content`** CSS pseudo-element represents the expandable/collapsible contents of a details element.

The ::details-content CSS pseudo-element represents the expandable/collapsible contents of a details element.

CSS Demo: ::details-content

details[open]::details-content {
  color: dodgerblue;
  padding: 0.5em;
  border: thin solid grey;
}
<details open>
  <summary>Example summary</summary>
  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
  <p>
    Architecto cupiditate ea optio modi quas sequi, esse libero asperiores
    debitis eveniet commodi hic ad.
  </p>
</details>

Syntax

selector::details-content

Examples

Basic example

This example demonstrates basic usage of the ::details-content pseudo-element to style the content of a details element.

HTML

Our <details> element includes a summary element, whose contents will always be visible. The details content includes a p element.

<details>
  <summary>Click me</summary>
  <p>Here is some content</p>
</details>

CSS

We set a background-color on the ::details-content pseudo-element:

details::details-content {
  background-color: #a29bfe;
}

Result

Click on the summary to view the detail contents.

Basic_example

Transition example

In this example the ::details-content pseudo-element is used to set a transition on the content of the details element so that it smoothly fades into view when expanded, and fades out again when collapsed.

HTML

The HTML is the same as in the previous example.

<details>
  <summary>Click me</summary>
  <p>Here is some content</p>
</details>

CSS

To achieve our transition, we specify two separate transitions inside the transition shorthand property:

  • The opacity property is given a basic transition over 600ms to create the fade-in/fade-out effect.
  • The content-visibility property (which is toggled between hidden and visible when the <details> content is expanded/collapsed) is given a 600ms transition with the transition-behavior value allow-discrete specified. This opts the browser into having a transition started on content-visibility, the animation behavior of which is discrete. The effect is that the content is visible for the entire duration of the transition, allowing other transitions to be seen. If this transition was not included, the content would immediately disappear when the <details> content was collapsed — you wouldn't see the smooth fade-out.
details::details-content {
  opacity: 0;
  transition:
    opacity 600ms,
    content-visibility 600ms allow-discrete;
}

details[open]::details-content {
  opacity: 1;
}

Result

To see the animation, toggle the visibility of the detail contents by clicking on the summary.

Transition_example

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。