Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
react styled components order issue

i'm making button using react and styled-components. i made 3 buttons in line and they should have margin-left : 1rem each other.

so i made Button.js component which get props(size, color) from App.js.

The size value consists of large, medium, and small values, and if one of them is specified, a button is generated in the appropriate size.

If you select one of the colors defined in palette, the value of the color changes to the color that suits it.

const palette = {
  blue: "#228be6",
  gray: "#adb5bd",
  pink: "#f06595",
};

I divided these factors into "sizeStyles" and "colorStyles", respectively.

const sizeStyles = css`
 
  ${({size}) => css`
    height: ${sizes[size].height};
    font-size: ${sizes[size].fontSize}
  `}
const colorStyles = css`
  ${({ theme, color }) => {
    const selected = theme.palette[color];

    return css`
      background: ${selected};
      &:hover {
        background: ${lighten(0.1, selected)};
      }
      &:active {
        background: ${darken(0.1, selected)};
      }
    `;
  }}
`;

The Button component renders a button called StyledButton.

 <StyledButton color={color} size={size} {...rest}>
      {children}
    </StyledButton>

so main issue is StyledButton. when I write some of codes in SytledButton this order,

${colorStyles}
${sizeStyles}

  & + & {
    margin-left: 1rem;
  } 

margin-left:1rem didn't apply. but If Changed order like this.

 & + & {
    margin-left: 1rem;
  } 

  ${colorStyles}
  ${sizeStyles}

it applied. Why is this happening?

here's full code of StyledButton just in case

const StyledButton = styled.button`
  display: inline-flex;
  align-items: center;
  outline: none;
  border: none;
  border-radius: 4px;
  color: white;
  font-weight: bold;
  cursor: pointer;
  padding-left: 1rem;
  padding-right: 1rem;
  & + & {
    margin-left: 1rem;
  } 
  ${colorStyles}
  ${sizeStyles}

`;
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!