:focus
Lớp giả CSS :focus
đại diện cho thành phần (như một input form) nhận focus. Nó được kích hoạt khi người dùng kích hoặc tap lên thành phần hoặc chọn nó bằng phím "tab".
/* Chọn bấy kỳ thành phần input nào được focus */
input:focus {
color: red;
}
Chú ý: Lớp giả này chỉ áp dụng với chính thành phần được focus. Sử dụng :focus-within
nếu bạn muốn chọn thành phần chứa một thành phần được focus.
Cú pháp
:focus
Ví dụ
HTML
<input class="red-input" value="I'll be red when focused."><br>
<input class="blue-input" value="I'll be blue when focused.">
CSS
.red-input:focus {
background: yellow;
color: red;
}
.blue-input:focus {
background: yellow;
color: blue;
}
Result
Chi tiết
Chi tiết | Trạng thái | Bình luận |
---|---|---|
HTML Living Standard The definition of ':focus' in that specification. |
Living Standard | Defines HTML-specific semantics. |
Selectors Level 4 The definition of ':focus' in that specification. |
Working Draft | No change. |
Selectors Level 3 The definition of ':focus' in that specification. |
Recommendation | No change. |
CSS Level 2 (Revision 1) The definition of ':focus' in that specification. |
Recommendation | Initial definition. |
Tương thích trình duyệt
BCD tables only load in the browser