The justifyContent in a component’s styles determines the alignment of Children View along with Primary Axis(Default Flex-direction). If your primary axis is flex-direction is column then the justifyContent will set all the children in Vertical format. You can learn more about Flex-direction from my this tutorial. So in this tutorial we would going to make a react native app with justifyContent style Explained With Example Tutorial in both iOS Android react native application.
Properties of justifyContent :
- flex-start
- center
- flex-end
- space-around
- space-between
Contents in this tutorial justifyContent style Explained With Example in React Native:
1. flex-start : The flex-start would set all the children views into vertically top side along with primary axis. If your primary axis is column then it will set all the views into vertically format and if the primary axis is row then it will set all the views into horizontal format.
Code for Above Screenshot with justifyContent:’flex-start’ :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<View style={{ justifyContent:‘flex-start’, flex:1,flexDirection: ‘column’, margin: 5}}>
<View style={{ height: 100, width: 100, justifyContent:‘center’, backgroundColor: ‘#00BCD4’ }} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 1 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#FF1744’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 2 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#4CAF50’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 3 </Text>
</View>
</View>
|
2. Center : This would push all the views in vertically center of screen along with primary axis.
Code for Above Screenshot with justifyContent:’center’ :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<View style={{ justifyContent:‘center’, flex:1,flexDirection: ‘column’, margin: 5}}>
<View style={{ height: 100, width: 100, justifyContent:‘center’, backgroundColor: ‘#00BCD4’ }} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 1 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#FF1744’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 2 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#4CAF50’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 3 </Text>
</View>
</View>
|
3. flex-end : This would set the children views into bottom of primary axis(Column) in vertically bottom of screen.
Code for Above Screenshot with justifyContent:’flex-end’ :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<View style={{ justifyContent:‘flex-end’, flex:1,flexDirection: ‘column’, margin: 5}}>
<View style={{ height: 100, width: 100, justifyContent:‘center’, backgroundColor: ‘#00BCD4’ }} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 1 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#FF1744’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 2 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#4CAF50’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 3 </Text>
</View>
|
4. space-around : This would gives the equal space between each View in primary axis. The first and last view also got space from top and bottom side.
Code for Above Screenshot with justifyContent:’space-around’ :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<View style={{ justifyContent:‘space-between’, flex:1,flexDirection: ‘column’, margin: 5}}>
<View style={{ height: 100, width: 100, justifyContent:‘center’, backgroundColor: ‘#00BCD4’ }} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 1 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#FF1744’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 2 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#4CAF50’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 3 </Text>
</View>
</View>
|
5. space-between : This would gives only space between items.
Code for Above Screenshot with justifyContent:’space-between’ :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<View style={{ justifyContent:‘space-between’, flex:1,flexDirection: ‘column’, margin: 5}}>
<View style={{ height: 100, width: 100, justifyContent:‘center’, backgroundColor: ‘#00BCD4’ }} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 1 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#FF1744’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 2 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#4CAF50’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 3 </Text>
</View>
</View>
|
Complete source code for App.js File :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import React, { Component } from ‘react’;
import { StyleSheet, Text, View } from ‘react-native’;
export default class MainActivity extends Component {
render()
{
return (
<View style={{ justifyContent:‘space-between’, flex:1,flexDirection: ‘column’, margin: 5}}>
<View style={{ height: 100, width: 100, justifyContent:‘center’, backgroundColor: ‘#00BCD4’ }} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 1 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#FF1744’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 2 </Text>
</View>
<View style={{ width: 100, height: 100, justifyContent:‘center’, backgroundColor: ‘#4CAF50’,}} >
<Text style={{fontSize: 25, textAlign: ‘center’, color: ‘#fff’}}> View 3 </Text>
</View>
</View>
);
}
}
|