React Native absolute positioning horizontal centre

PositionReact NativeFlexboxAbsolute

Position Problem Overview


It seems that with position:absolute in use an element cannot be centred using justifyContent or alignItems. There's a workaround to use marginLeft but does not display the same for all devices even using dimensions to detect height and width of device.

  bottom: {
    position: 'absolute',
    justifyContent: 'center',
    alignItems: 'center',
    top: height*0.93,
    marginLeft: width*0.18,
  },
  bottomNav: {
    flexDirection: 'row',
  },

Position Solutions


Solution 1 - Position

Wrap the child you want centered in a View and make the View absolute.

<View style={{position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, justifyContent: 'center', alignItems: 'center'}}>
  <Text>Centered text</Text>
</View>

Solution 2 - Position

If you want to center one element itself you could use alignSelf:

logoImg: {
	position: 'absolute',
	alignSelf: 'center',
	bottom: '-5%'
}

This is an example (Note the logo parent is a view with position: relative)

enter image description here

Solution 3 - Position

You can center absolute items by providing the left property with the width of the device divided by two and subtracting out half of the element you'd like to center's width.

For example, your style might look something like this.

bottom: {
    position: 'absolute',
    left: (Dimensions.get('window').width / 2) - 25,
    top: height*0.93,
}

Solution 4 - Position

create a full-width View with alignItems: "center" then insert desired children inside.

import React from "react";
import {View} from "react-native";

export default class AbsoluteComponent extends React.Component {
  render(){
    return(
     <View style={{position: "absolute", left: 0, right: 0, alignItems: "center"}}>
      {this.props.children}
     </View>    
    )
  }
}

you can add properties like bottom: 30 for bottom aligned component.

Solution 5 - Position

<View style={{...StyleSheet.absoluteFillObject, justifyContent: 'center', alignItems: 'center'}}>
	<Text>CENTERD TEXT</Text>
</View>

And add this

import {StyleSheet} from 'react-native';

Solution 6 - Position

You can try the code

<View
    style={{
      alignItems: 'center',
      justifyContent: 'center'
    }}
  >
    <View
      style={{
        position: 'absolute',
        margin: 'auto',
        width: 50,
        height: 50
      }}
    />
  </View>

Solution 7 - Position

It's very simple really. Use percentage for width and left properties. For example:

logo : {
  position: 'absolute',
  top : 50,
  left: '30%',
  zIndex: 1,
  width: '40%',
  height: 150,
}

Whatever width is, left equals (100% - width)/2

Solution 8 - Position

Well, I would use this way to center absolute Views

  <View style={{ position: 'absolute', left: '50%', marginLeft: -22 }}>
           <View style={{ position: 'absolute', width: 44, height: 44}}>
             <Ionicons name="close" color="#4775f2" size={32} />
           </View>
   </View>

Notice that In the View which is wrapping the Icon container I'm using left: 50% and the marginLeft is special because you need to put exactly the middle width of the child an turn it negative which in this case is 44 as you can see above, and that's it

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHasenView Question on Stackoverflow
Solution 1 - PositionStephen HorvathView Answer on Stackoverflow
Solution 2 - PositionDavid NoreñaView Answer on Stackoverflow
Solution 3 - PositionCoreyView Answer on Stackoverflow
Solution 4 - PositionmasbossunView Answer on Stackoverflow
Solution 5 - PositionMahdi BashirpourView Answer on Stackoverflow
Solution 6 - PositionEris MabuView Answer on Stackoverflow
Solution 7 - Positionuser11360198View Answer on Stackoverflow
Solution 8 - PositionJorge DuranView Answer on Stackoverflow