':hover element change ::after style another component

How can I change width StyledLi::after from 0% to 90%, after hover on StyledLink

const StyledLink = styled(Link)`
    text-decoration: none;
    margin-right: 20px;
    font-size: 20px;
    color: grey;
    transition: 0.2s;

    :hover {
        color: blue;
    }
`
const StyledLi = styled.li`
    ::after {
        content: "";
        display: block;
        width: 0%;
        height: 2px;
        background-color: blue;
    }
`


Solution 1:[1]

Try this. You can use absolute positioning. Set relative position for StyledLi and absolute position for :after in StyledLink.

Second variant, use pseudo element for StyledLi and hover for StyledLi.

You can see two examples below.

const Container = styled.div``;

const List = styled.ul``;

const StyledLi = styled.li`
  position: relative;
`;

const StyledLink = styled.a`
  text-decoration: none;
  margin-right: 20px;
  font-size: 20px;
  color: grey;
  transition: 0.2s;

  :after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    display: block;
    width: 0%;
    height: 2px;
    background-color: blue;
  }

  :hover {
    color: blue;
  }

  :hover:after {
    width: 90%;
  }
`;

const App = () => (
  <Container>
    <List>
      <StyledLi>
        <StyledLink>List Item</StyledLink>
      </StyledLi>
      <StyledLi>
        <StyledLink>List Item</StyledLink>
      </StyledLi>
      <StyledLi>
        <StyledLink>List Item</StyledLink>
      </StyledLi>
    </List>
  </Container>
);

Second variant

const Container = styled.div``;

const List = styled.ul``;

const StyledLi = styled.li`
  color: grey;

  :after {
    content: "";
    bottom: 0;
    left: 0;
    display: block;
    width: 0%;
    height: 2px;
    background-color: blue;
  }

  :hover:after {
    width: 90%;
  }

  :hover {
    color: blue;
  }
`;

const StyledLink = styled.a`
  text-decoration: none;
  margin-right: 20px;
  font-size: 20px;
  transition: 0.2s;
  color: inherit;
`;

const App = () => (
  <Container>
    <List>
      <StyledLi>
        <StyledLink>List Item</StyledLink>
      </StyledLi>
      <StyledLi>
        <StyledLink>List Item</StyledLink>
      </StyledLi>
      <StyledLi>
        <StyledLink>List Item</StyledLink>
      </StyledLi>
    </List>
  </Container>
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1