question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to target sibling styled component on focus

See original GitHub issue

Just 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lukashalacommented, Feb 20, 2019

I have a same issue as @aeciolevy

${StyledInput}:focus ~ & {...} is not working.

But input:focus ~ & {...} is working fine. Strange.

1reaction
mxstbrcommented, Sep 28, 2017

Just gotta put the input before the label and it works!

<Wrapper>
      <Input />
      <Label>Some text</Label>
     </Wrapper>

Demo: https://www.webpackbin.com/bins/-Kv7hj0-Erhm1Rvgjwv1

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found