I would like to style text as superscript in React Native. How would this be accomplished? Is there a way to make use of the Javascript sup() string method to return a string as superscript within a <Text> object?
10 Answers
I got this working for me using a view container and flex.
<View style={{flexDirection: 'row', alignItems: 'flex-start'}}>
<Text style={{fontSize: 20, lineHeight: 30}}>10</Text>
<Text style={{fontSize: 11, lineHeight: 18}}>am</Text>
</View>
Here is the link to this in action https://repl.it/Haap/0
Cheers!
1 Comment
i just do this to achieve superscript in my case
<View style={{ flexDirection: 'row' }}>
<Text style={{ fontSize: 30 }}>e</Text>
<Text style={{ fontSize: 10 }}>x</Text>
</View>
1 Comment
Just use fontSize, lineHeight, textAlignVertical:
<Text style={{fontSize:20, lineHeight:22}}>
foo
<Text style={{fontSize:20 * 1.6, lineHeight:22 * 1.1, textAlignVertical: 'top'}}>
bar
</Text>
</Text>
2 Comments
Javascripts sub() function will only surround your text with <sub></sub> tags and they are recognized as text in RN. You would need to build your own function like:
export default class Test extends Component {
sub = (base, exponent) => {
return <View style={{flexDirection: 'row'}}>
<View style={{alignItems: 'flex-end'}}>
<Text style={{fontSize: 13}}>{base}</Text>
</View>
<View style={{alignItems: 'flex-start'}}>
<Text style={{fontSize: 10}}>{exponent}</Text>
</View>
</View>
}
render() {
return(
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>{(() => 'hello'+'world'.sub())()}</Text>
{(() => this.sub('hello','world'))()}
{(() => this.sub('2','6'))()}
</View>
);
}
}
5 Comments
View: "Views nested within a <Text> must have a width and height"View within Text will work in iOS only. Not cross-platform.textAlignVertical: 'top'textAlignVertical: 'top' property like so: <Text>Productname<Text style={{fontSize: 9, textAlignVertical: 'top'}}>®</Text></Text> to no avail. Any thoughts?The best method I found for this problem is not ideal, but it works cross-platform. The character I needed to superscript is the ® which some fonts have superscripted by default. So, I simply included a similar font family with the superscripted ® and it worked like magic.
1 Comment
Simple method for superscript Text in 2023
import React, { Text } from 'react-native'
type Props = {
children: string
}
const SuperscriptText: React.FC<Props> = ({children}) => {
return (
<Text
style={{
fontSize: 8, // Should be smaller than the other text
textAlignVertical: 'top',
}}
>
{children}
</Text>
)
}
export default SuperscriptText
Working on Android & iOs:
<Text>Default Text</Text>
<SuperscriptText>Hi</SuperscriptText>
<Text>Continue here</Text>
Important note: Currently this does not work when nesting text elements. See here: https://github.com/facebook/react-native/issues/30375
Comments
For my use case (which runs up against this bug where textAlignVertical doesn't work), the only thing I could get to work was to create an image of the text I wanted superscripted, with space under it, and put that inline within the <Text> block I was working within.
Worked alright, because it was the "registered mark" symbol, rather than something the user actually needs to read; but admittedly a much-less-than-desirable solution. One drawback is you can't prevent line-breaking between the superscript and the word it's attached to without adding a .
Comments
As others have already mentioned, this is still an open issue with React Native's Text component.
There is a workaround, though.
Assuming this is what you're trying to display. Here, "sup" is supposed to be a superscript. This does not currently work with React Native.
<Text style={{ lineHeight: 22 }}>
Lorem ipsum
<Text
style={{"fontSize": 10, "lineHeight": 22, "textAlignVertical": "top"}}
>
sup
</Text>
</Text>
The trick is to wrap the superscript text in a View and match the lineHeight of the parent Text and the nested superscript/subscript Text
<Text style={{ lineHeight: 22 }}>
Lorem ipsum
<View>
<Text
style={{"fontSize": 10, "lineHeight": 22, "textAlignVertical": "top"}}
>
sup
</Text>
</View>
</Text>
And, here's what the fixed superscript looks like:

Hope that helps!
Comments
<View style={styles.container}>
<Text>This is a </Text>
<Text style={styles.superscript}>superscript</Text>
<Text> example.</Text>
</View>
const styles = StyleSheet.create({ container: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', }, superscript: { fontSize: 10, lineHeight: 10, alignSelf: 'baseline', }, });
This works perfectly, also provide independent styling ability

