
- 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 - pointer-events
- 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 - box-decoration-break
- 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 響應式 - 簡介
- CSS 響應式 - 視口
- CSS 響應式 - 網格檢視
- CSS 響應式 - 媒體查詢
- CSS 響應式 - 圖片
- CSS 響應式 - 影片
- CSS 響應式 - 框架
- CSS 工具
- CSS - px 到 em 轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS - 焦點效果
CSS 焦點效果用於使表單元素(如輸入欄位、按鈕和連結)對與網頁互動的使用者更具動態性和吸引力。
CSS 中的:focus 偽類用於在元素獲得焦點(透過單擊或按 Tab 鍵)時對其進行定位。其目的是應用樣式或觸發特定行為以增強使用者體驗或提供額外的視覺反饋。
聚焦我!
:focus 是一個工具,可以使互動式元素更具動態性和吸引力,尤其是在使用者使用鍵盤導航時。
目錄
什麼是焦點偽類?
- 在 CSS 中,偽類 :focus 是一種選擇器,用於定位和設定當元素獲得焦點(通常透過鍵盤導航或滑鼠互動)時的樣式。
- 焦點效果主要用於表單欄位、按鈕等互動式元素,以便向用戶清楚地指示獲得焦點的元素。
- 焦點效果有助於為網站新增動態且引人注目的外觀,並提高可訪問性。
輸入欄位的 CSS 焦點效果
以下是如何在獲得焦點時設定輸入欄位樣式的示例
示例
<!DOCTYPE html> <html> <head> <style> .input-field { width: 80%; height: 30px; padding: 5px; margin: 10px 0; background-color: #f0f0f0; border: 1px solid black; font-size: 16px; outline: none; } .input-field:focus { border: 3px solid darkblue; padding: 10px; } </style> </head> <body> <div class="main"> <input type="text" class="input-field" placeholder="Enter your name" tabindex="0"> <input type="text" class="input-field" placeholder="Enter your email" tabindex="1"> <input type="text" class="input-field" placeholder="Enter your password" tabindex="2"> </div> </body> </html>
帶有焦點效果的 CSS 按鈕
以下是如何在聚焦狀態下設定按鈕樣式的示例。
示例
<!DOCTYPE html> <html> <head> <style> button { padding: 10px 20px; margin: 10px; background-color: #ffffff; color: #228B22; border: 2px solid #228B22; border-radius: 5px; text-decoration: none; font-size: 16px; font-weight: bold; transition: background-color 0.3s, color 0.3s; } button:focus { background-color: #FFCC33; color: #ffffff; outline: none; transform: scale(1.2); } </style> </head> <body> <button> Focus on me! </button> </body> </html>
帶有焦點效果的 CSS 邊框
這是一個示例,它顯示了當元素獲得焦點時邊框是如何變化的
示例
<!DOCTYPE html> <html> <head> <style> div { padding: 10px 20px; margin: 10px; background-color: #228B22; color: #ffffff; border: 2px solid #228B22; border-radius: 5px; text-decoration: none; font-size: 16px; font-weight: bold; transition: all 0.3s; } div:focus { border-radius: 20px; outline: none; } </style> </head> <body> <div tabindex="0"> Focus on me! </div> </body> </html>
帶有焦點效果的 CSS 盒陰影
這是一個示例,當 div 獲得焦點時會新增盒陰影
示例
<!DOCTYPE html> <html> <head> <style> div { padding: 10px 20px; margin: 10px; background-color: #228B22; color: #ffffff; border: 2px solid #228B22; border-radius: 5px; text-decoration: none; font-size: 16px; font-weight: bold; transition: all 0.3s; } div:focus { box-shadow: 20px 20px 10px grey; outline: none; } </style> </head> <body> <div tabindex="0"> Focus on me! </div> </body> </html>
聚焦時的 CSS 樣式
這是一個示例,其中在聚焦時為按鈕添加了陰影效果
示例
<!DOCTYPE html> <html> <head> <style> body { height: 300px; overflow: hidden; display: grid; justify-content: center; align-items: center; } .glow { padding: 10px; width: 250px; height: 50px; border: none; outline: none; color: #fff; background: #111; cursor: pointer; position: relative; z-index: 0; border-radius: 20px; } .glow:before { content: ''; background: linear-gradient(60deg, #ff0000, #ff7300, #fffb00, #48ff00, #00ffd5, #002bff, #7a00ff, #ff00c8, #ff0000); position: absolute; top: -4px; left:-4px; background-size: 400%; z-index: -1; filter: blur(15px); width: calc(100% + 6px); height: calc(100% + 6px); animation: glowing 20s linear infinite; opacity: 0; transition: opacity .3s ease-in-out; border-radius: 10px; } .glow:focus:before { opacity: 1; } .glow:after { z-index: -1; content: ''; position: absolute; width: 100%; height: 100%; background: #111; left: 0; top: 0; border-radius: 10px; } @keyframes glowing { 0% { background-position: 0 0; } 50% { background-position: 400% 0; } 100% { background-position: 0 0; } } </style> </head> <body> <button class="glow" type="button" tabindex="0"> FOCUS ON ME! </button> </body> </html>
廣告