搜尋
首頁web前端css教學在 CSS 中隱藏元素:可訪問的方式

Hiding Elements in CSS: The Accessible Way

在 UI 中隱藏元素很常見,但以可訪問的方式這樣做可以確保您不會無意中排除某些用戶。在本文中,我們將介紹三種場景:

  1. 對螢幕閱讀器隱藏元素。
  2. 僅向螢幕閱讀器顯示元素。
  3. 對所有使用者隱藏元素,包括螢幕閱讀器。

每個部分都包含程式碼範例和實際用例


對螢幕閱讀器隱藏元素

如果您希望某個元素對視力正常的使用者可見,但對螢幕閱讀器隱藏,則可以使用 aria-hidden="true" 屬性或 CSS。

<div aria-hidden="true">This content is hidden from screen readers.</div>

使用案例

  1. 隱藏背景圖片
  2. 不傳達有意義訊息的裝飾圖示或視覺元素。
<button>
  <span aria-hidden="true">+</span>
  Add Item
</button>

僅向螢幕閱讀器顯示元素

要讓某個元素對螢幕閱讀器可見,但對視力正常的使用者不可見,您可以使用「視覺隱藏」技術。 Tailwind CSS 為此目的提供了一個預先建置的實用程式類,僅限 sr。

<div>



<p>Writing the sr-only class in CSS would look like this<br>
</p>

<pre class="brush:php;toolbar:false">.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

使用案例

  1. 用於標記互動元素(例如輸入),而不直觀地顯示文字:

    <label for="username" class="sr-only">Username</label>
    <input type="text">
    
    
  2. Providing additional information for screen reader users using aria-describedby

    <div class="sr-only">
    
    
    <li><p><strong>Hiding Input Elements for Custom Radio Buttons</strong><br>
    When creating custom radio buttons (or checkboxes), you often hide the native input element and replace it with a visually styled element. To do this accessibly, the hidden input must remain visible to screen readers.</p></li>
    
    
    <h2>
      
      
      Hiding Elements from All Users
    </h2>
    
    <p>To completely hide an element from both sighted users and screen readers, you can use display: none or visibility: hidden.<br>
    </p>
    
    <pre class="brush:php;toolbar:false">.hidden {
      display: none;
      /* or visibility: hidden; */
    }
    

    申請課程:

    <div>
    
    
    
    <h2>
      
      
      長話短說
    </h2>
    
    <p>下表總結了各種 CSS 屬性如何影響不同類型使用者的可見性:</p>
    <div><table>
    <thead>
    <tr>
    <th><strong>CSS Attribute</strong></th>
    <th><strong>Hides for All Users</strong></th>
    <th><strong>Hides for Sighted Users</strong></th>
    <th><strong>Hides for Screen Readers</strong></th>
    <th><strong>Notes</strong></th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td>display: none</td>
    <td>✅</td>
    <td>✅</td>
    <td>✅</td>
    <td>Completely removes the element from the visual and accessibility tree.</td>
    </tr>
    <tr>
    <td>visibility: hidden</td>
    <td>✅</td>
    <td>✅</td>
    <td>✅</td>
    <td>Hides the element visually but keeps it in the layout and accessibility tree.</td>
    </tr>
    <tr>
    <td>opacity: 0</td>
    <td>❌</td>
    <td>✅</td>
    <td>❌</td>
    <td>Makes the element fully transparent but still visible to screen readers and interactive.</td>
    </tr>
    <tr>
    <td>clip: rect(0, 0, 0, 0)</td>
    <td>✅</td>
    <td>✅</td>
    <td>❌</td>
    <td>Commonly used in the "visually hidden" technique; removes visual rendering but accessible.</td>
    </tr>
    <tr>
    <td>position: absolute; width: 1px; height: 1px;</td>
    <td>✅</td>
    <td>✅</td>
    <td>❌</td>
    <td>Used with the "visually hidden" technique; keeps elements accessible to screen readers.</td>
    </tr>
    <tr>
    <td>aria-hidden="true"</td>
    <td>❌</td>
    <td>❌</td>
    <td>✅</td>
    <td>Removes the element from the accessibility tree but leaves it visually present.</td>
    </tr>
    <tr>
    <td>overflow: hidden</td>
    <td>❌</td>
    <td>✅ (if out of bounds)</td>
    <td>❌</td>
    <td>Hides content visually if it overflows the container but does not affect screen readers.</td>
    </tr>
    <tr>
    <td>height: 0; width: 0;</td>
    <td>✅</td>
    <td>✅</td>
    <td>❌</td>
    <td>Hides content visually while keeping it accessible to screen readers.</td>
    </tr>
    <tr>
    <td>z-index: -1</td>
    <td>❌</td>
    <td>✅</td>
    <td>❌</td>
    <td>Moves the element behind others, making it invisible to sighted users but screen reader-accessible.</td>
    </tr>
    <tr>
    <td>opacity: 0; pointer-events: none;</td>
    <td>✅</td>
    <td>✅</td>
    <td>❌</td>
    <td>Makes an element fully transparent and non-interactive, but accessible to screen readers.</td>
    </tr>
    </tbody>
    </table></div>
    
    
              </div>
    
                
            

以上是在 CSS 中隱藏元素:可訪問的方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
錨定位只是不關心來源訂單錨定位只是不關心來源訂單Apr 29, 2025 am 09:37 AM

錨定定位避開HTML源順序的事實是如此之高,因為它在內容和演示文稿之間的另一個關注點是另一個分離的事實。

保證金是什麼:40px 100px 120px 80px表示?保證金是什麼:40px 100px 120px 80px表示?Apr 28, 2025 pm 05:31 PM

文章討論了CSS保證金屬性,特別是“保證金:40px 100px 120px 80px”,其應用程序以及對網頁佈局的影響。

什麼是不同的CSS邊框特性?什麼是不同的CSS邊框特性?Apr 28, 2025 pm 05:30 PM

本文討論了CSS邊境屬性,重點是自定義,最佳實踐和響應能力。主要論點:邊境 - 拉迪烏斯(Border-Radius)對響應式設計最有效。

什麼是CSS背景,列出屬性?什麼是CSS背景,列出屬性?Apr 28, 2025 pm 05:29 PM

本文討論了CSS背景屬性,它們在增強網站設計方面的用途以及避免的常見錯誤。重點是使用背景大小的響應式設計。

什麼是CSS HSL顏色?什麼是CSS HSL顏色?Apr 28, 2025 pm 05:28 PM

文章討論了CSS HSL顏色,其在網絡設計中的使用以及比RGB的優勢。主要重點是通過直觀的顏色操縱來增強設計和可訪問性。

我們如何在CSS中添加評論?我們如何在CSS中添加評論?Apr 28, 2025 pm 05:27 PM

本文討論了CSS中評論的使用,詳細介紹了單線和多行評論語法。它認為註釋可以增強代碼的可讀性,可維護性和協作,但如果無法正確管理,可能會影響網站性能。

什麼是CSS選擇器?什麼是CSS選擇器?Apr 28, 2025 pm 05:26 PM

本文討論了CSS選擇器,其類型和用於造型HTML元素的用法。它比較ID和類選擇器,並與復雜的選擇器解決性能問題。

哪種類型的CSS持有最高優先級?哪種類型的CSS持有最高優先級?Apr 28, 2025 pm 05:25 PM

本文討論了CSS優先級,重點是具有最高特異性的內聯風格。它解釋了特異性級別,覆蓋方法和用於管理CSS衝突的工具。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器