toggling styles on label (active/focus) for input field with css only

后端 未结 7 1452
无人共我
无人共我 2020-12-16 15:28

Wondering whether if there is a css-only way to perform to toggle styles on the corresponding label on input\'s focus. So far I have:

    $(         


        
7条回答
  •  醉梦人生
    2020-12-16 16:07

    It's possible with CSS only, without switching the order of the label and input. You can use a :focus-within CSS pseudo-class on the parent element, which applies to elements, that has a child element with the focus.

    In your example, you could use the following:

    .row:focus-within label {
        color: red;
    }
    

    Note, that this pseudo-class is relatively new, so only modern browsers support it.

提交回复
热议问题