from official styled components documentation

For complex selector patterns, the ampersand (&) can be used to refer back to the main component. Here are some more examples of its potential usage:

const Thing = styled.div.attrs({ tabIndex: 0 })`
  color: blue;

  &:hover {
    color: red; // <Thing> when hovered
  }

  & ~ & {
    background: tomato; // <Thing> as a sibling of <Thing>, but maybe not directly next to it
  }

  & + & {
    background: lime; // <Thing> next to <Thing>
  }

  &.something {
    background: orange; // <Thing> tagged with an additional CSS class ".something"
  }

  .something-else & {
    border: 1px solid; // <Thing> inside another element labeled ".something-else"
  }
`

Answer from Mulli on Stack Overflow
🌐
styled-components
styled-components.com › docs › basics
styled-components
const Thing = styled.div` && { color: blue; } ` const GlobalStyle = createGlobalStyle` div${Thing} { color: red; } ` render( <React.Fragment> <GlobalStyle /> <Thing> I'm blue, da ba dee da ba daa </Thing> </React.Fragment> ) ... If you put selectors in without the ampersand, they will refer to children of the component.
🌐
Josh W. Comeau
joshwcomeau.com › react › demystifying-styled-components
Demystifying styled-components • Josh W. Comeau
But in this case, we have two classes! The two selectors, .abc123 and .def456, are equally matched. So the algorithm falls back to a secondary rule: it looks at the order of rules defined in the stylesheet.
🌐
Emotion
emotion.sh › docs › styled
Emotion – Styled Components
By default, Emotion passes all props (except for theme) to custom components and only props that are valid html attributes for string tags. You can customize this by passing a custom shouldForwardProp function. You can also use @emotion/is-prop-valid (which is used by emotion internally) to filter out props that are not valid as html attributes. ... You can create dynamic styles that are based on props and use them in styles.
🌐
GitHub
github.com › styled-components › styled-components › issues › 142
RFC: Component Selector · Issue #142 · styled-components/styled-components
October 27, 2016 - I was chatting to @markdalgleish at Reactive conf and he had this great idea to implement a other-styled-component-selector. The API would look like this: const Button = styled.button`some: styles;` const Box = styled.box` > ${Button} { ...
Author   mxstbr
🌐
Josh W. Comeau
joshwcomeau.com › css › styled-components
The styled-components Happy Path: my personal suite of “best practices” • Josh W. Comeau
January 25, 2021 - styled-components allows us to “embed” one component in another like this. When the component is rendered, it pops in the appropriate selector, a class that matches the Wrapper styled-component.
🌐
egghead.io
egghead.io › lessons › react-style-pseudo-selectors-and-classes-in-styled-components
Style pseudo-selectors and classes in Styled Components | egghead.io
The pseudo-selectors, such as hover and before, work the exact same as regular CSS. In our element or class styling backticks, we can set them like :hover{} and :before{} to change what the element or component does. ... Sara Vieira: [0:00] I have this button, which doesn't do anything. It also doesn't have a hover state, which is quite annoying. We want to let people know that they can actually use this button. [0:11] How do you do this in styled components?
🌐
Medium
medium.com › @personnamedmike › react-styled-components-a-basic-guide-2e03c7bbbfcd
React Styled-Components — a Basic Guide | by starrdev | Medium
April 7, 2022 - styled refers to the class which was imported from the “styled-component” library · h1 can be any HTML selector you want to want to style
🌐
Gatsby
gatsbyjs.com › documentation › how-to guides › styling › using styled components
Styled Components | Gatsby
Styled Components lets you use actual CSS syntax inside your components. Styled Components is a variant on “CSS-in-JS”—which solves many of the problems with traditional CSS.
Find elsewhere
🌐
GitHub
github.com › styled-components › styled-components › issues › 2530
Using styled component as selector with ":focus" attribute · Issue #2530 · styled-components/styled-components
import React from 'react'; import styled from 'styled-components'; const StyledInput = styled.input``; const StyledBox = styled.div` background-color: blue; padding: 20px; ${StyledInput}:focus & { border: 5px solid green; } `; const Example = () => { return ( <StyledBox> <StyledInput /> </StyledBox> ); }; export default Example;
🌐
GitHub
github.com › mui › material-ui › issues › 40641
[system] Use components as selectors with styled() · Issue #40641 · mui/material-ui
January 16, 2024 - Duplicates I have searched the existing issues Summary Following the documentation on the Components Selector API, using a regular styled component as a selector works but it is not possible to use MUI components as selectors. Examples h...
Author   CharlineCouchot
🌐
Sentry
develop.sentry.dev › frontend › using-styled-components
Using Styled Components - Sentry Developer Documentation
When defining these attributes, don't forget that browsers have built-in selectors like :disabled, :focus, :first-child and so on. ... import styled from "@emotion/styled"; // ✅ Use a data attribute to pass in your state, and leverage CSS selectors: const Label = styled("label")` color: inherit; &[data-is-error="true"] { color: ${(p) => p.theme.error400}; } &:disabled { color: ${(p) => p.theme.gray200}; } `; return ( <Label disabled={false} data-is-error={true}> There is an error </Label> );
🌐
CodeMiner42
blog.codeminer42.com › home › beginner › a stylish guide to styled components #1
A stylish guide to styled components #1 - The Miners
September 4, 2024 - We still can have class name collisions as nothing prevent us from having the same class name for two different components. Taking a closer look at the code above, we no longer need a hard-coded reference to a class defined elsewhere in our system when using CSS-in-JS. Instead, the library itself is responsible for generating unique selectors automatically. We no longer have to worry about class name collisions in the global scope. On making use of CSS-in-JS, the scope of all style rules created only refers to that specific selector of the same scope.
Top answer
1 of 2
5

${Link} is pointing to const Link i.e.: "Hovering my parent changes my style" which gets a class of sc-kAzzGY.

& is kinda like saying "And add this to the current class(es)/id(s)/etc." So,

.my-class {
 some-css: awesomeness;
 &:hover {
   more-css: extra-cool;
 }
}

is equivalent to:

.my-class {
 some-css: awesomeness;
}
.my-class:hover {
   more-css: extra-cool;
}

Therefore, & points to the containing element const Icon i.e. the speech bubble and gets a class of kDmLky.

When Link is hovered, cause Icon to have fill: rebeccapurple

EDIT:

Just to clarify things a bit more:

When you have a declaration block inside of another declaration block like the example below, that inner declaration block becomes an independent one.

const Icon = styled.svg`
  flex: none;
  transition: fill 0.25s;
  width: 48px;
  height: 48px;

  ${Link}:hover & {       // This declaraition block becomes an independent one
    fill: rebeccapurple;
  }
`;

And the result, in this case, is a declaration block with a selection that says:

When you have a class & which is descendent of the class ${Link} which is in the hover state, apply these rules:

fill: rebeccapurple;

NOTE: ${Link} refers to the Link class and & refers to the Icon class (svg).

2 of 2
3
${Link}:hover &

Here the ampersand & is the short hand way for referring to the top level component being defined, so I would read it as

${Link}:hover ${Icon}

i.e. it is referring to an Icon component contained inside of a Link component being hovered over

I would also recommend this link to see the more general use case for component selectors with styled components, where it's used in a parent child configuration for selection, and applied to the child

🌐
GitHub
github.com › styled-components › styled-components › issues › 3401
Styled components with parent selectors are not re-rendering · Issue #3401 · styled-components/styled-components
February 9, 2021 - Environment styled-components@5.2.0 or newer Steps to reproduce Create a styled component with parent selector in it. Add some conditional css Change the props Expected Behavior The component re-renders when the props change and displays...
Author   andorthehood
🌐
DEV Community
dev.to › paulclindo › everything-you-need-to-know-about-styled-components-2a6a
Everything you need to know about Styled Components - DEV Community
May 4, 2021 - If you want to style the components, you put parentheses and pass the actual component. In this case, we are gonna style a button from material-ui and adjust his size with css. import Button from '@material-ui/core/Button'; const FullWidthButton = styled(Button)` width: 300px; ` Keep in mind that you can style pseudo-selectors and classes inside of the styled component if needed.
🌐
MUI
mui.com › system › styled
styled() - MUI System
If you inspect this element with the browser DevTools in development mode, you will notice that the class of the component now ends with the MyThemeComponent-root, which comes from the name and slot options that were provided. In addition to this, the color, sx, and variant props are not propagated to the generated div element. If you would like to remove some of the MUI System specific features, you can do it like this: const StyledComponent = styled('div', {}, { name: 'MuiStyled', slot: 'Root', - overridesResolver: (props, styles) => styles.root, // disables theme.components[name].styleOverrides + skipVariantsResolver: true, // disables theme.components[name].variants + skipSx: true, // disables the sx prop }); CopyCopied(or $keyC)