<summary>: 披露摘要元素
Baseline 廣泛可用 *
<summary> HTML 元素用於指定 <details> 元素的披露框的摘要、標題或圖例。點選 <summary> 元素會切換其父級 <details> 元素的開啟和關閉狀態。
試一試
<details>
<summary>
I have keys but no doors. I have space but no room. You can enter but can’t
leave. What am I?
</summary>
A keyboard.
</details>
details {
border: 1px solid #aaaaaa;
border-radius: 4px;
padding: 0.5em 0.5em 0;
}
summary {
font-weight: bold;
margin: -0.5em -0.5em 0;
padding: 0.5em;
}
details[open] {
padding: 0.5em;
}
details[open] summary {
border-bottom: 1px solid #aaaaaa;
margin-bottom: 0.5em;
}
屬性
此元素僅包含全域性屬性。
用法說明
<summary> 元素的內容可以是任何標題內容、純文字或可以在段落中使用的 HTML。
<summary> 元素只能作為 <details> 元素的第一個子元素使用。當用戶點選摘要時,父級 <details> 元素會切換開啟或關閉狀態,然後一個 toggle 事件會發送到 <details> 元素,你可以用它來了解狀態何時發生變化。
<details> 的內容為 <summary> 提供了可訪問描述。
預設標籤文字
如果 <details> 元素的第一個子元素不是 <summary> 元素,使用者代理將使用預設字串(通常是“Details”)作為披露框的標籤。
預設樣式
根據 HTML 規範,<summary> 元素的預設樣式包括 display: list-item。這使得可以更改或移除預設顯示為標籤旁邊的披露小部件的圖示(通常是三角形)。
你也可以將樣式更改為 display: block 以移除披露三角形。
有關詳細資訊,請參閱瀏覽器相容性部分,因為並非所有瀏覽器都完全支援此元素的全部功能。
對於基於 WebKit 的瀏覽器(如 Safari),可以透過非標準 CSS 偽元素 ::-webkit-details-marker 控制圖示顯示。要移除披露三角形,請使用 summary::-webkit-details-marker { display: none }。
示例
以下是一些顯示 <summary> 用法的示例。你可以在 <details> 元素的文件中找到更多示例。
基本示例
在一個 <details> 元素中使用 <summary> 的基本示例
<details open>
<summary>Overview</summary>
<ol>
<li>Cash on hand: $500.00</li>
<li>Current invoice: $75.30</li>
<li>Due date: 5/6/19</li>
</ol>
</details>
結果
作為標題的摘要
你可以在 <summary> 中使用標題元素,如下所示
<details open>
<summary><h4>Overview</h4></summary>
<ol>
<li>Cash on hand: $500.00</li>
<li>Current invoice: $75.30</li>
<li>Due date: 5/6/19</li>
</ol>
</details>
結果
目前存在一些可以透過 CSS 解決的間距問題。
警告: 分配給 <summary> 元素的角色因瀏覽器而異。有些瀏覽器仍然為其分配預設的button 角色,這會移除其所有子元素的角色。這種不一致性可能會給螢幕閱讀器等輔助技術使用者帶來問題(上一個示例中的 <h4> 將被移除其角色,並且不會被這些使用者視為標題)。你應該在多個平臺上測試你的 <summary> 實現,以確保一致的可訪問性支援。
摘要中的 HTML
此示例為 <summary> 元素添加了一些語義,以指示標籤的重要性
<details open>
<summary><strong>Overview</strong></summary>
<ol>
<li>Cash on hand: $500.00</li>
<li>Current invoice: $75.30</li>
<li>Due date: 5/6/19</li>
</ol>
</details>
結果
更改摘要的圖示
<summary> 元素的標記(即披露三角形)可以使用 CSS 進行自定義。可以使用 ::marker 偽元素定位標記,它接受 list-style 簡寫屬性及其長手元件屬性,例如 list-style-type。這使得可以將三角形更改為影像(通常使用 list-style-image)或字串(包括表情符號)。在此示例中,我們替換了一個披露小部件的內容,並透過設定 list-style: none 來移除另一個小部件的圖示,然後透過生成的內容新增自定義披露圖示。
CSS
在第一個披露小部件中,我們設定 ::marker 的樣式,根據 <details> 元素的 [open] 屬性更改content。對於第二個小部件,我們使用 list-style 屬性移除標記,然後使用 ::after 偽元素新增帶有樣式的生成內容。我們還包括 ::-webkit-details-marker 的樣式以定位 Safari。瀏覽器特定的偽元素的選擇器包含在 :is() 偽類中,這樣它就不會使選擇器列表失效。
details {
font-size: 1rem;
font-family: "Open Sans", "Calibri", sans-serif;
border: solid;
padding: 2px 6px;
margin-bottom: 1em;
}
details:first-of-type summary::marker,
:is(::-webkit-details-marker) {
content: "+ ";
font-family: monospace;
color: red;
font-weight: bold;
}
details[open]:first-of-type summary::marker {
content: "− ";
}
details:last-of-type summary {
list-style: none;
&::after {
content: "+";
color: white;
background-color: darkgreen;
border-radius: 1em;
font-weight: bold;
padding: 0 5px;
margin-inline-start: 5px;
}
[open] &::after {
content: "−";
}
}
details:last-of-type summary::-webkit-details-marker {
display: none;
}
CSS 包括 [open] 屬性選擇器,僅當 open 屬性存在時(當 <details> 開啟時)才匹配。:first-of-type 和 :last-of-type 偽類定位相同型別的第一個和同級元素。我們將帶字首的 -webkit- 偽元素包含在 :is() 偽類中,因為它接受寬容選擇器列表,所以如果帶字首的偽元素在瀏覽器中無效,整個選擇器塊也不會失效。我們還使用了 CSS 巢狀。請參閱 CSS 選擇器模組。
HTML
<h1>Quotes from Helen Keller</h1>
<details>
<summary>On women's rights</summary>
<p>
<q>We have prayed, we have coaxed, we have begged, for the vote, with the
hope that men, out of chivalry, would bestow equal rights upon women and
take them into partnership in the affairs of the state. We hoped that
their common sense would triumph over prejudices and stupidity. We thought
their boasted sense of justice would overcome the errors that so often
fetter the human spirit; but we have always gone away empty-handed. We
shall beg no more.</q>
</p>
</details>
<details>
<summary>On optimism</summary>
<p>
<q>Optimism is the faith that leads to achievement; nothing can be done
without hope.</q>
</p>
</details>
結果
技術摘要
規範
| 規範 |
|---|
| HTML # the-summary-element |
瀏覽器相容性
載入中…