要在網頁上創建可折疊部分,您可以使用HTML5 <details></details>
和<summary></summary>
元素。 <details></details>
元素表示用戶可以與之交互以獲取其他信息或控件的小部件。 <summary></summary>
元素必須是<details></details>
元素的直接子,為<details></details>
元素指定摘要或標籤。
這是如何使用這些元素的基本示例:
<code class="html"><details> <summary>Click to expand</summary> <p>This is the content that will be shown when the user expands the section.</p> </details></code>
在此示例中:
<details></details>
元素創建可折疊的小部件。<summary></summary>
元素為小部件提供了標籤。單擊此標籤將切換<details></details>
元素中內容的可見性。<details></details>
元素內的內容( <summary></summary>
元素)是隱藏或顯示的。您可以將任何HTML內容放置在<details></details>
元素中,從而在網頁上允許靈活且豐富的可折疊部分。
使用<details></details>
和<summary></summary>
元素可以通過多種方式顯著增強用戶體驗:
<details></details>
元素上的open
屬性也可以用於設置小部件的初始狀態,這可能對可訪問性有益。是的, <details></details>
和<summary></summary>
元素可以使用CSS進行樣式,從而可以高度自定義以匹配您的網站的設計。這是您可以設計這些元素的方式:
<details></details>
元素:您可以使用CSS更改整個可折疊部分的外觀。例如,您可以設置邊框,背景顏色和填充:<code class="css">details { border: 1px solid #aaa; border-radius: 4px; padding: .5em .5em 0; } details[open] { padding: .5em; } details[open] summary { border-bottom: 1px solid #aaa; margin-bottom: .5em; }</code>
<summary></summary>
元素:您可以修改摘要標籤的外觀。例如,您可以更改顏色,字體並添加圖標:<code class="css">summary { font-weight: bold; color: #333; } summary::marker { /* Hide default marker */ content: ""; } summary::before { /* Add custom marker */ content: "▶ "; font-size: .8em; } details[open] summary::before { content: "▼ "; }</code>
::marker
偽元素可用於樣式默認標記(通常是三角形),但瀏覽器支持受到限制。使用::before
和::after
偽元素可能是添加自定義標記的更一致的方法。<code class="css">summary:hover, summary:focus { color: #000; cursor: pointer; }</code>
請記住,儘管您可以設計這些元素,但一些瀏覽器可能具有默認樣式,您需要使用自定義CSS覆蓋這些樣式。
<details></details>
和<summary></summary>
是HTML5標準的一部分,但仍有一些瀏覽器兼容性問題要考慮:
::marker
偽元素,具有有限的瀏覽器支持。您可能需要使用替代方法,例如::before
和::after
,以實現跨瀏覽器的一致樣式。display: block
進行<summary></summary>
。您可能需要使用CSS將這些差異歸一化。您可以查看有關資源的最新瀏覽器兼容性信息,例如我可以使用嗎? (caniuse.com)在不同瀏覽器中對這些HTML元素的支持保持最新信息。
以上是您如何使用&lt;詳細信息&gt; 和&lt;摘要&gt; 創建可折疊部分的元素?的詳細內容。更多資訊請關注PHP中文網其他相關文章!