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

后端 未结 7 1486
无人共我
无人共我 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 15:46

    If you are willing to switch elements, than here you go

    Demo

    label {
        float: left;
        margin-right: 5px;
    }
    
    input[type=text]:focus + label {
        color: red;
    }
    

    Explanation: We are using + adjacent selector here, so when the textbox is focused, we select the label tag and apply color red

    Note: Don't forget to clear floats ;)

提交回复
热议问题