Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style an <input> element on focus using styled components?

Here is my styled component:

const StyledInput = styled.input`
    width: 90%;
    padding: grey;
    border: 0px;
    font-size: 12px;
    &:input:focus {
        outline: none;
        box-shadow: 0px 0px 2px red;
    }
`;

And the implementation:

 <StyledInput
     autoFocus={true}
     type="text"
     onChange={this.handleChange}
     placeholder={this.props.placeHolder}
 />

But the borders are not changing to red when focused (keep the default Chrome blue). How can I change the input borders on focus?

like image 663
Mendes Avatar asked Jan 07 '20 13:01

Mendes


1 Answers

Change &:input:focus { to &:focus {:

const StyledInput = styled.input`
    width: 90%;
    padding: grey;
    border: 0px;
    font-size: 12px;
    &:focus {
        outline: none;
        box-shadow: 0px 0px 2px red;
    }
`;

As isherwood pointed out:

you're essentially doubling up on the input portion of your selector as you have it now.

like image 131
Mr T Avatar answered Nov 13 '22 02:11

Mr T