Why can't I use a linear gradient with `styled-components`?
Asked Answered
E

4

7

The Problem:

When I create a simple View with this style in react-native using styled-components:

const Container = styled.View`
  flex: 1;
  background: linear-gradient(#006ded 0%, #1bace2 34.48%, #00e2ed 100%);
`;

I get this error:

Error: Failed to parse declaration "background: linear-gradient(#006ded 0%, #1bace2 34.48%, #00e2ed 100%)"

Is this not the correct use of linear gradients? I have a design file from a friend and just copied the css code. I'm not that good with css I have to confess but I looked it up on the mozilla docs. Seems like the syntax is not correct?

How can I achieve this Gradient using 3 colors ?

Eliseoelish answered 25/9, 2019 at 20:49 Comment(2)
Seems like no support: github.com/styled-components/styled-components/issues/1170Sideward
Unfortunately you are right. Than you then I will use it as a background image instead.Eliseoelish
E
2

It's not supported. You can find the confirmation here:

https://github.com/styled-components/styled-components/issues/1170

Workaround: Use a background image with react-native ImageBackgroundcomponent https://facebook.github.io/react-native/docs/imagebackground

Thx for the info @mulsun

Eliseoelish answered 27/9, 2019 at 8:2 Comment(0)
C
5

Use the attribute property

import LinearGradient from 'react-native-linear-gradient'

export const Container = styled(LinearGradient).attrs({
 colors: ['#000','#fff'],
 start: { x: 0, y: 0 },
 end: { x: 1, y: 0 },
})`
 /* your css here */
`;
Christianna answered 10/6, 2021 at 1:38 Comment(1)
how to change the colors of lineargradient when using ContainerCachinnate
E
2

It's not supported. You can find the confirmation here:

https://github.com/styled-components/styled-components/issues/1170

Workaround: Use a background image with react-native ImageBackgroundcomponent https://facebook.github.io/react-native/docs/imagebackground

Thx for the info @mulsun

Eliseoelish answered 27/9, 2019 at 8:2 Comment(0)
M
0

I tricked by created a div named overlay and set it over the wrapper and manipulated its opacity

export const Wrapper = styled.div`
    width: 100vw;
    height: 100vh;
    background-image:url(${BKHero}); 
    background-position:top;
    background-size:cover;
    .overlay  {
        width:100vw;
        height:100vh;
        background:rgba(333, 444, 331, 0.4);
    }
`
Mana answered 18/2, 2021 at 21:8 Comment(1)
The question is asking for React Native, not ReactJSGorgonzola
U
0

You are missing the direction at start:

Try:

const Container = styled.View`
  flex: 1;
  background: linear-gradient(to bottom, #006ded 0%, #1bace2 34.48%, #00e2ed 100%);
`;

It will probably work.

Urena answered 12/9, 2022 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.