簡體   English   中英

包裝輸入的 Label - 如何使焦點樣式僅在使用鍵盤聚焦時才可見,而不是鼠標單擊?

[英]Label that wraps an input - how to make focus style visible only when focused using the keyboard, not mouse click?

在以下示例中,按鈕的焦點樣式僅在使用鍵盤(而不是鼠標單擊)獲得焦點時可見。

如何在包含隱藏輸入的 label 上實現相同的效果?

在下面的代碼中,當 label 使用鍵盤或鼠標單擊聚焦時,標簽的聚焦樣式可見。 我希望它僅在使用鍵盤聚焦時才可見。

 .visually-hidden { position: absolute; border: 0; width: 1; height: 1; padding: 0; margin: -1; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; word-wrap: normal; } body { margin: 40px; } div { display: flex; gap: 20px; } input:focus { outline: 4px solid red; } button:focus-visible { outline: 4px solid red; } label:focus-within { outline: 4px solid red; }
 <div> <input type="text" /> <button>Submit</button> <label> <input class="visually-hidden" type="checkbox" /> Label </label> </div>

更新其內部輸入的 styles 以便它可以在視覺隱藏的同時獲得焦點:

 label { position:relative; }.visually-hidden { position: absolute; inset: 0; margin: 0; padding: 0; -webkit-appearance: none; -moz-appearance: none; appearance: none; overflow: hidden; } button:focus-visible, input:focus-visible{ outline: 4px solid red; } body { margin: 40px; } div { display: flex; gap: 20px; }
 <div> <input type="text" /> <button>Submit</button> <label> <input class="visually-hidden" type="checkbox" /> Label </label> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM