12

Can I use CSS for styling my ReactNative component. Currently I can only use StyleSheet like this:

var styles = React.StyleSheet.create({
 autocomplete: {
   backgroundColor: '#FFF',
   zIndex: 5,
 },
 ...

I want to use CSS instead of this as in ReactJs

Martin
  • 5,714
  • 2
  • 21
  • 41
PranavPinarayi
  • 3,037
  • 5
  • 21
  • 32

5 Answers5

5

First create a file named App.module.css

then write your pure css here

.container {
  background-color: rgb(44, 0, 95);
  width: 100%;
  height: 100%;
}

then,

import the file in your js file

import style from './App.module.css';

Now you can this style like normal react native styling

<View style={style.container}>
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
AJO ALEX
  • 128
  • 1
  • 3
  • 6
  • Great approach! But when I do this I get the following error: `"None of these files exist: * components/App.module.css(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)"` It looks like I can't import a file which ends in .css? – tog22 Mar 21 '23 at 00:47
0

What do you mean pure css ?

Have a look at styled-components, the project is in active development.

This allow you to style your react components using css.

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
0

I had the same issue once and I solved it using this component. I see is no longer under development however for me it was perfectly working.

What it does basically is taking your CSS files and it will generate from them some StyleSheet components that you can easily integrate in your react application.

rakwaht
  • 3,666
  • 3
  • 28
  • 45
0

Try this library: https://atom.io/packages/atom-react-native-css

You can style components with CSS and built in support for SASS.

Abhijith Sasikumar
  • 13,262
  • 4
  • 31
  • 45
0
<Text style={{backgroundColor:'#FFF',zIndex: 5}}></Text>
Saurabh Chavan
  • 136
  • 2
  • 5