9

in html I can achieve this by

<span style="background-color:red">ketan</span>

but in react native how to implement this so that color will be applied to the text only.

ketan kulkarni
  • 355
  • 1
  • 4
  • 11

6 Answers6

23

If you want the background color to only encompass the text, you can use this:

<Text style={{backgroundColor: 'blue', alignSelf: 'flex-start'}}>
    Ketan
</Text>

And just change alignSelf as needed, you need to set this property if you dont want the text to take the whole width of the container

Raphael Estrada
  • 1,109
  • 11
  • 18
3

In react native you can directly use "Text" component and apply "backgroundColor" style to it as follows:

<Text> 
    <Text style={{ backgroundColor: "red", color: "white" }}>color
    </Text>
</Text>
Sandy.....
  • 2,833
  • 2
  • 15
  • 26
1

In react native you have Text component to display text.

To set color of Text, you can use color property of it

<Text style={{color:'#ff0000'}}>Your Text</Text>

And for background color of that text, you can use View and than set backgroundColor of that View

Ravi
  • 34,851
  • 21
  • 122
  • 183
0

you can't on IOS. set backgroundColor for extra View

<View style={{backgroundColor: 'red',flex:1}}><Text >ketan
    </Text></View>
Arun
  • 41
  • 1
  • 8
dvvtms
  • 627
  • 3
  • 10
0

try this one:

<Text style={{backgroundColor:'red', color:'white'}}>{'Message'}</Text>
Hiren Vaghela
  • 916
  • 1
  • 9
  • 22
0
import { Text } from 'react-native';

<Text style={{backgroundColor: 'green'}}>
  Ketan
</Text>

In order to put text in ReactNative, you need Text component. And you can apply backgroundColor onto style property.

Here's a snack as a demo to show usage and as playground for you to play around

Isaac
  • 12,042
  • 16
  • 52
  • 116