344

How to center Text in ReactNative both in horizontal and vertical?

I have an example application in rnplay.org where justifyContent="center" and alignItems="center" is not working: https://rnplay.org/apps/AoxNKQ

The text should being centered. And why is there a margin at the top between the text (yellow) and parent container?

Code:

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  Image,
  View,
} = React;

var SampleApp = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
        <View style={styles.topBox}>
          <Text style={styles.headline}>
            lorem ipsum{'\n'}
            ipsum lorem lorem
          </Text>
        </View>
        <View style={styles.otherContainer}>
        </View>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',
    backgroundColor: 'red',
    justifyContent: 'center',
    alignItems: 'center',
  },
  topBox: {
    flex: 1,
    flexDirection: 'row',
    backgroundColor: 'lightgray',
    justifyContent: 'center',
    alignItems: 'center',
  },
  headline: {
    fontWeight: 'bold',
    fontSize: 18,
  marginTop: 0,
    width: 200,
    height: 80,
  backgroundColor: 'yellow',
    justifyContent: 'center',
    alignItems: 'center',
  },
  otherContainer: {
    flex: 4,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'green',
  },
});

AppRegistry.registerComponent('SampleApp', () => SampleApp);

module.exports = SampleApp;
5
  • Like this: rnplay.org/apps/1hbnSA ? Commented Jun 1, 2016 at 14:54
  • 1
    this is not horizontal centered
    – itinance
    Commented Jun 1, 2016 at 15:47
  • 1
    ok, so this: rnplay.org/apps/1hbnSA, updated Commented Jun 1, 2016 at 15:50
  • 1
    WAAAA... textAlign ? i knew it would something really stupid.... you should post this as an answer
    – itinance
    Commented Jun 1, 2016 at 15:58
  • The title states React Native but the tag says React JS. Please fix this. Commented Oct 17, 2022 at 7:33

22 Answers 22

527

From headline' style remove height, justifyContent and alignItems. It will center the text vertically. Add textAlign: 'center' and it will center the text horizontally.

  headline: {
    textAlign: 'center', // <-- the magic
    fontWeight: 'bold',
    fontSize: 18,
    marginTop: 0,
    width: 200,
    backgroundColor: 'yellow',
  }
5
  • 31
    Doesn't work with a width unless you wrap it in a <View> with justifyContent: 'center', alignItems: 'center', Commented Nov 6, 2017 at 19:54
  • 5
    your magic not worked however width: "100%", worked
    – user889030
    Commented Jan 25, 2021 at 18:24
  • I only got it to work by changing it to "text-Align: center;"
    – Ghost.Idle
    Commented Feb 12, 2022 at 1:27
  • 2023: Types of property 'textAlign' are incompatible. Type 'string' is not assignable to type 'TextAlign
    – sutan
    Commented Jan 9, 2023 at 11:12
  • perfect answer => " textAlign: 'center'
    – Omar Saade
    Commented Mar 7 at 10:29
99

Already answered but I'd like to add a bit more on the topic and different ways to do it depending on your use case.

You can add adjustsFontSizeToFit={true} (currently undocumented) to Text Component to auto adjust the size inside a parent node.

  <Text adjustsFontSizeToFit={true} numberOfLines={1}>Hiiiz</Text>

You can also add the following in your Text Component:

<Text style={{textAlignVertical: "center",textAlign: "center",}}>Hiiiz</Text>

Or you can add the following into the parent of the Text component:

<View style={{flex:1,justifyContent: "center",alignItems: "center"}}>
     <Text>Hiiiz</Text>
</View>

or both

 <View style={{flex:1,justifyContent: "center",alignItems: "center"}}>
     <Text style={{textAlignVertical: "center",textAlign: "center",}}>Hiiiz</Text>
</View>

or all three

 <View style={{flex:1,justifyContent: "center",alignItems: "center"}}>
     <Text adjustsFontSizeToFit={true} 
           numberOfLines={1} 
           style={{textAlignVertical: "center",textAlign: "center",}}>Hiiiz</Text>
</View>

It all depends on what you're doing. You can also checkout my full blog post on the topic

https://medium.com/@vygaio/how-to-auto-adjust-text-font-size-to-fit-into-a-nodes-width-in-react-native-9f7d1d68305b

4
  • this should be expected : <Text style={{textAlignVertical: "center",textAlign: "center",}}>Hiiiz</Text> Commented Jan 13, 2020 at 5:31
  • 1
    man you saved my 1.5 hours. I was trying to do same but couldn't until this answer as : textAlignVertical: "center", textAlign: "center" , as <Text/> Component Style. Commented Mar 2, 2020 at 5:22
  • textAlignVertical is Android only. None of these solutions are aligning my text vertically, only horizontally.
    – sudo
    Commented Apr 12, 2020 at 17:00
  • justifyContent: "center" in the parent component was the magic sauce. Thanks!
    – Forrest
    Commented Feb 12, 2022 at 21:00
25

Set these styles on the image component:

{
  textAlignVertical: "center",
  textAlign: "center"
}
19
textAlignVertical: "center"

