how to target sibling styled component on focus
See original GitHub issueJust trying to change background-color of an input and a label at the same time on input: focus. but can’t do it. Any help will be appreciated!
const Wrapper = styled.div`
width: 300px;
`;
const Label = styled.label`
background-color: black;
`;
const Input = styled.input`
background-color: grey;
&:focus ~ ${Label} {
background-color: white;
}
`;
<Wrapper>
<Label>Some text</Label>
<Input />
</Wrapper>
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
how to use sibling selector css in styled - Stack Overflow
I have a styled component which looks like:
Read more >How to style adjacent (sibling) elements on hover or focus ...
It should be noted that the + selects the immediatly adjacent element while using ~ instead will search through sibling elements that follow ......
Read more >Adjacent sibling combinator - CSS: Cascading Style Sheets
The adjacent sibling combinator ( + ) separates two selectors and matches the second element only if it immediately follows the first element, ......
Read more >Advanced Usage - styled-components
Passing a ref prop to a styled component will give you one of two things depending on the styled target: the underlying DOM...
Read more >Selectors - W3C
In CSS, pattern matching rules determine which style rules apply to elements ... E + F, Matches any F element immediately preceded by...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have a same issue as @aeciolevy
${StyledInput}:focus ~ & {...}
is not working.But
input:focus ~ & {...}
is working fine. Strange.Just gotta put the input before the label and it works!
Demo: https://www.webpackbin.com/bins/-Kv7hj0-Erhm1Rvgjwv1