
- CSS 教程
- CSS - 首頁
- CSS - 路線圖
- CSS - 簡介
- CSS - 語法
- CSS - 選擇器
- CSS - 包含
- CSS - 測量單位
- CSS - 顏色
- CSS - 背景
- CSS - 字型
- CSS - 文字
- CSS - 圖片
- CSS - 連結
- CSS - 表格
- CSS - 邊框
- CSS - 塊級邊框
- CSS - 內聯邊框
- CSS - 外邊距
- CSS - 列表
- CSS - 內邊距
- CSS - 游標
- CSS - 輪廓
- CSS - 尺寸
- CSS - 捲軸
- CSS - 行內塊
- CSS - 下拉選單
- CSS - 可見性
- CSS - 溢位
- CSS - 清除浮動
- CSS - 浮動
- CSS - 箭頭
- CSS - 調整大小
- CSS - 引號
- CSS - 順序
- CSS - 定位
- CSS - 連字元
- CSS - 懸停
- CSS - 顯示
- CSS - 聚焦
- CSS - 縮放
- CSS - 轉換
- CSS - 高度
- CSS - 連字元字元
- CSS - 寬度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪輯
- CSS - 書寫模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - 指標事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS 高階
- CSS - 網格
- CSS - 網格佈局
- CSS - Flexbox
- CSS - 可見性
- CSS - 定位
- CSS - 圖層
- CSS - 偽類
- CSS - 偽元素
- CSS - @規則
- CSS - 文字效果
- CSS - 分頁媒體
- CSS - 列印
- CSS - 佈局
- CSS - 驗證
- CSS - 圖片精靈
- CSS - Important
- CSS - 資料型別
- CSS3 教程
- CSS3 - 教程
- CSS - 圓角
- CSS - 邊框圖片
- CSS - 多重背景
- CSS - 顏色
- CSS - 漸變
- CSS - 盒陰影
- CSS - 盒裝飾中斷
- CSS - Caret Color
- CSS - 文字陰影
- CSS - 文字
- CSS - 2D 轉換
- CSS - 3D 轉換
- CSS - 過渡
- CSS - 動畫
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按鈕
- CSS - 分頁
- CSS - 變數
- CSS - 媒體查詢
- CSS - 函式
- CSS - 數學函式
- CSS - 遮罩
- CSS - 形狀
- CSS - 樣式圖片
- CSS - 特異性
- CSS - 自定義屬性
- CSS 響應式
- CSS RWD - 簡介
- CSS RWD - 視口
- CSS RWD - 網格檢視
- CSS RWD - 媒體查詢
- CSS RWD - 圖片
- CSS RWD - 影片
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS - :focus-visible偽類
CSS 中的:focus-visible偽類表示已獲得焦點的元素。此類通常在使用者單擊、點選元素或使用鍵盤的 Tab 鍵選擇元素時觸發。
:focus-visible偽類可用於根據使用者首選的操作模式(使用滑鼠或鍵盤)應用不同的焦點指示器。
語法
:focus-visible { /* ... */ }
:focus 與 :focus-visible
CSS 中的:focus 和:focus-visible偽類用於定位當前具有鍵盤焦點的元素,但它們的行為有所不同,尤其是在使用者體驗和可訪問性方面。
:focus | :focus-visible |
---|---|
定位具有鍵盤焦點的元素,無論該焦點是透過鍵盤導航還是透過滑鼠單擊獲得。 | 定位具有鍵盤焦點的元素,但僅在使用鍵盤導航時可見。它有助於透過確保僅在透過鍵盤輸入聚焦元素時才應用樣式(而不是透過滑鼠或觸控輸入聚焦時)來改善使用者體驗。 |
它不考慮使用者如何到達聚焦的元素,因此可能包括使用者使用滑鼠單擊或在觸控裝置上點選元素的情況。 | 它對於為鍵盤使用者建立更易訪問和使用者友好的體驗特別有用,因為它避免了在使用滑鼠單擊元素時應用可能分散注意力或不相關的樣式。 |
a:focus { // Styles for elements //that have keyboard focus } |
button:focus-visible { //Styles for elements //that have keyboard focus //and are visible through //keyboard navigation } |
CSS :focus-visible 示例
讓我們看一個顯示:focus 和:focus-visible區別的示例。嘗試使用滑鼠以及鍵盤將焦點放在按鈕上,並檢視區別。
<html> <head> <style> /* Apply focus styles only when accessed via keyboard navigation */ button:focus-visible { outline: 2px solid green; background-color: yellow; } /* Apply focus styles for all focused elements */ button:focus { outline: 3px solid blue; } </style> </head> <body> <div> <button>focus-visible</button> <button>focus-only</button> </div> </body> </html>
以下是一個button的經典示例,它展示了選擇器:focus-visible如何區別於:focus。我們通常不希望在單擊button後看到焦點環,但我們希望在使用鍵盤瀏覽網站時在button上看到焦點指示器。在下面的示例中,嘗試使用鍵盤並按 Tab 鍵,看看:focus-visible如何幫助我們透過鍵盤和 Tab 鍵解決焦點環問題。
<html> <head> <style> label { display: grid; font-size: 18px; color: black; width: 400px; } select { padding: 10px 16px; font-size: 16px; color: black; background-color: #fff; border: 1px solid #597183; border-radius: 8px; margin-top: 25px; width: 300px; transition: all 0.3s ease; } .focus-only:focus { outline: 3px solid orange; } .focus-visible-only:focus-visible { outline: 4px dashed aqua; } </style> </head> <body> <p>Compare what happens when you click on buttons with a mouse, versus when you tab through them using a keyboard. </p> <button class="focus-only"> focus-only button</button><br><br> <button class="focus-visible-only"> focus-visible button</button><br><br> </body> </html>
CSS :focus - 後備
使用@supports檢查:focus-visible的瀏覽器支援和相容性,並在:focus規則中重複相同的樣式,無論它在舊瀏覽器中是否正常工作。即使您完全沒有為:focus指定任何內容作為後備選項,舊瀏覽器也會顯示本機預設輪廓。這在下面的語法中得到演示
button:focus { /* some exciting button focus styles */ } @supports (:focus-visible) { button:focus { /* undo all the above focused button styles */ } button:focus-visible { /* and then reapply the styles here instead */ } }
廣告