is the real magic.

0
14

Horizontal and Vertical center alignment

<View style={{flex: 1, justifyContent: 'center',alignItems: 'center'}}>
     <Text> Example Test </Text>
</View>
1
  • 1
    Only solution that worked for me in terms of centering <Text> inside a <View> not just horizontally, but also vertically. Thank you! Commented Jan 4, 2020 at 11:38
11

this is a example for Horizontal and Vertical alignment simultaneously

<View style={{width: 200, flexDirection: 'row',alignItems: 'center'}}>
     <Text style={{width: '100%',textAlign: 'center'}} />
</View>
1
  • Thanks for the tip that inline styles must be enclosed in {{double-curly-braces}}.
    – MarkHu
    Commented May 25, 2018 at 22:04
11
<View style={{ backgroundColor: 'blue', justifyContent: 'center' }}>
  <Text style={{ fontSize: 25, textAlign: 'center' }}>A</Text>
</View>
10

The following property can be used: {{alignItems: 'center'}} becaus you are using item <Text> not "string".

<View style={{alignItems: 'center'}}>
 <Text> Hello i'm centered text</Text>
</View>
2
  • 1
    Even if this may be the solution to the question, please add some more explanation so we all can learn. Commented Aug 12, 2019 at 7:59
  • Please, improve it because you are using two asterisks to circulate the main correction but it's not clear and the other part is confuse Commented Oct 18, 2019 at 18:35
8

You can use alignSelf property on Text component

{ alignSelf : "center" }
0
5

Wherever you have Text component in your page just you need to set style of the Text component.

 <Text style={{ textAlign: 'center' }}> Text here </Text>

you don't need to add any other styling property, this is spectacular magic it will set you text in center of the your UI.

5

Simple add

textAlign: "center"

in your styleSheet, that's it. Hope this would help.

edit: "center"

4

Okey , so its a basic problem , dont worry about this just write the <View> component and wrap it around the <Text> component

<View style={{alignItems: 'center'}}>
<Text> Write your Text Here</Text>
</View>

alignitems:center is a prop use to center items on crossaxis


justifycontent:'center' is a prop use to center items on mainaxis

3

Set in Parent view

justifyContent:center

and in child view alignSelf:center

1
  • Keys should be in camel case justifyContent and alignSelf
    – Siddharth
    Commented Jun 28, 2020 at 13:41
1
const styles = StyleSheet.create({
        navigationView: {
        height: 44,
        width: '100%',
        backgroundColor:'darkgray',
        justifyContent: 'center', 
        alignItems: 'center' 
    },
    titleText: {
        fontSize: 20,
        fontWeight: 'bold',
        color: 'white',
        textAlign: 'center',
    },
})


render() {
    return (
        <View style = { styles.navigationView }>
            <Text style = { styles.titleText } > Title name here </Text>
        </View>
    )

}
1

In addition to the use cases mentioned in the other answers:

To center text in the specific use case of a BottomTabNavigator, remember to set showIcon to false (even if you don't have icons in the TabNavigator). Otherwise the text will be pushed toward bottom of Tab.

For example:

const TabNavigator = createBottomTabNavigator({
  Home: HomeScreen,
  Settings: SettingsScreen
}, {
  tabBarOptions: {
    activeTintColor: 'white',
    inactiveTintColor: 'black',
    showIcon: false, //do this
    labelStyle: {
      fontSize: 20,
      textAlign: 'center',
    },
    tabStyle: {
      backgroundColor: 'grey',
      marginTop: 0,
      textAlign: 'center',
      justifyContent: 'center',
      textAlignVertical: "center"
    }
  }
1

first add in parent view flex:1, justifyContent: 'center', alignItems: 'center'

then in text add textAlign:'center'

1

used

textalign:center

at the view

1

If you're running into this issue on Android, like I was, make sure to set includeFontPadding to false (documentation).

0

You can use two approaches for this...

  1. To make text align center horizontally, apply this Property (textAlign:"center"). Now to make the text align vertically, first check direction of flex. If flexDirection is column apply property (justifyContent:"center") and if flexDirection is row is row apply property (alignItems : "center") .

  2. To Make text align center apply same property (textAlign:"center"). Now to make it align vertically make the hieght of the <Text> </Text> equal to view and then apply property (textAlignVertical: "center")...

Most Probably it will Work...

0

In many instances, ReactNative supports CSS style properties, names as camelCase instead of the standard CSS dasherized-lisp-style (aka kebab-case)

Which is the same as React/JSX.

The CSS property text-align has the direct equivalent: textAlign

So usually, it's worth trying the CSS property, converted to camelCase.

-1
<View style={{alignSelf:'center'}}>
<Text>React Native</Text>
</View>
1
  • It would be nice to give a brief explanation of how this works / how it solves the problem, and how it's different than existing answers.
    – starball
    Commented Dec 24, 2022 at 3:27
-2

headline: { textAlign: 'center',fontWeight: 'bold'}

Not the answer you're looking for? Browse other questions tagged or ask your own question.