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

后端 未结 7 1492
无人共我
无人共我 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:48

    Try this way:- Place your label after input and float it left. And apply siblings.

    Html

    CSS

    label {
        float:left;
    }
    input:focus + label {
        color:red;
    }
    

    Demo

    This is a hack to get the adjacent sibling selector work as it applies only on the following element and not the preceding one. ~ will select all the adjascent siblings after this element. So if you are having different .row for each section of inputs then use +.

提交回复
热议问题